]>
diplodocus.org Git - mdfrm/blob - mdfrm.c
3e51e938674a2b34de854d782e478a4064de5da1
1 /* mdfrm.c -- mdfrm(1) for maildir.
5 * This program is in the public domain.
18 /* Note that PATH_MAX is supposed to include the terminating null. */
23 #define PATH_MAX MAXPATHLEN
28 FROMLEN
= 6, /* "From: " */
29 SUBJLEN
= 9, /* "Subject: " */
31 /* Number of characters for each field we actually output. This
32 * must also be changed in the printf functions at the end of the
39 static char rcsid
[] = "$Id$";
42 scan_message(char *filename
, char *fromheader
, char *subjheader
)
46 size_t len
, fromcount
, subjcount
;
48 if (!(file
= fopen(filename
, "r"))) {
49 err(EX_OSERR
, "Failed fopen(%s)", filename
);
52 fromheader
[0] = subjheader
[0] = '\0';
53 fromcount
= subjcount
= 0;
54 while ((line
= fgetln(file
, &len
))) {
60 if (len
> FROMLEN
&& strncmp(line
, "From: ", FROMLEN
) == 0) {
63 if (len
< FROMCOUNT
) {
66 fromcount
= FROMCOUNT
;
68 strncpy(fromheader
, line
, fromcount
);
69 } else if (len
> SUBJLEN
&& strncmp(line
, "Subject: ", SUBJLEN
) == 0) {
72 if (len
< SUBJCOUNT
) {
75 subjcount
= SUBJCOUNT
;
77 strncpy(subjheader
, line
, subjcount
);
88 struct dirent
*dirent
;
90 char fromheader
[FROMCOUNT
+ 1] = { '\0' };
91 char subjheader
[SUBJCOUNT
+ 1] = { '\0' };
93 if (chdir(dirname
) != 0) {
94 err(EX_OSERR
, "Failed chdir(%s)", dirname
);
97 if (chdir("new") != 0) {
98 err(EX_OSERR
, "Failed chdir(%s/new)", dirname
);
103 err(EX_OSERR
, "Failed opendir(%s/new)", dirname
);
106 for (dirent
= readdir(dir
); dirent
; dirent
= readdir(dir
)) {
107 filename
= dirent
->d_name
;
108 if (filename
[0] == '.') {
112 scan_message(filename
, fromheader
, subjheader
);
114 printf("%-24s ", fromheader
);
115 printf("%-53s\n", subjheader
);
120 main(int argc
, char *argv
[])
125 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
130 if (!(dirname
= argv
[1])) {
131 if (!(dirname
= getenv("MAILDIR"))) {
132 if (!(dirname
= getenv("HOME"))) {
133 errx(EX_UNAVAILABLE
, "Could not find maildir.");
135 snprintf(buf
, PATH_MAX
, "%s/Maildir", dirname
);