2 * Totally braindamaged mbox splitter program.
4 * It just splits a mbox into a list of files: "0001" "0002" ..
5 * so you can process them further from there.
10 #include <sys/types.h>
18 static int usage(void)
20 fprintf(stderr
, "mailsplit <mbox> <directory>\n");
24 static int linelen(const char *map
, unsigned long size
)
33 } while (size
&& c
!= '\n');
37 static int is_from_line(const char *line
, int len
)
41 if (len
< 20 || memcmp("From ", line
, 5))
44 colon
= line
+ len
- 2;
53 if (!isdigit(colon
[-4]) ||
54 !isdigit(colon
[-2]) ||
55 !isdigit(colon
[-1]) ||
56 !isdigit(colon
[ 1]) ||
61 if (strtol(colon
+3, NULL
, 10) <= 90)
64 /* Ok, close enough */
68 static int parse_email(const void *map
, unsigned long size
)
72 if (size
< 6 || memcmp("From ", map
, 5))
75 /* Make sure we don't trigger on this first line */
76 map
++; size
--; offset
=1;
79 * Search for a line beginning with "From ", and
80 * having smething that looks like a date format.
83 int len
= linelen(map
, size
);
84 if (is_from_line(map
, len
))
93 fprintf(stderr
, "corrupt mailbox\n");
97 int main(int argc
, char **argv
)
106 fd
= open(argv
[1], O_RDONLY
);
111 if (chdir(argv
[2]) < 0)
113 if (fstat(fd
, &st
) < 0) {
118 map
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
119 if (map
== MAP_FAILED
) {
128 unsigned long len
= parse_email(map
, size
);
130 sprintf(name
, "%04d", ++nr
);
131 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0600);
136 if (write(fd
, map
, len
) != len
) {