2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid[] = "@(#)aux.c 8.1 (Berkeley) 6/6/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
48 * Mail -- a mail program
50 * Auxiliary functions.
53 static char *save2str(char *, char *);
56 * Return a pointer to a dynamic copy of the argument.
63 int size = strlen(str) + 1;
65 if ((new = salloc(size)) != NULL)
66 bcopy(str, new, size);
71 * Make a copy of new argument incorporating old one.
78 int newsize = strlen(str) + 1;
79 int oldsize = old ? strlen(old) + 1 : 0;
81 if ((new = salloc(newsize + oldsize)) != NULL) {
83 bcopy(old, new, oldsize);
84 new[oldsize - 1] = ' ';
86 bcopy(str, new + oldsize, newsize);
92 * Touch the named message by setting its MTOUCH flag.
93 * Touched messages have the effect of not being sent
94 * back to the system mailbox on exit.
101 mp->m_flag |= MTOUCH;
102 if ((mp->m_flag & MREAD) == 0)
103 mp->m_flag |= MREAD|MSTATUS;
107 * Test to see if the passed file name is a directory.
108 * Return true if it is.
116 if (stat(name, &sbuf) < 0)
118 return (S_ISDIR(sbuf.st_mode));
122 * Count the number of arguments in the given string raw list.
130 for (ap = argv; *ap++ != NULL;)
132 return (ap - argv - 1);
136 * Return the desired header line from the passed message
137 * pointer (or NULL if the desired header field is not available).
145 char linebuf[LINESIZE];
148 char *colon, *oldhfield = NULL;
151 if ((lc = mp->m_lines - 1) < 0)
153 if (readline(ibuf, linebuf, LINESIZE) < 0)
156 if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
158 if ((hfield = ishfield(linebuf, colon, field)) != NULL)
159 oldhfield = save2str(hfield, oldhfield);
165 * Return the next header field found in the given message.
166 * Return >= 0 if something found, < 0 elsewise.
167 * "colon" is set to point to the colon in the header.
168 * Must deal with \ continuations & other such fraud.
171 gethfield(f, linebuf, rem, colon)
177 char line2[LINESIZE];
184 if ((c = readline(f, linebuf, LINESIZE)) <= 0)
186 for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
189 if (*cp != ':' || cp == linebuf)
192 * I guess we got a headline.
193 * Handle wraparounding
198 while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
203 ungetc(c = getc(f), f);
204 if (c != ' ' && c != '\t')
206 if ((c = readline(f, line2, LINESIZE)) < 0)
209 for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
212 if (cp + c >= linebuf + LINESIZE - 2)
225 * Check whether the passed line is a header line of
226 * the desired breed. Return the field body, or 0.
230 ishfield(linebuf, colon, field)
238 if (strcasecmp(linebuf, field) != 0) {
243 for (cp++; *cp == ' ' || *cp == '\t'; cp++)
249 * Copy a string and lowercase the result.
250 * dsize: space left in buffer (including space for NULL)
253 istrncpy(dest, src, dsize)
259 strlcpy(dest, src, dsize);
260 for (; *dest; dest++)
261 *dest = tolower((unsigned char)*dest);
265 * The following code deals with input stacking to do source
266 * commands. All but the current file pointer are saved on
270 static int ssp; /* Top of file stack */
272 FILE *s_file; /* File we were in. */
273 int s_cond; /* Saved state of conditionals */
274 int s_loading; /* Loading .mailrc, etc. */
276 #define SSTACK_SIZE 64 /* XXX was NOFILE. */
277 static struct sstack sstack[SSTACK_SIZE];
280 * Pushdown current input file and switch to a new one.
281 * Set the global flag "sourcing" so that others will realize
282 * that they are no longer reading from a tty (in all probability).
291 if ((cp = expand(*arglist)) == NULL)
293 if ((fi = Fopen(cp, "r")) == NULL) {
297 if (ssp >= SSTACK_SIZE - 1) {
298 printf("Too much \"sourcing\" going on.\n");
302 sstack[ssp].s_file = input;
303 sstack[ssp].s_cond = cond;
304 sstack[ssp].s_loading = loading;
314 * Pop the current input back to the previous level.
315 * Update the "sourcing" flag as appropriate.
321 printf("\"Source\" stack over-pop.\n");
327 printf("Unmatched \"if\"\n");
329 cond = sstack[ssp].s_cond;
330 loading = sstack[ssp].s_loading;
331 input = sstack[ssp].s_file;
338 * Touch the indicated file.
339 * This is nifty for the shell.
346 struct timeval tv[2];
350 (void)gettimeofday(&tv[0], (struct timezone *)NULL);
352 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
353 (void)utimes(name, tv);
357 * Get sender's name from this message. If the message has
358 * a bunch of arpanet stuff in it, we may have to skin the name
359 * before returning it.
368 cp = skin(name1(mp, reptype));
369 if (reptype != 0 || charcount(cp, '!') < 2)
371 cp2 = strrchr(cp, '!');
373 while (cp2 > cp && *cp2 != '!')
381 * Start of a "comment".
390 for (; nesting > 0 && *cp; cp++) {
408 * Skin an arpa net address according to the RFC 822 interpretation
415 char *nbuf, *bufend, *cp, *cp2;
416 int c, gotlt, lastsp;
420 if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
421 && strchr(name, ' ') == NULL)
424 /* We assume that length(input) <= length(output) */
425 if ((nbuf = malloc(strlen(name) + 1)) == NULL)
426 err(1, "Out of memory");
430 for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; ) {
433 cp = skip_comment(cp);
439 * Start of a "quoted-string".
440 * Copy it in its entirety.
442 while ((c = *cp) != '\0') {
448 else if ((c = *cp) != '\0') {
457 if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
458 cp += 3, *cp2++ = '@';
460 if (cp[0] == '@' && cp[1] == ' ')
461 cp += 2, *cp2++ = '@';
475 while ((c = *cp) != '\0' && c != ',') {
478 cp = skip_comment(cp);
480 while ((c = *cp) != '\0') {
484 if (c == '\\' && *cp != '\0')
499 if (c == ',' && !gotlt &&
500 (*cp == ' ' || *cp == '"' || *cp == '<')) {
511 if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
517 * Fetch the sender's name from the passed message.
519 * 0 -- get sender's name for display purposes
520 * 1 -- get sender's name for reply
521 * 2 -- get sender's name for Reply
528 char namebuf[LINESIZE];
529 char linebuf[LINESIZE];
534 if ((cp = hfield("from", mp)) != NULL)
536 if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
540 if (readline(ibuf, linebuf, LINESIZE) < 0)
541 return (savestr(namebuf));
543 for (cp = linebuf; *cp != '\0' && *cp != ' '; cp++)
545 for (; *cp == ' ' || *cp == '\t'; cp++)
547 for (cp2 = &namebuf[strlen(namebuf)];
548 *cp != '\0' && *cp != ' ' && *cp != '\t' &&
549 cp2 < namebuf + LINESIZE - 1;)
552 if (readline(ibuf, linebuf, LINESIZE) < 0)
553 return (savestr(namebuf));
554 if ((cp = strchr(linebuf, 'F')) == NULL)
555 return (savestr(namebuf));
556 if (strncmp(cp, "From", 4) != 0)
557 return (savestr(namebuf));
558 while ((cp = strchr(cp, 'r')) != NULL) {
559 if (strncmp(cp, "remote", 6) == 0) {
560 if ((cp = strchr(cp, 'f')) == NULL)
562 if (strncmp(cp, "from", 4) != 0)
564 if ((cp = strchr(cp, ' ')) == NULL)
571 cp2 = strrchr(namebuf, '!') + 1;
572 strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
573 strcat(namebuf, "!");
578 return (savestr(namebuf));
582 * Count the occurances of c in str
592 for (i = 0, cp = str; *cp != '\0'; cp++)
599 * See if the given header field is supposed to be ignored.
604 struct ignoretab ignore[2];
606 char realfld[LINESIZE];
608 if (ignore == ignoreall)
611 * Lower-case the string, so that "Status" and "status"
612 * will hash to the same place.
614 istrncpy(realfld, field, sizeof(realfld));
615 if (ignore[1].i_count > 0)
616 return (!member(realfld, ignore + 1));
618 return (member(realfld, ignore));
622 member(realfield, table)
624 struct ignoretab *table;
628 for (igp = table->i_head[hash(realfield)]; igp != NULL; igp = igp->i_link)
629 if (*igp->i_field == *realfield &&
630 equal(igp->i_field, realfield))