util.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * util.c:
  3. * Various utility functions.
  4. *
  5. * Copyright (c) 2002 Chris Lightfoot. All rights reserved.
  6. * Email: chris@ex-parrot.com; WWW: http://www.ex-parrot.com/~chris/
  7. *
  8. */
  9. static const char rcsid[] = "$Id: util.c,v 1.1 2002/03/24 17:27:12 chris Exp $";
  10. #include <sys/types.h>
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include "iftop.h"
  17. /* xmalloc:
  18. * Malloc, and abort if malloc fails. */
  19. void *xmalloc(size_t n) {
  20. void *v;
  21. v = malloc(n);
  22. if (!v) abort();
  23. return v;
  24. }
  25. /* xcalloc:
  26. * As above. */
  27. void *xcalloc(size_t n, size_t m) {
  28. void *v;
  29. v = calloc(n, m);
  30. if (!v) abort();
  31. return v;
  32. }
  33. /* xrealloc:
  34. * As above. */
  35. void *xrealloc(void *w, size_t n) {
  36. void *v;
  37. v = realloc(w, n);
  38. if (n != 0 && !v) abort();
  39. return v;
  40. }
  41. /* xstrdup:
  42. * As above. */
  43. char *xstrdup(const char *s) {
  44. char *t;
  45. t = strdup(s);
  46. if (!t) abort();
  47. return t;
  48. }
  49. /* xfree:
  50. * Free, ignoring a passed NULL value. */
  51. void xfree(void *v) {
  52. if (v) free(v);
  53. }