[BACK]Return to from.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / from

Annotation of src/usr.bin/from/from.c, Revision 1.9

1.9     ! millert     1: /*     $OpenBSD: from.c,v 1.8 2001/11/19 19:02:14 mpech Exp $  */
1.1       deraadt     2: /*     $NetBSD: from.c,v 1.6 1995/09/01 01:39:10 jtc Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.9     ! millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1980, 1988, 1993\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)from.c     8.1 (Berkeley) 6/6/93";
                     42: #endif
1.9     ! millert    43: static char rcsid[] = "$OpenBSD: from.c,v 1.8 2001/11/19 19:02:14 mpech Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
                     46: #include <sys/types.h>
                     47: #include <ctype.h>
                     48: #include <pwd.h>
                     49: #include <stdio.h>
                     50: #include <stdlib.h>
                     51: #include <unistd.h>
                     52: #include <paths.h>
1.7       deraadt    53: #include <string.h>
1.4       mickey     54: #include <err.h>
1.1       deraadt    55:
1.7       deraadt    56: int    match(char *, char *);
                     57:
                     58: int
1.1       deraadt    59: main(argc, argv)
                     60:        int argc;
                     61:        char **argv;
                     62: {
                     63:        extern char *optarg;
                     64:        extern int optind;
                     65:        struct passwd *pwd;
                     66:        int ch, newline;
                     67:        char *file, *sender, *p;
                     68: #if MAXPATHLEN > BUFSIZ
                     69:        char buf[MAXPATHLEN];
                     70: #else
                     71:        char buf[BUFSIZ];
                     72: #endif
                     73:
                     74:        file = sender = NULL;
1.3       millert    75:        while ((ch = getopt(argc, argv, "f:s:")) != -1)
1.1       deraadt    76:                switch((char)ch) {
                     77:                case 'f':
                     78:                        file = optarg;
                     79:                        break;
                     80:                case 's':
                     81:                        sender = optarg;
                     82:                        for (p = sender; *p; ++p)
                     83:                                if (isupper(*p))
                     84:                                        *p = tolower(*p);
                     85:                        break;
                     86:                case '?':
                     87:                default:
                     88:                        fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n");
                     89:                        exit(1);
                     90:                }
                     91:        argv += optind;
                     92:
                     93:        /*
                     94:         * We find the mailbox by:
                     95:         *      1 -f flag
                     96:         *      2 user
                     97:         *      2 MAIL environment variable
                     98:         *      3 _PATH_MAILDIR/file
                     99:         */
                    100:        if (!file) {
                    101:                if (!(file = *argv)) {
                    102:                        if (!(file = getenv("MAIL"))) {
1.4       mickey    103:                                if (!(pwd = getpwuid(getuid())))
                    104:                                        errx(1, "no password file entry for you");
1.7       deraadt   105:                                if ((file = getenv("USER"))) {
1.5       aaron     106:                                        (void)snprintf(buf, sizeof(buf),
                    107:                                                "%s/%s", _PATH_MAILDIR, file);
1.1       deraadt   108:                                        file = buf;
                    109:                                } else
1.5       aaron     110:                                        (void)snprintf(file = buf, sizeof(buf),
                    111:                                                "%s/%s", _PATH_MAILDIR,
                    112:                                                pwd->pw_name);
1.1       deraadt   113:                        }
                    114:                } else {
1.5       aaron     115:                        (void)snprintf(buf, sizeof(buf), "%s/%s",
                    116:                                _PATH_MAILDIR, file);
1.1       deraadt   117:                        file = buf;
                    118:                }
                    119:        }
1.4       mickey    120:        if (!freopen(file, "r", stdin))
1.6       millert   121:                err(1, "%s", file);
1.1       deraadt   122:        for (newline = 1; fgets(buf, sizeof(buf), stdin);) {
                    123:                if (*buf == '\n') {
                    124:                        newline = 1;
                    125:                        continue;
                    126:                }
                    127:                if (newline && !strncmp(buf, "From ", 5) &&
                    128:                    (!sender || match(buf + 5, sender)))
                    129:                        printf("%s", buf);
                    130:                newline = 0;
                    131:        }
                    132:        exit(0);
                    133: }
                    134:
1.7       deraadt   135: int
1.1       deraadt   136: match(line, sender)
1.7       deraadt   137:        char *line, *sender;
1.1       deraadt   138: {
1.8       mpech     139:        char ch, pch, first, *p, *t;
1.1       deraadt   140:
                    141:        for (first = *sender++;;) {
                    142:                if (isspace(ch = *line))
                    143:                        return(0);
                    144:                ++line;
                    145:                if (isupper(ch))
                    146:                        ch = tolower(ch);
                    147:                if (ch != first)
                    148:                        continue;
                    149:                for (p = sender, t = line;;) {
                    150:                        if (!(pch = *p++))
                    151:                                return(1);
                    152:                        if (isupper(ch = *t++))
                    153:                                ch = tolower(ch);
                    154:                        if (ch != pch)
                    155:                                break;
                    156:                }
                    157:        }
                    158:        /* NOTREACHED */
                    159: }