From 6164461b7ee850da5b577b96b120e2a4b1b05a09 Mon Sep 17 00:00:00 2001 From: xhe Date: Sun, 21 May 2017 22:00:32 +0800 Subject: fix bug of reading from stdin: found when compiling dnsmasq we can't fseek in stdin, we can only read it for one time so now i write them to a temporary file first instead --- src/msgfmt.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/msgfmt.c b/src/msgfmt.c index 362e403..df5ed39 100644 --- a/src/msgfmt.c +++ b/src/msgfmt.c @@ -475,7 +475,21 @@ int process(FILE *in, FILE *out) { void set_file(int out, char* fn, FILE** dest) { if(streq(fn, "-")) { - *dest = out ? stdout : stdin; + if(out) { + *dest = stdout; + } else { + char b[4096]; + size_t n=0; + FILE* tmpf = tmpfile(); + if(!tmpf) + perror("tmpfile"); + + while((n=fread(b, sizeof(*b), sizeof(b), stdin)) > 0) + fwrite(b, sizeof(*b), n, tmpf); + + fseek(tmpf, 0, SEEK_SET); + *dest = tmpf; + } } else { *dest = fopen(fn, out ? "w" : "r"); } @@ -553,9 +567,15 @@ int main(int argc, char**argv) { // no support for -d at this time fprintf(stderr, "EINVAL\n"); exit(1); - } else if (streq(A+1, "h")) syntax(); + } else if (streq(A+1, "h")) { + syntax(); + } else if (expect_in_fn) { + set_file(0, A, &in); + expect_in_fn = 0; + } } else if (expect_in_fn) { set_file(0, A, &in); + expect_in_fn = 0; } } if(in == NULL || out == NULL) { -- cgit v1.2.1