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
33 * @(#)aux.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/mail/aux.c,v 1.4.6.4 2003/01/06 05:46:03 mikeh Exp $
35 * $DragonFly: src/usr.bin/mail/aux.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
44 * Mail -- a mail program
46 * Auxiliary functions.
49 static char *save2str(char *, char *);
52 * Return a pointer to a dynamic copy of the argument.
59 int size = strlen(str) + 1;
61 if ((new = salloc(size)) != NULL)
62 bcopy(str, new, size);
67 * Make a copy of new argument incorporating old one.
74 int newsize = strlen(str) + 1;
75 int oldsize = old ? strlen(old) + 1 : 0;
77 if ((new = salloc(newsize + oldsize)) != NULL) {
79 bcopy(old, new, oldsize);
80 new[oldsize - 1] = ' ';
82 bcopy(str, new + oldsize, newsize);
88 * Touch the named message by setting its MTOUCH flag.
89 * Touched messages have the effect of not being sent
90 * back to the system mailbox on exit.
98 if ((mp->m_flag & MREAD) == 0)
99 mp->m_flag |= MREAD|MSTATUS;
103 * Test to see if the passed file name is a directory.
104 * Return true if it is.
112 if (stat(name, &sbuf) < 0)
114 return (S_ISDIR(sbuf.st_mode));
118 * Count the number of arguments in the given string raw list.
126 for (ap = argv; *ap++ != NULL;)
128 return (ap - argv - 1);
132 * Return the desired header line from the passed message
133 * pointer (or NULL if the desired header field is not available).
141 char linebuf[LINESIZE];
144 char *colon, *oldhfield = NULL;
147 if ((lc = mp->m_lines - 1) < 0)
149 if (readline(ibuf, linebuf, LINESIZE) < 0)
152 if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
154 if ((hfield = ishfield(linebuf, colon, field)) != NULL)
155 oldhfield = save2str(hfield, oldhfield);
161 * Return the next header field found in the given message.
162 * Return >= 0 if something found, < 0 elsewise.
163 * "colon" is set to point to the colon in the header.
164 * Must deal with \ continuations & other such fraud.
167 gethfield(f, linebuf, rem, colon)
173 char line2[LINESIZE];
180 if ((c = readline(f, linebuf, LINESIZE)) <= 0)
182 for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
185 if (*cp != ':' || cp == linebuf)
188 * I guess we got a headline.
189 * Handle wraparounding
194 while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
199 ungetc(c = getc(f), f);
200 if (c != ' ' && c != '\t')
202 if ((c = readline(f, line2, LINESIZE)) < 0)
205 for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
208 if (cp + c >= linebuf + LINESIZE - 2)
221 * Check whether the passed line is a header line of
222 * the desired breed. Return the field body, or 0.
226 ishfield(linebuf, colon, field)
234 if (strcasecmp(linebuf, field) != 0) {
239 for (cp++; *cp == ' ' || *cp == '\t'; cp++)
245 * Copy a string and lowercase the result.
246 * dsize: space left in buffer (including space for NULL)
249 istrncpy(dest, src, dsize)
255 strlcpy(dest, src, dsize);
257 *dest++ = tolower((unsigned char)*dest);
261 * The following code deals with input stacking to do source
262 * commands. All but the current file pointer are saved on
266 static int ssp; /* Top of file stack */
268 FILE *s_file; /* File we were in. */
269 int s_cond; /* Saved state of conditionals */
270 int s_loading; /* Loading .mailrc, etc. */
272 #define SSTACK_SIZE 64 /* XXX was NOFILE. */
273 static struct sstack sstack[SSTACK_SIZE];
276 * Pushdown current input file and switch to a new one.
277 * Set the global flag "sourcing" so that others will realize
278 * that they are no longer reading from a tty (in all probability).
287 if ((cp = expand(*arglist)) == NULL)
289 if ((fi = Fopen(cp, "r")) == NULL) {
293 if (ssp >= SSTACK_SIZE - 1) {
294 printf("Too much \"sourcing\" going on.\n");
298 sstack[ssp].s_file = input;
299 sstack[ssp].s_cond = cond;
300 sstack[ssp].s_loading = loading;
310 * Pop the current input back to the previous level.
311 * Update the "sourcing" flag as appropriate.
317 printf("\"Source\" stack over-pop.\n");
323 printf("Unmatched \"if\"\n");
325 cond = sstack[ssp].s_cond;
326 loading = sstack[ssp].s_loading;
327 input = sstack[ssp].s_file;
334 * Touch the indicated file.
335 * This is nifty for the shell.
342 struct timeval tv[2];
346 (void)gettimeofday(&tv[0], (struct timezone *)NULL);
348 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
349 (void)utimes(name, tv);
353 * Get sender's name from this message. If the message has
354 * a bunch of arpanet stuff in it, we may have to skin the name
355 * before returning it.
364 cp = skin(name1(mp, reptype));
365 if (reptype != 0 || charcount(cp, '!') < 2)
367 cp2 = strrchr(cp, '!');
369 while (cp2 > cp && *cp2 != '!')
377 * Start of a "comment".
386 for (; nesting > 0 && *cp; cp++) {
404 * Skin an arpa net address according to the RFC 822 interpretation
411 char *nbuf, *bufend, *cp, *cp2;
412 int c, gotlt, lastsp;
416 if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
417 && strchr(name, ' ') == NULL)
420 /* We assume that length(input) <= length(output) */
421 if ((nbuf = malloc(strlen(name) + 1)) == NULL)
422 err(1, "Out of memory");
426 for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; ) {
429 cp = skip_comment(cp);
435 * Start of a "quoted-string".
436 * Copy it in its entirety.
438 while ((c = *cp) != '\0') {
444 else if ((c = *cp) != '\0') {
453 if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
454 cp += 3, *cp2++ = '@';
456 if (cp[0] == '@' && cp[1] == ' ')
457 cp += 2, *cp2++ = '@';
471 while ((c = *cp) != '\0' && c != ',') {
474 cp = skip_comment(cp);
476 while ((c = *cp) != '\0') {
480 if (c == '\\' && *cp != '\0')
495 if (c == ',' && *cp == ' ' && !gotlt) {
506 if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
512 * Fetch the sender's name from the passed message.
514 * 0 -- get sender's name for display purposes
515 * 1 -- get sender's name for reply
516 * 2 -- get sender's name for Reply
523 char namebuf[LINESIZE];
524 char linebuf[LINESIZE];
529 if ((cp = hfield("from", mp)) != NULL)
531 if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
535 if (readline(ibuf, linebuf, LINESIZE) < 0)
536 return (savestr(namebuf));
538 for (cp = linebuf; *cp != '\0' && *cp != ' '; cp++)
540 for (; *cp == ' ' || *cp == '\t'; cp++)
542 for (cp2 = &namebuf[strlen(namebuf)];
543 *cp != '\0' && *cp != ' ' && *cp != '\t' &&
544 cp2 < namebuf + LINESIZE - 1;)
547 if (readline(ibuf, linebuf, LINESIZE) < 0)
548 return (savestr(namebuf));
549 if ((cp = strchr(linebuf, 'F')) == NULL)
550 return (savestr(namebuf));
551 if (strncmp(cp, "From", 4) != 0)
552 return (savestr(namebuf));
553 while ((cp = strchr(cp, 'r')) != NULL) {
554 if (strncmp(cp, "remote", 6) == 0) {
555 if ((cp = strchr(cp, 'f')) == NULL)
557 if (strncmp(cp, "from", 4) != 0)
559 if ((cp = strchr(cp, ' ')) == NULL)
566 cp2 = strrchr(namebuf, '!') + 1;
567 strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
568 strcat(namebuf, "!");
573 return (savestr(namebuf));
577 * Count the occurances of c in str
587 for (i = 0, cp = str; *cp != '\0'; cp++)
594 * See if the given header field is supposed to be ignored.
599 struct ignoretab ignore[2];
601 char realfld[LINESIZE];
603 if (ignore == ignoreall)
606 * Lower-case the string, so that "Status" and "status"
607 * will hash to the same place.
609 istrncpy(realfld, field, sizeof(realfld));
610 if (ignore[1].i_count > 0)
611 return (!member(realfld, ignore + 1));
613 return (member(realfld, ignore));
617 member(realfield, table)
619 struct ignoretab *table;
623 for (igp = table->i_head[hash(realfield)]; igp != NULL; igp = igp->i_link)
624 if (*igp->i_field == *realfield &&
625 equal(igp->i_field, realfield))