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