From 07b0d2860d14640f898da7fd6375424b297f5d1f Mon Sep 17 00:00:00 2001 From: Edward Wang Date: Mon, 25 Jun 1990 07:02:01 -0800 Subject: [PATCH 1/1] remove call of _fwalk SCCS-vsn: usr.bin/mail/lex.c 5.21 SCCS-vsn: usr.bin/mail/names.c 5.16 SCCS-vsn: usr.bin/mail/edit.c 5.15 SCCS-vsn: usr.bin/mail/collect.c 5.22 SCCS-vsn: usr.bin/mail/cmd2.c 5.14 SCCS-vsn: usr.bin/mail/quit.c 5.15 SCCS-vsn: usr.bin/mail/send.c 5.21 SCCS-vsn: usr.bin/mail/glob.h 5.20 SCCS-vsn: usr.bin/mail/cmd3.c 5.24 SCCS-vsn: usr.bin/mail/main.c 5.27 SCCS-vsn: usr.bin/mail/fio.c 5.23 SCCS-vsn: usr.bin/mail/cmd1.c 5.21 SCCS-vsn: usr.bin/mail/popen.c 5.15 SCCS-vsn: usr.bin/mail/def.h 5.22 SCCS-vsn: usr.bin/mail/aux.c 5.20 --- usr/src/usr.bin/mail/aux.c | 11 ++-- usr/src/usr.bin/mail/cmd1.c | 26 ++++---- usr/src/usr.bin/mail/cmd2.c | 8 +-- usr/src/usr.bin/mail/cmd3.c | 6 +- usr/src/usr.bin/mail/collect.c | 36 +++++------ usr/src/usr.bin/mail/def.h | 3 +- usr/src/usr.bin/mail/edit.c | 17 +++-- usr/src/usr.bin/mail/fio.c | 24 +------ usr/src/usr.bin/mail/glob.h | 3 +- usr/src/usr.bin/mail/lex.c | 49 +++------------ usr/src/usr.bin/mail/main.c | 4 +- usr/src/usr.bin/mail/names.c | 50 +++++++++------ usr/src/usr.bin/mail/popen.c | 89 ++++++++++++++++++++++++-- usr/src/usr.bin/mail/quit.c | 110 ++++++++++++++++----------------- usr/src/usr.bin/mail/send.c | 26 ++++---- 15 files changed, 254 insertions(+), 208 deletions(-) diff --git a/usr/src/usr.bin/mail/aux.c b/usr/src/usr.bin/mail/aux.c index 338ae2f1af..b488b166a7 100644 --- a/usr/src/usr.bin/mail/aux.c +++ b/usr/src/usr.bin/mail/aux.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)aux.c 5.19 (Berkeley) %G%"; +static char sccsid[] = "@(#)aux.c 5.20 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -45,7 +45,8 @@ panic(fmt, a, b) fprintf(stderr, "panic: "); fprintf(stderr, fmt, a, b); putc('\n', stderr); - exit(1); + fflush(stdout); + abort(); } /* @@ -241,13 +242,13 @@ source(arglist) if ((cp = expand(*arglist)) == NOSTR) return(1); - if ((fi = fopen(cp, "r")) == NULL) { + if ((fi = Fopen(cp, "r")) == NULL) { perror(cp); return(1); } if (ssp >= NOFILE - 1) { printf("Too much \"sourcing\" going on.\n"); - fclose(fi); + Fclose(fi); return(1); } sstack[ssp].s_file = input; @@ -272,7 +273,7 @@ unstack() sourcing = 0; return(1); } - fclose(input); + Fclose(input); if (cond != CANY) printf("Unmatched \"if\"\n"); ssp--; diff --git a/usr/src/usr.bin/mail/cmd1.c b/usr/src/usr.bin/mail/cmd1.c index f7acf6c059..ead5e8f8ff 100644 --- a/usr/src/usr.bin/mail/cmd1.c +++ b/usr/src/usr.bin/mail/cmd1.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)cmd1.c 5.20 (Berkeley) %G%"; +static char sccsid[] = "@(#)cmd1.c 5.21 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -266,14 +266,8 @@ type1(msgvec, doign, page) FILE *obuf; obuf = stdout; - if (setjmp(pipestop)) { - if (obuf != stdout) { - pipef = NULL; - Pclose(obuf); - } - signal(SIGPIPE, SIG_DFL); - return(0); - } + if (setjmp(pipestop)) + goto close_pipe; if (value("interactive") != NOSTR && (page || (cp = value("crt")) != NOSTR)) { nlines = 0; @@ -289,10 +283,8 @@ type1(msgvec, doign, page) if (obuf == NULL) { perror(cp); obuf = stdout; - } else { - pipef = obuf; + } else signal(SIGPIPE, brokpipe); - } } } for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) { @@ -303,17 +295,21 @@ type1(msgvec, doign, page) fprintf(obuf, "Message %d:\n", *ip); (void) send(mp, obuf, doign ? ignore : 0, NOSTR); } +close_pipe: if (obuf != stdout) { - pipef = NULL; + /* + * Ignore SIGPIPE so it can't cause a duplicate close. + */ + signal(SIGPIPE, SIG_IGN); Pclose(obuf); + signal(SIGPIPE, SIG_DFL); } - signal(SIGPIPE, SIG_DFL); return(0); } /* * Respond to a broken pipe signal -- - * probably caused by using quitting more. + * probably caused by quitting more. */ brokpipe() diff --git a/usr/src/usr.bin/mail/cmd2.c b/usr/src/usr.bin/mail/cmd2.c index ccfb3d4b62..8daa7943e0 100644 --- a/usr/src/usr.bin/mail/cmd2.c +++ b/usr/src/usr.bin/mail/cmd2.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)cmd2.c 5.13 (Berkeley) %G%"; +static char sccsid[] = "@(#)cmd2.c 5.14 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -155,7 +155,7 @@ save1(str, mark, cmd, ignore) disp = "[Appended]"; else disp = "[New file]"; - if ((obuf = fopen(file, "a")) == NULL) { + if ((obuf = Fopen(file, "a")) == NULL) { perror(NOSTR); return(1); } @@ -164,7 +164,7 @@ save1(str, mark, cmd, ignore) touch(mp); if (send(mp, obuf, ignore, NOSTR) < 0) { perror(file); - fclose(obuf); + Fclose(obuf); return(1); } if (mark) @@ -173,7 +173,7 @@ save1(str, mark, cmd, ignore) fflush(obuf); if (ferror(obuf)) perror(file); - fclose(obuf); + Fclose(obuf); printf("%s\n", disp); return(0); } diff --git a/usr/src/usr.bin/mail/cmd3.c b/usr/src/usr.bin/mail/cmd3.c index be45d32b8b..c890540df3 100644 --- a/usr/src/usr.bin/mail/cmd3.c +++ b/usr/src/usr.bin/mail/cmd3.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)cmd3.c 5.23 (Berkeley) %G%"; +static char sccsid[] = "@(#)cmd3.c 5.24 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -120,13 +120,13 @@ help() register c; register FILE *f; - if ((f = fopen(_PATH_HELP, "r")) == NULL) { + if ((f = Fopen(_PATH_HELP, "r")) == NULL) { perror(_PATH_HELP); return(1); } while ((c = getc(f)) != EOF) putchar(c); - fclose(f); + Fclose(f); return(0); } diff --git a/usr/src/usr.bin/mail/collect.c b/usr/src/usr.bin/mail/collect.c index ade3e43cd5..8a3bc951fd 100644 --- a/usr/src/usr.bin/mail/collect.c +++ b/usr/src/usr.bin/mail/collect.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)collect.c 5.21 (Berkeley) %G%"; +static char sccsid[] = "@(#)collect.c 5.22 (Berkeley) %G%"; #endif /* not lint */ /* @@ -75,7 +75,7 @@ collect(hp, printheaders) sigsetmask(omask & ~(sigmask(SIGINT) | sigmask(SIGHUP))); noreset++; - if ((collf = fopen(tempMail, "w+")) == NULL) { + if ((collf = Fopen(tempMail, "w+")) == NULL) { perror(tempMail); goto err; } @@ -247,7 +247,7 @@ cont: printf("%s: Directory\n", cp); break; } - if ((fbuf = fopen(cp, "r")) == NULL) { + if ((fbuf = Fopen(cp, "r")) == NULL) { perror(cp); break; } @@ -258,12 +258,12 @@ cont: while (readline(fbuf, linebuf, LINESIZE) >= 0) { lc++; if ((t = putline(collf, linebuf)) < 0) { - fclose(fbuf); + Fclose(fbuf); goto err; } cc += t; } - fclose(fbuf); + Fclose(fbuf); printf("%d/%d\n", lc, cc); break; case 'w': @@ -296,13 +296,13 @@ cont: goto err; goto cont; case '?': - if ((fbuf = fopen(_PATH_TILDE, "r")) == NULL) { + if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) { perror(_PATH_TILDE); break; } while ((t = getc(fbuf)) != EOF) (void) putchar(t); - fclose(fbuf); + Fclose(fbuf); break; case 'p': /* @@ -338,7 +338,7 @@ cont: goto out; err: if (collf != NULL) { - fclose(collf); + Fclose(collf); collf = NULL; } out: @@ -379,7 +379,7 @@ exwrite(name, fp, f) fprintf(stderr, "File exists\n"); return(-1); } - if ((of = fopen(name, "w")) == NULL) { + if ((of = Fopen(name, "w")) == NULL) { perror(NOSTR); return(-1); } @@ -392,11 +392,11 @@ exwrite(name, fp, f) (void) putc(c, of); if (ferror(of)) { perror(name); - fclose(of); + Fclose(of); return(-1); } } - fclose(of); + Fclose(of); printf("%d/%ld\n", lc, cc); fflush(stdout); return(0); @@ -415,7 +415,7 @@ mesedit(fp, c) if (nf != NULL) { fseek(nf, (off_t)0, 2); collf = nf; - fclose(fp); + Fclose(fp); } (void) signal(SIGINT, sigint); } @@ -434,7 +434,7 @@ mespipe(fp, cmd) sig_t sigint = signal(SIGINT, SIG_IGN); extern char tempEdit[]; - if ((nf = fopen(tempEdit, "w+")) == NULL) { + if ((nf = Fopen(tempEdit, "w+")) == NULL) { perror(tempEdit); goto out; } @@ -444,12 +444,12 @@ mespipe(fp, cmd) * stdout = new message. */ if (run_command(cmd, 0, fileno(fp), fileno(nf), NOSTR) < 0) { - (void) fclose(nf); + (void) Fclose(nf); goto out; } if (fsize(nf) == 0) { fprintf(stderr, "No bytes from \"%s\" !?\n", cmd); - (void) fclose(nf); + (void) Fclose(nf); goto out; } /* @@ -457,7 +457,7 @@ mespipe(fp, cmd) */ (void) fseek(nf, 0L, 2); collf = nf; - (void) fclose(fp); + (void) Fclose(fp); out: (void) signal(SIGINT, sigint); } @@ -580,12 +580,12 @@ savedeadletter(fp) return; cp = getdeadletter(); c = umask(077); - dbuf = fopen(cp, "a"); + dbuf = Fopen(cp, "a"); (void) umask(c); if (dbuf == NULL) return; while ((c = getc(fp)) != EOF) (void) putc(c, dbuf); - fclose(dbuf); + Fclose(dbuf); rewind(fp); } diff --git a/usr/src/usr.bin/mail/def.h b/usr/src/usr.bin/mail/def.h index ea966eee3a..0fc95eb2c0 100644 --- a/usr/src/usr.bin/mail/def.h +++ b/usr/src/usr.bin/mail/def.h @@ -4,7 +4,7 @@ * * %sccs.include.redist.c% * - * @(#)def.h 5.21 (Berkeley) %G% + * @(#)def.h 5.22 (Berkeley) %G% */ #include /* includes */ @@ -246,6 +246,7 @@ struct ignoretab { * Forward declarations of routine types to keep lint and cc happy. */ +FILE *Fopen(); FILE *Fdopen(); FILE *Popen(); FILE *collect(); diff --git a/usr/src/usr.bin/mail/edit.c b/usr/src/usr.bin/mail/edit.c index e91006c786..36eae7e797 100644 --- a/usr/src/usr.bin/mail/edit.c +++ b/usr/src/usr.bin/mail/edit.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)edit.c 5.14 (Berkeley) %G%"; +static char sccsid[] = "@(#)edit.c 5.15 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -96,7 +96,7 @@ edit1(msgvec, type) } if (ferror(otf)) perror("/tmp"); - (void) fclose(fp); + (void) Fclose(fp); } (void) signal(SIGINT, sigint); } @@ -126,7 +126,7 @@ run_editor(fp, size, type, readonly) perror(tempEdit); goto out; } - if ((nf = fdopen(t, "w")) == NULL) { + if ((nf = Fdopen(t, "w")) == NULL) { perror(tempEdit); (void) unlink(tempEdit); goto out; @@ -142,7 +142,14 @@ run_editor(fp, size, type, readonly) modtime = 0; else modtime = statb.st_mtime; - if (ferror(nf) || fclose(nf) < 0) { + if (ferror(nf)) { + (void) Fclose(nf); + perror(tempEdit); + (void) unlink(tempEdit); + nf = NULL; + goto out; + } + if (Fclose(nf) < 0) { perror(tempEdit); (void) unlink(tempEdit); nf = NULL; @@ -174,7 +181,7 @@ run_editor(fp, size, type, readonly) /* * Now switch to new file. */ - if ((nf = fopen(tempEdit, "a+")) == NULL) { + if ((nf = Fopen(tempEdit, "a+")) == NULL) { perror(tempEdit); (void) unlink(tempEdit); goto out; diff --git a/usr/src/usr.bin/mail/fio.c b/usr/src/usr.bin/mail/fio.c index 5dcfe7d32c..8f9e6c2145 100644 --- a/usr/src/usr.bin/mail/fio.c +++ b/usr/src/usr.bin/mail/fio.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)fio.c 5.22 (Berkeley) %G%"; +static char sccsid[] = "@(#)fio.c 5.23 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -40,7 +40,7 @@ setptr(ibuf) if ((c = opentemp(tempSet)) < 0) exit(1); - if ((mestmp = fdopen(c, "r+")) == NULL) + if ((mestmp = Fdopen(c, "r+")) == NULL) panic("Can't open temporary"); msgCount = 0; maybe = 1; @@ -57,7 +57,6 @@ setptr(ibuf) perror(tempSet); exit(1); } - fclose(ibuf); makemessage(mestmp); return; } @@ -186,7 +185,7 @@ makemessage(f) panic("Message temporary file corrupted"); message[msgCount].m_size = 0; message[msgCount].m_lines = 0; - fclose(f); + Fclose(f); } /* @@ -405,20 +404,3 @@ getdeadletter() } return cp; } - -/* - * A nicer version of Fdopen, which allows us to fclose - * without losing the open file. - */ -FILE * -Fdopen(fildes, mode) - char *mode; -{ - int f; - - if ((f = dup(fildes)) < 0) { - perror("dup"); - return (NULL); - } - return fdopen(f, mode); -} diff --git a/usr/src/usr.bin/mail/glob.h b/usr/src/usr.bin/mail/glob.h index 1af1e83e08..14712b2a69 100644 --- a/usr/src/usr.bin/mail/glob.h +++ b/usr/src/usr.bin/mail/glob.h @@ -4,7 +4,7 @@ * * %sccs.include.redist.c% * - * @(#)glob.h 5.19 (Berkeley) %G% + * @(#)glob.h 5.20 (Berkeley) %G% */ /* @@ -25,7 +25,6 @@ int loading; /* Loading user definitions */ int cond; /* Current state of conditional exc. */ FILE *itf; /* Input temp file buffer */ FILE *otf; /* Output temp file buffer */ -FILE *pipef; /* Pipe file we have opened */ int image; /* File descriptor for image of msg */ FILE *input; /* Current command input file */ char mailname[PATHSIZE]; /* Name of current file */ diff --git a/usr/src/usr.bin/mail/lex.c b/usr/src/usr.bin/mail/lex.c index fab67f1cae..055221559c 100644 --- a/usr/src/usr.bin/mail/lex.c +++ b/usr/src/usr.bin/mail/lex.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)lex.c 5.20 (Berkeley) %G%"; +static char sccsid[] = "@(#)lex.c 5.21 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -42,7 +42,7 @@ setfile(name) if ((name = expand(name)) == NOSTR) return -1; - if ((ibuf = fopen(name, "r")) == NULL) { + if ((ibuf = Fopen(name, "r")) == NULL) { if (!isedit && errno == ENOENT) goto nomail; perror(name); @@ -51,13 +51,13 @@ setfile(name) if (fstat(fileno(ibuf), &stb) < 0) { perror("fstat"); - fclose(ibuf); + Fclose(ibuf); return (-1); } switch (stb.st_mode & S_IFMT) { case S_IFDIR: - fclose(ibuf); + Fclose(ibuf); errno = EISDIR; perror(name); return (-1); @@ -66,7 +66,7 @@ setfile(name) break; default: - fclose(ibuf); + Fclose(ibuf); errno = EINVAL; perror(name); return (-1); @@ -114,7 +114,7 @@ setfile(name) remove(tempMesg); setptr(ibuf); setmsize(msgCount); - fclose(ibuf); + Fclose(ibuf); relsesigs(); sawcom = 0; if (!edit && msgCount == 0) { @@ -458,34 +458,6 @@ isprefix(as1, as2) int inithdr; /* am printing startup headers */ -#ifdef _NFILE -static -_fwalk(function) - register int (*function)(); -{ - register FILE *iop; - - for (iop = _iob; iop < _iob + _NFILE; iop++) - (*function)(iop); -} -#endif - -static -xclose(iop) - register FILE *iop; -{ - if (iop == stdin || iop == stdout || - iop == stderr || iop == itf || iop == otf) - return; - - if (iop != pipef) - fclose(iop); - else { - Pclose(pipef); - pipef = NULL; - } -} - /*ARGSUSED*/ intr(s) { @@ -497,10 +469,7 @@ intr(s) while (sourcing) unstack(); - /* - * Walk through all the open FILEs, applying xclose() to them - */ - _fwalk(xclose); + close_all_files(); if (image >= 0) { close(image); @@ -638,7 +607,7 @@ load(name) { register FILE *in, *oldin; - if ((in = fopen(name, "r")) == NULL) + if ((in = Fopen(name, "r")) == NULL) return; oldin = input; input = in; @@ -648,5 +617,5 @@ load(name) loading = 0; sourcing = 0; input = oldin; - fclose(in); + Fclose(in); } diff --git a/usr/src/usr.bin/mail/main.c b/usr/src/usr.bin/mail/main.c index ffa6922337..23c1d8447a 100644 --- a/usr/src/usr.bin/mail/main.c +++ b/usr/src/usr.bin/mail/main.c @@ -12,7 +12,7 @@ char copyright[] = #endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)main.c 5.26 (Berkeley) %G%"; +static char sccsid[] = "@(#)main.c 5.27 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -184,7 +184,7 @@ Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\ load(_PATH_MASTER_RC); /* * Expand returns a savestr, but load only uses the file name - * for fopen, so it's save to do this. + * for fopen, so it's safe to do this. */ load(expand("~/.mailrc")); if (!rcvmode) { diff --git a/usr/src/usr.bin/mail/names.c b/usr/src/usr.bin/mail/names.c index 8101bf86c4..c98c0250de 100644 --- a/usr/src/usr.bin/mail/names.c +++ b/usr/src/usr.bin/mail/names.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)names.c 5.15 (Berkeley) %G%"; +static char sccsid[] = "@(#)names.c 5.16 (Berkeley) %G%"; #endif /* not lint */ /* @@ -215,7 +215,7 @@ outof(names, fo, hp) */ if (image < 0) { - if ((fout = fopen(tempEdit, "a")) == NULL) { + if ((fout = Fopen(tempEdit, "a")) == NULL) { perror(tempEdit); senderr++; goto cant; @@ -225,20 +225,19 @@ outof(names, fo, hp) if (image < 0) { perror(tempEdit); senderr++; + (void) Fclose(fout); goto cant; } - else { - fprintf(fout, "From %s %s", myname, date); - puthead(hp, fout, GTO|GSUBJECT|GCC|GNL); - while ((c = getc(fo)) != EOF) - (void) putc(c, fout); - rewind(fo); - (void) putc('\n', fout); - (void) fflush(fout); - if (ferror(fout)) - perror(tempEdit); - (void) fclose(fout); - } + fprintf(fout, "From %s %s", myname, date); + puthead(hp, fout, GTO|GSUBJECT|GCC|GNL); + while ((c = getc(fo)) != EOF) + (void) putc(c, fout); + rewind(fo); + (void) putc('\n', fout); + (void) fflush(fout); + if (ferror(fout)) + perror(tempEdit); + (void) Fclose(fout); } /* @@ -251,7 +250,13 @@ outof(names, fo, hp) int pid; char *shell; - /* XXX, can't really reuse the same image file */ + /* + * XXX + * We can't really reuse the same image file, + * because multiple piped recipients will + * share the same lseek location and trample + * on one another. + */ if ((shell = value("SHELL")) == NOSTR) shell = _PATH_CSHELL; pid = start_command(shell, sigmask(SIGHUP)| @@ -263,15 +268,20 @@ outof(names, fo, hp) } free_child(pid); } else { - if ((fout = fopen(fname, "a")) == NULL) { + int f; + if ((fout = Fopen(fname, "a")) == NULL) { perror(fname); senderr++; goto cant; } - fin = Fdopen(image, "r"); + if ((f = dup(image)) < 0) { + perror("dup"); + fin = NULL; + } else + fin = Fdopen(f, "r"); if (fin == NULL) { fprintf(stderr, "Can't reopen image\n"); - (void) fclose(fout); + (void) Fclose(fout); senderr++; goto cant; } @@ -280,8 +290,8 @@ outof(names, fo, hp) (void) putc(c, fout); if (ferror(fout)) senderr++, perror(fname); - (void) fclose(fout); - (void) fclose(fin); + (void) Fclose(fout); + (void) Fclose(fin); } cant: /* diff --git a/usr/src/usr.bin/mail/popen.c b/usr/src/usr.bin/mail/popen.c index 33a0e8a3e4..623801ecdd 100644 --- a/usr/src/usr.bin/mail/popen.c +++ b/usr/src/usr.bin/mail/popen.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)popen.c 5.14 (Berkeley) %G%"; +static char sccsid[] = "@(#)popen.c 5.15 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -17,6 +17,42 @@ static char sccsid[] = "@(#)popen.c 5.14 (Berkeley) %G%"; #define WRITE 1 static int *pid; +struct fp { + FILE *fp; + int pipe; + struct fp *link; +}; +static struct fp *fp_head; + +FILE * +Fopen(file, mode) + char *file, *mode; +{ + FILE *fp; + + if ((fp = fopen(file, mode)) != NULL) + register_file(fp, 0); + return fp; +} + +FILE * +Fdopen(fd, mode) + char *mode; +{ + FILE *fp; + + if ((fp = fdopen(fd, mode)) != NULL) + register_file(fp, 0); + return fp; +} + +Fclose(fp) + FILE *fp; +{ + unregister_file(fp); + return fclose(fp); +} + FILE * Popen(cmd, mode) char *cmd; @@ -24,6 +60,7 @@ Popen(cmd, mode) { int p[2]; int myside, hisside, fd0, fd1; + FILE *fp; if (pid == 0) pid = (int *) malloc((unsigned) sizeof (int) * getdtablesize()); @@ -43,8 +80,10 @@ Popen(cmd, mode) close(p[WRITE]); return NULL; } - close(hisside); - return fdopen(myside, mode); + (void) close(hisside); + if ((fp = fdopen(myside, mode)) != NULL) + register_file(fp, 1); + return fp; } Pclose(ptr) @@ -54,13 +93,55 @@ Pclose(ptr) int omask; i = fileno(ptr); - fclose(ptr); + unregister_file(ptr); + (void) fclose(ptr); omask = sigblock(sigmask(SIGINT)|sigmask(SIGHUP)); i = wait_child(pid[i]); sigsetmask(omask); return i; } +close_all_files() +{ + + while (fp_head) + if (fp_head->pipe) + (void) Pclose(fp_head->fp); + else + (void) Fclose(fp_head->fp); +} + +register_file(fp, pipe) + FILE *fp; +{ + struct fp *fpp; + + if ((fpp = (struct fp *) malloc(sizeof *fpp)) == NULL) + panic("Out of memory"); + fpp->fp = fp; + fpp->pipe = pipe; + fpp->link = fp_head; + fp_head = fpp; +} + +unregister_file(fp) + FILE *fp; +{ + struct fp **pp, *p; + + for (pp = &fp_head; p = *pp; pp = &p->link) + if (p->fp == fp) { + *pp = p->link; + free((char *) p); + return; + } + /* XXX + * Ignore this for now; there may still be uncaught + * duplicate closes. + panic("Invalid file pointer"); + */ +} + /* * Run a command without a shell, with optional arguments and splicing * of stdin and stdout. The command name can be a sequence of words. diff --git a/usr/src/usr.bin/mail/quit.c b/usr/src/usr.bin/mail/quit.c index c0466f213f..68d13120ae 100644 --- a/usr/src/usr.bin/mail/quit.c +++ b/usr/src/usr.bin/mail/quit.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)quit.c 5.14 (Berkeley) %G%"; +static char sccsid[] = "@(#)quit.c 5.15 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -74,14 +74,14 @@ quit() * a message. */ - fbuf = fopen(mailname, "r"); + fbuf = Fopen(mailname, "r"); if (fbuf == NULL) goto newmail; flock(fileno(fbuf), LOCK_EX); rbuf = NULL; if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) { printf("New mail has arrived.\n"); - rbuf = fopen(tempResid, "w"); + rbuf = Fopen(tempResid, "w"); if (rbuf == NULL || fbuf == NULL) goto newmail; #ifdef APPEND @@ -97,8 +97,8 @@ quit() (void) putc(c, rbuf); } #endif - fclose(rbuf); - if ((rbuf = fopen(tempResid, "r")) == NULL) + Fclose(rbuf); + if ((rbuf = Fopen(tempResid, "r")) == NULL) goto newmail; remove(tempResid); } @@ -127,7 +127,7 @@ quit() } modify = 0; if (Tflag != NOSTR) { - if ((readstat = fopen(Tflag, "w")) == NULL) + if ((readstat = Fopen(Tflag, "w")) == NULL) Tflag = NOSTR; } for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) { @@ -145,17 +145,17 @@ quit() } } if (Tflag != NOSTR) - fclose(readstat); + Fclose(readstat); if (p == msgCount && !modify && !anystat) { printf("Held %d message%s in %s\n", p, p == 1 ? "" : "s", mailname); - fclose(fbuf); + Fclose(fbuf); return; } if (c == 0) { if (p != 0) { writeback(rbuf); - fclose(fbuf); + Fclose(fbuf); return; } goto cream; @@ -171,44 +171,44 @@ quit() mbox = expand("&"); mcount = c; if (value("append") == NOSTR) { - if ((obuf = fopen(tempQuit, "w")) == NULL) { + if ((obuf = Fopen(tempQuit, "w")) == NULL) { perror(tempQuit); - fclose(fbuf); + Fclose(fbuf); return; } - if ((ibuf = fopen(tempQuit, "r")) == NULL) { + if ((ibuf = Fopen(tempQuit, "r")) == NULL) { perror(tempQuit); remove(tempQuit); - fclose(obuf); - fclose(fbuf); + Fclose(obuf); + Fclose(fbuf); return; } remove(tempQuit); - if ((abuf = fopen(mbox, "r")) != NULL) { + if ((abuf = Fopen(mbox, "r")) != NULL) { while ((c = getc(abuf)) != EOF) (void) putc(c, obuf); - fclose(abuf); + Fclose(abuf); } if (ferror(obuf)) { perror(tempQuit); - fclose(ibuf); - fclose(obuf); - fclose(fbuf); + Fclose(ibuf); + Fclose(obuf); + Fclose(fbuf); return; } - fclose(obuf); + Fclose(obuf); close(creat(mbox, 0600)); - if ((obuf = fopen(mbox, "r+")) == NULL) { + if ((obuf = Fopen(mbox, "r+")) == NULL) { perror(mbox); - fclose(ibuf); - fclose(fbuf); + Fclose(ibuf); + Fclose(fbuf); return; } } if (value("append") != NOSTR) { - if ((obuf = fopen(mbox, "a")) == NULL) { + if ((obuf = Fopen(mbox, "a")) == NULL) { perror(mbox); - fclose(fbuf); + Fclose(fbuf); return; } fchmod(fileno(obuf), 0600); @@ -217,9 +217,9 @@ quit() if (mp->m_flag & MBOX) if (send(mp, obuf, saveignore, NOSTR) < 0) { perror(mbox); - fclose(ibuf); - fclose(obuf); - fclose(fbuf); + Fclose(ibuf); + Fclose(obuf); + Fclose(fbuf); return; } @@ -238,17 +238,17 @@ quit() break; c = getc(ibuf); } - fclose(ibuf); + Fclose(ibuf); fflush(obuf); } trunc(obuf); if (ferror(obuf)) { perror(mbox); - fclose(obuf); - fclose(fbuf); + Fclose(obuf); + Fclose(fbuf); return; } - fclose(obuf); + Fclose(obuf); if (mcount == 1) printf("Saved 1 message in mbox\n"); else @@ -261,7 +261,7 @@ quit() if (p != 0) { writeback(rbuf); - fclose(fbuf); + Fclose(fbuf); return; } @@ -272,26 +272,26 @@ quit() cream: if (rbuf != NULL) { - abuf = fopen(mailname, "r+"); + abuf = Fopen(mailname, "r+"); if (abuf == NULL) goto newmail; while ((c = getc(rbuf)) != EOF) (void) putc(c, abuf); - fclose(rbuf); + Fclose(rbuf); trunc(abuf); - fclose(abuf); + Fclose(abuf); alter(mailname); - fclose(fbuf); + Fclose(fbuf); return; } demail(); - fclose(fbuf); + Fclose(fbuf); return; newmail: printf("Thou hast new mail.\n"); if (fbuf != NULL) - fclose(fbuf); + Fclose(fbuf); } /* @@ -308,7 +308,7 @@ writeback(res) FILE *obuf; p = 0; - if ((obuf = fopen(mailname, "r+")) == NULL) { + if ((obuf = Fopen(mailname, "r+")) == NULL) { perror(mailname); return(-1); } @@ -322,7 +322,7 @@ writeback(res) p++; if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) { perror(mailname); - fclose(obuf); + Fclose(obuf); return(-1); } } @@ -335,12 +335,12 @@ writeback(res) trunc(obuf); if (ferror(obuf)) { perror(mailname); - fclose(obuf); + Fclose(obuf); return(-1); } if (res != NULL) - fclose(res); - fclose(obuf); + Fclose(res); + Fclose(obuf); alter(mailname); if (p == 1) printf("Held 1 message in %s\n", mailname); @@ -366,7 +366,7 @@ edstop() return; holdsigs(); if (Tflag != NOSTR) { - if ((readstat = fopen(Tflag, "w")) == NULL) + if ((readstat = Fopen(Tflag, "w")) == NULL) Tflag = NOSTR; } for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) { @@ -384,7 +384,7 @@ edstop() } } if (Tflag != NOSTR) - fclose(readstat); + Fclose(readstat); if (!gotcha || Tflag != NOSTR) goto done; ibuf = NULL; @@ -392,14 +392,14 @@ edstop() strcpy(tempname, _PATH_TMP); strcat(tempname, "mboxXXXXXX"); mktemp(tempname); - if ((obuf = fopen(tempname, "w")) == NULL) { + if ((obuf = Fopen(tempname, "w")) == NULL) { perror(tempname); relsesigs(); reset(0); } - if ((ibuf = fopen(mailname, "r")) == NULL) { + if ((ibuf = Fopen(mailname, "r")) == NULL) { perror(mailname); - fclose(obuf); + Fclose(obuf); remove(tempname); relsesigs(); reset(0); @@ -407,9 +407,9 @@ edstop() fseek(ibuf, mailsize, 0); while ((c = getc(ibuf)) != EOF) (void) putc(c, obuf); - fclose(ibuf); - fclose(obuf); - if ((ibuf = fopen(tempname, "r")) == NULL) { + Fclose(ibuf); + Fclose(obuf); + if ((ibuf = Fopen(tempname, "r")) == NULL) { perror(tempname); remove(tempname); relsesigs(); @@ -419,7 +419,7 @@ edstop() } printf("\"%s\" ", mailname); fflush(stdout); - if ((obuf = fopen(mailname, "r+")) == NULL) { + if ((obuf = Fopen(mailname, "r+")) == NULL) { perror(mailname); relsesigs(); reset(0); @@ -440,7 +440,7 @@ edstop() if (ibuf != NULL) { while ((c = getc(ibuf)) != EOF) (void) putc(c, obuf); - fclose(ibuf); + Fclose(ibuf); } fflush(obuf); if (ferror(obuf)) { @@ -448,7 +448,7 @@ edstop() relsesigs(); reset(0); } - fclose(obuf); + Fclose(obuf); if (gotcha) { remove(mailname); printf("removed\n"); diff --git a/usr/src/usr.bin/mail/send.c b/usr/src/usr.bin/mail/send.c index 99ace8e84f..4cc429944b 100644 --- a/usr/src/usr.bin/mail/send.c +++ b/usr/src/usr.bin/mail/send.c @@ -6,7 +6,7 @@ */ #ifndef lint -static char sccsid[] = "@(#)send.c 5.20 (Berkeley) %G%"; +static char sccsid[] = "@(#)send.c 5.21 (Berkeley) %G%"; #endif /* not lint */ #include "rcv.h" @@ -337,10 +337,10 @@ mail1(hp, printheaders) if (access(_PATH_MAIL_LOG, 0) == 0) { FILE *postage; - if ((postage = fopen(_PATH_MAIL_LOG, "a")) != NULL) { + if ((postage = Fopen(_PATH_MAIL_LOG, "a")) != NULL) { fprintf(postage, "%s %d %ld\n", myname, count(to), fsize(mtf)); - (void) fclose(postage); + (void) Fclose(postage); } } prepare_child(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)| @@ -359,7 +359,7 @@ mail1(hp, printheaders) else free_child(pid); out: - (void) fclose(mtf); + (void) Fclose(mtf); } /* @@ -400,13 +400,13 @@ infix(hp, fi) register FILE *nfo, *nfi; register int c; - if ((nfo = fopen(tempMail, "w")) == NULL) { + if ((nfo = Fopen(tempMail, "w")) == NULL) { perror(tempMail); return(fi); } - if ((nfi = fopen(tempMail, "r")) == NULL) { + if ((nfi = Fopen(tempMail, "r")) == NULL) { perror(tempMail); - (void) fclose(nfo); + (void) Fclose(nfo); return(fi); } (void) remove(tempMail); @@ -424,13 +424,13 @@ infix(hp, fi) (void) fflush(nfo); if (ferror(nfo)) { perror(tempMail); - (void) fclose(nfo); - (void) fclose(nfi); + (void) Fclose(nfo); + (void) Fclose(nfi); rewind(fi); return(fi); } - (void) fclose(nfo); - (void) fclose(fi); + (void) Fclose(nfo); + (void) Fclose(fi); rewind(nfi); return(nfi); } @@ -507,7 +507,7 @@ savemail(name, fi) time_t now, time(); char *ctime(); - if ((fo = fopen(name, "a")) == NULL) { + if ((fo = Fopen(name, "a")) == NULL) { perror(name); return (-1); } @@ -519,7 +519,7 @@ savemail(name, fi) (void) fflush(fo); if (ferror(fo)) perror(name); - (void) fclose(fo); + (void) Fclose(fo); rewind(fi); return (0); } -- 2.20.1