cleaned up file handling code, now allows "mbox" variable
authorEdward Wang <edward@ucbvax.Berkeley.EDU>
Sat, 18 Jun 1988 06:05:17 +0000 (22:05 -0800)
committerEdward Wang <edward@ucbvax.Berkeley.EDU>
Sat, 18 Jun 1988 06:05:17 +0000 (22:05 -0800)
SCCS-vsn: usr.bin/mail/v7.local.c 5.5
SCCS-vsn: usr.bin/mail/lex.c 5.12
SCCS-vsn: usr.bin/mail/quit.c 5.7
SCCS-vsn: usr.bin/mail/glob.h 5.8
SCCS-vsn: usr.bin/mail/temp.c 5.5
SCCS-vsn: usr.bin/mail/cmd3.c 5.8
SCCS-vsn: usr.bin/mail/main.c 5.12
SCCS-vsn: usr.bin/mail/fio.c 5.7
SCCS-vsn: usr.bin/mail/def.h 5.9

usr/src/usr.bin/mail/cmd3.c
usr/src/usr.bin/mail/def.h
usr/src/usr.bin/mail/fio.c
usr/src/usr.bin/mail/glob.h
usr/src/usr.bin/mail/lex.c
usr/src/usr.bin/mail/main.c
usr/src/usr.bin/mail/quit.c
usr/src/usr.bin/mail/temp.c
usr/src/usr.bin/mail/v7.local.c

index 899e6c3..29ad557 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifdef notdef
  */
 
 #ifdef notdef
-static char sccsid[] = "@(#)cmd3.c     5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)cmd3.c     5.8 (Berkeley) %G%";
 #endif /* notdef */
 
 #include "rcv.h"
 #endif /* notdef */
 
 #include "rcv.h"
@@ -575,110 +575,25 @@ null(e)
  */
 
 file(argv)
  */
 
 file(argv)
-       char **argv;
+       register char **argv;
 {
        register char *cp;
 {
        register char *cp;
-       int edit;
 
        if (argv[0] == NOSTR) {
                newfileinfo();
 
        if (argv[0] == NOSTR) {
                newfileinfo();
-               return(0);
+               return 0;
        }
        }
-
-       /*
-        * Acker's!  Must switch to the new file.
-        * We use a funny interpretation --
-        *      # -- gets the previous file
-        *      % -- gets the invoker's post office box
-        *      %user -- gets someone else's post office box
-        *      & -- gets invoker's mbox file
-        *      string -- reads the given file
-        */
-
-       cp = getfilename(argv[0], &edit);
-       if (cp == NOSTR)
-               return(-1);
-       if (setfile(cp, edit)) {
+       if ((cp = expand(*argv)) == NOSTR)
+               return -1;
+       strcpy(prevfile, mailname);
+       if (setfile(cp, **argv != '%')) {
                perror(cp);
                perror(cp);
-               return(-1);
+               return -1;
        }
        announce(0);
        return 0;
 }
 
        }
        announce(0);
        return 0;
 }
 
-/*
- * Evaluate the string given as a new mailbox name.
- * Ultimately, we want this to support a number of meta characters.
- * Possibly:
- *     % -- for my system mail box
- *     %user -- for user's system mail box
- *     # -- for previous file
- *     & -- get's invoker's mbox file
- *     file name -- for any other file
- */
-
-char   prevfile[PATHSIZE];
-
-char *
-getfilename(name, aedit)
-       char *name;
-       int *aedit;
-{
-       register char *cp;
-       char savename[BUFSIZ];
-       char oldmailname[BUFSIZ];
-
-       /*
-        * Assume we will be in "edit file" mode, until
-        * proven wrong.
-        */
-       *aedit = 1;
-       switch (*name) {
-       case '%':
-               *aedit = 0;
-               strcpy(prevfile, mailname);
-               if (name[1] != 0) {
-                       strcpy(savename, myname);
-                       strcpy(oldmailname, mailname);
-                       strncpy(myname, name+1, PATHSIZE-1);
-                       myname[PATHSIZE-1] = 0;
-                       findmail();
-                       cp = savestr(mailname);
-                       strcpy(myname, savename);
-                       strcpy(mailname, oldmailname);
-                       return(cp);
-               }
-               strcpy(oldmailname, mailname);
-               findmail();
-               cp = savestr(mailname);
-               strcpy(mailname, oldmailname);
-               return(cp);
-
-       case '#':
-               if (name[1] != 0)
-                       goto regular;
-               if (prevfile[0] == 0) {
-                       printf("No previous file\n");
-                       return(NOSTR);
-               }
-               cp = savestr(prevfile);
-               strcpy(prevfile, mailname);
-               return(cp);
-
-       case '&':
-               strcpy(prevfile, mailname);
-               if (name[1] == 0)
-                       return(mbox);
-               /* Fall into . . . */
-
-       default:
-regular:
-               strcpy(prevfile, mailname);
-               cp = expand(name);
-               return(cp);
-       }
-}
-
 /*
  * Expand file names like echo
  */
 /*
  * Expand file names like echo
  */
index 6a6394e..b0a03e2 100644 (file)
@@ -9,7 +9,7 @@
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  *
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  *
- *     @(#)def.h       5.8 (Berkeley) %G%
+ *     @(#)def.h       5.9 (Berkeley) %G%
  */
 
 #include <sys/param.h>         /* includes <sys/types.h> */
  */
 
 #include <sys/param.h>         /* includes <sys/types.h> */
@@ -274,6 +274,7 @@ char        *nameof();
 char   *nextword();
 char   *getenv();
 char   *getfilename();
 char   *nextword();
 char   *getenv();
 char   *getfilename();
+char   *findmail();
 char   *ishfield();
 char   *malloc();
 char   *netmap();
 char   *ishfield();
 char   *malloc();
 char   *netmap();
index a5a7d05..ebab76e 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifdef notdef
  */
 
 #ifdef notdef
-static char sccsid[] = "@(#)fio.c      5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)fio.c      5.7 (Berkeley) %G%";
 #endif /* notdef */
 
 #include "rcv.h"
 #endif /* notdef */
 
 #include "rcv.h"
@@ -260,7 +260,7 @@ edstop()
        if (!gotcha || Tflag != NOSTR)
                goto done;
        ibuf = NULL;
        if (!gotcha || Tflag != NOSTR)
                goto done;
        ibuf = NULL;
-       if (stat(editfile, &statb) >= 0 && statb.st_size > mailsize) {
+       if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
                strcpy(tempname, "/tmp/mboxXXXXXX");
                mktemp(tempname);
                if ((obuf = fopen(tempname, "w")) == NULL) {
                strcpy(tempname, "/tmp/mboxXXXXXX");
                mktemp(tempname);
                if ((obuf = fopen(tempname, "w")) == NULL) {
@@ -268,8 +268,8 @@ edstop()
                        relsesigs();
                        reset(0);
                }
                        relsesigs();
                        reset(0);
                }
-               if ((ibuf = fopen(editfile, "r")) == NULL) {
-                       perror(editfile);
+               if ((ibuf = fopen(mailname, "r")) == NULL) {
+                       perror(mailname);
                        fclose(obuf);
                        remove(tempname);
                        relsesigs();
                        fclose(obuf);
                        remove(tempname);
                        relsesigs();
@@ -288,10 +288,10 @@ edstop()
                }
                remove(tempname);
        }
                }
                remove(tempname);
        }
-       printf("\"%s\" ", editfile);
+       printf("\"%s\" ", mailname);
        fflush(stdout);
        fflush(stdout);
-       if ((obuf = fopen(editfile, "r+")) == NULL) {
-               perror(editfile);
+       if ((obuf = fopen(mailname, "r+")) == NULL) {
+               perror(mailname);
                relsesigs();
                reset(0);
        }
                relsesigs();
                reset(0);
        }
@@ -302,7 +302,7 @@ edstop()
                        continue;
                c++;
                if (send(mp, obuf, 0) < 0) {
                        continue;
                c++;
                if (send(mp, obuf, 0) < 0) {
-                       perror(editfile);
+                       perror(mailname);
                        relsesigs();
                        reset(0);
                }
                        relsesigs();
                        reset(0);
                }
@@ -315,16 +315,15 @@ edstop()
        }
        fflush(obuf);
        if (ferror(obuf)) {
        }
        fflush(obuf);
        if (ferror(obuf)) {
-               perror(editfile);
+               perror(mailname);
                relsesigs();
                reset(0);
        }
        fclose(obuf);
        if (gotcha) {
                relsesigs();
                reset(0);
        }
        fclose(obuf);
        if (gotcha) {
-               remove(editfile);
+               remove(mailname);
                printf("removed\n");
                printf("removed\n");
-       }
-       else
+       } else
                printf("complete\n");
        fflush(stdout);
 
                printf("complete\n");
        fflush(stdout);
 
@@ -385,8 +384,14 @@ fsize(iob)
 }
 
 /*
 }
 
 /*
- * Take a file name, possibly with shell meta characters
- * in it and expand it by using "sh -c echo filename"
+ * Evaluate the string given as a new mailbox name.
+ * Supported meta characters:
+ *     %       for my system mail box
+ *     %user   for user's system mail box
+ *     #       for previous file
+ *     &       invoker's mbox file
+ *     +file   file in folder directory
+ *     any shell meta character
  * Return the file name as a dynamic string.
  */
 char *
  * Return the file name as a dynamic string.
  */
 char *
@@ -401,9 +406,28 @@ expand(name)
        struct stat sbuf;
        union wait s;
 
        struct stat sbuf;
        union wait s;
 
+       switch (*name) {
+       case '%':
+               return cp = savestr(findmail(name[1] ? name + 1 : myname));
+       case '#':
+               if (name[1] != 0)
+                       break;
+               if (prevfile[0] == 0) {
+                       printf("No previous file\n");
+                       return NOSTR;
+               }
+               cp = savestr(prevfile);
+               return cp;
+       case '&':
+               if (name[1] == 0 && (name = value("mbox")) == NOSTR) {
+                       sprintf(xname, "%s/mbox", homedir);
+                       name = savestr(xname);
+               }
+               /* fall through */
+       }
        if (name[0] == '+' && getfold(cmdbuf) >= 0) {
                sprintf(xname, "%s/%s", cmdbuf, name + 1);
        if (name[0] == '+' && getfold(cmdbuf) >= 0) {
                sprintf(xname, "%s/%s", cmdbuf, name + 1);
-               return(expand(savestr(xname)));
+               name = savestr(xname);
        }
        if (!anyof(name, "~{[*?$`'\"\\"))
                return(name);
        }
        if (!anyof(name, "~{[*?$`'\"\\"))
                return(name);
index 03a6fc1..1b4fffe 100644 (file)
@@ -9,7 +9,7 @@
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  *
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  *
- *     @(#)glob.h      5.7 (Berkeley) %G%
+ *     @(#)glob.h      5.8 (Berkeley) %G%
  */
 
 /*
  */
 
 /*
@@ -38,10 +38,9 @@ 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 */
 FILE   *pipef;                         /* Pipe file we have opened */
 int    image;                          /* File descriptor for image of msg */
 FILE   *input;                         /* Current command input file */
-char   *editfile;                      /* Name of file being edited */
 char   *sflag;                         /* Subject given from non tty */
 char   *sflag;                         /* Subject given from non tty */
-char   mbox[PATHSIZE];                 /* Name of mailbox file */
-char   mailname[PATHSIZE];             /* Name of system mailbox */
+char   mailname[PATHSIZE];             /* Name of current file */
+char   prevfile[PATHSIZE];             /* Name of previous file */
 int    uid;                            /* The invoker's user id */
 char   mailrc[PATHSIZE];               /* Name of startup file */
 char   deadletter[PATHSIZE];           /* Name of #/dead.letter */
 int    uid;                            /* The invoker's user id */
 char   mailrc[PATHSIZE];               /* Name of startup file */
 char   deadletter[PATHSIZE];           /* Name of #/dead.letter */
index 2569583..06d7d86 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifdef notdef
  */
 
 #ifdef notdef
-static char sccsid[] = "@(#)lex.c      5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)lex.c      5.12 (Berkeley) %G%";
 #endif /* notdef */
 
 #include "rcv.h"
 #endif /* notdef */
 
 #include "rcv.h"
@@ -40,7 +40,6 @@ setfile(name, isedit)
        int i;
        struct stat stb;
        static int shudclob;
        int i;
        struct stat stb;
        static int shudclob;
-       static char efile[128];
        extern char tempMesg[];
        extern int errno;
 
        extern char tempMesg[];
        extern int errno;
 
@@ -103,8 +102,6 @@ setfile(name, isedit)
        }
        shudclob = 1;
        edit = isedit;
        }
        shudclob = 1;
        edit = isedit;
-       strncpy(efile, name, 128);
-       editfile = efile;
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
        if (name != mailname)
                strcpy(mailname, name);
        mailsize = fsize(ibuf);
index 85d9491..add7dfe 100644 (file)
@@ -17,7 +17,7 @@ char copyright[] =
 #endif /* notdef */
 
 #ifdef notdef
 #endif /* notdef */
 
 #ifdef notdef
-static char sccsid[] = "@(#)main.c     5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     5.12 (Berkeley) %G%";
 #endif /* notdef */
 
 #include "rcv.h"
 #endif /* notdef */
 
 #include "rcv.h"
@@ -48,7 +48,7 @@ main(argc, argv)
        register char *ef, opt;
        register int i;
        struct name *to, *cc, *bcc, *smopts;
        register char *ef, opt;
        register int i;
        struct name *to, *cc, *bcc, *smopts;
-       int  mustsend, hdrstop(), (*prevint)(), f;
+       int mustsend, hdrstop(), (*prevint)(), f;
        extern int getopt(), optind, opterr;
        extern char *optarg;
 
        extern int getopt(), optind, opterr;
        extern char *optarg;
 
@@ -145,7 +145,7 @@ main(argc, argv)
                         * User is specifying file to "edit" with Mail,
                         * as opposed to reading system mailbox.
                         * If no argument is given after -f, we read his
                         * User is specifying file to "edit" with Mail,
                         * as opposed to reading system mailbox.
                         * If no argument is given after -f, we read his
-                        * mbox file in his home directory.
+                        * mbox file.
                         *
                         * getopt() can't handle optional arguments, so here
                         * is an ugly hack to get around it.
                         *
                         * getopt() can't handle optional arguments, so here
                         * is an ugly hack to get around it.
@@ -153,7 +153,7 @@ main(argc, argv)
                        if ((argv[optind]) && (argv[optind][0] != '-'))
                                ef = argv[optind++];
                        else
                        if ((argv[optind]) && (argv[optind][0] != '-'))
                                ef = argv[optind++];
                        else
-                               ef = mbox;
+                               ef = "&";
                        break;
                case 'n':
                        /*
                        break;
                case 'n':
                        /*
@@ -244,22 +244,15 @@ Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
         * Decide whether we are editing a mailbox or reading
         * the system mailbox, and open up the right stuff.
         */
         * Decide whether we are editing a mailbox or reading
         * the system mailbox, and open up the right stuff.
         */
-
-       if (ef != NOSTR) {
-               char *ename;
-
+       if (ef != NOSTR)
                edit++;
                edit++;
-               ename = expand(ef);
-               if (ename != ef) {
-                       ef = malloc((unsigned) strlen(ename) + 1);
-                       strcpy(ef, ename);
-               }
-               editfile = ef;
-               strcpy(mailname, ef);
-       }
-       if (setfile(mailname, edit) < 0) {
+       else
+               ef = "%";
+       if ((ef = expand(ef)) == NOSTR)
+               exit(1);                /* error already reported */
+       if (setfile(ef, edit) < 0) {
                if (edit)
                if (edit)
-                       perror(mailname);
+                       perror(ef);
                else
                        fprintf(stderr, "No mail for %s\n", myname);
                exit(1);
                else
                        fprintf(stderr, "No mail for %s\n", myname);
                exit(1);
index ca9033f..b7fc584 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifdef notdef
  */
 
 #ifdef notdef
-static char sccsid[] = "@(#)quit.c     5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)quit.c     5.7 (Berkeley) %G%";
 #endif /* notdef */
 
 #include "rcv.h"
 #endif /* notdef */
 
 #include "rcv.h"
@@ -39,6 +39,7 @@ quit()
        extern char tempQuit[], tempResid[];
        struct stat minfo;
        char *id;
        extern char tempQuit[], tempResid[];
        struct stat minfo;
        char *id;
+       char *mbox;
 
        /*
         * If we are read only, we can't do anything,
 
        /*
         * If we are read only, we can't do anything,
@@ -152,6 +153,7 @@ quit()
         * just copy saveable entries at the end.
         */
 
         * just copy saveable entries at the end.
         */
 
+       mbox = expand("&");
        mcount = c;
        if (value("append") == NOSTR) {
                if ((obuf = fopen(tempQuit, "w")) == NULL) {
        mcount = c;
        if (value("append") == NOSTR) {
                if ((obuf = fopen(tempQuit, "w")) == NULL) {
index ef26cc6..af639db 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifdef notdef
  */
 
 #ifdef notdef
-static char sccsid[] = "@(#)temp.c     5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)temp.c     5.5 (Berkeley) %G%";
 #endif /* notdef */
 
 #include "rcv.h"
 #endif /* notdef */
 
 #include "rcv.h"
@@ -66,14 +66,10 @@ tinit()
        if ((cp = value("HOME")) == NOSTR)
                cp = ".";
        strcpy(homedir, cp);
        if ((cp = value("HOME")) == NOSTR)
                cp = ".";
        strcpy(homedir, cp);
-       findmail();
-       strcpy(copy(homedir, mbox), "/mbox");
        strcpy(copy(homedir, mailrc), "/.mailrc");
        strcpy(copy(homedir, deadletter), "/dead.letter");
        if (debug) {
        strcpy(copy(homedir, mailrc), "/.mailrc");
        strcpy(copy(homedir, deadletter), "/dead.letter");
        if (debug) {
-               printf("uid = %d, user = %s, mailname = %s\n",
-                   uid, myname, mailname);
-               printf("deadletter = %s, mailrc = %s, mbox = %s\n",
-                   deadletter, mailrc, mbox);
+               printf("uid = %d, user = %s\n", uid, myname);
+               printf("deadletter = %s, mailrc = %s\n", deadletter, mailrc);
        }
 }
        }
 }
index f91761f..a7fa265 100644 (file)
@@ -11,7 +11,7 @@
  */
 
 #ifdef notdef
  */
 
 #ifdef notdef
-static char sccsid[] = "@(#)v7.local.c 5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)v7.local.c 5.5 (Berkeley) %G%";
 #endif /* notdef */
 
 /*
 #endif /* notdef */
 
 /*
@@ -26,17 +26,16 @@ static char sccsid[] = "@(#)v7.local.c      5.4 (Berkeley) %G%";
 
 /*
  * Locate the user's mailbox file (ie, the place where new, unread
 
 /*
  * Locate the user's mailbox file (ie, the place where new, unread
- * mail is queued).  In Version 7, it is in /usr/spool/mail/name.
+ * mail is queued).
  */
  */
-
-findmail()
+char *
+findmail(user)
+       char *user;
 {
 {
+       static char buf[PATHSIZE];
 
 
-       strcpy(copy("/usr/spool/mail/", mailname), myname);
-       if (isdir(mailname)) {
-               stradd(mailname, '/');
-               strcat(mailname, myname);
-       }
+       strcpy(copy("/usr/spool/mail/", buf), user);
+       return buf;
 }
 
 /*
 }
 
 /*