X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Ffmt_new.c;h=c1386d4c710bb4de354bbbbc9d219b6753ccc5eb;hp=6cdcfcc7075db5fcb09d9e48066c4973eedcdb4c;hb=18591f8e001ecedbee48a51c1d1f08ebaa1c15c8;hpb=a0b824f146a18ad99633addede9e81a470d4cb59 diff --git a/sbr/fmt_new.c b/sbr/fmt_new.c index 6cdcfcc..c1386d4 100644 --- a/sbr/fmt_new.c +++ b/sbr/fmt_new.c @@ -8,6 +8,9 @@ #include #include +#include +#include +#include static char *formats = NULL; @@ -21,48 +24,49 @@ static void normalize(char *); ** Copy first available format string, store in static memory and normalize it. */ char * -new_fs(char *form, char *format, char *def_form, char *default_fs) +new_fs(char *form, char *def_form) { struct stat st; - register FILE *fp; + FILE *fp; if (formats) { - free(formats); + mh_free0(&formats); } if (form) { - if ((fp = fopen(etcpath(form), "r")) == NULL) { - adios(form, "unable to open format file"); + if (*form == '=') { + formats = mh_xstrdup(form+1); + } else { + if ((fp = fopen(etcpath(form), "r")) == NULL) { + adios(EX_IOERR, form, "unable to open format file"); + } + if (fstat(fileno(fp), &st) == -1) { + adios(EX_IOERR, form, "unable to stat format file"); + } + formats = mh_xcalloc(st.st_size + 1, sizeof(char)); + if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) { + adios(EX_IOERR, form, "error reading format file"); + } + formats[st.st_size] = '\0'; + fclose(fp); } - if (fstat(fileno(fp), &st) == -1) { - adios(form, "unable to stat format file"); - } - formats = mh_xmalloc((size_t) st.st_size + 1); - if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) { - adios(form, "error reading format file"); - } - formats[st.st_size] = '\0'; - fclose(fp); - - } else if (format) { - formats = getcpy(format); - } else if (def_form) { - if ((fp = fopen(etcpath(def_form), "r")) == NULL) { - adios(def_form, "unable to open format file"); + if (*def_form == '=') { + formats = mh_xstrdup(def_form+1); + } else { + if ((fp = fopen(etcpath(def_form), "r")) == NULL) { + adios(EX_IOERR, def_form, "unable to open format file"); + } + if (fstat(fileno(fp), &st) == -1) { + adios(EX_IOERR, def_form, "unable to stat format file"); + } + formats = mh_xcalloc(st.st_size + 1, sizeof(char)); + if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) { + adios(EX_IOERR, def_form, "error reading format file"); + } + formats[st.st_size] = '\0'; + fclose(fp); } - if (fstat(fileno(fp), &st) == -1) { - adios(def_form, "unable to stat format file"); - } - formats = mh_xmalloc((size_t) st.st_size + 1); - if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) { - adios(def_form, "error reading format file"); - } - formats[st.st_size] = '\0'; - fclose(fp); - - } else { - formats = getcpy(default_fs); } normalize(formats); /* expand escapes */