2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)aux.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __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.
62 int size = strlen(str) + 1;
64 if ((new = salloc(size)) != NULL)
65 bcopy(str, new, size);
70 * Make a copy of new argument incorporating old one.
73 save2str(char *str, char *old)
76 int newsize = strlen(str) + 1;
77 int oldsize = old ? strlen(old) + 1 : 0;
79 if ((new = salloc(newsize + oldsize)) != NULL) {
81 bcopy(old, new, oldsize);
82 new[oldsize - 1] = ' ';
84 bcopy(str, new + oldsize, newsize);
90 * Touch the named message by setting its MTOUCH flag.
91 * Touched messages have the effect of not being sent
92 * back to the system mailbox on exit.
95 touch(struct message *mp)
99 if ((mp->m_flag & MREAD) == 0)
100 mp->m_flag |= MREAD|MSTATUS;
104 * Test to see if the passed file name is a directory.
105 * 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.
121 argcount(char **argv)
125 for (ap = argv; *ap++ != NULL;)
127 return (ap - argv - 1);
131 * Return the desired header line from the passed message
132 * pointer (or NULL if the desired header field is not available).
135 hfield(const char *field, struct message *mp)
138 char linebuf[LINESIZE];
141 char *colon, *oldhfield = NULL;
144 if ((lc = mp->m_lines - 1) < 0)
146 if (readline(ibuf, linebuf, LINESIZE) < 0)
149 if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
151 if ((hfield = ishfield(linebuf, colon, field)) != NULL)
152 oldhfield = save2str(hfield, oldhfield);
158 * Return the next header field found in the given message.
159 * Return >= 0 if something found, < 0 elsewise.
160 * "colon" is set to point to the colon in the header.
161 * Must deal with \ continuations & other such fraud.
164 gethfield(FILE *f, char linebuf[], int rem, char **colon)
166 char line2[LINESIZE];
173 if ((c = readline(f, linebuf, LINESIZE)) <= 0)
175 for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
178 if (*cp != ':' || cp == linebuf)
181 * I guess we got a headline.
182 * Handle wraparounding
187 while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
192 ungetc(c = getc(f), f);
193 if (c != ' ' && c != '\t')
195 if ((c = readline(f, line2, LINESIZE)) < 0)
198 for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
201 if (cp + c >= linebuf + LINESIZE - 2)
214 * Check whether the passed line is a header line of
215 * the desired breed. Return the field body, or 0.
219 ishfield(char *linebuf, char *colon, const char *field)
224 if (strcasecmp(linebuf, field) != 0) {
229 for (cp++; *cp == ' ' || *cp == '\t'; cp++)
235 * Copy a string and lowercase the result.
236 * dsize: space left in buffer (including space for NULL)
239 istrncpy(char *dest, const char *src, size_t dsize)
242 strlcpy(dest, src, dsize);
243 for (; *dest; dest++)
244 *dest = tolower((unsigned char)*dest);
248 * The following code deals with input stacking to do source
249 * commands. All but the current file pointer are saved on
253 static int ssp; /* Top of file stack */
255 FILE *s_file; /* File we were in. */
256 int s_cond; /* Saved state of conditionals */
257 int s_loading; /* Loading .mailrc, etc. */
259 #define SSTACK_SIZE 64 /* XXX was NOFILE. */
260 static struct sstack sstack[SSTACK_SIZE];
263 * Pushdown current input file and switch to a new one.
264 * Set the global flag "sourcing" so that others will realize
265 * that they are no longer reading from a tty (in all probability).
268 source(char **arglist)
273 if ((cp = expand(*arglist)) == NULL)
275 if ((fi = Fopen(cp, "r")) == NULL) {
279 if (ssp >= SSTACK_SIZE - 1) {
280 printf("Too much \"sourcing\" going on.\n");
284 sstack[ssp].s_file = input;
285 sstack[ssp].s_cond = cond;
286 sstack[ssp].s_loading = loading;
296 * Pop the current input back to the previous level.
297 * Update the "sourcing" flag as appropriate.
303 printf("\"Source\" stack over-pop.\n");
309 printf("Unmatched \"if\"\n");
311 cond = sstack[ssp].s_cond;
312 loading = sstack[ssp].s_loading;
313 input = sstack[ssp].s_file;
320 * Touch the indicated file.
321 * This is nifty for the shell.
326 struct timespec ts[2];
328 (void)clock_gettime(CLOCK_REALTIME, &ts[0]);
331 ts[1].tv_nsec = UTIME_OMIT;
332 (void)utimensat(AT_FDCWD, name, ts, 0);
336 * Get sender's name from this message. If the message has
337 * a bunch of arpanet stuff in it, we may have to skin the name
338 * before returning it.
341 nameof(struct message *mp, int reptype)
345 cp = skin(name1(mp, reptype));
346 if (reptype != 0 || charcount(cp, '!') < 2)
348 cp2 = strrchr(cp, '!');
350 while (cp2 > cp && *cp2 != '!')
358 * Start of a "comment".
362 skip_comment(char *cp)
366 for (; nesting > 0 && *cp; cp++) {
384 * Skin an arpa net address according to the RFC 822 interpretation
390 char *nbuf, *bufend, *cp, *cp2;
391 int c, gotlt, lastsp;
395 if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
396 && strchr(name, ' ') == NULL)
399 /* We assume that length(input) <= length(output) */
400 if ((nbuf = malloc(strlen(name) + 1)) == NULL)
401 err(1, "Out of memory");
405 for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; ) {
408 cp = skip_comment(cp);
414 * Start of a "quoted-string".
415 * Copy it in its entirety.
417 while ((c = *cp) != '\0') {
423 else if ((c = *cp) != '\0') {
432 if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
433 cp += 3, *cp2++ = '@';
435 if (cp[0] == '@' && cp[1] == ' ')
436 cp += 2, *cp2++ = '@';
450 while ((c = *cp) != '\0' && c != ',') {
453 cp = skip_comment(cp);
455 while ((c = *cp) != '\0') {
459 if (c == '\\' && *cp != '\0')
474 if (c == ',' && !gotlt &&
475 (*cp == ' ' || *cp == '"' || *cp == '<')) {
486 if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
492 * Fetch the sender's name from the passed message.
494 * 0 -- get sender's name for display purposes
495 * 1 -- get sender's name for reply
496 * 2 -- get sender's name for Reply
499 name1(struct message *mp, int reptype)
501 char namebuf[LINESIZE];
502 char linebuf[LINESIZE];
507 if ((cp = hfield("from", mp)) != NULL)
509 if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
513 if (readline(ibuf, linebuf, LINESIZE) < 0)
514 return (savestr(namebuf));
516 for (cp = linebuf; *cp != '\0' && *cp != ' '; cp++)
518 for (; *cp == ' ' || *cp == '\t'; cp++)
520 for (cp2 = &namebuf[strlen(namebuf)];
521 *cp != '\0' && *cp != ' ' && *cp != '\t' &&
522 cp2 < namebuf + LINESIZE - 1;)
525 if (readline(ibuf, linebuf, LINESIZE) < 0)
526 return (savestr(namebuf));
527 if ((cp = strchr(linebuf, 'F')) == NULL)
528 return (savestr(namebuf));
529 if (strncmp(cp, "From", 4) != 0)
530 return (savestr(namebuf));
531 while ((cp = strchr(cp, 'r')) != NULL) {
532 if (strncmp(cp, "remote", 6) == 0) {
533 if ((cp = strchr(cp, 'f')) == NULL)
535 if (strncmp(cp, "from", 4) != 0)
537 if ((cp = strchr(cp, ' ')) == NULL)
544 cp2 = strrchr(namebuf, '!') + 1;
545 strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
546 strcat(namebuf, "!");
551 return (savestr(namebuf));
555 * Count the occurrences of c in str
558 charcount(char *str, int c)
563 for (i = 0, cp = str; *cp != '\0'; cp++)
570 * See if the given header field is supposed to be ignored.
573 isign(const char *field, struct ignoretab ignore[2])
575 char realfld[LINESIZE];
577 if (ignore == ignoreall)
580 * Lower-case the string, so that "Status" and "status"
581 * will hash to the same place.
583 istrncpy(realfld, field, sizeof(realfld));
584 if (ignore[1].i_count > 0)
585 return (!member(realfld, ignore + 1));
587 return (member(realfld, ignore));
591 member(char *realfield, struct ignoretab *table)
595 for (igp = table->i_head[hash(realfield)]; igp != NULL; igp = igp->i_link)
596 if (*igp->i_field == *realfield &&
597 equal(igp->i_field, realfield))