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