summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2013-04-21 14:52:01 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2013-04-21 15:06:40 -0700
commitf36566239cc9119882a36273c3eefb90962d6ee5 (patch)
treecd70ca10a819ae4cba0a2db2b968e49a546eef6a
parent249695649bb25f500d1525f655ca317428ea6044 (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.c21
1 files 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);