diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2009-06-07 13:29:51 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2009-06-07 13:29:51 +0000 |
commit | 7091d1b9290f0b0c85027ee37002bea859ec75d8 (patch) | |
tree | c43454e635ef86f251099053d39490ae9a8984b3 /usr.bin | |
parent | dc7bb66ca2e1d8a4ec665ac26bc240a80c5b3ace (diff) |
Don't leak FILE * if multiple -o flags are given.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/sdiff/edit.c | 4 | ||||
-rw-r--r-- | usr.bin/sdiff/extern.h | 4 | ||||
-rw-r--r-- | usr.bin/sdiff/sdiff.c | 24 |
3 files changed, 17 insertions, 15 deletions
diff --git a/usr.bin/sdiff/edit.c b/usr.bin/sdiff/edit.c index 47a222f5a42..8b0f522a4f6 100644 --- a/usr.bin/sdiff/edit.c +++ b/usr.bin/sdiff/edit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: edit.c,v 1.18 2007/10/17 20:02:33 deraadt Exp $ */ +/* $OpenBSD: edit.c,v 1.19 2009/06/07 13:29:50 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -188,7 +188,7 @@ RIGHT: break; /* Write data we just read. */ - nwritten = fwrite(buf, sizeof(*buf), nread, outfile); + nwritten = fwrite(buf, sizeof(*buf), nread, outfp); if (nwritten != nread) { warnx("error writing to output file"); cleanup(filename); diff --git a/usr.bin/sdiff/extern.h b/usr.bin/sdiff/extern.h index 01ad27144c9..c9ebaef7d22 100644 --- a/usr.bin/sdiff/extern.h +++ b/usr.bin/sdiff/extern.h @@ -1,11 +1,11 @@ -/* $OpenBSD: extern.h,v 1.4 2006/05/25 03:20:32 ray Exp $ */ +/* $OpenBSD: extern.h,v 1.5 2009/06/07 13:29:50 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. * Public domain. */ -extern FILE *outfile; /* file to save changes to */ +extern FILE *outfp; /* file to save changes to */ extern const char *tmpdir; int eparse(const char *, const char *, const char *); diff --git a/usr.bin/sdiff/sdiff.c b/usr.bin/sdiff/sdiff.c index e9a840f8fd6..2aa684e0c2a 100644 --- a/usr.bin/sdiff/sdiff.c +++ b/usr.bin/sdiff/sdiff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sdiff.c,v 1.27 2009/06/07 13:15:13 ray Exp $ */ +/* $OpenBSD: sdiff.c,v 1.28 2009/06/07 13:29:50 ray Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -65,7 +65,7 @@ size_t file1ln, file2ln; /* line number of file1 and file2 */ int Iflag = 0; /* ignore sets matching regexp */ int lflag; /* print only left column for identical lines */ int sflag; /* skip identical lines */ -FILE *outfile; /* file to save changes to */ +FILE *outfp; /* file to save changes to */ const char *tmpdir; /* TMPDIR or /tmp */ static struct option longopts[] = { @@ -160,6 +160,7 @@ main(int argc, char **argv) size_t diffargc = 0, wflag = WIDTH; int ch, fd[2], status; pid_t pid; + const char *outfile = NULL; char **diffargv, *diffprog = "diff", *filename1, *filename2, *tmp1, *tmp2, *s1, *s2; @@ -218,8 +219,7 @@ main(int argc, char **argv) lflag = 1; break; case 'o': - if ((outfile = fopen(optarg, "w")) == NULL) - err(2, "could not open: %s", optarg); + outfile = optarg; break; case 'S': diffargv[diffargc++] = "--strip-trailing-cr"; @@ -250,6 +250,9 @@ main(int argc, char **argv) if (argc != 2) usage(); + if (outfile && (outfp = fopen(outfile, "w")) == NULL) + err(2, "could not open: %s", optarg); + if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') tmpdir = _PATH_TMP; @@ -375,7 +378,6 @@ main(int argc, char **argv) static void printcol(const char *s, size_t *col, const size_t col_max) { - for (; *s && *col < col_max; ++s) { size_t new_col; @@ -441,7 +443,7 @@ prompt(const char *s1, const char *s2) case '1': /* Choose left column as-is. */ if (s1 != NULL) - fprintf(outfile, "%s\n", s1); + fprintf(outfp, "%s\n", s1); /* End of command parsing. */ break; @@ -453,7 +455,7 @@ prompt(const char *s1, const char *s2) case '2': /* Choose right column as-is. */ if (s2 != NULL) - fprintf(outfile, "%s\n", s2); + fprintf(outfp, "%s\n", s2); /* End of command parsing. */ break; @@ -486,7 +488,7 @@ PROMPT: * should quit. */ QUIT: - fclose(outfile); + fclose(outfp); exit(0); } @@ -886,11 +888,11 @@ processq(void) freediff(diffp); } - /* Write to outfile, prompting user if lines are different. */ - if (outfile) + /* Write to outfp, prompting user if lines are different. */ + if (outfp) switch (divc) { case ' ': case '(': case ')': - fprintf(outfile, "%s\n", left); + fprintf(outfp, "%s\n", left); break; case '|': case '<': case '>': prompt(left, right); |