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 * 4. 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
32 static char sccsid
[] = "@(#)aux.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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.
58 int size
= strlen(str
) + 1;
60 if ((new = salloc(size
)) != NULL
)
61 bcopy(str
, new, size
);
66 * Make a copy of new argument incorporating old one.
69 save2str(char *str
, char *old
)
72 int newsize
= strlen(str
) + 1;
73 int oldsize
= old
? strlen(old
) + 1 : 0;
75 if ((new = salloc(newsize
+ oldsize
)) != NULL
) {
77 bcopy(old
, new, oldsize
);
78 new[oldsize
- 1] = ' ';
80 bcopy(str
, new + oldsize
, newsize
);
86 * Touch the named message by setting its MTOUCH flag.
87 * Touched messages have the effect of not being sent
88 * back to the system mailbox on exit.
91 touch(struct message
*mp
)
95 if ((mp
->m_flag
& MREAD
) == 0)
96 mp
->m_flag
|= MREAD
|MSTATUS
;
100 * Test to see if the passed file name is a directory.
101 * Return true if it is.
108 if (stat(name
, &sbuf
) < 0)
110 return (S_ISDIR(sbuf
.st_mode
));
114 * Count the number of arguments in the given string raw list.
117 argcount(char **argv
)
121 for (ap
= argv
; *ap
++ != NULL
;)
123 return (ap
- argv
- 1);
127 * Return the desired header line from the passed message
128 * pointer (or NULL if the desired header field is not available).
131 hfield(const char *field
, struct message
*mp
)
134 char linebuf
[LINESIZE
];
137 char *colon
, *oldhfield
= NULL
;
140 if ((lc
= mp
->m_lines
- 1) < 0)
142 if (readline(ibuf
, linebuf
, LINESIZE
) < 0)
145 if ((lc
= gethfield(ibuf
, linebuf
, lc
, &colon
)) < 0)
147 if ((hfield
= ishfield(linebuf
, colon
, field
)) != NULL
)
148 oldhfield
= save2str(hfield
, oldhfield
);
154 * Return the next header field found in the given message.
155 * Return >= 0 if something found, < 0 elsewise.
156 * "colon" is set to point to the colon in the header.
157 * Must deal with \ continuations & other such fraud.
160 gethfield(FILE *f
, char linebuf
[], int rem
, char **colon
)
162 char line2
[LINESIZE
];
169 if ((c
= readline(f
, linebuf
, LINESIZE
)) <= 0)
171 for (cp
= linebuf
; isprint((unsigned char)*cp
) && *cp
!= ' ' && *cp
!= ':';
174 if (*cp
!= ':' || cp
== linebuf
)
177 * I guess we got a headline.
178 * Handle wraparounding
183 while (--cp
>= linebuf
&& (*cp
== ' ' || *cp
== '\t'))
188 ungetc(c
= getc(f
), f
);
189 if (c
!= ' ' && c
!= '\t')
191 if ((c
= readline(f
, line2
, LINESIZE
)) < 0)
194 for (cp2
= line2
; *cp2
== ' ' || *cp2
== '\t'; cp2
++)
197 if (cp
+ c
>= linebuf
+ LINESIZE
- 2)
210 * Check whether the passed line is a header line of
211 * the desired breed. Return the field body, or 0.
215 ishfield(char linebuf
[], char *colon
, const char *field
)
220 if (strcasecmp(linebuf
, field
) != 0) {
225 for (cp
++; *cp
== ' ' || *cp
== '\t'; cp
++)
231 * Copy a string and lowercase the result.
232 * dsize: space left in buffer (including space for NULL)
235 istrncpy(char *dest
, const char *src
, size_t dsize
)
238 strlcpy(dest
, src
, dsize
);
239 for (; *dest
; dest
++)
240 *dest
= tolower((unsigned char)*dest
);
244 * The following code deals with input stacking to do source
245 * commands. All but the current file pointer are saved on
249 static int ssp
; /* Top of file stack */
251 FILE *s_file
; /* File we were in. */
252 int s_cond
; /* Saved state of conditionals */
253 int s_loading
; /* Loading .mailrc, etc. */
255 #define SSTACK_SIZE 64 /* XXX was NOFILE. */
256 static struct sstack sstack
[SSTACK_SIZE
];
259 * Pushdown current input file and switch to a new one.
260 * Set the global flag "sourcing" so that others will realize
261 * that they are no longer reading from a tty (in all probability).
264 source(char **arglist
)
269 if ((cp
= expand(*arglist
)) == NULL
)
271 if ((fi
= Fopen(cp
, "r")) == NULL
) {
275 if (ssp
>= SSTACK_SIZE
- 1) {
276 printf("Too much \"sourcing\" going on.\n");
280 sstack
[ssp
].s_file
= input
;
281 sstack
[ssp
].s_cond
= cond
;
282 sstack
[ssp
].s_loading
= loading
;
292 * Pop the current input back to the previous level.
293 * Update the "sourcing" flag as appropriate.
299 printf("\"Source\" stack over-pop.\n");
305 printf("Unmatched \"if\"\n");
307 cond
= sstack
[ssp
].s_cond
;
308 loading
= sstack
[ssp
].s_loading
;
309 input
= sstack
[ssp
].s_file
;
316 * Touch the indicated file.
317 * This is nifty for the shell.
323 struct timeval tv
[2];
327 (void)gettimeofday(&tv
[0], NULL
);
329 TIMESPEC_TO_TIMEVAL(&tv
[1], &sb
.st_mtim
);
330 (void)utimes(name
, tv
);
334 * Get sender's name from this message. If the message has
335 * a bunch of arpanet stuff in it, we may have to skin the name
336 * before returning it.
339 nameof(struct message
*mp
, int reptype
)
343 cp
= skin(name1(mp
, reptype
));
344 if (reptype
!= 0 || charcount(cp
, '!') < 2)
346 cp2
= strrchr(cp
, '!');
348 while (cp2
> cp
&& *cp2
!= '!')
356 * Start of a "comment".
360 skip_comment(char *cp
)
364 for (; nesting
> 0 && *cp
; cp
++) {
382 * Skin an arpa net address according to the RFC 822 interpretation
388 char *nbuf
, *bufend
, *cp
, *cp2
;
389 int c
, gotlt
, lastsp
;
393 if (strchr(name
, '(') == NULL
&& strchr(name
, '<') == NULL
394 && strchr(name
, ' ') == NULL
)
397 /* We assume that length(input) <= length(output) */
398 if ((nbuf
= malloc(strlen(name
) + 1)) == NULL
)
399 err(1, "Out of memory");
403 for (cp
= name
, cp2
= bufend
; (c
= *cp
++) != '\0'; ) {
406 cp
= skip_comment(cp
);
412 * Start of a "quoted-string".
413 * Copy it in its entirety.
415 while ((c
= *cp
) != '\0') {
421 else if ((c
= *cp
) != '\0') {
430 if (cp
[0] == 'a' && cp
[1] == 't' && cp
[2] == ' ')
431 cp
+= 3, *cp2
++ = '@';
433 if (cp
[0] == '@' && cp
[1] == ' ')
434 cp
+= 2, *cp2
++ = '@';
448 while ((c
= *cp
) != '\0' && c
!= ',') {
451 cp
= skip_comment(cp
);
453 while ((c
= *cp
) != '\0') {
457 if (c
== '\\' && *cp
!= '\0')
472 if (c
== ',' && !gotlt
&&
473 (*cp
== ' ' || *cp
== '"' || *cp
== '<')) {
484 if ((cp
= realloc(nbuf
, strlen(nbuf
) + 1)) != NULL
)
490 * Fetch the sender's name from the passed message.
492 * 0 -- get sender's name for display purposes
493 * 1 -- get sender's name for reply
494 * 2 -- get sender's name for Reply
497 name1(struct message
*mp
, int reptype
)
499 char namebuf
[LINESIZE
];
500 char linebuf
[LINESIZE
];
505 if ((cp
= hfield("from", mp
)) != NULL
)
507 if (reptype
== 0 && (cp
= hfield("sender", mp
)) != NULL
)
511 if (readline(ibuf
, linebuf
, LINESIZE
) < 0)
512 return (savestr(namebuf
));
514 for (cp
= linebuf
; *cp
!= '\0' && *cp
!= ' '; cp
++)
516 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
518 for (cp2
= &namebuf
[strlen(namebuf
)];
519 *cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t' &&
520 cp2
< namebuf
+ LINESIZE
- 1;)
523 if (readline(ibuf
, linebuf
, LINESIZE
) < 0)
524 return (savestr(namebuf
));
525 if ((cp
= strchr(linebuf
, 'F')) == NULL
)
526 return (savestr(namebuf
));
527 if (strncmp(cp
, "From", 4) != 0)
528 return (savestr(namebuf
));
529 while ((cp
= strchr(cp
, 'r')) != NULL
) {
530 if (strncmp(cp
, "remote", 6) == 0) {
531 if ((cp
= strchr(cp
, 'f')) == NULL
)
533 if (strncmp(cp
, "from", 4) != 0)
535 if ((cp
= strchr(cp
, ' ')) == NULL
)
542 cp2
= strrchr(namebuf
, '!') + 1;
543 strlcpy(cp2
, cp
, sizeof(namebuf
) - (cp2
- namebuf
) - 1);
544 strcat(namebuf
, "!");
549 return (savestr(namebuf
));
553 * Count the occurrences of c in str
556 charcount(char *str
, int c
)
561 for (i
= 0, cp
= str
; *cp
!= '\0'; cp
++)
568 * See if the given header field is supposed to be ignored.
571 isign(const char *field
, struct ignoretab ignore
[2])
573 char realfld
[LINESIZE
];
575 if (ignore
== ignoreall
)
578 * Lower-case the string, so that "Status" and "status"
579 * will hash to the same place.
581 istrncpy(realfld
, field
, sizeof(realfld
));
582 if (ignore
[1].i_count
> 0)
583 return (!member(realfld
, ignore
+ 1));
585 return (member(realfld
, ignore
));
589 member(char *realfield
, struct ignoretab
*table
)
593 for (igp
= table
->i_head
[hash(realfield
)]; igp
!= NULL
; igp
= igp
->i_link
)
594 if (*igp
->i_field
== *realfield
&&
595 equal(igp
->i_field
, realfield
))