simplified util.c
3d25a327
1 file(s) · +10 −19
| 9 | 9 | #include <sys/wait.h> |
|
| 10 | 10 | #include <unistd.h> |
|
| 11 | 11 | ||
| 12 | - | /* static */ |
|
| 12 | + | void * |
|
| 13 | + | emalloc(unsigned int size) { |
|
| 14 | + | void *res = malloc(size); |
|
| 13 | 15 | ||
| 14 | - | static void |
|
| 15 | - | badmalloc(unsigned int size) { |
|
| 16 | - | eprint("fatal: could not malloc() %u bytes\n", size); |
|
| 16 | + | if(!res) |
|
| 17 | + | eprint("fatal: could not malloc() %u bytes\n", size); |
|
| 18 | + | return res; |
|
| 17 | 19 | } |
|
| 18 | 20 | ||
| 19 | - | /* extern */ |
|
| 20 | - | ||
| 21 | - | void * |
|
| 22 | - | emalloc(unsigned int size) { |
|
| 23 | - | void *res = malloc(size); |
|
| 21 | + | char * |
|
| 22 | + | estrdup(const char *str) { |
|
| 23 | + | void *res = strdup(str); |
|
| 24 | 24 | ||
| 25 | 25 | if(!res) |
|
| 26 | - | badmalloc(size); |
|
| 26 | + | eprint("fatal: could not malloc() %u bytes\n", strlen(str)); |
|
| 27 | 27 | return res; |
|
| 28 | 28 | } |
|
| 29 | 29 | ||
| 36 | 36 | va_end(ap); |
|
| 37 | 37 | exit(EXIT_FAILURE); |
|
| 38 | 38 | } |
|
| 39 | - | ||
| 40 | - | char * |
|
| 41 | - | estrdup(const char *str) { |
|
| 42 | - | void *res = strdup(str); |
|
| 43 | - | ||
| 44 | - | if(!res) |
|
| 45 | - | badmalloc(strlen(str)); |
|
| 46 | - | return res; |
|
| 47 | - | } |
|