[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.18

1.18    ! millert     1: /*     $OpenBSD: from.c,v 1.17 2015/01/16 06:40:07 deraadt 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: #include <sys/types.h>
                     34: #include <ctype.h>
                     35: #include <pwd.h>
                     36: #include <stdio.h>
                     37: #include <stdlib.h>
                     38: #include <unistd.h>
                     39: #include <paths.h>
1.7       deraadt    40: #include <string.h>
1.4       mickey     41: #include <err.h>
1.1       deraadt    42:
1.7       deraadt    43: int    match(char *, char *);
1.18    ! millert    44: FILE   *open_mbox(const char *file, const char *user);
1.7       deraadt    45:
                     46: int
1.10      deraadt    47: main(int argc, char *argv[])
1.1       deraadt    48: {
                     49:        int ch, newline;
1.18    ! millert    50:        char *file, *line, *sender, *p;
        !            51:        size_t linesize = 0;
        !            52:        ssize_t linelen;
        !            53:        FILE *fp;
1.1       deraadt    54:
1.18    ! millert    55:        file = line = sender = NULL;
        !            56:        while ((ch = getopt(argc, argv, "f:s:")) != -1) {
1.16      okan       57:                switch(ch) {
1.1       deraadt    58:                case 'f':
                     59:                        file = optarg;
                     60:                        break;
                     61:                case 's':
                     62:                        sender = optarg;
                     63:                        for (p = sender; *p; ++p)
1.15      deraadt    64:                                if (isupper((unsigned char)*p))
                     65:                                        *p = tolower((unsigned char)*p);
1.1       deraadt    66:                        break;
                     67:                default:
1.13      jmc        68:                        fprintf(stderr,
                     69:                            "usage: from [-f file] [-s sender] [user]\n");
1.18    ! millert    70:                        exit(EXIT_FAILURE);
1.1       deraadt    71:                }
1.18    ! millert    72:        }
1.1       deraadt    73:        argv += optind;
                     74:
1.18    ! millert    75:        if ((fp = open_mbox(file, *argv)) == NULL)
        !            76:                err(1, "%s", file);
        !            77:        for (newline = 1; (linelen = getline(&line, &linesize, fp)) != -1;) {
        !            78:                if (*line == '\n') {
        !            79:                        newline = 1;
        !            80:                        continue;
        !            81:                }
        !            82:                if (newline && !strncmp(line, "From ", 5) &&
        !            83:                    (!sender || match(line + 5, sender)))
        !            84:                        printf("%s", line);
        !            85:                newline = 0;
        !            86:        }
        !            87:        free(line);
        !            88:        exit(EXIT_SUCCESS);
        !            89: }
        !            90:
        !            91: FILE *
        !            92: open_mbox(const char *file, const char *user)
        !            93: {
        !            94:        struct passwd *pwd;
        !            95:        char *buf = NULL;
        !            96:        FILE *fp;
        !            97:
1.1       deraadt    98:        /*
                     99:         * We find the mailbox by:
                    100:         *      1 -f flag
1.18    ! millert   101:         *      2 _PATH_MAILDIR/user (from argv)
1.1       deraadt   102:         *      2 MAIL environment variable
1.18    ! millert   103:         *      3 _PATH_MAILDIR/user (from environment or passwd db)
1.1       deraadt   104:         */
1.18    ! millert   105:        if (file == NULL) {
        !           106:                if (user == NULL) {
        !           107:                        if ((file = getenv("MAIL")) == NULL) {
        !           108:                                if ((user = getenv("LOGNAME")) == NULL &&
        !           109:                                    (user = getenv("USER")) == NULL) {
        !           110:                                        if (!(pwd = getpwuid(getuid())))
        !           111:                                                errx(1, "no password file "
        !           112:                                                    "entry for you");
        !           113:                                        user = pwd->pw_name;
        !           114:                                }
1.1       deraadt   115:                        }
1.18    ! millert   116:                }
        !           117:                if (file == NULL) {
        !           118:                        if (asprintf(&buf, "%s/%s", _PATH_MAILDIR, user) == -1)
        !           119:                                err(1, NULL);
1.1       deraadt   120:                        file = buf;
                    121:                }
                    122:        }
1.18    ! millert   123:        fp = fopen(file, "r");
        !           124:        free(buf);
        !           125:        return(fp);
1.1       deraadt   126: }
                    127:
1.7       deraadt   128: int
1.10      deraadt   129: match(char *line, char *sender)
1.1       deraadt   130: {
1.8       mpech     131:        char ch, pch, first, *p, *t;
1.1       deraadt   132:
                    133:        for (first = *sender++;;) {
1.15      deraadt   134:                if (isspace((unsigned char)(ch = *line)))
1.1       deraadt   135:                        return(0);
                    136:                ++line;
1.15      deraadt   137:                if (isupper((unsigned char)ch))
                    138:                        ch = tolower((unsigned char)ch);
1.1       deraadt   139:                if (ch != first)
                    140:                        continue;
                    141:                for (p = sender, t = line;;) {
                    142:                        if (!(pch = *p++))
                    143:                                return(1);
1.15      deraadt   144:                        if (isupper((unsigned char)(ch = *t++)))
                    145:                                ch = tolower((unsigned char)ch);
1.1       deraadt   146:                        if (ch != pch)
                    147:                                break;
                    148:                }
                    149:        }
                    150:        /* NOTREACHED */
                    151: }