Commit | Line | Data |
---|---|---|
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 |
435e8dff | 19 | static char sccsid[] = "@(#)fio.c 5.21 (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 | 38 | setptr(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); |
2ee3bce2 | 75 | (void) 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 |
126 | putline(obuf, linebuf) |
127 | FILE *obuf; | |
128 | char *linebuf; | |
129 | { | |
130 | register int c; | |
131 | ||
132 | c = strlen(linebuf); | |
2ee3bce2 EW |
133 | (void) fwrite(linebuf, sizeof *linebuf, c, obuf); |
134 | (void) putc('\n', obuf); | |
77a2ce8a | 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 | */ | |
38ed7007 | 145 | readline(ibuf, linebuf, linesize) |
77a2ce8a KS |
146 | FILE *ibuf; |
147 | char *linebuf; | |
148 | { | |
87d10954 | 149 | register int n; |
77a2ce8a | 150 | |
87d10954 | 151 | clearerr(ibuf); |
38ed7007 | 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 |
164 | FILE * |
165 | setinput(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 | 181 | makemessage(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); | |
2ee3bce2 | 194 | (void) lseek(fileno(f), (long) sizeof *message, 0); |
828615a1 EW |
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 |
206 | append(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 |
216 | remove(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 | ||
e4920814 EW |
231 | static int sigdepth; /* depth of holdsigs() */ |
232 | static int omask; | |
b3d49ad3 | 233 | /* |
828615a1 | 234 | * Hold signals SIGHUP, SIGINT, and SIGQUIT. |
b3d49ad3 | 235 | */ |
985c722d | 236 | holdsigs() |
b3d49ad3 | 237 | { |
b3d49ad3 | 238 | |
f5df67b9 | 239 | if (sigdepth++ == 0) |
56d6aa4a | 240 | omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)); |
b3d49ad3 KS |
241 | } |
242 | ||
243 | /* | |
828615a1 | 244 | * Release signals SIGHUP, SIGINT, and SIGQUIT. |
b3d49ad3 | 245 | */ |
985c722d | 246 | relsesigs() |
b3d49ad3 | 247 | { |
985c722d | 248 | |
f5df67b9 | 249 | if (--sigdepth == 0) |
56d6aa4a | 250 | sigsetmask(omask); |
77a2ce8a KS |
251 | } |
252 | ||
77a2ce8a | 253 | /* |
828615a1 EW |
254 | * Open a temp file by creating and unlinking. |
255 | * Return the open file descriptor. | |
77a2ce8a | 256 | */ |
77a2ce8a KS |
257 | opentemp(file) |
258 | char file[]; | |
259 | { | |
828615a1 | 260 | int f; |
77a2ce8a | 261 | |
828615a1 | 262 | if ((f = open(file, O_CREAT|O_EXCL|O_RDWR, 0600)) < 0) |
77a2ce8a | 263 | perror(file); |
77a2ce8a | 264 | remove(file); |
828615a1 | 265 | return (f); |
77a2ce8a KS |
266 | } |
267 | ||
77a2ce8a KS |
268 | /* |
269 | * Determine the size of the file possessed by | |
270 | * the passed buffer. | |
271 | */ | |
77a2ce8a KS |
272 | off_t |
273 | fsize(iob) | |
274 | FILE *iob; | |
275 | { | |
77a2ce8a KS |
276 | struct stat sbuf; |
277 | ||
828615a1 EW |
278 | if (fstat(fileno(iob), &sbuf) < 0) |
279 | return 0; | |
280 | return sbuf.st_size; | |
77a2ce8a KS |
281 | } |
282 | ||
283 | /* | |
2a0f6531 EW |
284 | * Evaluate the string given as a new mailbox name. |
285 | * Supported meta characters: | |
286 | * % for my system mail box | |
287 | * %user for user's system mail box | |
288 | * # for previous file | |
289 | * & invoker's mbox file | |
290 | * +file file in folder directory | |
291 | * any shell meta character | |
77a2ce8a KS |
292 | * Return the file name as a dynamic string. |
293 | */ | |
77a2ce8a KS |
294 | char * |
295 | expand(name) | |
4d20fb09 | 296 | register char *name; |
77a2ce8a | 297 | { |
f674e088 EW |
298 | char xname[PATHSIZE]; |
299 | char cmdbuf[PATHSIZE]; /* also used for file names */ | |
828615a1 | 300 | register int pid, l; |
d33aa50d | 301 | register char *cp, *shell; |
828615a1 | 302 | int pivec[2]; |
77a2ce8a | 303 | struct stat sbuf; |
322c8626 | 304 | extern union wait wait_status; |
77a2ce8a | 305 | |
954642f5 EW |
306 | /* |
307 | * The order of evaluation is "%" and "#" expand into constants. | |
308 | * "&" can expand into "+". "+" can expand into shell meta characters. | |
309 | * Shell meta characters expand into constants. | |
310 | * This way, we make no recursive expansion. | |
311 | */ | |
2a0f6531 EW |
312 | switch (*name) { |
313 | case '%': | |
f674e088 EW |
314 | findmail(name[1] ? name + 1 : myname, xname); |
315 | return savestr(xname); | |
2a0f6531 EW |
316 | case '#': |
317 | if (name[1] != 0) | |
318 | break; | |
319 | if (prevfile[0] == 0) { | |
320 | printf("No previous file\n"); | |
321 | return NOSTR; | |
322 | } | |
4d20fb09 | 323 | return savestr(prevfile); |
2a0f6531 | 324 | case '&': |
62e28b79 | 325 | if (name[1] == 0 && (name = value("MBOX")) == NOSTR) |
954642f5 | 326 | name = "~/mbox"; |
2a0f6531 EW |
327 | /* fall through */ |
328 | } | |
df3c4fbd KS |
329 | if (name[0] == '+' && getfold(cmdbuf) >= 0) { |
330 | sprintf(xname, "%s/%s", cmdbuf, name + 1); | |
2a0f6531 | 331 | name = savestr(xname); |
38a42545 | 332 | } |
954642f5 EW |
333 | /* catch the most common shell meta character */ |
334 | if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) { | |
62e28b79 | 335 | sprintf(xname, "%s%s", homedir, name + 1); |
954642f5 EW |
336 | name = savestr(xname); |
337 | } | |
77a2ce8a | 338 | if (!anyof(name, "~{[*?$`'\"\\")) |
4d20fb09 | 339 | return name; |
77a2ce8a KS |
340 | if (pipe(pivec) < 0) { |
341 | perror("pipe"); | |
4d20fb09 | 342 | return name; |
77a2ce8a KS |
343 | } |
344 | sprintf(cmdbuf, "echo %s", name); | |
d33aa50d | 345 | if ((shell = value("SHELL")) == NOSTR) |
435e8dff | 346 | shell = _PATH_CSHELL; |
d33aa50d EW |
347 | pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR); |
348 | if (pid < 0) { | |
77a2ce8a KS |
349 | close(pivec[0]); |
350 | close(pivec[1]); | |
4d20fb09 | 351 | return NOSTR; |
77a2ce8a KS |
352 | } |
353 | close(pivec[1]); | |
354 | l = read(pivec[0], xname, BUFSIZ); | |
355 | close(pivec[0]); | |
322c8626 | 356 | if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) { |
39251e41 | 357 | fprintf(stderr, "\"%s\": Expansion failed.\n", name); |
4d20fb09 | 358 | return NOSTR; |
77a2ce8a KS |
359 | } |
360 | if (l < 0) { | |
361 | perror("read"); | |
4d20fb09 | 362 | return NOSTR; |
77a2ce8a KS |
363 | } |
364 | if (l == 0) { | |
954642f5 | 365 | fprintf(stderr, "\"%s\": No match.\n", name); |
4d20fb09 | 366 | return NOSTR; |
77a2ce8a KS |
367 | } |
368 | if (l == BUFSIZ) { | |
954642f5 | 369 | fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name); |
4d20fb09 | 370 | return NOSTR; |
77a2ce8a KS |
371 | } |
372 | xname[l] = 0; | |
373 | for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--) | |
374 | ; | |
4d20fb09 | 375 | cp[1] = '\0'; |
470c33f3 | 376 | if (index(xname, ' ') && stat(xname, &sbuf) < 0) { |
954642f5 | 377 | fprintf(stderr, "\"%s\": Ambiguous.\n", name); |
4d20fb09 | 378 | return NOSTR; |
77a2ce8a | 379 | } |
4d20fb09 | 380 | return savestr(xname); |
77a2ce8a KS |
381 | } |
382 | ||
df3c4fbd KS |
383 | /* |
384 | * Determine the current folder directory name. | |
385 | */ | |
386 | getfold(name) | |
387 | char *name; | |
388 | { | |
389 | char *folder; | |
390 | ||
391 | if ((folder = value("folder")) == NOSTR) | |
828615a1 | 392 | return (-1); |
df3c4fbd KS |
393 | if (*folder == '/') |
394 | strcpy(name, folder); | |
395 | else | |
396 | sprintf(name, "%s/%s", homedir, folder); | |
828615a1 | 397 | return (0); |
df3c4fbd KS |
398 | } |
399 | ||
62e28b79 EW |
400 | /* |
401 | * Return the name of the dead.letter file. | |
402 | */ | |
403 | char * | |
404 | getdeadletter() | |
405 | { | |
406 | register char *cp; | |
407 | ||
408 | if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR) | |
409 | cp = expand("~/dead.letter"); | |
410 | else if (*cp != '/') { | |
411 | char buf[PATHSIZE]; | |
412 | ||
413 | (void) sprintf(buf, "~/%s", cp); | |
414 | cp = expand(buf); | |
415 | } | |
416 | return cp; | |
417 | } | |
418 | ||
77a2ce8a KS |
419 | /* |
420 | * A nicer version of Fdopen, which allows us to fclose | |
421 | * without losing the open file. | |
422 | */ | |
77a2ce8a KS |
423 | FILE * |
424 | Fdopen(fildes, mode) | |
425 | char *mode; | |
426 | { | |
828615a1 | 427 | int f; |
77a2ce8a | 428 | |
828615a1 | 429 | if ((f = dup(fildes)) < 0) { |
77a2ce8a | 430 | perror("dup"); |
828615a1 | 431 | return (NULL); |
77a2ce8a | 432 | } |
828615a1 | 433 | return fdopen(f, mode); |
77a2ce8a | 434 | } |