]>
diplodocus.org Git - mdfrm/blob - mdfrm.c
8977cdd0899c12d737fcf3c818137f00d467835d
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 scan_message(char *filename
, char *fromheader
, char *subjheader
)
47 size_t len
, fromcount
, subjcount
;
49 if (!(file
= fopen(filename
, "r"))) {
50 err(EX_OSERR
, "Failed fopen(%s)", filename
);
53 fromcount
= subjcount
= 0;
54 fromheader
[0] = subjheader
[0] = '\0';
55 while ((line
= fgetln(file
, &len
))) {
61 if (len
> FROMLEN
&& strncasecmp(line
, "From:", FROMLEN
) == 0) {
64 while (len
> 0 && isspace(line
[0])) {
68 if (len
< FROMCOUNT
) {
71 fromcount
= FROMCOUNT
;
73 strncpy(fromheader
, line
, fromcount
);
74 fromheader
[fromcount
] = '\0';
75 } else if (len
> SUBJLEN
&& strncasecmp(line
, "Subject:", SUBJLEN
) == 0) {
78 while (len
> 0 && isspace(line
[0])) {
82 if (len
< SUBJCOUNT
) {
85 subjcount
= SUBJCOUNT
;
87 strncpy(subjheader
, line
, subjcount
);
88 subjheader
[subjcount
] = '\0';
96 frm(char *dirname
, int showfn
)
99 struct dirent
*dirent
;
101 char fromheader
[FROMCOUNT
+ 1] = { '\0' };
102 char subjheader
[SUBJCOUNT
+ 1] = { '\0' };
104 if (chdir(dirname
) != 0) {
105 err(EX_OSERR
, "Failed chdir(%s)", dirname
);
108 if (chdir("new") != 0) {
109 err(EX_OSERR
, "Failed chdir(%s/new)", dirname
);
114 err(EX_OSERR
, "Failed opendir(%s/new)", dirname
);
117 for (dirent
= readdir(dir
); dirent
; dirent
= readdir(dir
)) {
118 filename
= dirent
->d_name
;
119 if (filename
[0] == '.') {
123 scan_message(filename
, fromheader
, subjheader
);
126 printf("%s: ", filename
);
128 printf("%-24s ", fromheader
);
129 printf("%-53s\n", subjheader
);
134 main(int argc
, char *argv
[])
140 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'v') {
145 if (argv
[1] && argv
[1][0] == '-' && argv
[1][1] == 'f') {
150 if (!(dirname
= argv
[1])) {
151 if (!(dirname
= getenv("MAILDIR"))) {
152 if (!(dirname
= getenv("HOME"))) {
153 errx(EX_UNAVAILABLE
, "Could not find maildir.");
155 snprintf(buf
, PATH_MAX
, "%s/Maildir", dirname
);
161 frm(dirname
, showfn
);