do chmod last in install
[unix-history] / usr / src / usr.bin / mail / aux.c
CommitLineData
6447a23f
KS
1#
2
3#include "rcv.h"
4#include <sys/stat.h>
6447a23f
KS
5#include <ctype.h>
6
7/*
8 * Mail -- a mail program
9 *
10 * Auxiliary functions.
11 */
12
8bcfa450 13static char *SccsId = "@(#)aux.c 2.3 %G%";
6447a23f
KS
14
15/*
16 * Return a pointer to a dynamic copy of the argument.
17 */
18
19char *
20savestr(str)
21 char *str;
22{
23 register char *cp, *cp2, *top;
24
25 for (cp = str; *cp; cp++)
26 ;
27 top = salloc(cp-str + 1);
28 if (top == NOSTR)
29 return(NOSTR);
30 for (cp = str, cp2 = top; *cp; cp++)
31 *cp2++ = *cp;
32 *cp2 = 0;
33 return(top);
34}
35
36/*
37 * Copy the name from the passed header line into the passed
38 * name buffer. Null pad the name buffer.
39 */
40
41copyname(linebuf, nbuf)
42 char *linebuf, *nbuf;
43{
44 register char *cp, *cp2;
45
46 for (cp = linebuf + 5, cp2 = nbuf; *cp != ' ' && cp2-nbuf < 8; cp++)
47 *cp2++ = *cp;
48 while (cp2-nbuf < 8)
49 *cp2++ = 0;
50}
51
52/*
53 * Announce a fatal error and die.
54 */
55
56panic(str)
57 char *str;
58{
59 prs("panic: ");
60 prs(str);
61 prs("\n");
62 exit(1);
63}
64
65/*
66 * Catch stdio errors and report them more nicely.
67 */
68
69_error(str)
70 char *str;
71{
72 prs("Stdio Error: ");
73 prs(str);
74 prs("\n");
75 abort();
76}
77
78/*
79 * Print a string on diagnostic output.
80 */
81
82prs(str)
83 char *str;
84{
85 register char *s;
86
87 for (s = str; *s; s++)
88 ;
89 write(2, str, s-str);
90}
91
92/*
93 * Touch the named message by setting its MTOUCH flag.
94 * Touched messages have the effect of not being sent
95 * back to the system mailbox on exit.
96 */
97
98touch(mesg)
99{
dffe7e4f
KS
100 register struct message *mp;
101
102 if (mesg < 1 || mesg > msgCount)
103 return;
104 mp = &message[mesg-1];
105 mp->m_flag |= MTOUCH;
106 if ((mp->m_flag & MREAD) == 0)
107 mp->m_flag |= MREAD|MSTATUS;
6447a23f
KS
108}
109
110/*
111 * Test to see if the passed file name is a directory.
112 * Return true if it is.
113 */
114
115isdir(name)
116 char name[];
117{
118 struct stat sbuf;
119
120 if (stat(name, &sbuf) < 0)
121 return(0);
122 return((sbuf.st_mode & S_IFMT) == S_IFDIR);
123}
124
125/*
126 * Compute the size in characters of the passed message
127 */
128
129unsigned int
130msize(messp)
131 struct message *messp;
132{
133 register struct message *mp;
134
135 mp = messp;
136 return(mp->m_size);
137}
138
139/*
140 * Count the number of arguments in the given string raw list.
141 */
142
143argcount(argv)
144 char **argv;
145{
146 register char **ap;
147
148 for (ap = argv; *ap != NOSTR; ap++)
149 ;
150 return(ap-argv);
151}
152
153/*
154 * Given a file address, determine the
155 * block number it represents.
156 */
157
158blockof(off)
159 off_t off;
160{
161 off_t a;
162
163 a = off >> 9;
164 a &= 077777;
165 return((int) a);
166}
167
168/*
169 * Take a file address, and determine
170 * its offset in the current block.
171 */
172
173offsetof(off)
174 off_t off;
175{
176 off_t a;
177
178 a = off & 0777;
179 return((int) a);
180}
181
182/*
183 * Determine if the passed file is actually a tty, via a call to
184 * gtty. This is not totally reliable, but . . .
185 */
186
187isatty(f)
188{
189 struct sgttyb buf;
190
191 if (gtty(f, &buf) < 0)
192 return(0);
193 return(1);
194}
195
196/*
197 * Return the desired header line from the passed message
198 * pointer (or NOSTR if the desired header field is not available).
199 */
200
201char *
202hfield(field, mp)
203 char field[];
204 struct message *mp;
205{
206 register FILE *ibuf;
207 char linebuf[LINESIZE];
208 register int lc;
209
210 ibuf = setinput(mp);
211 if ((lc = mp->m_lines) <= 0)
212 return(NOSTR);
213 if (readline(ibuf, linebuf) < 0)
214 return(NOSTR);
215 lc--;
216 do {
217 lc = gethfield(ibuf, linebuf, lc);
218 if (lc == -1)
219 return(NOSTR);
220 if (ishfield(linebuf, field))
221 return(savestr(hcontents(linebuf)));
222 } while (lc > 0);
223 return(NOSTR);
224}
225
226/*
227 * Return the next header field found in the given message.
228 * Return > 0 if something found, <= 0 elsewise.
229 * Must deal with \ continuations & other such fraud.
230 */
231
232gethfield(f, linebuf, rem)
233 register FILE *f;
234 char linebuf[];
235 register int rem;
236{
237 char line2[LINESIZE];
238 long loc;
239 register char *cp, *cp2;
240 register int c;
241
242
243 for (;;) {
244 if (rem <= 0)
245 return(-1);
246 if (readline(f, linebuf) < 0)
247 return(-1);
248 rem--;
249 if (strlen(linebuf) == 0)
250 return(-1);
251 if (isspace(linebuf[0]))
252 continue;
253 if (linebuf[0] == '>')
254 continue;
255 cp = index(linebuf, ':');
256 if (cp == NOSTR)
257 continue;
258 for (cp2 = linebuf; cp2 < cp; cp2++)
259 if (isdigit(*cp2))
260 continue;
261
262 /*
263 * I guess we got a headline.
264 * Handle wraparounding
265 */
266
267 for (;;) {
268 if (rem <= 0)
269 break;
270#ifdef CANTELL
271 loc = ftell(f);
272 if (readline(f, line2) < 0)
273 break;
274 rem--;
275 if (!isspace(line2[0])) {
276 fseek(f, loc, 0);
277 rem++;
278 break;
279 }
280#else
281 c = getc(f);
282 ungetc(c, f);
283 if (!isspace(c) || c == '\n')
284 break;
285 if (readline(f, line2) < 0)
286 break;
287 rem--;
288#endif
289 cp2 = line2;
290 for (cp2 = line2; *cp2 != 0 && isspace(*cp2); cp2++)
291 ;
292 if (strlen(linebuf) + strlen(cp2) >= LINESIZE-2)
293 break;
294 cp = &linebuf[strlen(linebuf)];
295 while (cp > linebuf &&
296 (isspace(cp[-1]) || cp[-1] == '\\'))
297 cp--;
298 *cp++ = ' ';
299 for (cp2 = line2; *cp2 != 0 && isspace(*cp2); cp2++)
300 ;
301 strcpy(cp, cp2);
302 }
303 if ((c = strlen(linebuf)) > 0) {
304 cp = &linebuf[c-1];
305 while (cp > linebuf && isspace(*cp))
306 cp--;
307 *++cp = 0;
308 }
309 return(rem);
310 }
311 /* NOTREACHED */
312}
313
314/*
315 * Check whether the passed line is a header line of
316 * the desired breed.
317 */
318
319ishfield(linebuf, field)
320 char linebuf[], field[];
321{
322 register char *cp;
323 register int c;
324
325 if ((cp = index(linebuf, ':')) == NOSTR)
326 return(0);
327 if (cp == linebuf)
328 return(0);
329 cp--;
330 while (cp > linebuf && isspace(*cp))
331 cp--;
332 c = *++cp;
333 *cp = 0;
334 if (icequal(linebuf ,field)) {
335 *cp = c;
336 return(1);
337 }
338 *cp = c;
339 return(0);
340}
341
342/*
343 * Extract the non label information from the given header field
344 * and return it.
345 */
346
347char *
348hcontents(hfield)
349 char hfield[];
350{
351 register char *cp;
352
353 if ((cp = index(hfield, ':')) == NOSTR)
354 return(NOSTR);
355 cp++;
356 while (*cp && isspace(*cp))
357 cp++;
358 return(cp);
359}
360
361/*
362 * Compare two strings, ignoring case.
363 */
364
365icequal(s1, s2)
366 register char *s1, *s2;
367{
368
369 while (raise(*s1++) == raise(*s2))
370 if (*s2++ == 0)
371 return(1);
372 return(0);
373}
374
375/*
376 * The following code deals with input stacking to do source
377 * commands. All but the current file pointer are saved on
378 * the stack.
379 */
380
381static int ssp = -1; /* Top of file stack */
e48b42f1
KS
382struct sstack {
383 FILE *s_file; /* File we were in. */
384 int s_cond; /* Saved state of conditionals */
385} sstack[_NFILE];
6447a23f
KS
386
387/*
388 * Pushdown current input file and switch to a new one.
389 * Set the global flag "sourcing" so that others will realize
390 * that they are no longer reading from a tty (in all probability).
391 */
392
393source(name)
394 char name[];
395{
396 register FILE *fi;
5c7ba847 397 register char *cp;
6447a23f 398
5c7ba847
KS
399 if ((cp = expand(name)) == NOSTR)
400 return(1);
401 if ((fi = fopen(cp, "r")) == NULL) {
402 perror(cp);
6447a23f
KS
403 return(1);
404 }
405 if (ssp >= _NFILE-2) {
406 printf("Too much \"sourcing\" going on.\n");
407 fclose(fi);
408 return(1);
409 }
e48b42f1
KS
410 sstack[++ssp].s_file = input;
411 sstack[ssp].s_cond = cond;
412 cond = CANY;
6447a23f
KS
413 input = fi;
414 sourcing++;
415 return(0);
416}
417
418/*
419 * Source a file, but do nothing if the file cannot be opened.
420 */
421
422source1(name)
423 char name[];
424{
425 register int f;
426
427 if ((f = open(name, 0)) < 0)
428 return(0);
429 close(f);
430 source(name);
431}
432
433/*
434 * Pop the current input back to the previous level.
435 * Update the "sourcing" flag as appropriate.
436 */
437
438unstack()
439{
440 if (ssp < 0) {
441 printf("\"Source\" stack over-pop.\n");
442 sourcing = 0;
443 return(1);
444 }
445 fclose(input);
e48b42f1
KS
446 if (cond != CANY)
447 printf("Unmatched \"if\"\n");
448 cond = sstack[ssp].s_cond;
449 input = sstack[ssp--].s_file;
6447a23f
KS
450 if (ssp < 0)
451 sourcing = 0;
452 return(0);
453}
454
455/*
456 * Touch the indicated file.
457 * This is nifty for the shell.
458 * If we have the utime() system call, this is better served
459 * by using that, since it will work for empty files.
460 * On non-utime systems, we must sleep a second, then read.
461 */
462
463alter(name)
464 char name[];
465{
466#ifdef UTIME
467 struct stat statb;
468 long time();
469 time_t time_p[2];
470#else
471 register int pid, f;
472 char w;
473#endif UTIME
474
475#ifdef UTIME
476 if (stat(name, &statb) < 0)
477 return;
478 time_p[0] = time((long *) 0) + 1;
479 time_p[1] = statb.st_mtime;
480 utime(name, time_p);
481#else
6447a23f
KS
482 sleep(1);
483 if ((f = open(name, 0)) < 0)
a0852086 484 return;
6447a23f
KS
485 read(f, &w, 1);
486 exit(0);
487#endif
488}
489
490/*
491 * Examine the passed line buffer and
492 * return true if it is all blanks and tabs.
493 */
494
495blankline(linebuf)
496 char linebuf[];
497{
498 register char *cp;
499
500 for (cp = linebuf; *cp; cp++)
501 if (!any(*cp, " \t"))
502 return(0);
503 return(1);
504}
505
12388009
KS
506/*
507 * Get sender's name from this message. If the message has
508 * a bunch of arpanet stuff in it, we may have to skin the name
509 * before returning it.
510 */
511char *
512nameof(mp, reptype)
513 register struct message *mp;
514{
8bcfa450 515 register char *cp, *cp2;
12388009 516
8bcfa450
KS
517 cp = skin(name1(mp, reptype));
518 if (reptype != 0 || charcount(cp, '!') < 2)
519 return(cp);
520 cp2 = rindex(cp, '!');
521 cp2--;
522 while (cp2 > cp && *cp2 != '!')
523 cp2--;
524 if (*cp2 == '!')
525 return(cp2 + 1);
526 return(cp);
12388009
KS
527}
528
529/*
530 * Skin an arpa net address according to the RFC 733 interpretation
531 * of "host-phrase."
532 */
533char *
534skin(name)
535 char *name;
536{
537 register int c;
538 register char *cp, *cp2;
539 int gotlt, lastsp;
540 char nbuf[BUFSIZ];
541
542 if (name == NOSTR)
543 return(NOSTR);
544 if (index(name, '(') == NOSTR && index(name, '<') == NOSTR)
545 return(name);
546 gotlt = 0;
547 lastsp = 0;
548 for (cp = name, cp2 = nbuf, c = *cp++; *cp; c = *cp++) {
549 switch (c) {
550 case '(':
551 while (*cp != ')' && *cp != 0)
552 cp++;
553 if (*cp)
554 cp++;
555 break;
556
557 case ' ':
558 lastsp = 1;
559 break;
560
561 case '<':
562 cp2 = nbuf;
563 gotlt++;
564 lastsp = 0;
565 break;
566
567 case '>':
568 if (gotlt)
569 goto done;
570
571 /* Fall into . . . */
572
573 default:
574 if (lastsp) {
575 lastsp = 0;
576 *cp2++ = ' ';
577 }
578 *cp2++ = c;
579 break;
580 }
581 }
582done:
583 *cp2 = 0;
584
585 return(savestr(nbuf));
586}
587
6447a23f
KS
588/*
589 * Fetch the sender's name from the passed message.
12388009
KS
590 * Reptype can be
591 * 0 -- get sender's name for display purposes
592 * 1 -- get sender's name for reply
593 * 2 -- get sender's name for Reply
6447a23f
KS
594 */
595
596char *
12388009 597name1(mp, reptype)
6447a23f
KS
598 register struct message *mp;
599{
600 char namebuf[LINESIZE];
601 char linebuf[LINESIZE];
602 register char *cp, *cp2;
603 register FILE *ibuf;
604 int first = 1;
605
12388009
KS
606#ifndef DELIVERMAIL
607 if ((cp = hfield("from", mp)) != NOSTR)
608 return(cp);
609 if (reptype == 0 && (cp = hfield("sender", mp)) != NOSTR)
610 return(cp);
611#endif
6447a23f
KS
612 ibuf = setinput(mp);
613 copy("", namebuf);
614 if (readline(ibuf, linebuf) <= 0)
615 return(savestr(namebuf));
616newname:
617 for (cp = linebuf; *cp != ' '; cp++)
618 ;
619 while (any(*cp, " \t"))
620 cp++;
621 for (cp2 = &namebuf[strlen(namebuf)]; *cp && !any(*cp, " \t") &&
622 cp2-namebuf < LINESIZE-1; *cp2++ = *cp++)
623 ;
624 *cp2 = '\0';
625 if (readline(ibuf, linebuf) <= 0)
626 return(savestr(namebuf));
627 if ((cp = index(linebuf, 'F')) == NULL)
628 return(savestr(namebuf));
629 if (strncmp(cp, "From", 4) != 0)
630 return(savestr(namebuf));
631 while ((cp = index(cp, 'r')) != NULL) {
632 if (strncmp(cp, "remote", 6) == 0) {
633 if ((cp = index(cp, 'f')) == NULL)
634 break;
635 if (strncmp(cp, "from", 4) != 0)
636 break;
637 if ((cp = index(cp, ' ')) == NULL)
638 break;
639 cp++;
640 if (first) {
641 copy(cp, namebuf);
642 first = 0;
643 } else
644 strcpy(rindex(namebuf, '!')+1, cp);
645 strcat(namebuf, "!");
646 goto newname;
647 }
648 cp++;
649 }
650 return(savestr(namebuf));
651}
652
8bcfa450
KS
653/*
654 * Count the occurances of c in str
655 */
656charcount(str, c)
657 char *str;
658{
659 register char *cp;
660 register int i;
661
662 for (i = 0, cp = str; *cp; cp++)
663 if (*cp == c)
664 i++;
665 return(i);
666}
667
6447a23f
KS
668/*
669 * Find the rightmost pointer to an instance of the
670 * character in the string and return it.
671 */
6447a23f
KS
672char *
673rindex(str, c)
674 char str[];
675 register int c;
676{
677 register char *cp, *cp2;
678
679 for (cp = str, cp2 = NOSTR; *cp; cp++)
680 if (c == *cp)
681 cp2 = cp;
682 return(cp2);
683}
684
685/*
686 * See if the string is a number.
687 */
688
689numeric(str)
690 char str[];
691{
692 register char *cp = str;
693
694 while (*cp)
695 if (!isdigit(*cp++))
696 return(0);
697 return(1);
698}
699
700/*
701 * Are any of the characters in the two strings the same?
702 */
703
704anyof(s1, s2)
705 register char *s1, *s2;
706{
707 register int c;
708
709 while (c = *s1++)
710 if (any(c, s2))
711 return(1);
712 return(0);
713}
714
715/*
716 * Determine the leftmost index of the character
717 * in the string.
718 */
719
720char *
721index(str, ch)
722 char *str;
723{
724 register char *cp;
725 register int c;
726
727 for (c = ch, cp = str; *cp; cp++)
728 if (*cp == c)
729 return(cp);
730 return(NOSTR);
731}
732
733/*
734 * String compare two strings of bounded length.
735 */
736
737strncmp(as1, as2, an)
738 char *as1, *as2;
739{
740 register char *s1, *s2;
741 register int n;
742
743 s1 = as1;
744 s2 = as2;
745 n = an;
746 while (--n >= 0 && *s1 == *s2++)
747 if (*s1++ == '\0')
748 return(0);
749 return(n<0 ? 0 : *s1 - *--s2);
750}
751