]>
diplodocus.org Git - mdfrm/blob - mdfrm.c
55fac72ecb8d27a96b4ac8cb8f8d8d92cc0cb55a
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.
39 main(int argc
, char *argv
[])
44 struct dirent
*dirent
;
47 char *line
, *fromheader
, *subjheader
;
48 size_t len
, fromcount
, subjcount
;
50 if (!(dirname
= argv
[1])) {
51 if (!(dirname
= getenv("MAILDIR"))) {
52 if (!(dirname
= getenv("HOME"))) {
53 errx(EX_UNAVAILABLE
, "Could not find maildir.");
55 snprintf(buf
, PATH_MAX
, "%s/Maildir", dirname
);
61 if (chdir(dirname
) != 0) {
62 err(EX_OSERR
, "Failed chdir(%s)", dirname
);
65 if (chdir("new") != 0) {
66 err(EX_OSERR
, "Failed chdir(%s/new)", dirname
);
71 err(EX_OSERR
, "Failed opendir(%s/new)", dirname
);
74 fromheader
= malloc(FROMCOUNT
+ 1);
75 subjheader
= malloc(SUBJCOUNT
+ 1);
76 for (dirent
= readdir(dir
); dirent
; dirent
= readdir(dir
)) {
77 filename
= dirent
->d_name
;
78 if (filename
[0] == '.') {
82 if (!(file
= fopen(filename
, "r"))) {
83 err(EX_OSERR
, "Failed fopen(%s)", filename
);
86 fromheader
[0] = subjheader
[0] = '\0';
87 fromcount
= subjcount
= 0;
88 while ((line
= fgetln(file
, &len
))) {
95 && strncmp(line
, "From: ", FROMLEN
) == 0) {
98 if (len
< FROMCOUNT
) {
101 fromcount
= FROMCOUNT
;
103 strncpy(fromheader
, line
, fromcount
);
104 } else if (len
> SUBJLEN
105 && strncmp(line
, "Subject: ", SUBJLEN
) == 0) {
108 if (len
< SUBJCOUNT
) {
111 subjcount
= SUBJCOUNT
;
113 strncpy(subjheader
, line
, subjcount
);
117 printf("%-24s ", fromheader
);
118 printf("%-53s\n", subjheader
);