Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/mail Check tmpdir for being empty and remove trailin...



details:   https://anonhg.NetBSD.org/src/rev/3f63cc99e93a
branches:  trunk
changeset: 523187:3f63cc99e93a
user:      wiz <wiz%NetBSD.org@localhost>
date:      Tue Mar 05 20:14:02 2002 +0000

description:
Check tmpdir for being empty and remove trailing slashes in it. Replace one
tempnam() with mkstemp(), and remove the tempQuit variable.
Inspired by OpenBSD.

diffstat:

 usr.bin/mail/quit.c |  25 +++++++++++++++----------
 usr.bin/mail/temp.c |  18 +++++++++++++-----
 2 files changed, 28 insertions(+), 15 deletions(-)

diffs (129 lines):

diff -r 6bd4280855a0 -r 3f63cc99e93a usr.bin/mail/quit.c
--- a/usr.bin/mail/quit.c       Tue Mar 05 19:53:19 2002 +0000
+++ b/usr.bin/mail/quit.c       Tue Mar 05 20:14:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: quit.c,v 1.14 2002/03/04 03:07:26 wiz Exp $    */
+/*     $NetBSD: quit.c,v 1.15 2002/03/05 20:14:02 wiz Exp $    */
 
 /*
  * Copyright (c) 1980, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)quit.c     8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: quit.c,v 1.14 2002/03/04 03:07:26 wiz Exp $");
+__RCSID("$NetBSD: quit.c,v 1.15 2002/03/05 20:14:02 wiz Exp $");
 #endif
 #endif /* not lint */
 
@@ -52,7 +52,7 @@
  */
 
 extern char *tmpdir;
-extern char *tempQuit, *tempResid;
+extern char *tempResid;
 
 /*
  * The "quit" command.
@@ -81,8 +81,10 @@
        FILE *ibuf = NULL, *obuf, *fbuf, *rbuf, *readstat = NULL, *abuf;
        struct message *mp;
        int c;
+       int fd; /* temporary file descriptor for temp file */
        struct stat minfo;
        char *mbox;
+       char tempname[PATHSIZE];
 
 #ifdef __GNUC__
        obuf = NULL;            /* XXX gcc -Wuninitialized */
@@ -227,28 +229,31 @@
        mbox = expand("&");
        mcount = c;
        if (value("append") == NULL) {
-               if ((obuf = Fopen(tempQuit, "w")) == NULL) {
-                       perror(tempQuit);
+               (void)snprintf(tempname, sizeof(tempname),
+                   "%s/mail.RmXXXXXXXXXX", tmpdir);
+               if ((fd = mkstemp(tempname)) == -1 ||
+                   (obuf = Fdopen(fd, "w")) == NULL) {
+                       warn("%s", tempname);
                        Fclose(fbuf);
                        dot_unlock(mailname);
                        return;
                }
-               if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
-                       perror(tempQuit);
-                       rm(tempQuit);
+               if ((ibuf = Fopen(tempname, "r")) == NULL) {
+                       warn("%s", tempname);
+                       rm(tempname);
                        Fclose(obuf);
                        Fclose(fbuf);
                        dot_unlock(mailname);
                        return;
                }
-               rm(tempQuit);
+               rm(tempname);
                if ((abuf = Fopen(mbox, "r")) != NULL) {
                        while ((c = getc(abuf)) != EOF)
                                (void) putc(c, obuf);
                        Fclose(abuf);
                }
                if (ferror(obuf)) {
-                       perror(tempQuit);
+                       warn("%s", tempname);
                        Fclose(ibuf);
                        Fclose(obuf);
                        Fclose(fbuf);
diff -r 6bd4280855a0 -r 3f63cc99e93a usr.bin/mail/temp.c
--- a/usr.bin/mail/temp.c       Tue Mar 05 19:53:19 2002 +0000
+++ b/usr.bin/mail/temp.c       Tue Mar 05 20:14:02 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: temp.c,v 1.9 2002/03/04 03:07:27 wiz Exp $     */
+/*     $NetBSD: temp.c,v 1.10 2002/03/05 20:14:02 wiz Exp $    */
 
 /*
  * Copyright (c) 1980, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)temp.c     8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: temp.c,v 1.9 2002/03/04 03:07:27 wiz Exp $");
+__RCSID("$NetBSD: temp.c,v 1.10 2002/03/05 20:14:02 wiz Exp $");
 #endif
 #endif /* not lint */
 
@@ -52,7 +52,6 @@
  */
 
 char   *tempMail;
-char   *tempQuit;
 char   *tempEdit;
 char   *tempResid;
 char   *tempMesg;
@@ -62,14 +61,23 @@
 tinit(void)
 {
        const char *cp;
+       char *p;
 
-       if ((tmpdir = getenv("TMPDIR")) == NULL) {
+       if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
                tmpdir = _PATH_TMP;
+
+       if ((tmpdir = strdup(tmpdir)) == NULL)
+               errx(1, "Out of memory");
+
+       /* Remove trailing slashes. */
+       p = tmpdir + strlen(tmpdir) - 1;
+       while (p > tmpdir && *p == '/') {
+               *p = '\0';
+               p--;
        }
 
        tempMail  = tempnam (tmpdir, "Rs");
        tempResid = tempnam (tmpdir, "Rq");
-       tempQuit  = tempnam (tmpdir, "Rm");
        tempEdit  = tempnam (tmpdir, "Re");
        tempMesg  = tempnam (tmpdir, "Rx");
 



Home | Main Index | Thread Index | Old Index