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, "git-mailsplit [-d<prec>] <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 something 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)
99 int fd, nr, nr_prec = 4;
104 if (argc == 4 && !strncmp(argv[1], "-d", 2)) {
105 nr_prec = strtol(argv[1] + 2, NULL, 10);
106 if (nr_prec < 3 || 10 <= nr_prec)
112 fd = open(argv[1], O_RDONLY);
117 if (chdir(argv[2]) < 0)
119 if (fstat(fd, &st) < 0) {
124 map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
125 if (map == MAP_FAILED) {
134 unsigned long len = parse_email(map, size);
136 sprintf(name, "%0*d", nr_prec, ++nr);
137 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
142 if (write(fd, map, len) != len) {