fio.c revision 1.1.1.3 1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)fio.c 8.2 (Berkeley) 4/20/95";
36 #endif /* not lint */
37
38 #include "rcv.h"
39 #include <sys/file.h>
40 #include <sys/wait.h>
41
42 #include <unistd.h>
43 #include <paths.h>
44 #include <errno.h>
45 #include "extern.h"
46
47 /*
48 * Mail -- a mail program
49 *
50 * File I/O.
51 */
52
53 /*
54 * Set up the input pointers while copying the mail file into /tmp.
55 */
56 void
57 setptr(ibuf, offset)
58 register FILE *ibuf;
59 off_t offset;
60 {
61 extern char *tmpdir;
62 register int c, count;
63 register char *cp, *cp2;
64 struct message this;
65 FILE *mestmp;
66 int maybe, inhead;
67 char linebuf[LINESIZE];
68 int omsgCount;
69
70 /* Get temporary file. */
71 (void)sprintf(linebuf, "%s/mail.XXXXXX", tmpdir);
72 if ((c = mkstemp(linebuf)) == -1 ||
73 (mestmp = Fdopen(c, "r+")) == NULL) {
74 (void)fprintf(stderr, "mail: can't open %s\n", linebuf);
75 exit(1);
76 }
77 (void)unlink(linebuf);
78
79 if (offset == 0) {
80 msgCount = 0;
81 } else {
82 /* Seek into the file to get to the new messages */
83 (void) fseek(ibuf, offset, 0);
84 /*
85 * We need to make "offset" a pointer to the end of
86 * the temp file that has the copy of the mail file.
87 * If any messages have been edited, this will be
88 * different from the offset into the mail file.
89 */
90 (void) fseek(otf, 0L, 2);
91 offset = ftell(otf);
92 }
93 omsgCount = msgCount;
94 maybe = 1;
95 inhead = 0;
96 this.m_flag = MUSED|MNEW;
97 this.m_size = 0;
98 this.m_lines = 0;
99 this.m_block = 0;
100 this.m_offset = 0;
101 for (;;) {
102 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
103 if (append(&this, mestmp)) {
104 perror("temporary file");
105 exit(1);
106 }
107 makemessage(mestmp, omsgCount);
108 return;
109 }
110 count = strlen(linebuf);
111 (void) fwrite(linebuf, sizeof *linebuf, count, otf);
112 if (ferror(otf)) {
113 perror("/tmp");
114 exit(1);
115 }
116 linebuf[count - 1] = 0;
117 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
118 msgCount++;
119 if (append(&this, mestmp)) {
120 perror("temporary file");
121 exit(1);
122 }
123 this.m_flag = MUSED|MNEW;
124 this.m_size = 0;
125 this.m_lines = 0;
126 this.m_block = blockof(offset);
127 this.m_offset = offsetof(offset);
128 inhead = 1;
129 } else if (linebuf[0] == 0) {
130 inhead = 0;
131 } else if (inhead) {
132 for (cp = linebuf, cp2 = "status";; cp++) {
133 if ((c = *cp2++) == 0) {
134 while (isspace(*cp++))
135 ;
136 if (cp[-1] != ':')
137 break;
138 while (c = *cp++)
139 if (c == 'R')
140 this.m_flag |= MREAD;
141 else if (c == 'O')
142 this.m_flag &= ~MNEW;
143 inhead = 0;
144 break;
145 }
146 if (*cp != c && *cp != toupper(c))
147 break;
148 }
149 }
150 offset += count;
151 this.m_size += count;
152 this.m_lines++;
153 maybe = linebuf[0] == 0;
154 }
155 }
156
157 /*
158 * Drop the passed line onto the passed output buffer.
159 * If a write error occurs, return -1, else the count of
160 * characters written, including the newline.
161 */
162 int
163 putline(obuf, linebuf)
164 FILE *obuf;
165 char *linebuf;
166 {
167 register int c;
168
169 c = strlen(linebuf);
170 (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
171 (void) putc('\n', obuf);
172 if (ferror(obuf))
173 return (-1);
174 return (c + 1);
175 }
176
177 /*
178 * Read up a line from the specified input into the line
179 * buffer. Return the number of characters read. Do not
180 * include the newline at the end.
181 */
182 int
183 readline(ibuf, linebuf, linesize)
184 FILE *ibuf;
185 char *linebuf;
186 int linesize;
187 {
188 register int n;
189
190 clearerr(ibuf);
191 if (fgets(linebuf, linesize, ibuf) == NULL)
192 return -1;
193 n = strlen(linebuf);
194 if (n > 0 && linebuf[n - 1] == '\n')
195 linebuf[--n] = '\0';
196 return n;
197 }
198
199 /*
200 * Return a file buffer all ready to read up the
201 * passed message pointer.
202 */
203 FILE *
204 setinput(mp)
205 register struct message *mp;
206 {
207
208 fflush(otf);
209 if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
210 perror("fseek");
211 panic("temporary file seek");
212 }
213 return (itf);
214 }
215
216 /*
217 * Take the data out of the passed ghost file and toss it into
218 * a dynamically allocated message structure.
219 */
220 void
221 makemessage(f, omsgCount)
222 FILE *f;
223 int omsgCount;
224 {
225 register size = (msgCount + 1) * sizeof (struct message);
226
227 if (omsgCount) {
228 message = (struct message *)realloc(message, (unsigned) size);
229 if (message == 0)
230 panic("Insufficient memory for %d messages\n", msgCount);
231 } else {
232 if (message != 0)
233 free((char *) message);
234 if ((message = (struct message *) malloc((unsigned) size)) == 0)
235 panic("Insufficient memory for %d messages", msgCount);
236 dot = message;
237 }
238 size -= (omsgCount + 1) * sizeof (struct message);
239 fflush(f);
240 (void) lseek(fileno(f), (off_t)sizeof *message, 0);
241 if (read(fileno(f), (char *) &message[omsgCount], size) != size)
242 panic("Message temporary file corrupted");
243 message[msgCount].m_size = 0;
244 message[msgCount].m_lines = 0;
245 Fclose(f);
246 }
247
248 /*
249 * Append the passed message descriptor onto the temp file.
250 * If the write fails, return 1, else 0
251 */
252 int
253 append(mp, f)
254 struct message *mp;
255 FILE *f;
256 {
257 return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
258 }
259
260 /*
261 * Delete a file, but only if the file is a plain file.
262 */
263 int
264 rm(name)
265 char *name;
266 {
267 struct stat sb;
268
269 if (stat(name, &sb) < 0)
270 return(-1);
271 if (!S_ISREG(sb.st_mode)) {
272 errno = EISDIR;
273 return(-1);
274 }
275 return(unlink(name));
276 }
277
278 static int sigdepth; /* depth of holdsigs() */
279 static int omask;
280 /*
281 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
282 */
283 void
284 holdsigs()
285 {
286
287 if (sigdepth++ == 0)
288 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
289 }
290
291 /*
292 * Release signals SIGHUP, SIGINT, and SIGQUIT.
293 */
294 void
295 relsesigs()
296 {
297
298 if (--sigdepth == 0)
299 sigsetmask(omask);
300 }
301
302 /*
303 * Determine the size of the file possessed by
304 * the passed buffer.
305 */
306 off_t
307 fsize(iob)
308 FILE *iob;
309 {
310 struct stat sbuf;
311
312 if (fstat(fileno(iob), &sbuf) < 0)
313 return 0;
314 return sbuf.st_size;
315 }
316
317 /*
318 * Evaluate the string given as a new mailbox name.
319 * Supported meta characters:
320 * % for my system mail box
321 * %user for user's system mail box
322 * # for previous file
323 * & invoker's mbox file
324 * +file file in folder directory
325 * any shell meta character
326 * Return the file name as a dynamic string.
327 */
328 char *
329 expand(name)
330 register char *name;
331 {
332 char xname[PATHSIZE];
333 char cmdbuf[PATHSIZE]; /* also used for file names */
334 register int pid, l;
335 register char *cp, *shell;
336 int pivec[2];
337 struct stat sbuf;
338 extern union wait wait_status;
339
340 /*
341 * The order of evaluation is "%" and "#" expand into constants.
342 * "&" can expand into "+". "+" can expand into shell meta characters.
343 * Shell meta characters expand into constants.
344 * This way, we make no recursive expansion.
345 */
346 switch (*name) {
347 case '%':
348 findmail(name[1] ? name + 1 : myname, xname);
349 return savestr(xname);
350 case '#':
351 if (name[1] != 0)
352 break;
353 if (prevfile[0] == 0) {
354 printf("No previous file\n");
355 return NOSTR;
356 }
357 return savestr(prevfile);
358 case '&':
359 if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
360 name = "~/mbox";
361 /* fall through */
362 }
363 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
364 sprintf(xname, "%s/%s", cmdbuf, name + 1);
365 name = savestr(xname);
366 }
367 /* catch the most common shell meta character */
368 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
369 sprintf(xname, "%s%s", homedir, name + 1);
370 name = savestr(xname);
371 }
372 if (!anyof(name, "~{[*?$`'\"\\"))
373 return name;
374 if (pipe(pivec) < 0) {
375 perror("pipe");
376 return name;
377 }
378 sprintf(cmdbuf, "echo %s", name);
379 if ((shell = value("SHELL")) == NOSTR)
380 shell = _PATH_CSHELL;
381 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
382 if (pid < 0) {
383 close(pivec[0]);
384 close(pivec[1]);
385 return NOSTR;
386 }
387 close(pivec[1]);
388 l = read(pivec[0], xname, BUFSIZ);
389 close(pivec[0]);
390 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
391 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
392 return NOSTR;
393 }
394 if (l < 0) {
395 perror("read");
396 return NOSTR;
397 }
398 if (l == 0) {
399 fprintf(stderr, "\"%s\": No match.\n", name);
400 return NOSTR;
401 }
402 if (l == BUFSIZ) {
403 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
404 return NOSTR;
405 }
406 xname[l] = 0;
407 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
408 ;
409 cp[1] = '\0';
410 if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
411 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
412 return NOSTR;
413 }
414 return savestr(xname);
415 }
416
417 /*
418 * Determine the current folder directory name.
419 */
420 int
421 getfold(name)
422 char *name;
423 {
424 char *folder;
425
426 if ((folder = value("folder")) == NOSTR)
427 return (-1);
428 if (*folder == '/')
429 strcpy(name, folder);
430 else
431 sprintf(name, "%s/%s", homedir, folder);
432 return (0);
433 }
434
435 /*
436 * Return the name of the dead.letter file.
437 */
438 char *
439 getdeadletter()
440 {
441 register char *cp;
442
443 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
444 cp = expand("~/dead.letter");
445 else if (*cp != '/') {
446 char buf[PATHSIZE];
447
448 (void) sprintf(buf, "~/%s", cp);
449 cp = expand(buf);
450 }
451 return cp;
452 }
453