date and time created 87/02/15 16:03:36 by lepreau
[unix-history] / usr / src / usr.bin / mail / fio.c
CommitLineData
9552e6b8
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
2ae9f53f 7#ifndef lint
ac676849 8static char *sccsid = "@(#)fio.c 5.4 (Berkeley) %G%";
9552e6b8 9#endif not lint
77a2ce8a
KS
10
11#include "rcv.h"
12#include <sys/stat.h>
13#include <errno.h>
ac676849 14#include <strings.h>
77a2ce8a
KS
15
16/*
17 * Mail -- a mail program
18 *
19 * File I/O.
20 */
21
77a2ce8a
KS
22/*
23 * Set up the input pointers while copying the mail file into
24 * /tmp.
25 */
26
27setptr(ibuf)
28 FILE *ibuf;
29{
9008b544 30 register int c;
9b888765 31 register char *cp, *cp2;
0154300b
CS
32 register int count, l;
33 long s;
77a2ce8a
KS
34 off_t offset;
35 char linebuf[LINESIZE];
9b888765
KS
36 char wbuf[LINESIZE];
37 int maybe, mestmp, flag, inhead;
77a2ce8a
KS
38 struct message this;
39 extern char tempSet[];
40
41 if ((mestmp = opentemp(tempSet)) < 0)
42 exit(1);
43 msgCount = 0;
44 offset = 0;
0154300b 45 s = 0L;
77a2ce8a
KS
46 l = 0;
47 maybe = 1;
9b888765 48 flag = MUSED|MNEW;
77a2ce8a 49 for (;;) {
87d10954 50 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
77a2ce8a 51 this.m_flag = flag;
9b888765 52 flag = MUSED|MNEW;
77a2ce8a
KS
53 this.m_offset = offsetof(offset);
54 this.m_block = blockof(offset);
55 this.m_size = s;
56 this.m_lines = l;
57 if (append(&this, mestmp)) {
58 perror(tempSet);
59 exit(1);
60 }
61 fclose(ibuf);
62 makemessage(mestmp);
63 close(mestmp);
64 return;
65 }
87d10954
DS
66 count = strlen(linebuf);
67 fputs(linebuf, otf);
68 cp = linebuf + (count - 1);
69 if (*cp == '\n')
70 *cp = 0;
9008b544 71 if (ferror(otf)) {
77a2ce8a
KS
72 perror("/tmp");
73 exit(1);
74 }
9b888765 75 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
77a2ce8a
KS
76 msgCount++;
77 this.m_flag = flag;
9b888765 78 flag = MUSED|MNEW;
29c01f3c 79 inhead = 1;
77a2ce8a
KS
80 this.m_block = blockof(offset);
81 this.m_offset = offsetof(offset);
82 this.m_size = s;
83 this.m_lines = l;
0154300b 84 s = 0L;
77a2ce8a
KS
85 l = 0;
86 if (append(&this, mestmp)) {
87 perror(tempSet);
88 exit(1);
89 }
90 }
9b888765
KS
91 if (linebuf[0] == 0)
92 inhead = 0;
87d10954
DS
93 if (inhead && (cp = index(linebuf, ':'))) {
94 *cp = 0;
95 if (icequal(linebuf, "status")) {
96 ++cp;
9b888765
KS
97 if (index(cp, 'R'))
98 flag |= MREAD;
99 if (index(cp, 'O'))
100 flag &= ~MNEW;
101 inhead = 0;
102 }
103 }
77a2ce8a 104 offset += count;
0154300b 105 s += (long) count;
77a2ce8a
KS
106 l++;
107 maybe = 0;
108 if (linebuf[0] == 0)
109 maybe = 1;
110 }
111}
112
113/*
114 * Drop the passed line onto the passed output buffer.
115 * If a write error occurs, return -1, else the count of
116 * characters written, including the newline.
117 */
118
119putline(obuf, linebuf)
120 FILE *obuf;
121 char *linebuf;
122{
123 register int c;
124
125 c = strlen(linebuf);
126 fputs(linebuf, obuf);
127 putc('\n', obuf);
128 if (ferror(obuf))
129 return(-1);
130 return(c+1);
131}
132
133/*
134 * Read up a line from the specified input into the line
135 * buffer. Return the number of characters read. Do not
136 * include the newline at the end.
137 */
138
139readline(ibuf, linebuf)
140 FILE *ibuf;
141 char *linebuf;
142{
87d10954 143 register int n;
77a2ce8a 144
87d10954
DS
145 clearerr(ibuf);
146 if (fgets(linebuf, LINESIZE, ibuf) == NULL)
77a2ce8a 147 return(0);
87d10954
DS
148 n = strlen(linebuf);
149 if (n >= 1 && linebuf[n-1] == '\n')
150 linebuf[n-1] = '\0';
151 return(n);
77a2ce8a
KS
152}
153
154/*
155 * Return a file buffer all ready to read up the
156 * passed message pointer.
157 */
158
159FILE *
160setinput(mp)
161 register struct message *mp;
162{
163 off_t off;
164
165 fflush(otf);
166 off = mp->m_block;
167 off <<= 9;
168 off += mp->m_offset;
169 if (fseek(itf, off, 0) < 0) {
170 perror("fseek");
171 panic("temporary file seek");
172 }
173 return(itf);
174}
175
176/*
177 * Take the data out of the passed ghost file and toss it into
178 * a dynamically allocated message structure.
179 */
180
181makemessage(f)
182{
183 register struct message *m;
184 register char *mp;
185 register count;
186
187 mp = calloc((unsigned) (msgCount + 1), sizeof *m);
188 if (mp == NOSTR) {
189 printf("Insufficient memory for %d messages\n", msgCount);
190 exit(1);
191 }
838681f8
KS
192 if (message != (struct message *) 0)
193 cfree((char *) message);
77a2ce8a
KS
194 message = (struct message *) mp;
195 dot = message;
196 lseek(f, 0L, 0);
197 while (count = read(f, mp, BUFSIZ))
198 mp += count;
199 for (m = &message[0]; m < &message[msgCount]; m++) {
200 m->m_size = (m+1)->m_size;
201 m->m_lines = (m+1)->m_lines;
9b888765 202 m->m_flag = (m+1)->m_flag;
77a2ce8a 203 }
0154300b 204 message[msgCount].m_size = 0L;
77a2ce8a
KS
205 message[msgCount].m_lines = 0;
206}
207
208/*
209 * Append the passed message descriptor onto the temp file.
210 * If the write fails, return 1, else 0
211 */
212
213append(mp, f)
214 struct message *mp;
215{
216 if (write(f, (char *) mp, sizeof *mp) != sizeof *mp)
217 return(1);
218 return(0);
219}
220
221/*
222 * Delete a file, but only if the file is a plain file.
223 */
224
225remove(name)
226 char name[];
227{
228 struct stat statb;
229 extern int errno;
230
231 if (stat(name, &statb) < 0)
232 return(-1);
233 if ((statb.st_mode & S_IFMT) != S_IFREG) {
234 errno = EISDIR;
235 return(-1);
236 }
237 return(unlink(name));
238}
239
240/*
241 * Terminate an editing session by attempting to write out the user's
5ab0568d 242 * file from the temporary. Save any new stuff appended to the file.
77a2ce8a 243 */
77a2ce8a
KS
244edstop()
245{
246 register int gotcha, c;
247 register struct message *mp;
8b5a06af 248 FILE *obuf, *ibuf, *readstat;
5ab0568d 249 struct stat statb;
8b5a06af 250 char tempname[30], *id;
b3d49ad3 251 int (*sigs[3])();
77a2ce8a 252
838681f8
KS
253 if (readonly)
254 return;
985c722d 255 holdsigs();
8b5a06af
KS
256 if (Tflag != NOSTR) {
257 if ((readstat = fopen(Tflag, "w")) == NULL)
258 Tflag = NOSTR;
259 }
953c713a
KS
260 for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
261 if (mp->m_flag & MNEW) {
262 mp->m_flag &= ~MNEW;
263 mp->m_flag |= MSTATUS;
264 }
8b5a06af 265 if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
77a2ce8a 266 gotcha++;
8b5a06af
KS
267 if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
268 if ((id = hfield("article-id", mp)) != NOSTR)
269 fprintf(readstat, "%s\n", id);
77a2ce8a 270 }
953c713a 271 }
8b5a06af
KS
272 if (Tflag != NOSTR)
273 fclose(readstat);
d85bd93d 274 if (!gotcha || Tflag != NOSTR)
b3d49ad3 275 goto done;
5ab0568d
KS
276 ibuf = NULL;
277 if (stat(editfile, &statb) >= 0 && statb.st_size > mailsize) {
278 strcpy(tempname, "/tmp/mboxXXXXXX");
279 mktemp(tempname);
280 if ((obuf = fopen(tempname, "w")) == NULL) {
281 perror(tempname);
985c722d 282 relsesigs();
5ab0568d
KS
283 reset(0);
284 }
285 if ((ibuf = fopen(editfile, "r")) == NULL) {
286 perror(editfile);
287 fclose(obuf);
288 remove(tempname);
985c722d 289 relsesigs();
5ab0568d
KS
290 reset(0);
291 }
0c3a2f40 292 fseek(ibuf, mailsize, 0);
5ab0568d
KS
293 while ((c = getc(ibuf)) != EOF)
294 putc(c, obuf);
295 fclose(ibuf);
296 fclose(obuf);
297 if ((ibuf = fopen(tempname, "r")) == NULL) {
298 perror(tempname);
299 remove(tempname);
985c722d 300 relsesigs();
5ab0568d
KS
301 reset(0);
302 }
303 remove(tempname);
304 }
77a2ce8a 305 printf("\"%s\" ", editfile);
80187484 306 fflush(stdout);
b17445e6 307 if ((obuf = fopen(editfile, "r+")) == NULL) {
77a2ce8a 308 perror(editfile);
985c722d 309 relsesigs();
77a2ce8a
KS
310 reset(0);
311 }
b17445e6 312 trunc(obuf);
77a2ce8a
KS
313 c = 0;
314 for (mp = &message[0]; mp < &message[msgCount]; mp++) {
315 if ((mp->m_flag & MDELETED) != 0)
316 continue;
317 c++;
c718ab56 318 if (send(mp, obuf, 0) < 0) {
77a2ce8a 319 perror(editfile);
985c722d 320 relsesigs();
77a2ce8a
KS
321 reset(0);
322 }
323 }
5ab0568d
KS
324 gotcha = (c == 0 && ibuf == NULL);
325 if (ibuf != NULL) {
326 while ((c = getc(ibuf)) != EOF)
327 putc(c, obuf);
328 fclose(ibuf);
329 }
77a2ce8a
KS
330 fflush(obuf);
331 if (ferror(obuf)) {
332 perror(editfile);
985c722d 333 relsesigs();
77a2ce8a
KS
334 reset(0);
335 }
5ab0568d
KS
336 fclose(obuf);
337 if (gotcha) {
77a2ce8a
KS
338 remove(editfile);
339 printf("removed\n");
340 }
341 else
342 printf("complete\n");
80187484 343 fflush(stdout);
b3d49ad3
KS
344
345done:
985c722d 346 relsesigs();
b3d49ad3
KS
347}
348
f5df67b9 349static int sigdepth = 0; /* depth of holdsigs() */
56d6aa4a 350static int omask = 0;
b3d49ad3 351/*
985c722d 352 * Hold signals SIGHUP - SIGQUIT.
b3d49ad3 353 */
985c722d 354holdsigs()
b3d49ad3
KS
355{
356 register int i;
357
f5df67b9 358 if (sigdepth++ == 0)
56d6aa4a 359 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
b3d49ad3
KS
360}
361
362/*
985c722d 363 * Release signals SIGHUP - SIGQUIT
b3d49ad3 364 */
985c722d 365relsesigs()
b3d49ad3
KS
366{
367 register int i;
985c722d 368
f5df67b9 369 if (--sigdepth == 0)
56d6aa4a 370 sigsetmask(omask);
77a2ce8a
KS
371}
372
77a2ce8a
KS
373/*
374 * Open a temp file by creating, closing, unlinking, and
375 * reopening. Return the open file descriptor.
376 */
377
378opentemp(file)
379 char file[];
380{
381 register int f;
382
383 if ((f = creat(file, 0600)) < 0) {
384 perror(file);
385 return(-1);
386 }
387 close(f);
388 if ((f = open(file, 2)) < 0) {
389 perror(file);
390 remove(file);
391 return(-1);
392 }
393 remove(file);
394 return(f);
395}
396
77a2ce8a
KS
397/*
398 * Determine the size of the file possessed by
399 * the passed buffer.
400 */
401
402off_t
403fsize(iob)
404 FILE *iob;
405{
406 register int f;
407 struct stat sbuf;
408
409 f = fileno(iob);
410 if (fstat(f, &sbuf) < 0)
411 return(0);
412 return(sbuf.st_size);
413}
414
415/*
416 * Take a file name, possibly with shell meta characters
417 * in it and expand it by using "sh -c echo filename"
418 * Return the file name as a dynamic string.
419 */
420
421char *
422expand(name)
423 char name[];
424{
425 char xname[BUFSIZ];
426 char cmdbuf[BUFSIZ];
427 register int pid, l, rc;
428 register char *cp, *Shell;
429 int s, pivec[2], (*sigint)();
430 struct stat sbuf;
431
df3c4fbd
KS
432 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
433 sprintf(xname, "%s/%s", cmdbuf, name + 1);
38a42545
KS
434 return(expand(savestr(xname)));
435 }
77a2ce8a
KS
436 if (!anyof(name, "~{[*?$`'\"\\"))
437 return(name);
77a2ce8a
KS
438 if (pipe(pivec) < 0) {
439 perror("pipe");
77a2ce8a
KS
440 return(name);
441 }
442 sprintf(cmdbuf, "echo %s", name);
443 if ((pid = vfork()) == 0) {
9ff57b47 444 sigchild();
77a2ce8a
KS
445 Shell = value("SHELL");
446 if (Shell == NOSTR)
447 Shell = SHELL;
448 close(pivec[0]);
449 close(1);
450 dup(pivec[1]);
451 close(pivec[1]);
452 close(2);
453 execl(Shell, Shell, "-c", cmdbuf, 0);
454 _exit(1);
455 }
456 if (pid == -1) {
457 perror("fork");
458 close(pivec[0]);
459 close(pivec[1]);
460 return(NOSTR);
461 }
462 close(pivec[1]);
463 l = read(pivec[0], xname, BUFSIZ);
464 close(pivec[0]);
465 while (wait(&s) != pid);
466 ;
467 s &= 0377;
468 if (s != 0 && s != SIGPIPE) {
469 fprintf(stderr, "\"Echo\" failed\n");
470 goto err;
471 }
472 if (l < 0) {
473 perror("read");
474 goto err;
475 }
476 if (l == 0) {
477 fprintf(stderr, "\"%s\": No match\n", name);
478 goto err;
479 }
480 if (l == BUFSIZ) {
481 fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
482 goto err;
483 }
484 xname[l] = 0;
485 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
486 ;
487 *++cp = '\0';
488 if (any(' ', xname) && stat(xname, &sbuf) < 0) {
489 fprintf(stderr, "\"%s\": Ambiguous\n", name);
490 goto err;
491 }
77a2ce8a
KS
492 return(savestr(xname));
493
494err:
77a2ce8a
KS
495 return(NOSTR);
496}
497
df3c4fbd
KS
498/*
499 * Determine the current folder directory name.
500 */
501getfold(name)
502 char *name;
503{
504 char *folder;
505
506 if ((folder = value("folder")) == NOSTR)
507 return(-1);
508 if (*folder == '/')
509 strcpy(name, folder);
510 else
511 sprintf(name, "%s/%s", homedir, folder);
512 return(0);
513}
514
77a2ce8a
KS
515/*
516 * A nicer version of Fdopen, which allows us to fclose
517 * without losing the open file.
518 */
519
520FILE *
521Fdopen(fildes, mode)
522 char *mode;
523{
524 register int f;
525 FILE *fdopen();
526
527 f = dup(fildes);
528 if (f < 0) {
529 perror("dup");
530 return(NULL);
531 }
532 return(fdopen(f, mode));
533}