util.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef _UTIL_H
  2. #define _UTIL_H
  3. /*
  4. * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  19. * USA
  20. */
  21. static inline void __attribute__((noreturn)) die(char * str, ...)
  22. {
  23. va_list ap;
  24. va_start(ap, str);
  25. fprintf(stderr, "FATAL ERROR: ");
  26. vfprintf(stderr, str, ap);
  27. exit(1);
  28. }
  29. static inline void *xmalloc(size_t len)
  30. {
  31. void *new = malloc(len);
  32. if (!new)
  33. die("malloc() failed\n");
  34. return new;
  35. }
  36. static inline void *xrealloc(void *p, size_t len)
  37. {
  38. void *new = realloc(p, len);
  39. if (!new)
  40. die("realloc() failed (len=%d)\n", len);
  41. return new;
  42. }
  43. extern char *xstrdup(const char *s);
  44. extern char *join_path(const char *path, const char *name);
  45. #endif /* _UTIL_H */