diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2013-04-21 14:52:01 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2013-04-21 15:06:40 -0700 |
commit | f36566239cc9119882a36273c3eefb90962d6ee5 (patch) | |
tree | cd70ca10a819ae4cba0a2db2b968e49a546eef6a | |
parent | 249695649bb25f500d1525f655ca317428ea6044 (diff) |
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 <alan.coopersmith@oracle.com>
-rw-r--r-- | bmtoa.c | 21 |
1 files changed, 8 insertions, 13 deletions
@@ -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); |