sfrom.c.texi
1.94 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* sfrom, Simple From */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <mailutils.h>
#include <paths.h>
#ifndef _PATH_MAILDIR
#define _PATH_MAILDIR "/usr/spool/mail"
#endif
int main (int argc, char **argv)
@{
char buffer[1024];
char from[64];
char subject[64];
char *mail;
mailbox_t mbox;
int status
size_t msgno, msg_total, size;
if (argc == 2)
@{
mail = argv[1];
@}
else
@{
mail = getenv ("MAIL");
if (mail == NULL)
@{
char * user = getlogin ();
size_t len = strlen (_PATH_MAILDIR)
+ 1 /* for slash */ + strlen (user) + 1 /* null */;
mail = malloc (len);
if (mail == NULL)
@{
fprintf (stderr, "malloc failed\n");
exit (EXIT_FAILURE);
@}
snprintf (mail, len, "%s/%s", _PATH_MAILDIR, user);
@}
@}
status = mailbox_init (&mbox, mail, 0);
if (status != 0)
@{
fprintf (stderr, "mailbox_init(%s) failed\n", mail);
exit (EXIT_FAILURE);
@}
status = mailbox_open (mbox, MU_MB_ORDONLY);
if (status != 0)
@{
fprintf (stderr, "mailbox_open(%s), %s\n", mail);
exit (EXIT_FAILURE);
@}
mailbox_scan (mbox, &msg_total);
for (msgno = 0; msgno < msg_total; msgno++)
@{
if (mailbox_get_header (mbox, msgno, buffer,
sizeof (buffer), &size) != 0)
@{
fprintf (stderr, "header corrupted\n");
exit (EXIT_FAILURE);
@}
header_gvalue (buffer, size, MU_HDR_FROM, from, sizeof (from), NULL);
header_gvalue (buffer, size, MU_HDR_SUBJECT, subject,
sizeof (subject), NULL);
printf ("%s %s\n", from, subject);
@}
mailbox_close (mbox);
mailbox_destroy (&mbox);
return 0;
@}