version 1.17, 2015/01/16 06:40:07 |
version 1.18, 2015/06/02 15:44:17 |
|
|
#include <err.h> |
#include <err.h> |
|
|
int match(char *, char *); |
int match(char *, char *); |
|
FILE *open_mbox(const char *file, const char *user); |
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
struct passwd *pwd; |
|
int ch, newline; |
int ch, newline; |
char *file, *sender, *p; |
char *file, *line, *sender, *p; |
#if PATH_MAX > BUFSIZ |
size_t linesize = 0; |
char buf[PATH_MAX]; |
ssize_t linelen; |
#else |
FILE *fp; |
char buf[BUFSIZ]; |
|
#endif |
|
|
|
file = sender = NULL; |
file = line = sender = NULL; |
while ((ch = getopt(argc, argv, "f:s:")) != -1) |
while ((ch = getopt(argc, argv, "f:s:")) != -1) { |
switch(ch) { |
switch(ch) { |
case 'f': |
case 'f': |
file = optarg; |
file = optarg; |
|
|
if (isupper((unsigned char)*p)) |
if (isupper((unsigned char)*p)) |
*p = tolower((unsigned char)*p); |
*p = tolower((unsigned char)*p); |
break; |
break; |
case '?': |
|
default: |
default: |
fprintf(stderr, |
fprintf(stderr, |
"usage: from [-f file] [-s sender] [user]\n"); |
"usage: from [-f file] [-s sender] [user]\n"); |
exit(1); |
exit(EXIT_FAILURE); |
} |
} |
|
} |
argv += optind; |
argv += optind; |
|
|
|
if ((fp = open_mbox(file, *argv)) == NULL) |
|
err(1, "%s", file); |
|
for (newline = 1; (linelen = getline(&line, &linesize, fp)) != -1;) { |
|
if (*line == '\n') { |
|
newline = 1; |
|
continue; |
|
} |
|
if (newline && !strncmp(line, "From ", 5) && |
|
(!sender || match(line + 5, sender))) |
|
printf("%s", line); |
|
newline = 0; |
|
} |
|
free(line); |
|
exit(EXIT_SUCCESS); |
|
} |
|
|
|
FILE * |
|
open_mbox(const char *file, const char *user) |
|
{ |
|
struct passwd *pwd; |
|
char *buf = NULL; |
|
FILE *fp; |
|
|
/* |
/* |
* We find the mailbox by: |
* We find the mailbox by: |
* 1 -f flag |
* 1 -f flag |
* 2 user |
* 2 _PATH_MAILDIR/user (from argv) |
* 2 MAIL environment variable |
* 2 MAIL environment variable |
* 3 _PATH_MAILDIR/file |
* 3 _PATH_MAILDIR/user (from environment or passwd db) |
*/ |
*/ |
if (!file) { |
if (file == NULL) { |
if (!(file = *argv)) { |
if (user == NULL) { |
if (!(file = getenv("MAIL"))) { |
if ((file = getenv("MAIL")) == NULL) { |
if (!(pwd = getpwuid(getuid()))) |
if ((user = getenv("LOGNAME")) == NULL && |
errx(1, "no password file entry for you"); |
(user = getenv("USER")) == NULL) { |
if ((file = getenv("USER"))) { |
if (!(pwd = getpwuid(getuid()))) |
(void)snprintf(buf, sizeof(buf), |
errx(1, "no password file " |
"%s/%s", _PATH_MAILDIR, file); |
"entry for you"); |
file = buf; |
user = pwd->pw_name; |
} else |
} |
(void)snprintf(file = buf, sizeof(buf), |
|
"%s/%s", _PATH_MAILDIR, |
|
pwd->pw_name); |
|
} |
} |
} else { |
} |
(void)snprintf(buf, sizeof(buf), "%s/%s", |
if (file == NULL) { |
_PATH_MAILDIR, file); |
if (asprintf(&buf, "%s/%s", _PATH_MAILDIR, user) == -1) |
|
err(1, NULL); |
file = buf; |
file = buf; |
} |
} |
} |
} |
if (!freopen(file, "r", stdin)) |
fp = fopen(file, "r"); |
err(1, "%s", file); |
free(buf); |
for (newline = 1; fgets(buf, sizeof(buf), stdin);) { |
return(fp); |
if (*buf == '\n') { |
|
newline = 1; |
|
continue; |
|
} |
|
if (newline && !strncmp(buf, "From ", 5) && |
|
(!sender || match(buf + 5, sender))) |
|
printf("%s", buf); |
|
newline = 0; |
|
} |
|
exit(0); |
|
} |
} |
|
|
int |
int |