Modified to truncate read/write files before closing.
[unix-history] / usr / src / usr.bin / mail / main.c
index c53529d..db01c93 100644 (file)
@@ -9,7 +9,9 @@
  * Startup -- interface with user.
  */
 
  * Startup -- interface with user.
  */
 
-static char *SccsId = "@(#)main.c      2.2 %G%";
+static char *SccsId = "@(#)main.c      2.10 %G%";
+
+jmp_buf        hdrjmp;
 
 /*
  * Find out who the user is, copy his mail file (if exists) into
 
 /*
  * Find out who the user is, copy his mail file (if exists) into
@@ -27,9 +29,10 @@ main(argc, argv)
 {
        register char *ef;
        register int i, argp;
 {
        register char *ef;
        register int i, argp;
-       int mustsend, uflag;
+       int mustsend, uflag, hdrstop(), (*prevint)(), f;
        FILE *ibuf, *ftat;
        extern char _sobuf[];
        FILE *ibuf, *ftat;
        extern char _sobuf[];
+       struct sgttyb tbuf;
 
 #ifdef signal
        Siginit();
 
 #ifdef signal
        Siginit();
@@ -44,9 +47,18 @@ main(argc, argv)
 
        uflag = 0;
        argv[argc] = (char *) -1;
 
        uflag = 0;
        argv[argc] = (char *) -1;
+#ifdef GETHOST
+       inithost();
+#endif GETHOST
        mypid = getpid();
        intty = isatty(0);
        outtty = isatty(1);
        mypid = getpid();
        intty = isatty(0);
        outtty = isatty(1);
+       if (outtty) {
+               gtty(1, &tbuf);
+               baud = tbuf.sg_ospeed;
+       }
+       else
+               baud = B9600;
        image = -1;
        setbuf(stdout, _sobuf);
 
        image = -1;
        setbuf(stdout, _sobuf);
 
@@ -100,6 +112,11 @@ main(argc, argv)
                                exit(1);
                        }
                        Tflag = argv[i+1];
                                exit(1);
                        }
                        Tflag = argv[i+1];
+                       if ((f = creat(Tflag, 0600)) < 0) {
+                               perror(Tflag);
+                               exit(1);
+                       }
+                       close(f);
                        i++;
                        break;
 
                        i++;
                        break;
 
@@ -109,7 +126,7 @@ main(argc, argv)
                         */
                        uflag++;
                        if (i >= argc - 1) {
                         */
                        uflag++;
                        if (i >= argc - 1) {
-                               fprintf(stderr, "You obviously dont know what you're doing\n");
+                               fprintf(stderr, "Missing user name for -u\n");
                                exit(1);
                        }
                        strcpy(myname, argv[i+1]);
                                exit(1);
                        }
                        strcpy(myname, argv[i+1]);
@@ -190,6 +207,13 @@ main(argc, argv)
                        noheader++;
                        break;
 
                        noheader++;
                        break;
 
+               case 'v':
+                       /*
+                        * Send mailer verbose flag
+                        */
+                       assign("verbose", "");
+                       break;
+
                default:
                        fprintf(stderr, "Unknown flag: %s\n", argv[i]);
                        exit(1);
                default:
                        fprintf(stderr, "Unknown flag: %s\n", argv[i]);
                        exit(1);
@@ -200,13 +224,6 @@ main(argc, argv)
         * Check for inconsistent arguments.
         */
 
         * Check for inconsistent arguments.
         */
 
-       if (rflag != NOSTR && strcmp(rflag, "daemon") == 0) {
-               ftat = fopen("/crp/kas/gotcha", "a");
-               if (ftat != NULL) {
-                       fprintf(ftat, "user daemon, real uid %d\n", getuid());
-                       fclose(ftat);
-               }
-       }
        if (ef != NOSTR && argp != -1) {
                fprintf(stderr, "Cannot give -f and people to send to.\n");
                exit(1);
        if (ef != NOSTR && argp != -1) {
                fprintf(stderr, "Cannot give -f and people to send to.\n");
                exit(1);
@@ -216,8 +233,12 @@ main(argc, argv)
                exit(1);
        }
        tinit();
                exit(1);
        }
        tinit();
+       input = stdin;
+       rcvmode = argp == -1;
+       if (!nosrc)
+               load(MASTER);
+       load(mailrc);
        if (argp != -1) {
        if (argp != -1) {
-               commands();
                mail(&argv[argp]);
 
                /*
                mail(&argv[argp]);
 
                /*
@@ -233,13 +254,41 @@ main(argc, argv)
         * the system mailbox, and open up the right stuff.
         */
 
         * the system mailbox, and open up the right stuff.
         */
 
-       rcvmode++;
        if (ef != NOSTR) {
        if (ef != NOSTR) {
+               char *ename;
+
                edit++;
                edit++;
-               editfile = mailname = ef;
+               ename = expand(ef);
+               if (ename != ef) {
+                       ef = (char *) calloc(1, strlen(ename) + 1);
+                       strcpy(ef, ename);
+               }
+               editfile = ef;
+               strcpy(mailname, ef);
        }
        }
-       if (setfile(mailname, edit) < 0)
+       if (setfile(mailname, edit) < 0) {
+               if (edit)
+                       perror(mailname);
+               else
+                       fprintf(stderr, "No mail for %s\n", myname);
                exit(1);
                exit(1);
+       }
+       if (!edit && !noheader && value("noheader") == NOSTR) {
+               if (setjmp(hdrjmp) == 0) {
+                       if ((prevint = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
+                               sigset(SIGINT, hdrstop);
+                       announce(!0);
+                       fflush(stdout);
+                       sigset(SIGINT, prevint);
+               }
+       }
+       if (edit)
+               newfileinfo();
+       if (!edit && msgCount == 0) {
+               printf("No mail\n");
+               fflush(stdout);
+               exit(0);
+       }
        commands();
        if (!edit) {
                sigset(SIGHUP, SIG_IGN);
        commands();
        if (!edit) {
                sigset(SIGHUP, SIG_IGN);
@@ -249,3 +298,16 @@ main(argc, argv)
        }
        exit(0);
 }
        }
        exit(0);
 }
+
+/*
+ * Interrupt printing of the headers.
+ */
+hdrstop()
+{
+
+       clrbuf(stdout);
+       printf("\nInterrupt\n");
+       fflush(stdout);
+       sigrelse(SIGINT);
+       longjmp(hdrjmp, 1);
+}