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