grow some buffers
[unix-history] / usr / src / bin / rmail / rmail.c
CommitLineData
205a2d85 1#ifndef lint
ecbd0932 2static char sccsid[] = "@(#)rmail.c 4.6 (Berkeley) %G%";
205a2d85
SL
3#endif
4
9adc8f2c 5/*
b8b8df54
EA
6** RMAIL -- UUCP mail server.
7**
8** This program reads the >From ... remote from ... lines that
9** UUCP is so fond of and turns them into something reasonable.
10** It calls sendmail giving it a -f option built from these
11** lines.
09fa495a 12*/
9adc8f2c 13
0f5791a6
EA
14# include <stdio.h>
15# include <sysexits.h>
b5fd168f 16# include "conf.h"
9adc8f2c 17
44ae4c24
EA
18typedef char bool;
19#define TRUE 1
20#define FALSE 0
21
22extern FILE *popen();
23extern char *index();
24extern char *rindex();
0f5791a6
EA
25
26bool Debug;
27
28# define MAILER "/usr/lib/sendmail"
9adc8f2c
EA
29
30main(argc, argv)
0f5791a6 31 char **argv;
9adc8f2c 32{
0f5791a6 33 FILE *out; /* output to sendmail */
ecbd0932
JL
34 char lbuf[1024]; /* one line of the message */
35 char from[512]; /* accumulated path of sender */
36 char ufrom[512]; /* user on remote system */
37 char sys[512]; /* a system in path */
38 char junk[1024]; /* scratchpad */
0f5791a6
EA
39 char cmd[2000];
40 register char *cp;
09fa495a 41 register char *uf; /* ptr into ufrom */
9777ef27 42 int i;
9adc8f2c 43
0f5791a6
EA
44# ifdef DEBUG
45 if (argc > 1 && strcmp(argv[1], "-T") == 0)
46 {
47 Debug = TRUE;
48 argc--;
49 argv++;
9adc8f2c 50 }
0f5791a6 51# endif DEBUG
9adc8f2c 52
0f5791a6
EA
53 if (argc < 2)
54 {
55 fprintf(stderr, "Usage: rmail user ...\n");
56 exit(EX_USAGE);
57 }
58
f9566d23
EA
59 (void) strcpy(from, "");
60 (void) strcpy(ufrom, "/dev/null");
b8b8df54 61
0f5791a6
EA
62 for (;;)
63 {
74c5fe7c 64 (void) fgets(lbuf, sizeof lbuf, stdin);
0f5791a6 65 if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
9adc8f2c 66 break;
ed45aae1 67 (void) sscanf(lbuf, "%s %s", junk, ufrom);
9adc8f2c 68 cp = lbuf;
09fa495a 69 uf = ufrom;
0f5791a6
EA
70 for (;;)
71 {
9adc8f2c
EA
72 cp = index(cp+1, 'r');
73 if (cp == NULL)
09fa495a
EA
74 {
75 register char *p = rindex(uf, '!');
76
77 if (p != NULL)
78 {
79 *p = '\0';
f9566d23 80 (void) strcpy(sys, uf);
09fa495a
EA
81 uf = p + 1;
82 break;
83 }
9adc8f2c 84 cp = "remote from somewhere";
09fa495a 85 }
9adc8f2c 86#ifdef DEBUG
0f5791a6
EA
87 if (Debug)
88 printf("cp='%s'\n", cp);
9adc8f2c
EA
89#endif
90 if (strncmp(cp, "remote from ", 12)==0)
91 break;
92 }
09fa495a
EA
93 if (cp != NULL)
94 (void) sscanf(cp, "remote from %s", sys);
f9566d23
EA
95 (void) strcat(from, sys);
96 (void) strcat(from, "!");
9adc8f2c 97#ifdef DEBUG
0f5791a6 98 if (Debug)
09fa495a 99 printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
9adc8f2c
EA
100#endif
101 }
f9566d23 102 (void) strcat(from, uf);
9adc8f2c 103
2ef5b50e 104 (void) sprintf(cmd, "%s -ee -f%s", MAILER, from);
0f5791a6
EA
105 while (*++argv != NULL)
106 {
f9566d23 107 (void) strcat(cmd, " '");
674f6e70 108 if (**argv == '(')
f9566d23 109 (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
674f6e70 110 else
f9566d23
EA
111 (void) strcat(cmd, *argv);
112 (void) strcat(cmd, "'");
0f5791a6 113 }
9adc8f2c 114#ifdef DEBUG
0f5791a6
EA
115 if (Debug)
116 printf("cmd='%s'\n", cmd);
9adc8f2c
EA
117#endif
118 out = popen(cmd, "w");
119 fputs(lbuf, out);
120 while (fgets(lbuf, sizeof lbuf, stdin))
121 fputs(lbuf, out);
9777ef27
EA
122 i = pclose(out);
123 if ((i & 0377) != 0)
124 {
125 fprintf(stderr, "pclose: status 0%o\n", i);
126 exit(EX_OSERR);
127 }
9adc8f2c 128
9777ef27 129 exit((i >> 8) & 0377);
9adc8f2c 130}