from.c
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Code in the public domain, Sean 'Shaleh' Perry <shaleh@debian.org>, 1999
*
* Created as an example of using libmailbox
*
**/
#include <mailbox.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
mailbox *mail;
unsigned int i;
char *from, *date;
char *user;
char mailpath[256];
user = getenv("USER");
if (user == NULL)
{
fprintf(stderr, "who am I?\n");
exit(-1);
}
if (!strcmp(user, "root") && argc == 2)
user = argv[1];
else if (argc > 2)
{
fprintf(stderr,
"Usage: from [username] (argument valid when ran as root)\n");
exit(-1);
}
snprintf (mailpath, 256, "%s/%s", _PATH_MAILDIR, user);
mail = mbox_open(mailpath);
if( mail == NULL ) {
perror("mbox_open");
exit(-1);
}
for(i = 0; i < mail->messages; ++i) {
from = mbox_header_line(mail, i, "From");
if (from == NULL)
{
perror("mbox_header_line");
exit(-1);
}
date = mbox_header_line(mail, i, "Received");
if (date == NULL)
{
perror("mbox_header_line");
exit(-1);
}
printf("%s %s\n", from, date);
free(from);
free(date);
}
mbox_close(mail);
exit(0);
}