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