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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)aux.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/mail/aux.c,v 1.4.6.4 2003/01/06 05:46:03 mikeh Exp $
39 * Mail -- a mail program
41 * Auxiliary functions.
44 static char *save2str(char *, char *);
47 * Return a pointer to a dynamic copy of the argument.
53 int size = strlen(str) + 1;
55 if ((new = salloc(size)) != NULL)
56 bcopy(str, new, size);
61 * Make a copy of new argument incorporating old one.
64 save2str(char *str, char *old)
67 int newsize = strlen(str) + 1;
68 int oldsize = old ? strlen(old) + 1 : 0;
70 if ((new = salloc(newsize + oldsize)) != NULL) {
72 bcopy(old, new, oldsize);
73 new[oldsize - 1] = ' ';
75 bcopy(str, new + oldsize, newsize);
81 * Touch the named message by setting its MTOUCH flag.
82 * Touched messages have the effect of not being sent
83 * back to the system mailbox on exit.
86 touch(struct message *mp)
89 if ((mp->m_flag & MREAD) == 0)
90 mp->m_flag |= MREAD|MSTATUS;
94 * Test to see if the passed file name is a directory.
95 * Return true if it is.
102 if (stat(name, &sbuf) < 0)
104 return (S_ISDIR(sbuf.st_mode));
108 * Count the number of arguments in the given string raw list.
111 argcount(char **argv)
115 for (ap = argv; *ap++ != NULL;)
117 return (ap - argv - 1);
121 * Return the desired header line from the passed message
122 * pointer (or NULL if the desired header field is not available).
125 hfield(const char *field, struct message *mp)
128 char linebuf[LINESIZE];
131 char *colon, *oldhfield = NULL;
134 if ((lc = mp->m_lines - 1) < 0)
136 if (readline(ibuf, linebuf, LINESIZE) < 0)
139 if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
141 if ((hfield = ishfield(linebuf, colon, field)) != NULL)
142 oldhfield = save2str(hfield, oldhfield);
148 * Return the next header field found in the given message.
149 * Return >= 0 if something found, < 0 elsewise.
150 * "colon" is set to point to the colon in the header.
151 * Must deal with \ continuations & other such fraud.
154 gethfield(FILE *f, char *linebuf, int rem, char **colon)
156 char line2[LINESIZE];
163 if ((c = readline(f, linebuf, LINESIZE)) <= 0)
165 for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
168 if (*cp != ':' || cp == linebuf)
171 * I guess we got a headline.
172 * Handle wraparounding
177 while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
182 ungetc(c = getc(f), f);
183 if (c != ' ' && c != '\t')
185 if ((c = readline(f, line2, LINESIZE)) < 0)
188 for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
191 if (cp + c >= linebuf + LINESIZE - 2)
204 * Check whether the passed line is a header line of
205 * the desired breed. Return the field body, or 0.
209 ishfield(char *linebuf, char *colon, const char *field)
214 if (strcasecmp(linebuf, field) != 0) {
219 for (cp++; *cp == ' ' || *cp == '\t'; cp++)
225 * Copy a string and lowercase the result.
226 * dsize: space left in buffer (including space for NULL)
229 istrncpy(char *dest, const char *src, size_t dsize)
231 strlcpy(dest, src, dsize);
232 for (; *dest; dest++)
233 *dest = tolower((unsigned char)*dest);
237 * The following code deals with input stacking to do source
238 * commands. All but the current file pointer are saved on
242 static int ssp; /* Top of file stack */
244 FILE *s_file; /* File we were in. */
245 int s_cond; /* Saved state of conditionals */
246 int s_loading; /* Loading .mailrc, etc. */
248 #define SSTACK_SIZE 64 /* XXX was NOFILE. */
249 static struct sstack sstack[SSTACK_SIZE];
252 * Pushdown current input file and switch to a new one.
253 * Set the global flag "sourcing" so that others will realize
254 * that they are no longer reading from a tty (in all probability).
257 source(char **arglist)
262 if ((cp = expand(*arglist)) == NULL)
264 if ((fi = Fopen(cp, "r")) == NULL) {
268 if (ssp >= SSTACK_SIZE - 1) {
269 printf("Too much \"sourcing\" going on.\n");
273 sstack[ssp].s_file = input;
274 sstack[ssp].s_cond = cond;
275 sstack[ssp].s_loading = loading;
285 * Pop the current input back to the previous level.
286 * Update the "sourcing" flag as appropriate.
292 printf("\"Source\" stack over-pop.\n");
298 printf("Unmatched \"if\"\n");
300 cond = sstack[ssp].s_cond;
301 loading = sstack[ssp].s_loading;
302 input = sstack[ssp].s_file;
309 * Touch the indicated file.
310 * This is nifty for the shell.
316 struct timeval tv[2];
320 gettimeofday(&tv[0], NULL);
322 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
327 * Get sender's name from this message. If the message has
328 * a bunch of arpanet stuff in it, we may have to skin the name
329 * before returning it.
332 nameof(struct message *mp, int reptype)
336 cp = skin(name1(mp, reptype));
337 if (reptype != 0 || charcount(cp, '!') < 2)
339 cp2 = strrchr(cp, '!');
341 while (cp2 > cp && *cp2 != '!')
349 * Start of a "comment".
353 skip_comment(char *cp)
357 for (; nesting > 0 && *cp; cp++) {
375 * Skin an arpa net address according to the RFC 822 interpretation
381 char *nbuf, *bufend, *cp, *cp2;
382 int c, gotlt, lastsp;
386 if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
387 && strchr(name, ' ') == NULL)
390 /* We assume that length(input) <= length(output) */
391 if ((nbuf = malloc(strlen(name) + 1)) == NULL)
392 err(1, "Out of memory");
396 for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; ) {
399 cp = skip_comment(cp);
405 * Start of a "quoted-string".
406 * Copy it in its entirety.
408 while ((c = *cp) != '\0') {
414 else if ((c = *cp) != '\0') {
423 if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
424 cp += 3, *cp2++ = '@';
426 if (cp[0] == '@' && cp[1] == ' ')
427 cp += 2, *cp2++ = '@';
441 while ((c = *cp) != '\0' && c != ',') {
444 cp = skip_comment(cp);
446 while ((c = *cp) != '\0') {
450 if (c == '\\' && *cp != '\0')
465 if (c == ',' && *cp == ' ' && !gotlt) {
476 if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
482 * Fetch the sender's name from the passed message.
484 * 0 -- get sender's name for display purposes
485 * 1 -- get sender's name for reply
486 * 2 -- get sender's name for Reply
489 name1(struct message *mp, int reptype)
491 char namebuf[LINESIZE];
492 char linebuf[LINESIZE];
497 if ((cp = hfield("from", mp)) != NULL)
499 if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
503 if (readline(ibuf, linebuf, LINESIZE) < 0)
504 return (savestr(namebuf));
506 for (cp = linebuf; *cp != '\0' && *cp != ' '; cp++)
508 for (; *cp == ' ' || *cp == '\t'; cp++)
510 for (cp2 = &namebuf[strlen(namebuf)];
511 *cp != '\0' && *cp != ' ' && *cp != '\t' &&
512 cp2 < namebuf + LINESIZE - 1;)
515 if (readline(ibuf, linebuf, LINESIZE) < 0)
516 return (savestr(namebuf));
517 if ((cp = strchr(linebuf, 'F')) == NULL)
518 return (savestr(namebuf));
519 if (strncmp(cp, "From", 4) != 0)
520 return (savestr(namebuf));
521 while ((cp = strchr(cp, 'r')) != NULL) {
522 if (strncmp(cp, "remote", 6) == 0) {
523 if ((cp = strchr(cp, 'f')) == NULL)
525 if (strncmp(cp, "from", 4) != 0)
527 if ((cp = strchr(cp, ' ')) == NULL)
534 cp2 = strrchr(namebuf, '!') + 1;
535 strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
536 strcat(namebuf, "!");
541 return (savestr(namebuf));
545 * Count the occurances of c in str
548 charcount(char *str, int c)
553 for (i = 0, cp = str; *cp != '\0'; cp++)
560 * See if the given header field is supposed to be ignored.
563 isign(const char *field, struct ignoretab ignore[2])
565 char realfld[LINESIZE];
567 if (ignore == ignoreall)
570 * Lower-case the string, so that "Status" and "status"
571 * will hash to the same place.
573 istrncpy(realfld, field, sizeof(realfld));
574 if (ignore[1].i_count > 0)
575 return (!member(realfld, ignore + 1));
577 return (member(realfld, ignore));
581 member(char *realfield, struct ignoretab *table)
585 for (igp = table->i_head[hash(realfield)]; igp != NULL; igp = igp->i_link)
586 if (*igp->i_field == *realfield &&
587 equal(igp->i_field, realfield))