fixes q order problems and temporary file names when inchar > 'z'.
[unix-history] / usr / src / bin / rmail / rmail.c
index e669b16..aa114df 100644 (file)
@@ -1,27 +1,25 @@
 /*
 /*
- * rmail: front end for mail to stack up those stupid >From ... remote from ...
- * lines and make a correct return address.  This works with the -f option
- * to /usr/lib/sendmail so it won't work on systems without sendmail.
- * However, it ought to be easy to modify a standard /bin/mail to do the
- * same thing.
- *
- * NOTE: Rmail is SPECIFICALLY INTENDED for ERNIE COVAX because of its
- * physical position as a gateway between the uucp net and the arpanet.
- * By default, other sites will probably want /bin/rmail to be a link
- * to /bin/mail, as it was intended by BTL.  However, other than the
- * (somewhat annoying) loss of information about when the mail was
- * originally sent, rmail should work OK on other systems running uucp.
- * If you don't run uucp you don't even need any rmail.
- */
-
-static char    SccsId[] =      "@(#)rmail.c    3.2     %G%";
+**  RMAIL -- UUCP mail server.
+**
+**     This program reads the >From ... remote from ... lines that
+**     UUCP is so fond of and turns them into something reasonable.
+**     It calls sendmail giving it a -f option built from these
+**     lines.
+*/
 
 # include <stdio.h>
 # include <sysexits.h>
 
 # include <stdio.h>
 # include <sysexits.h>
-# include "useful.h"
+# include "conf.h"
+
+static char    SccsId[] =      "@(#)rmail.c    4.3     (Berkeley)      %G%";
 
 
-extern FILE *popen();
-extern char *index();
+typedef char   bool;
+#define TRUE   1
+#define FALSE  0
+
+extern FILE    *popen();
+extern char    *index();
+extern char    *rindex();
 
 bool   Debug;
 
 
 bool   Debug;
 
@@ -38,6 +36,8 @@ main(argc, argv)
        char junk[512]; /* scratchpad */
        char cmd[2000];
        register char *cp;
        char junk[512]; /* scratchpad */
        char cmd[2000];
        register char *cp;
+       register char *uf;      /* ptr into ufrom */
+       int i;
 
 # ifdef DEBUG
        if (argc > 1 && strcmp(argv[1], "-T") == 0)
 
 # ifdef DEBUG
        if (argc > 1 && strcmp(argv[1], "-T") == 0)
@@ -54,18 +54,33 @@ main(argc, argv)
                exit(EX_USAGE);
        }
 
                exit(EX_USAGE);
        }
 
+       (void) strcpy(from, "");
+       (void) strcpy(ufrom, "/dev/null");
+
        for (;;)
        {
        for (;;)
        {
-               fgets(lbuf, sizeof lbuf, stdin);
+               (void) fgets(lbuf, sizeof lbuf, stdin);
                if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
                        break;
                if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
                        break;
-               sscanf(lbuf, "%s %s", junk, ufrom);
+               (void) sscanf(lbuf, "%s %s", junk, ufrom);
                cp = lbuf;
                cp = lbuf;
+               uf = ufrom;
                for (;;)
                {
                        cp = index(cp+1, 'r');
                        if (cp == NULL)
                for (;;)
                {
                        cp = index(cp+1, 'r');
                        if (cp == NULL)
+                       {
+                               register char *p = rindex(uf, '!');
+
+                               if (p != NULL)
+                               {
+                                       *p = '\0';
+                                       (void) strcpy(sys, uf);
+                                       uf = p + 1;
+                                       break;
+                               }
                                cp = "remote from somewhere";
                                cp = "remote from somewhere";
+                       }
 #ifdef DEBUG
                        if (Debug)
                                printf("cp='%s'\n", cp);
 #ifdef DEBUG
                        if (Debug)
                                printf("cp='%s'\n", cp);
@@ -73,22 +88,26 @@ main(argc, argv)
                        if (strncmp(cp, "remote from ", 12)==0)
                                break;
                }
                        if (strncmp(cp, "remote from ", 12)==0)
                                break;
                }
-               sscanf(cp, "remote from %s", sys);
-               strcat(from, sys);
-               strcat(from, "!");
+               if (cp != NULL)
+                       (void) sscanf(cp, "remote from %s", sys);
+               (void) strcat(from, sys);
+               (void) strcat(from, "!");
 #ifdef DEBUG
                if (Debug)
 #ifdef DEBUG
                if (Debug)
-                       printf("ufrom='%s', sys='%s', from now '%s'\n", ufrom, sys, from);
+                       printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
 #endif
        }
 #endif
        }
-       strcat(from, ufrom);
+       (void) strcat(from, uf);
 
 
-       sprintf(cmd, "%s -em -f%s", MAILER, from);
+       (void) sprintf(cmd, "%s -em -f%s", MAILER, from);
        while (*++argv != NULL)
        {
        while (*++argv != NULL)
        {
-               strcat(cmd, " '");
-               strcat(cmd, *argv);
-               strcat(cmd, "'");
+               (void) strcat(cmd, " '");
+               if (**argv == '(')
+                       (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
+               else
+                       (void) strcat(cmd, *argv);
+               (void) strcat(cmd, "'");
        }
 #ifdef DEBUG
        if (Debug)
        }
 #ifdef DEBUG
        if (Debug)
@@ -98,7 +117,12 @@ main(argc, argv)
        fputs(lbuf, out);
        while (fgets(lbuf, sizeof lbuf, stdin))
                fputs(lbuf, out);
        fputs(lbuf, out);
        while (fgets(lbuf, sizeof lbuf, stdin))
                fputs(lbuf, out);
-       pclose(out);
+       i = pclose(out);
+       if ((i & 0377) != 0)
+       {
+               fprintf(stderr, "pclose: status 0%o\n", i);
+               exit(EX_OSERR);
+       }
 
 
-       exit(EX_OK);
+       exit((i >> 8) & 0377);
 }
 }