Added optional argument to "file" command which allows
[unix-history] / usr / src / usr.bin / mail / main.c
CommitLineData
83f7d6ec
KS
1#
2
3#include "rcv.h"
4#include <sys/stat.h>
5
6/*
7 * Mail -- a mail program
8 *
9 * Startup -- interface with user.
10 */
11
12static char *SccsId = "@(#)main.c 1.1 %G%";
13
14/*
15 * Find out who the user is, copy his mail file (if exists) into
16 * /tmp/Rxxxxx and set up the message pointers. Then, print out the
17 * message headers and read user commands.
18 *
19 * Command line syntax:
20 * Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ]
21 * or:
22 * Mail [ -i ] [ -r address ] [ -h number ] people ...
23 */
24
25main(argc, argv)
26 char **argv;
27{
28 register char *ef;
29 register int i, argp;
30 int mustsend, uflag;
31 FILE *ibuf, *ftat;
32 extern char tempMesg[], _sobuf[];
33
34#ifdef signal
35 Siginit();
36#endif
37
38 /*
39 * Set up a reasonable environment. We clobber the last
40 * element of argument list for compatibility with version 6,
41 * figure out whether we are being run interactively, set up
42 * all the temporary files, buffer standard output, and so forth.
43 */
44
45 uflag = 0;
46 argv[argc] = (char *) -1;
47 mypid = getpid();
48 intty = isatty(0);
49 outtty = isatty(1);
50 image = -1;
51 setbuf(stdout, _sobuf);
52
53 /*
54 * Now, determine how we are being used.
55 * We successively pick off instances of -r, -h, -f, and -i.
56 * If called as "rmail" we note this fact for letter sending.
57 * If there is anything left, it is the base of the list
58 * of users to mail to. Argp will be set to point to the
59 * first of these users.
60 */
61
62 ef = NOSTR;
63 argp = -1;
64 mustsend = 0;
65 if (argc > 0 && **argv == 'r')
66 rmail++;
67 for (i = 1; i < argc; i++) {
68
69 /*
70 * If current argument is not a flag, then the
71 * rest of the arguments must be recipients.
72 */
73
74 if (*argv[i] != '-') {
75 argp = i;
76 break;
77 }
78 switch (argv[i][1]) {
79 case 'r':
80 /*
81 * Next argument is address to be sent along
82 * to the mailer.
83 */
84 if (i >= argc - 1) {
85 fprintf(stderr, "Address required after -r\n");
86 exit(1);
87 }
88 mustsend++;
89 rflag = argv[i+1];
90 i++;
91 break;
92
93 case 'u':
94 /*
95 * Next argument is person to pretend to be.
96 */
97 uflag++;
98 if (i >= argc - 1) {
99 fprintf(stderr, "You obviously dont know what you're doing\n");
100 exit(1);
101 }
102 strcpy(myname, argv[i+1]);
103 i++;
104 break;
105
106 case 'i':
107 /*
108 * User wants to ignore interrupts.
109 * Set the variable "ignore"
110 */
111 assign("ignore", "");
112 break;
113
114 case 'd':
115 debug++;
116 break;
117
118 case 'h':
119 /*
120 * Specified sequence number for network.
121 * This is the number of "hops" made so
122 * far (count of times message has been
123 * forwarded) to help avoid infinite mail loops.
124 */
125 if (i >= argc - 1) {
126 fprintf(stderr, "Number required for -h\n");
127 exit(1);
128 }
129 mustsend++;
130 hflag = atoi(argv[i+1]);
131 if (hflag == 0) {
132 fprintf(stderr, "-h needs non-zero number\n");
133 exit(1);
134 }
135 i++;
136 break;
137
138 case 's':
139 /*
140 * Give a subject field for sending from
141 * non terminal
142 */
143 if (i >= argc - 1) {
144 fprintf(stderr, "Subject req'd for -s\n");
145 exit(1);
146 }
147 mustsend++;
148 sflag = argv[i+1];
149 i++;
150 break;
151
152 case 'f':
153 /*
154 * User is specifying file to "edit" with Mail,
155 * as opposed to reading system mailbox.
156 * If no argument is given after -f, we read his
157 * mbox file in his home directory.
158 */
159 if (i >= argc - 1)
160 ef = mbox;
161 else
162 ef = argv[i + 1];
163 i++;
164 break;
165
166 case 'n':
167 /*
168 * User doesn't want to source /usr/lib/Mail.rc
169 */
170 nosrc++;
171 break;
172
173 default:
174 fprintf(stderr, "Unknown flag: %s\n", argv[i]);
175 exit(1);
176 }
177 }
178
179 /*
180 * Check for inconsistent arguments.
181 */
182
183 if (rflag != NOSTR && strcmp(rflag, "daemon") == 0) {
184 ftat = fopen("/crp/kas/gotcha", "a");
185 if (ftat != NULL) {
186 fprintf(ftat, "user daemon, real uid %d\n", getuid());
187 fclose(ftat);
188 }
189 }
190 if (ef != NOSTR && argp != -1) {
191 fprintf(stderr, "Cannot give -f and people to send to.\n");
192 exit(1);
193 }
194 if (mustsend && argp == -1) {
195 fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n");
196 exit(1);
197 }
198 tinit();
199 if (argp != -1) {
200 commands();
201 mail(&argv[argp]);
202
203 /*
204 * why wait?
205 */
206
207 exit(senderr);
208 }
209
210 /*
211 * Ok, we are reading mail.
212 * Decide whether we are editing a mailbox or reading
213 * the system mailbox, and open up the right stuff.
214 */
215
216 rcvmode++;
217 if (ef != NOSTR) {
218 edit++;
219 editfile = mailname = ef;
220 if ((ibuf = fopen(mailname, "r")) == NULL) {
221 perror(mailname);
222 exit(1);
223 }
224 if ((i = open(mailname, 1)) < 0)
225 printf("Warning: \"%s\" not writable.\n", mailname);
226 else
227 close(i);
228 }
229 else {
230 if ((ibuf = fopen(mailname, "r")) == NULL) {
231 if (uflag)
232 printf("No mail for %s\n", myname);
233 else
234 printf("No mail.\n");
235 exit(0);
236 }
237 }
238
239 /*
240 * Copy the messages into /tmp
241 * and set pointers.
242 */
243
244 mailsize = fsize(ibuf);
245 if ((otf = fopen(tempMesg, "w")) == NULL) {
246 perror(tempMesg);
247 exit(1);
248 }
249 if ((itf = fopen(tempMesg, "r")) == NULL) {
250 perror(tempMesg);
251 exit(1);
252 }
253 remove(tempMesg);
254 setptr(ibuf);
255 fclose(ibuf);
256
257 /*
258 * print headings and accept user commands.
259 */
260
261 if (msgCount == 0) {
262 if (uflag)
263 printf("No mail for %s\n", myname);
264 else
265 printf("No messages.\n");
266 exit(1);
267 }
268 commands();
269 if (!edit)
270 quit();
271 exit(0);
272}