/* See LICENSE file for copyright and license details. */ #include #include #include #include #include #include #include "util.h" void die(const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "sframe: "); vfprintf(stderr, fmt, ap); if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { fprintf(stderr, " %s", strerror(errno)); } fprintf(stderr, "\n"); va_end(ap); exit(1); } void warn(const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "sframe: "); vfprintf(stderr, fmt, ap); if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { fprintf(stderr, " %s", strerror(errno)); } fprintf(stderr, "\n"); va_end(ap); } void * ecalloc(size_t nmemb, size_t size) { void *p; p = calloc(nmemb, size); if (!p) die("calloc:"); return p; } void * emalloc(size_t size) { void *p; p = malloc(size); if (!p) die("malloc:"); return p; } char * estrdup(const char *s) { char *p; p = strdup(s); if (!p) die("strdup:"); return p; } /* mkdir -p: create all components of path */ int mkdirp(const char *path) { char buf[4096]; char *p; size_t len; len = strlen(path); if (len == 0 || len >= sizeof(buf)) return -1; memcpy(buf, path, len + 1); for (p = buf + 1; *p; p++) { if (*p == '/') { *p = '\0'; if (mkdir(buf, 0755) < 0 && errno != EEXIST) return -1; *p = '/'; } } if (mkdir(buf, 0755) < 0 && errno != EEXIST) return -1; return 0; }