new copyright notice
[unix-history] / usr / src / usr.bin / mail / fio.c
CommitLineData
9552e6b8
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
0c5f72fb
KB
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
9552e6b8
DF
6 */
7
acfc7e9b 8#ifndef lint
f15db449 9static char sccsid[] = "@(#)fio.c 5.22 (Berkeley) %G%";
acfc7e9b 10#endif /* not lint */
77a2ce8a
KS
11
12#include "rcv.h"
13#include <sys/stat.h>
828615a1
EW
14#include <sys/file.h>
15#include <sys/wait.h>
77a2ce8a
KS
16#include <errno.h>
17
18/*
19 * Mail -- a mail program
20 *
21 * File I/O.
22 */
23
77a2ce8a
KS
24/*
25 * Set up the input pointers while copying the mail file into
26 * /tmp.
27 */
77a2ce8a 28setptr(ibuf)
828615a1 29 register FILE *ibuf;
77a2ce8a 30{
828615a1 31 register c;
9b888765 32 register char *cp, *cp2;
828615a1 33 register count;
77a2ce8a 34 char linebuf[LINESIZE];
828615a1
EW
35 int maybe, inhead;
36 FILE *mestmp;
37 off_t offset;
77a2ce8a
KS
38 struct message this;
39 extern char tempSet[];
40
828615a1 41 if ((c = opentemp(tempSet)) < 0)
77a2ce8a 42 exit(1);
828615a1
EW
43 if ((mestmp = fdopen(c, "r+")) == NULL)
44 panic("Can't open temporary");
77a2ce8a 45 msgCount = 0;
77a2ce8a 46 maybe = 1;
828615a1
EW
47 inhead = 0;
48 offset = 0;
49 this.m_flag = MUSED|MNEW;
50 this.m_size = 0;
51 this.m_lines = 0;
52 this.m_block = 0;
53 this.m_offset = 0;
77a2ce8a 54 for (;;) {
87d10954 55 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
77a2ce8a
KS
56 if (append(&this, mestmp)) {
57 perror(tempSet);
58 exit(1);
59 }
60 fclose(ibuf);
61 makemessage(mestmp);
77a2ce8a
KS
62 return;
63 }
87d10954 64 count = strlen(linebuf);
2ee3bce2 65 (void) fwrite(linebuf, sizeof *linebuf, count, otf);
9008b544 66 if (ferror(otf)) {
77a2ce8a
KS
67 perror("/tmp");
68 exit(1);
69 }
828615a1 70 linebuf[count - 1] = 0;
9b888765 71 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
77a2ce8a 72 msgCount++;
77a2ce8a
KS
73 if (append(&this, mestmp)) {
74 perror(tempSet);
75 exit(1);
76 }
828615a1
EW
77 this.m_flag = MUSED|MNEW;
78 this.m_size = 0;
79 this.m_lines = 0;
80 this.m_block = blockof(offset);
81 this.m_offset = offsetof(offset);
82 inhead = 1;
83 } else if (linebuf[0] == 0) {
9b888765 84 inhead = 0;
828615a1
EW
85 } else if (inhead) {
86 for (cp = linebuf, cp2 = "status";; cp++) {
87 if ((c = *cp2++) == 0) {
88 while (isspace(*cp++))
89 ;
90 if (cp[-1] != ':')
91 break;
92 while (c = *cp++)
93 if (c == 'R')
94 this.m_flag |= MREAD;
95 else if (c == 'O')
96 this.m_flag &= ~MNEW;
97 inhead = 0;
98 break;
99 }
100 if (*cp != c && *cp != toupper(c))
101 break;
9b888765
KS
102 }
103 }
77a2ce8a 104 offset += count;
828615a1
EW
105 this.m_size += count;
106 this.m_lines++;
107 maybe = linebuf[0] == 0;
77a2ce8a
KS
108 }
109}
110
111/*
112 * Drop the passed line onto the passed output buffer.
113 * If a write error occurs, return -1, else the count of
114 * characters written, including the newline.
115 */
77a2ce8a
KS
116putline(obuf, linebuf)
117 FILE *obuf;
118 char *linebuf;
119{
120 register int c;
121
122 c = strlen(linebuf);
2ee3bce2
EW
123 (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
124 (void) putc('\n', obuf);
77a2ce8a 125 if (ferror(obuf))
828615a1
EW
126 return (-1);
127 return (c + 1);
77a2ce8a
KS
128}
129
130/*
131 * Read up a line from the specified input into the line
132 * buffer. Return the number of characters read. Do not
133 * include the newline at the end.
134 */
38ed7007 135readline(ibuf, linebuf, linesize)
77a2ce8a
KS
136 FILE *ibuf;
137 char *linebuf;
138{
87d10954 139 register int n;
77a2ce8a 140
87d10954 141 clearerr(ibuf);
38ed7007 142 if (fgets(linebuf, linesize, ibuf) == NULL)
828615a1 143 return -1;
87d10954 144 n = strlen(linebuf);
828615a1
EW
145 if (n > 0 && linebuf[n - 1] == '\n')
146 linebuf[--n] = '\0';
147 return n;
77a2ce8a
KS
148}
149
150/*
151 * Return a file buffer all ready to read up the
152 * passed message pointer.
153 */
77a2ce8a
KS
154FILE *
155setinput(mp)
156 register struct message *mp;
157{
77a2ce8a
KS
158
159 fflush(otf);
828615a1 160 if (fseek(itf, positionof(mp->m_block, mp->m_offset), 0) < 0) {
77a2ce8a
KS
161 perror("fseek");
162 panic("temporary file seek");
163 }
828615a1 164 return (itf);
77a2ce8a
KS
165}
166
167/*
168 * Take the data out of the passed ghost file and toss it into
169 * a dynamically allocated message structure.
170 */
77a2ce8a 171makemessage(f)
828615a1 172 FILE *f;
77a2ce8a 173{
828615a1
EW
174 register size = (msgCount + 1) * sizeof (struct message);
175 off_t lseek();
77a2ce8a 176
828615a1
EW
177 if (message != 0)
178 free((char *) message);
179 if ((message = (struct message *) malloc((unsigned) size)) == 0)
180 panic("Insufficient memory for %d messages", msgCount);
77a2ce8a 181 dot = message;
828615a1
EW
182 size -= sizeof (struct message);
183 fflush(f);
2ee3bce2 184 (void) lseek(fileno(f), (long) sizeof *message, 0);
828615a1
EW
185 if (read(fileno(f), (char *) message, size) != size)
186 panic("Message temporary file corrupted");
187 message[msgCount].m_size = 0;
77a2ce8a 188 message[msgCount].m_lines = 0;
828615a1 189 fclose(f);
77a2ce8a
KS
190}
191
192/*
193 * Append the passed message descriptor onto the temp file.
194 * If the write fails, return 1, else 0
195 */
77a2ce8a
KS
196append(mp, f)
197 struct message *mp;
828615a1 198 FILE *f;
77a2ce8a 199{
828615a1 200 return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
77a2ce8a
KS
201}
202
203/*
204 * Delete a file, but only if the file is a plain file.
205 */
77a2ce8a
KS
206remove(name)
207 char name[];
208{
209 struct stat statb;
210 extern int errno;
211
212 if (stat(name, &statb) < 0)
213 return(-1);
214 if ((statb.st_mode & S_IFMT) != S_IFREG) {
215 errno = EISDIR;
216 return(-1);
217 }
828615a1 218 return unlink(name);
77a2ce8a
KS
219}
220
e4920814
EW
221static int sigdepth; /* depth of holdsigs() */
222static int omask;
b3d49ad3 223/*
828615a1 224 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
b3d49ad3 225 */
985c722d 226holdsigs()
b3d49ad3 227{
b3d49ad3 228
f5df67b9 229 if (sigdepth++ == 0)
56d6aa4a 230 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
b3d49ad3
KS
231}
232
233/*
828615a1 234 * Release signals SIGHUP, SIGINT, and SIGQUIT.
b3d49ad3 235 */
985c722d 236relsesigs()
b3d49ad3 237{
985c722d 238
f5df67b9 239 if (--sigdepth == 0)
56d6aa4a 240 sigsetmask(omask);
77a2ce8a
KS
241}
242
77a2ce8a 243/*
828615a1
EW
244 * Open a temp file by creating and unlinking.
245 * Return the open file descriptor.
77a2ce8a 246 */
77a2ce8a
KS
247opentemp(file)
248 char file[];
249{
828615a1 250 int f;
77a2ce8a 251
828615a1 252 if ((f = open(file, O_CREAT|O_EXCL|O_RDWR, 0600)) < 0)
77a2ce8a 253 perror(file);
77a2ce8a 254 remove(file);
828615a1 255 return (f);
77a2ce8a
KS
256}
257
77a2ce8a
KS
258/*
259 * Determine the size of the file possessed by
260 * the passed buffer.
261 */
77a2ce8a
KS
262off_t
263fsize(iob)
264 FILE *iob;
265{
77a2ce8a
KS
266 struct stat sbuf;
267
828615a1
EW
268 if (fstat(fileno(iob), &sbuf) < 0)
269 return 0;
270 return sbuf.st_size;
77a2ce8a
KS
271}
272
273/*
2a0f6531
EW
274 * Evaluate the string given as a new mailbox name.
275 * Supported meta characters:
276 * % for my system mail box
277 * %user for user's system mail box
278 * # for previous file
279 * & invoker's mbox file
280 * +file file in folder directory
281 * any shell meta character
77a2ce8a
KS
282 * Return the file name as a dynamic string.
283 */
77a2ce8a
KS
284char *
285expand(name)
4d20fb09 286 register char *name;
77a2ce8a 287{
f674e088
EW
288 char xname[PATHSIZE];
289 char cmdbuf[PATHSIZE]; /* also used for file names */
828615a1 290 register int pid, l;
d33aa50d 291 register char *cp, *shell;
828615a1 292 int pivec[2];
77a2ce8a 293 struct stat sbuf;
322c8626 294 extern union wait wait_status;
77a2ce8a 295
954642f5
EW
296 /*
297 * The order of evaluation is "%" and "#" expand into constants.
298 * "&" can expand into "+". "+" can expand into shell meta characters.
299 * Shell meta characters expand into constants.
300 * This way, we make no recursive expansion.
301 */
2a0f6531
EW
302 switch (*name) {
303 case '%':
f674e088
EW
304 findmail(name[1] ? name + 1 : myname, xname);
305 return savestr(xname);
2a0f6531
EW
306 case '#':
307 if (name[1] != 0)
308 break;
309 if (prevfile[0] == 0) {
310 printf("No previous file\n");
311 return NOSTR;
312 }
4d20fb09 313 return savestr(prevfile);
2a0f6531 314 case '&':
62e28b79 315 if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
954642f5 316 name = "~/mbox";
2a0f6531
EW
317 /* fall through */
318 }
df3c4fbd
KS
319 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
320 sprintf(xname, "%s/%s", cmdbuf, name + 1);
2a0f6531 321 name = savestr(xname);
38a42545 322 }
954642f5
EW
323 /* catch the most common shell meta character */
324 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
62e28b79 325 sprintf(xname, "%s%s", homedir, name + 1);
954642f5
EW
326 name = savestr(xname);
327 }
77a2ce8a 328 if (!anyof(name, "~{[*?$`'\"\\"))
4d20fb09 329 return name;
77a2ce8a
KS
330 if (pipe(pivec) < 0) {
331 perror("pipe");
4d20fb09 332 return name;
77a2ce8a
KS
333 }
334 sprintf(cmdbuf, "echo %s", name);
d33aa50d 335 if ((shell = value("SHELL")) == NOSTR)
435e8dff 336 shell = _PATH_CSHELL;
d33aa50d
EW
337 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
338 if (pid < 0) {
77a2ce8a
KS
339 close(pivec[0]);
340 close(pivec[1]);
4d20fb09 341 return NOSTR;
77a2ce8a
KS
342 }
343 close(pivec[1]);
344 l = read(pivec[0], xname, BUFSIZ);
345 close(pivec[0]);
322c8626 346 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
39251e41 347 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
4d20fb09 348 return NOSTR;
77a2ce8a
KS
349 }
350 if (l < 0) {
351 perror("read");
4d20fb09 352 return NOSTR;
77a2ce8a
KS
353 }
354 if (l == 0) {
954642f5 355 fprintf(stderr, "\"%s\": No match.\n", name);
4d20fb09 356 return NOSTR;
77a2ce8a
KS
357 }
358 if (l == BUFSIZ) {
954642f5 359 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
4d20fb09 360 return NOSTR;
77a2ce8a
KS
361 }
362 xname[l] = 0;
363 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
364 ;
4d20fb09 365 cp[1] = '\0';
470c33f3 366 if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
954642f5 367 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
4d20fb09 368 return NOSTR;
77a2ce8a 369 }
4d20fb09 370 return savestr(xname);
77a2ce8a
KS
371}
372
df3c4fbd
KS
373/*
374 * Determine the current folder directory name.
375 */
376getfold(name)
377 char *name;
378{
379 char *folder;
380
381 if ((folder = value("folder")) == NOSTR)
828615a1 382 return (-1);
df3c4fbd
KS
383 if (*folder == '/')
384 strcpy(name, folder);
385 else
386 sprintf(name, "%s/%s", homedir, folder);
828615a1 387 return (0);
df3c4fbd
KS
388}
389
62e28b79
EW
390/*
391 * Return the name of the dead.letter file.
392 */
393char *
394getdeadletter()
395{
396 register char *cp;
397
398 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
399 cp = expand("~/dead.letter");
400 else if (*cp != '/') {
401 char buf[PATHSIZE];
402
403 (void) sprintf(buf, "~/%s", cp);
404 cp = expand(buf);
405 }
406 return cp;
407}
408
77a2ce8a
KS
409/*
410 * A nicer version of Fdopen, which allows us to fclose
411 * without losing the open file.
412 */
77a2ce8a
KS
413FILE *
414Fdopen(fildes, mode)
415 char *mode;
416{
828615a1 417 int f;
77a2ce8a 418
828615a1 419 if ((f = dup(fildes)) < 0) {
77a2ce8a 420 perror("dup");
828615a1 421 return (NULL);
77a2ce8a 422 }
828615a1 423 return fdopen(f, mode);
77a2ce8a 424}