]>
diplodocus.org Git - mdfrm/blob - mdfrm.c
23d95071a835f22ea23a94c4bd55113c3d3af1d3
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 ident
[] = "$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 fromcount
= subjcount
= 0;
53 fromheader
[0] = subjheader
[0] = '\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 fromheader
[fromcount
] = '\0';
70 } else if (len
> SUBJLEN
&& strncmp(line
, "Subject: ", SUBJLEN
) == 0) {
73 if (len
< SUBJCOUNT
) {
76 subjcount
= SUBJCOUNT
;
78 strncpy(subjheader
, line
, subjcount
);
79 subjheader
[subjcount
] = '\0';
90 struct dirent
*dirent
;
92 char fromheader
[FROMCOUNT
+ 1] = { '\0' };
93 char subjheader
[SUBJCOUNT
+ 1] = { '\0' };
95 if (chdir(dirname
) != 0) {
96 err(EX_OSERR
, "Failed chdir(%s)", dirname
);
99 if (chdir("new") != 0) {
100 err(EX_OSERR
, "Failed chdir(%s/new)", dirname
);
105 err(EX_OSERR
, "Failed opendir(%s/new)", dirname
);
108 for (dirent
= readdir(dir
); dirent
; dirent
= readdir(dir
)) {
109 filename
= dirent
->d_name
;
110 if (filename
[0] == '.') {
114 scan_message(filename
, fromheader
, subjheader
);
116 printf("%-24s ", fromheader
);
117 printf("%-53s\n", subjheader
);
122 main(int argc
, char *argv
[])
127 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
132 if (!(dirname
= argv
[1])) {
133 if (!(dirname
= getenv("MAILDIR"))) {
134 if (!(dirname
= getenv("HOME"))) {
135 errx(EX_UNAVAILABLE
, "Could not find maildir.");
137 snprintf(buf
, PATH_MAX
, "%s/Maildir", dirname
);