]>
git.cameronkatri.com Git - apple_cmds.git/blob - mail_cmds/mail/names.c
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
[] = "@(#)names.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid
[] =
39 "$FreeBSD: src/usr.bin/mail/names.c,v 1.9 2004/02/29 20:44:44 mikeh Exp $";
42 #include <sys/cdefs.h>
45 * Mail -- a mail program
55 * Allocate a single element of a name list,
56 * initialize its name field to the passed
66 np
= (struct name
*)salloc(sizeof(*np
));
70 np
->n_name
= savestr(str
);
75 * Find the tail of a list and return it.
86 while (np
->n_flink
!= NULL
)
92 * Extract a list of names from a line,
93 * and make a list of names from it.
94 * Return the list or NULL if none found.
102 struct name
*top
, *np
, *t
;
104 if (line
== NULL
|| *line
== '\0')
106 if ((nbuf
= malloc(strlen(line
) + 1)) == NULL
)
107 err(1, "Out of memory");
111 while ((cp
= yankword(cp
, nbuf
)) != NULL
) {
112 t
= nalloc(nbuf
, ntype
);
125 * Turn a list of names into a string of the same names.
136 comma
= ntype
& GCOMMA
;
142 fprintf(stderr
, "detract asked to insert commas\n");
143 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
144 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
146 s
+= strlen(p
->n_name
) + 1;
155 for (p
= np
; p
!= NULL
; p
= p
->n_flink
) {
156 if (ntype
&& (p
->n_type
& GMASK
) != ntype
)
158 cp
+= strlcpy(cp
, p
->n_name
, strlen(p
->n_name
) + 1);
159 if (comma
&& p
->n_flink
!= NULL
)
164 if (comma
&& *--cp
== ',')
170 * Grab a single word (liberal word)
171 * Throw away things between ()'s, and take anything between <>.
186 while (*cp
!= '\0') {
198 } else if (*cp
== ' ' || *cp
== '\t' || *cp
== ',')
204 for (cp2
= wbuf
; *cp
&& (*cp2
++ = *cp
++) != '>';)
207 for (cp2
= wbuf
; *cp
!= '\0' && strchr(" \t,(", *cp
) == NULL
;
215 * Grab a single login name (liberal word)
216 * Throw away things between ()'s, take anything between <>,
217 * and look for words before metacharacters %, @, !.
223 char *cp
, *cp2
, *cp_temp
;
233 while (*cp
!= '\0') {
245 } else if (*cp
== ' ' || *cp
== '\t' || *cp
== ',')
252 * Now, let's go forward till we meet the needed character,
253 * and step one word back.
256 /* First, remember current point. */
261 * Note that we look ahead in a cycle. This is safe, since
262 * non-end of string is checked first.
264 while(*cp
!= '\0' && strchr("@%!", *(cp
+ 1)) == NULL
)
268 * Now, start stepping back to the first non-word character,
269 * while counting the number of symbols in a word.
271 while(cp
!= cp_temp
&& strchr(" \t,<>", *(cp
- 1)) == NULL
) {
276 /* Finally, grab the word forward. */
288 * For each recipient in the passed name list with a /
289 * in the name, append the message to the end of the named file
290 * and remove him from the recipient list.
292 * Recipients whose name begins with | are piped through the given
293 * program and removed.
302 struct name
*np
, *top
;
312 if (!isfileaddr(np
->n_name
) && np
->n_name
[0] != '|') {
316 ispipe
= np
->n_name
[0] == '|';
318 fname
= np
->n_name
+1;
320 fname
= expand(np
->n_name
);
323 * See if we have copied the complete message out yet.
329 char tempname
[PATHSIZE
];
331 (void)snprintf(tempname
, sizeof(tempname
),
332 "%s/mail.ReXXXXXXXXXX", tmpdir
);
333 if ((fd
= mkstemp(tempname
)) == -1 ||
334 (fout
= Fdopen(fd
, "a")) == NULL
) {
335 warn("%s", tempname
);
339 image
= open(tempname
, O_RDWR
);
342 warn("%s", tempname
);
347 (void)fcntl(image
, F_SETFD
, 1);
348 fprintf(fout
, "From %s %s", myname
, date
);
350 GTO
|GSUBJECT
|GCC
|GREPLYTO
|GINREPLYTO
|GNL
);
351 while ((c
= getc(fo
)) != EOF
)
357 warn("%s", tempname
);
366 * Now either copy "image" to the desired file
367 * or give it as the standard input to the desired
368 * program as appropriate.
378 * We can't really reuse the same image file,
379 * because multiple piped recipients will
380 * share the same lseek location and trample
383 if ((sh
= value("SHELL")) == NULL
)
385 (void)sigemptyset(&nset
);
386 (void)sigaddset(&nset
, SIGHUP
);
387 (void)sigaddset(&nset
, SIGINT
);
388 (void)sigaddset(&nset
, SIGQUIT
);
389 pid
= start_command(sh
, &nset
, image
, -1, "-c", fname
,
398 if ((fout
= Fopen(fname
, "a")) == NULL
) {
403 if ((f
= dup(image
)) < 0) {
407 fin
= Fdopen(f
, "r");
409 fprintf(stderr
, "Can't reopen image\n");
415 while ((c
= getc(fin
)) != EOF
)
429 * In days of old we removed the entry from the
430 * the list; now for sake of header expansion
431 * we leave it in and mark it as deleted.
444 * Determine if the passed address is a local "send to file" address.
445 * If any of the network metacharacters precedes any slashes, it can't
446 * be a filename. We cheat with .'s to allow path names like ./...
456 for (cp
= name
; *cp
!= '\0'; cp
++) {
457 if (*cp
== '!' || *cp
== '%' || *cp
== '@')
466 * Map all of the aliased users in the invoker's mailrc
467 * file and insert them into the list.
468 * Changed after all these months of service to recursively
469 * expand names (2/14/80).
476 struct name
*new, *np
, *cp
;
477 struct grouphead
*gh
;
482 metoo
= (value("metoo") != NULL
);
484 if (np
->n_name
[0] == '\\') {
490 gh
= findgroup(np
->n_name
);
493 new = gexpand(new, gh
, metoo
, np
->n_type
);
502 * Recursively expand a group name. We limit the expansion to some
503 * fixed level to keep things from going haywire.
504 * Direct recursion is not expanded for convenience.
508 gexpand(nlist
, gh
, metoo
, ntype
)
510 struct grouphead
*gh
;
514 struct grouphead
*ngh
;
519 if (depth
> MAXEXP
) {
520 printf("Expanding alias to depth larger than %d\n", MAXEXP
);
524 for (gp
= gh
->g_list
; gp
!= NULL
; gp
= gp
->ge_link
) {
528 if (strcmp(cp
, gh
->g_name
) == 0)
530 if ((ngh
= findgroup(cp
)) != NULL
) {
531 nlist
= gexpand(nlist
, ngh
, metoo
, ntype
);
535 np
= nalloc(cp
, ntype
);
537 * At this point should allow to expand
538 * to self if only person in group
540 if (gp
== gh
->g_list
&& gp
->ge_link
== NULL
)
542 if (!metoo
&& strcmp(cp
, myname
) == 0)
545 nlist
= put(nlist
, np
);
552 * Concatenate the two passed name lists, return the result.
556 struct name
*n1
, *n2
;
571 * Unpack the name list onto a vector of strings.
572 * Return an error if the name list won't fit.
580 int t
, extra
, metoo
, verbose
;
583 if ((t
= count(n
)) == 0)
584 errx(1, "No names to unpack");
586 * Compute the number of extra arguments we will need.
587 * We need at least two extra -- one for "mail" and one for
588 * the terminating 0 pointer. Additional spots may be needed
589 * to pass along -f to the host mailer.
593 metoo
= value("metoo") != NULL
;
596 verbose
= value("verbose") != NULL
;
599 top
= (char **)salloc((t
+ extra
) * sizeof(*top
));
607 for (; n
!= NULL
; n
= n
->n_flink
)
608 if ((n
->n_type
& GDEL
) == 0)
615 * Remove all of the duplicates from the passed name list by
616 * insertion sorting them, then checking for dups.
617 * Return the head of the new list.
623 struct name
*np
, *t
, *new;
636 while (strcasecmp(t
->n_name
, np
->n_name
) < 0) {
637 if (t
->n_flink
== NULL
)
643 * If we ran out of t's, put the new entry after
644 * the current value of t.
647 if (strcasecmp(t
->n_name
, np
->n_name
) < 0) {
657 * Otherwise, put the new entry in front of the
658 * current t. If at the front of the list,
659 * the new guy becomes the new head of the list.
673 * The normal case -- we are inserting into the
674 * middle of the list.
680 x
->n_blink
= t
->n_blink
;
681 t
->n_blink
->n_flink
= x
;
686 * Now the list headed up by new is sorted.
687 * Go through it and remove duplicates.
693 while (t
->n_flink
!= NULL
&&
694 strcasecmp(np
->n_name
, t
->n_flink
->n_name
) == 0)
696 if (t
== np
|| t
== NULL
) {
702 * Now t points to the last entry with the same name
703 * as np. Make np point beyond t.
706 np
->n_flink
= t
->n_flink
;
707 if (t
->n_flink
!= NULL
)
708 t
->n_flink
->n_blink
= np
;
715 * Put another node onto a list of names and return
720 struct name
*list
, *node
;
722 node
->n_flink
= list
;
723 node
->n_blink
= NULL
;
725 list
->n_blink
= node
;
730 * Determine the number of undeleted elements in
731 * a name list and return it.
739 for (c
= 0; np
!= NULL
; np
= np
->n_flink
)
740 if ((np
->n_type
& GDEL
) == 0)
746 * Delete the given name from a namelist.
755 for (p
= np
; p
!= NULL
; p
= p
->n_flink
)
756 if (strcasecmp(p
->n_name
, name
) == 0) {
757 if (p
->n_blink
== NULL
) {
758 if (p
->n_flink
!= NULL
)
759 p
->n_flink
->n_blink
= NULL
;
763 if (p
->n_flink
== NULL
) {
764 if (p
->n_blink
!= NULL
)
765 p
->n_blink
->n_flink
= NULL
;
768 p
->n_blink
->n_flink
= p
->n_flink
;
769 p
->n_flink
->n_blink
= p
->n_blink
;
775 * Pretty print a name list
776 * Uncomment it if you need it.
788 fprintf(stderr, "%s(%d) ", np->n_name, np->n_type);
791 fprintf(stderr, "\n");