GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/from/from.c Lines: 0 59 0.0 %
Date: 2016-12-06 Branches: 0 59 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: from.c,v 1.24 2015/11/05 18:42:41 mmcc Exp $	*/
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.
16
 * 3. Neither the name of the University nor the names of its contributors
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>
40
#include <string.h>
41
#include <err.h>
42
#include <errno.h>
43
44
int	match(char *, char *);
45
char	*mail_spool(char *file, const char *user);
46
47
int
48
main(int argc, char *argv[])
49
{
50
	int ch, newline, fflag = 0;
51
	char *file, *line, *sender, *p;
52
	size_t linesize = 0;
53
	ssize_t linelen;
54
	FILE *fp;
55
56
	file = line = sender = NULL;
57
	while ((ch = getopt(argc, argv, "f:s:")) != -1) {
58
		switch(ch) {
59
		case 'f':
60
			fflag = 1;
61
			file = optarg;
62
			break;
63
		case 's':
64
			sender = optarg;
65
			for (p = sender; *p; ++p)
66
				if (isupper((unsigned char)*p))
67
					*p = tolower((unsigned char)*p);
68
			break;
69
		default:
70
			fprintf(stderr,
71
			    "usage: from [-f file] [-s sender] [user]\n");
72
			exit(EXIT_FAILURE);
73
		}
74
	}
75
	argv += optind;
76
77
	if (pledge("stdio rpath getpw wpath cpath", NULL) == -1)
78
		err(1, "pledge");
79
80
	file = mail_spool(file, *argv);
81
	if ((fp = fopen(file, "r")) == NULL) {
82
		if (!fflag && errno == ENOENT)
83
			exit(EXIT_SUCCESS);
84
		err(1, "%s", file);
85
	}
86
87
	if (pledge("stdio wpath cpath rpath", NULL) == -1)
88
		err(1, "pledge");
89
90
	for (newline = 1; (linelen = getline(&line, &linesize, fp)) != -1;) {
91
		if (*line == '\n') {
92
			newline = 1;
93
			continue;
94
		}
95
		if (newline && !strncmp(line, "From ", 5) &&
96
		    (!sender || match(line + 5, sender)))
97
			printf("%s", line);
98
		newline = 0;
99
	}
100
	free(line);
101
	exit(EXIT_SUCCESS);
102
}
103
104
char *
105
mail_spool(char *file, const char *user)
106
{
107
	struct passwd *pwd;
108
109
	/*
110
	 * We find the mailbox by:
111
	 *	1 -f flag
112
	 *	2 _PATH_MAILDIR/user (from argv)
113
	 *	2 MAIL environment variable
114
	 *	3 _PATH_MAILDIR/user (from environment or passwd db)
115
	 */
116
	if (file == NULL) {
117
		if (user == NULL) {
118
			if ((file = getenv("MAIL")) == NULL) {
119
				if ((user = getenv("LOGNAME")) == NULL &&
120
				    (user = getenv("USER")) == NULL) {
121
					if (!(pwd = getpwuid(getuid())))
122
						errx(1, "no password file "
123
						    "entry for you");
124
					user = pwd->pw_name;
125
				}
126
			}
127
		}
128
		if (file == NULL) {
129
			if (asprintf(&file, "%s/%s", _PATH_MAILDIR, user) == -1)
130
				err(1, NULL);
131
		}
132
	}
133
	return(file);
134
}
135
136
int
137
match(char *line, char *sender)
138
{
139
	char ch, pch, first, *p, *t;
140
141
	for (first = *sender++;;) {
142
		if (isspace((unsigned char)(ch = *line)))
143
			return(0);
144
		++line;
145
		if (isupper((unsigned char)ch))
146
			ch = tolower((unsigned char)ch);
147
		if (ch != first)
148
			continue;
149
		for (p = sender, t = line;;) {
150
			if (!(pch = *p++))
151
				return(1);
152
			if (isupper((unsigned char)(ch = *t++)))
153
				ch = tolower((unsigned char)ch);
154
			if (ch != pch)
155
				break;
156
		}
157
	}
158
	/* NOTREACHED */
159
}