From f36566239cc9119882a36273c3eefb90962d6ee5 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 21 Apr 2013 14:52:01 -0700 Subject: Simplify & unify error path between mktemp & mkstemp versions This also now catches errors when fopen() or fdopen() fail, before we try to fwrite() to a null FILE pointer. Signed-off-by: Alan Coopersmith --- bmtoa.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/bmtoa.c b/bmtoa.c index 4281e22..6682fe8 100644 --- a/bmtoa.c +++ b/bmtoa.c @@ -72,28 +72,23 @@ copy_stdin (void) static char tmpfilename[] = "/tmp/bmtoa.XXXXXX"; #endif char buf[BUFSIZ]; - FILE *fp; + FILE *fp = NULL; int nread, nwritten; #ifndef HAVE_MKSTEMP - if (mktemp (tmpfilename) == NULL) { - fprintf (stderr, - "%s: unable to generate temporary file name for stdin.\n", - ProgramName); - exit (1); - } - fp = fopen (tmpfilename, "w"); + if (mktemp (tmpfilename) != NULL) + fp = fopen (tmpfilename, "w"); #else int fd; - - if ((fd = mkstemp(tmpfilename)) < 0) { + if ((fd = mkstemp(tmpfilename)) >= 0) + fp = fdopen(fd, "w"); +#endif + if (fp == NULL) { fprintf (stderr, - "%s: unable to generate temporary file name for stdin.\n", + "%s: unable to generate temporary file for stdin.\n", ProgramName); exit (1); } - fp = fdopen(fd, "w"); -#endif while (1) { buf[0] = '\0'; nread = fread (buf, 1, sizeof buf, stdin); -- cgit v1.2.3