]>
diplodocus.org Git - mdfrm/blob - mdfrm.c
66e1b6f41ef8d58e5b0e9b8e7e86ccbd343115c1
1 /* mdfrm.c -- mdfrm(1) for maildir.
5 * This program is in the public domain.
19 /* Note that PATH_MAX is supposed to include the terminating null. */
24 #define PATH_MAX MAXPATHLEN
29 FROMLEN
= 5, /* "From:" */
30 SUBJLEN
= 8, /* "Subject:" */
32 /* Number of characters for each field we actually output. This
33 * must also be changed in the printf functions at the end of the
40 static char ident
[] = "$Id$";
43 sanitize(char *str
, size_t len
)
47 for (i
= 0; i
< len
; i
++) {
48 if (isprint(str
[i
]) == 0) {
55 scan_message(char *filename
, char *fromheader
, char *subjheader
)
59 size_t len
, fromcount
, subjcount
;
61 if (!(file
= fopen(filename
, "r"))) {
62 err(EX_OSERR
, "Failed fopen(%s)", filename
);
65 fromcount
= subjcount
= 0;
66 fromheader
[0] = subjheader
[0] = '\0';
67 while ((line
= fgetln(file
, &len
))) {
73 if (len
> FROMLEN
&& strncasecmp(line
, "From:", FROMLEN
) == 0) {
76 while (len
> 0 && isspace(line
[0])) {
80 if (len
< FROMCOUNT
) {
83 fromcount
= FROMCOUNT
;
85 strncpy(fromheader
, line
, fromcount
);
86 fromheader
[fromcount
] = '\0';
87 sanitize(fromheader
, fromcount
);
88 } else if (len
> SUBJLEN
&& strncasecmp(line
, "Subject:", SUBJLEN
) == 0) {
91 while (len
> 0 && isspace(line
[0])) {
95 if (len
< SUBJCOUNT
) {
98 subjcount
= SUBJCOUNT
;
100 strncpy(subjheader
, line
, subjcount
);
101 subjheader
[subjcount
] = '\0';
102 sanitize(subjheader
, subjcount
);
110 frm(char *dirname
, int showfn
)
113 struct dirent
*dirent
;
115 char fromheader
[FROMCOUNT
+ 1] = { '\0' };
116 char subjheader
[SUBJCOUNT
+ 1] = { '\0' };
118 if (chdir(dirname
) != 0) {
119 err(EX_OSERR
, "Failed chdir(%s)", dirname
);
122 if (chdir("new") != 0) {
123 err(EX_OSERR
, "Failed chdir(%s/new)", dirname
);
128 err(EX_OSERR
, "Failed opendir(%s/new)", dirname
);
131 for (dirent
= readdir(dir
); dirent
; dirent
= readdir(dir
)) {
132 filename
= dirent
->d_name
;
133 if (filename
[0] == '.') {
137 scan_message(filename
, fromheader
, subjheader
);
140 printf("%s ", filename
);
142 printf("%-24s ", fromheader
);
143 printf("%s\n", subjheader
);
148 main(int argc
, char *argv
[])
154 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
159 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'f') {
164 if (!(dirname
= argv
[1])) {
165 if (!(dirname
= getenv("MAILDIR"))) {
166 if (!(dirname
= getenv("HOME"))) {
167 errx(EX_UNAVAILABLE
, "Could not find maildir.");
169 snprintf(buf
, PATH_MAX
, "%s/Maildir", dirname
);
175 frm(dirname
, showfn
);