]> gitweb.fperrin.net Git - iftop.git/blob - util.c
Import iftop-1.0pre4
[iftop.git] / util.c
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
10 static const char rcsid[] = "$Id: util.c,v 1.1 2002/03/24 17:27:12 chris Exp $";
11
12 #include <sys/types.h>
13
14 #include <errno.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "iftop.h"
21
22 /* xmalloc:
23  * Malloc, and abort if malloc fails. */
24 void *xmalloc(size_t n) {
25     void *v;
26     v = malloc(n);
27     if (!v) abort();
28     return v;
29 }
30
31 /* xcalloc:
32  * As above. */
33 void *xcalloc(size_t n, size_t m) {
34     void *v;
35     v = calloc(n, m);
36     if (!v) abort();
37     return v;
38 }
39
40 /* xrealloc:
41  * As above. */
42 void *xrealloc(void *w, size_t n) {
43     void *v;
44     v = realloc(w, n);
45     if (n != 0 && !v) abort();
46     return v;
47 }
48
49 /* xstrdup:
50  * As above. */
51 char *xstrdup(const char *s) {
52     char *t;
53     t = strdup(s);
54     if (!t) abort();
55     return t;
56 }
57
58 /* xfree:
59  * Free, ignoring a passed NULL value. */
60 void xfree(void *v) {
61     if (v) free(v);
62 }
63