#include #include #include #include #include #include "options.h" #include "utils.h" /* exported function documented in utils.h */ char *genb_fpath(const char *fname) { char *fpath; int fpathl; fpathl = strlen(options->outdirname) + strlen(fname) + 2; fpath = malloc(fpathl); snprintf(fpath, fpathl, "%s/%s", options->outdirname, fname); return fpath; } /* exported function documented in utils.h */ FILE *genb_fopen(const char *fname, const char *mode) { char *fpath; FILE *filef; fpath = genb_fpath(fname); filef = fopen(fpath, mode); if (filef == NULL) { fprintf(stderr, "Error: unable to open file %s (%s)\n", fpath, strerror(errno)); free(fpath); return NULL; } free(fpath); return filef; } #ifdef NEED_STRNDUP char *strndup(const char *s, size_t n) { size_t len; char *s2; for (len = 0; len != n && s[len]; len++) continue; s2 = malloc(len + 1); if (!s2) return 0; memcpy(s2, s, len); s2[len] = 0; return s2; } #endif