| Aditya Kali | f239fef | 2011-07-20 11:40:02 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Common things for all utilities |
| 3 | * |
| 4 | * Jan Kara <jack@suse.cz> - sponsored by SuSE CR |
| 5 | * |
| 6 | * Jani Jaakkola <jjaakkol@cs.helsinki.fi> - syslog support |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <string.h> |
| 13 | #include <syslog.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | |
| 17 | #include "common.h" |
| 18 | |
| 19 | void *smalloc(size_t size) |
| 20 | { |
| 21 | void *ret = malloc(size); |
| 22 | |
| 23 | if (!ret) { |
| 24 | fputs("Not enough memory.\n", stderr); |
| 25 | exit(3); |
| 26 | } |
| 27 | return ret; |
| 28 | } |
| 29 | |
| 30 | void *srealloc(void *ptr, size_t size) |
| 31 | { |
| 32 | void *ret = realloc(ptr, size); |
| 33 | |
| 34 | if (!ret) { |
| 35 | fputs("Not enough memory.\n", stderr); |
| 36 | exit(3); |
| 37 | } |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | void sstrncpy(char *d, const char *s, size_t len) |
| 42 | { |
| 43 | strncpy(d, s, len); |
| 44 | d[len - 1] = 0; |
| 45 | } |
| 46 | |
| 47 | void sstrncat(char *d, const char *s, size_t len) |
| 48 | { |
| 49 | strncat(d, s, len); |
| 50 | d[len - 1] = 0; |
| 51 | } |
| 52 | |
| 53 | char *sstrdup(const char *s) |
| 54 | { |
| 55 | char *r = strdup(s); |
| 56 | |
| 57 | if (!r) { |
| 58 | puts("Not enough memory."); |
| 59 | exit(3); |
| 60 | } |
| 61 | return r; |
| 62 | } |