]>
Commit | Line | Data |
---|---|---|
2744b234 LT |
1 | /* |
2 | * Totally braindamaged mbox splitter program. | |
3 | * | |
4 | * It just splits a mbox into a list of files: "0001" "0002" .. | |
5 | * so you can process them further from there. | |
6 | */ | |
7 | #include <unistd.h> | |
8 | #include <stdlib.h> | |
9 | #include <fcntl.h> | |
10 | #include <sys/types.h> | |
11 | #include <sys/stat.h> | |
2744b234 LT |
12 | #include <string.h> |
13 | #include <stdio.h> | |
8b73edf4 | 14 | #include "cache.h" |
2744b234 | 15 | |
8b73edf4 | 16 | static const char git_mailsplit_usage[] = |
b3f041fb | 17 | "git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>..."; |
2744b234 LT |
18 | |
19 | static int is_from_line(const char *line, int len) | |
20 | { | |
21 | const char *colon; | |
22 | ||
23 | if (len < 20 || memcmp("From ", line, 5)) | |
24 | return 0; | |
25 | ||
26 | colon = line + len - 2; | |
27 | line += 5; | |
28 | for (;;) { | |
29 | if (colon < line) | |
30 | return 0; | |
31 | if (*--colon == ':') | |
32 | break; | |
33 | } | |
34 | ||
35 | if (!isdigit(colon[-4]) || | |
36 | !isdigit(colon[-2]) || | |
37 | !isdigit(colon[-1]) || | |
38 | !isdigit(colon[ 1]) || | |
39 | !isdigit(colon[ 2])) | |
40 | return 0; | |
41 | ||
42 | /* year */ | |
43 | if (strtol(colon+3, NULL, 10) <= 90) | |
44 | return 0; | |
45 | ||
46 | /* Ok, close enough */ | |
47 | return 1; | |
48 | } | |
49 | ||
8b73edf4 JH |
50 | /* Could be as small as 64, enough to hold a Unix "From " line. */ |
51 | static char buf[4096]; | |
52 | ||
53 | /* Called with the first line (potentially partial) | |
54 | * already in buf[] -- normally that should begin with | |
55 | * the Unix "From " line. Write it into the specified | |
56 | * file. | |
57 | */ | |
b3f041fb | 58 | static int split_one(FILE *mbox, const char *name, int allow_bare) |
2744b234 | 59 | { |
8b73edf4 JH |
60 | FILE *output = NULL; |
61 | int len = strlen(buf); | |
62 | int fd; | |
63 | int status = 0; | |
b3f041fb | 64 | int is_bare = !is_from_line(buf, len); |
2744b234 | 65 | |
b3f041fb | 66 | if (is_bare && !allow_bare) |
2744b234 LT |
67 | goto corrupt; |
68 | ||
8b73edf4 JH |
69 | fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666); |
70 | if (fd < 0) | |
71 | die("cannot open output file %s", name); | |
72 | output = fdopen(fd, "w"); | |
2744b234 | 73 | |
8b73edf4 JH |
74 | /* Copy it out, while searching for a line that begins with |
75 | * "From " and having something that looks like a date format. | |
2744b234 | 76 | */ |
8b73edf4 JH |
77 | for (;;) { |
78 | int is_partial = (buf[len-1] != '\n'); | |
79 | ||
80 | if (fputs(buf, output) == EOF) | |
81 | die("cannot write output"); | |
82 | ||
83 | if (fgets(buf, sizeof(buf), mbox) == NULL) { | |
84 | if (feof(mbox)) { | |
85 | status = 1; | |
86 | break; | |
87 | } | |
88 | die("cannot read mbox"); | |
89 | } | |
90 | len = strlen(buf); | |
b3f041fb | 91 | if (!is_partial && !is_bare && is_from_line(buf, len)) |
8b73edf4 JH |
92 | break; /* done with one message */ |
93 | } | |
94 | fclose(output); | |
95 | return status; | |
96 | ||
97 | corrupt: | |
98 | if (output) | |
99 | fclose(output); | |
100 | unlink(name); | |
2744b234 LT |
101 | fprintf(stderr, "corrupt mailbox\n"); |
102 | exit(1); | |
103 | } | |
104 | ||
8b73edf4 | 105 | int main(int argc, const char **argv) |
2744b234 | 106 | { |
b3f041fb PA |
107 | int nr = 0, nr_prec = 4; |
108 | int allow_bare = 0; | |
109 | const char *dir = NULL; | |
110 | const char **argp; | |
111 | static const char *stdin_only[] = { "-", NULL }; | |
112 | char *name; | |
8b73edf4 | 113 | |
b3f041fb PA |
114 | for (argp = argv+1; *argp; argp++) { |
115 | const char *arg = *argp; | |
8b73edf4 JH |
116 | |
117 | if (arg[0] != '-') | |
118 | break; | |
119 | /* do flags here */ | |
b3f041fb PA |
120 | if ( arg[1] == 'd' ) { |
121 | nr_prec = strtol(arg+2, NULL, 10); | |
8b73edf4 JH |
122 | if (nr_prec < 3 || 10 <= nr_prec) |
123 | usage(git_mailsplit_usage); | |
124 | continue; | |
b3f041fb PA |
125 | } else if ( arg[1] == 'f' ) { |
126 | nr = strtol(arg+2, NULL, 10); | |
127 | } else if ( arg[1] == 'b' && !arg[2] ) { | |
128 | allow_bare = 1; | |
129 | } else if ( arg[1] == 'o' && arg[2] ) { | |
130 | dir = arg+2; | |
131 | } else if ( arg[1] == '-' && !arg[2] ) { | |
132 | argp++; /* -- marks end of options */ | |
133 | break; | |
134 | } else { | |
135 | die("unknown option: %s", arg); | |
8b73edf4 | 136 | } |
2744b234 | 137 | } |
8b73edf4 | 138 | |
b3f041fb PA |
139 | if ( !dir ) { |
140 | /* Backwards compatibility: if no -o specified, accept | |
141 | <mbox> <dir> or just <dir> */ | |
142 | switch (argc - (argp-argv)) { | |
143 | case 1: | |
144 | dir = argp[0]; | |
145 | argp = stdin_only; | |
146 | break; | |
147 | case 2: | |
148 | stdin_only[0] = argp[0]; | |
149 | dir = argp[1]; | |
150 | argp = stdin_only; | |
151 | break; | |
152 | default: | |
153 | usage(git_mailsplit_usage); | |
154 | } | |
155 | } else { | |
156 | /* New usage: if no more argument, parse stdin */ | |
157 | if ( !*argp ) | |
158 | argp = stdin_only; | |
2744b234 | 159 | } |
8b73edf4 | 160 | |
b3f041fb | 161 | name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr)); |
8b73edf4 | 162 | |
b3f041fb PA |
163 | while (*argp) { |
164 | const char *file = *argp++; | |
165 | FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "rt"); | |
166 | int file_done = 0; | |
8b73edf4 | 167 | |
b3f041fb PA |
168 | if ( !f ) |
169 | die ("cannot open mbox %s", file); | |
170 | ||
6f2eacfe JH |
171 | if (fgets(buf, sizeof(buf), f) == NULL) { |
172 | if (f == stdin) | |
173 | break; /* empty stdin is OK */ | |
b3f041fb | 174 | die("cannot read mbox %s", file); |
6f2eacfe | 175 | } |
b3f041fb PA |
176 | |
177 | while (!file_done) { | |
178 | sprintf(name, "%s/%0*d", dir, nr_prec, ++nr); | |
179 | file_done = split_one(f, name, allow_bare); | |
2744b234 | 180 | } |
b3f041fb PA |
181 | |
182 | if (f != stdin) | |
183 | fclose(f); | |
8b73edf4 | 184 | } |
b3f041fb PA |
185 | |
186 | printf("%d\n", nr); | |
187 | return 0; | |
2744b234 | 188 | } |