]> diplodocus.org Git - mdfrm/blob - mdfrm.c
2f48ee532b42d43b064c5dd54f5a55b2c1efb8cb
[mdfrm] / mdfrm.c
1 /* mdfrm.c -- mdfrm(1) for maildir.
2 *
3 * $Id$
4 *
5 * This program is in the public domain.
6 */
7
8 #include <sys/types.h>
9
10 #include <dirent.h>
11 #include <err.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sysexits.h>
16 #include <unistd.h>
17
18 /* Note that PATH_MAX is supposed to include the terminating null. */
19 #ifndef PATH_MAX
20 #ifndef MAXPATHLEN
21 #define PATH_MAX 65
22 #else
23 #define PATH_MAX MAXPATHLEN
24 #endif
25 #endif
26
27 enum {
28 FROMLEN = 6, /* "From: " */
29 SUBJLEN = 9, /* "Subject: " */
30
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
33 * frm function.
34 */
35 FROMCOUNT = 24,
36 SUBJCOUNT = 53
37 };
38
39 static char rcsid[] = "$Id$";
40
41 static void
42 scan_message(char *filename, char *fromheader, char *subjheader)
43 {
44 FILE *file;
45 char *line;
46 size_t len, fromcount, subjcount;
47
48 if (!(file = fopen(filename, "r"))) {
49 err(EX_OSERR, "Failed fopen(%s)", filename);
50 }
51
52 fromcount = subjcount = 0;
53 while ((line = fgetln(file, &len))) {
54 if (len <= 1) {
55 break;
56 }
57 --len;
58
59 if (len > FROMLEN && strncmp(line, "From: ", FROMLEN) == 0) {
60 line += FROMLEN;
61 len -= FROMLEN;
62 if (len < FROMCOUNT) {
63 fromcount = len;
64 } else {
65 fromcount = FROMCOUNT;
66 }
67 strncpy(fromheader, line, fromcount);
68 fromheader[fromcount] = '\0';
69 } else if (len > SUBJLEN && strncmp(line, "Subject: ", SUBJLEN) == 0) {
70 line += SUBJLEN;
71 len -= SUBJLEN;
72 if (len < SUBJCOUNT) {
73 subjcount = len;
74 } else {
75 subjcount = SUBJCOUNT;
76 }
77 strncpy(subjheader, line, subjcount);
78 subjheader[subjcount] = '\0';
79 }
80 }
81
82 fclose(file);
83 }
84
85 static void
86 frm(char *dirname)
87 {
88 DIR *dir;
89 struct dirent *dirent;
90 char *filename;
91 char fromheader[FROMCOUNT + 1] = { '\0' };
92 char subjheader[SUBJCOUNT + 1] = { '\0' };
93
94 if (chdir(dirname) != 0) {
95 err(EX_OSERR, "Failed chdir(%s)", dirname);
96 }
97
98 if (chdir("new") != 0) {
99 err(EX_OSERR, "Failed chdir(%s/new)", dirname);
100 }
101
102 dir = opendir(".");
103 if (!dir) {
104 err(EX_OSERR, "Failed opendir(%s/new)", dirname);
105 }
106
107 for (dirent = readdir(dir); dirent; dirent = readdir(dir)) {
108 filename = dirent->d_name;
109 if (filename[0] == '.') {
110 continue;
111 }
112
113 scan_message(filename, fromheader, subjheader);
114
115 printf("%-24s ", fromheader);
116 printf("%-53s\n", subjheader);
117 }
118 }
119
120 int
121 main(int argc, char *argv[])
122 {
123 char *dirname;
124 char buf[PATH_MAX];
125
126 if (argv[1] && argv[1][0] == '-' && argv[1][1] == 'v') {
127 puts(rcsid);
128 return 0;
129 }
130
131 if (!(dirname = argv[1])) {
132 if (!(dirname = getenv("MAILDIR"))) {
133 if (!(dirname = getenv("HOME"))) {
134 errx(EX_UNAVAILABLE, "Could not find maildir.");
135 } else {
136 snprintf(buf, PATH_MAX, "%s/Maildir", dirname);
137 dirname = buf;
138 }
139 }
140 }
141
142 frm(dirname);
143
144 return 0;
145 }