386BSD 0.1 development
[unix-history] / usr / src / usr.bin / mail / fio.c
index 5dcfe7d..4eff69b 100644 (file)
@@ -2,17 +2,44 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)fio.c      5.22 (Berkeley) %G%";
+static char sccsid[] = "@(#)fio.c      5.24 (Berkeley) 2/3/91";
 #endif /* not lint */
 
 #include "rcv.h"
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/wait.h>
 #endif /* not lint */
 
 #include "rcv.h"
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/wait.h>
+#include <paths.h>
 #include <errno.h>
 
 /*
 #include <errno.h>
 
 /*
@@ -22,26 +49,28 @@ static char sccsid[] = "@(#)fio.c   5.22 (Berkeley) %G%";
  */
 
 /*
  */
 
 /*
- * Set up the input pointers while copying the mail file into
- * /tmp.
+ * Set up the input pointers while copying the mail file into /tmp.
  */
 setptr(ibuf)
        register FILE *ibuf;
 {
  */
 setptr(ibuf)
        register FILE *ibuf;
 {
-       register c;
+       register int c, count;
        register char *cp, *cp2;
        register char *cp, *cp2;
-       register count;
-       char linebuf[LINESIZE];
-       int maybe, inhead;
+       struct message this;
        FILE *mestmp;
        off_t offset;
        FILE *mestmp;
        off_t offset;
-       struct message this;
-       extern char tempSet[];
+       int maybe, inhead;
+       char linebuf[LINESIZE];
 
 
-       if ((c = opentemp(tempSet)) < 0)
+       /* Get temporary file. */
+       (void)sprintf(linebuf, "%s/mail.XXXXXX", _PATH_TMP);
+       if ((c = mkstemp(linebuf)) == -1 ||
+           (mestmp = Fdopen(c, "r+")) == NULL) {
+               (void)fprintf(stderr, "mail: can't open %s\n", linebuf);
                exit(1);
                exit(1);
-       if ((mestmp = fdopen(c, "r+")) == NULL)
-               panic("Can't open temporary");
+       }
+       (void)unlink(linebuf);
+
        msgCount = 0;
        maybe = 1;
        inhead = 0;
        msgCount = 0;
        maybe = 1;
        inhead = 0;
@@ -54,10 +83,9 @@ setptr(ibuf)
        for (;;) {
                if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
                        if (append(&this, mestmp)) {
        for (;;) {
                if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
                        if (append(&this, mestmp)) {
-                               perror(tempSet);
+                               perror("temporary file");
                                exit(1);
                        }
                                exit(1);
                        }
-                       fclose(ibuf);
                        makemessage(mestmp);
                        return;
                }
                        makemessage(mestmp);
                        return;
                }
@@ -71,7 +99,7 @@ setptr(ibuf)
                if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
                        msgCount++;
                        if (append(&this, mestmp)) {
                if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
                        msgCount++;
                        if (append(&this, mestmp)) {
-                               perror(tempSet);
+                               perror("temporary file");
                                exit(1);
                        }
                        this.m_flag = MUSED|MNEW;
                                exit(1);
                        }
                        this.m_flag = MUSED|MNEW;
@@ -186,7 +214,7 @@ makemessage(f)
                panic("Message temporary file corrupted");
        message[msgCount].m_size = 0;
        message[msgCount].m_lines = 0;
                panic("Message temporary file corrupted");
        message[msgCount].m_size = 0;
        message[msgCount].m_lines = 0;
-       fclose(f);
+       Fclose(f);
 }
 
 /*
 }
 
 /*
@@ -203,19 +231,18 @@ append(mp, f)
 /*
  * Delete a file, but only if the file is a plain file.
  */
 /*
  * Delete a file, but only if the file is a plain file.
  */
-remove(name)
-       char name[];
+rm(name)
+       char *name;
 {
 {
-       struct stat statb;
-       extern int errno;
+       struct stat sb;
 
 
-       if (stat(name, &statb) < 0)
+       if (stat(name, &sb) < 0)
                return(-1);
                return(-1);
-       if ((statb.st_mode & S_IFMT) != S_IFREG) {
+       if (!S_ISREG(sb.st_mode)) {
                errno = EISDIR;
                return(-1);
        }
                errno = EISDIR;
                return(-1);
        }
-       return unlink(name);
+       return(unlink(name));
 }
 
 static int sigdepth;           /* depth of holdsigs() */
 }
 
 static int sigdepth;           /* depth of holdsigs() */
@@ -240,21 +267,6 @@ relsesigs()
                sigsetmask(omask);
 }
 
                sigsetmask(omask);
 }
 
-/*
- * Open a temp file by creating and unlinking.
- * Return the open file descriptor.
- */
-opentemp(file)
-       char file[];
-{
-       int f;
-
-       if ((f = open(file, O_CREAT|O_EXCL|O_RDWR, 0600)) < 0)
-               perror(file);
-       remove(file);
-       return (f);
-}
-
 /*
  * Determine the size of the file possessed by
  * the passed buffer.
 /*
  * Determine the size of the file possessed by
  * the passed buffer.
@@ -405,20 +417,3 @@ getdeadletter()
        }
        return cp;
 }
        }
        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);
-}