cleanups: Remove unused vars from combine-diff.c
[git/fastimport.git] / mailsplit.c
blobc529e2d060b610d8f0dd80464634a954c4d058e8
1 /*
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.
6 */
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include "cache.h"
16 static const char git_mailsplit_usage[] =
17 "git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
19 static int is_from_line(const char *line, int len)
21 const char *colon;
23 if (len < 20 || memcmp("From ", line, 5))
24 return 0;
26 colon = line + len - 2;
27 line += 5;
28 for (;;) {
29 if (colon < line)
30 return 0;
31 if (*--colon == ':')
32 break;
35 if (!isdigit(colon[-4]) ||
36 !isdigit(colon[-2]) ||
37 !isdigit(colon[-1]) ||
38 !isdigit(colon[ 1]) ||
39 !isdigit(colon[ 2]))
40 return 0;
42 /* year */
43 if (strtol(colon+3, NULL, 10) <= 90)
44 return 0;
46 /* Ok, close enough */
47 return 1;
50 /* Could be as small as 64, enough to hold a Unix "From " line. */
51 static char buf[4096];
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.
58 static int split_one(FILE *mbox, const char *name, int allow_bare)
60 FILE *output = NULL;
61 int len = strlen(buf);
62 int fd;
63 int status = 0;
64 int is_bare = !is_from_line(buf, len);
66 if (is_bare && !allow_bare)
67 goto corrupt;
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");
74 /* Copy it out, while searching for a line that begins with
75 * "From " and having something that looks like a date format.
77 for (;;) {
78 int is_partial = (buf[len-1] != '\n');
80 if (fputs(buf, output) == EOF)
81 die("cannot write output");
83 if (fgets(buf, sizeof(buf), mbox) == NULL) {
84 if (feof(mbox)) {
85 status = 1;
86 break;
88 die("cannot read mbox");
90 len = strlen(buf);
91 if (!is_partial && !is_bare && is_from_line(buf, len))
92 break; /* done with one message */
94 fclose(output);
95 return status;
97 corrupt:
98 if (output)
99 fclose(output);
100 unlink(name);
101 fprintf(stderr, "corrupt mailbox\n");
102 exit(1);
105 int main(int argc, const char **argv)
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;
114 for (argp = argv+1; *argp; argp++) {
115 const char *arg = *argp;
117 if (arg[0] != '-')
118 break;
119 /* do flags here */
120 if ( arg[1] == 'd' ) {
121 nr_prec = strtol(arg+2, NULL, 10);
122 if (nr_prec < 3 || 10 <= nr_prec)
123 usage(git_mailsplit_usage);
124 continue;
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);
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);
155 } else {
156 /* New usage: if no more argument, parse stdin */
157 if ( !*argp )
158 argp = stdin_only;
161 name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr));
163 while (*argp) {
164 const char *file = *argp++;
165 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "rt");
166 int file_done = 0;
168 if ( !f )
169 die ("cannot open mbox %s", file);
171 if (fgets(buf, sizeof(buf), f) == NULL) {
172 if (f == stdin)
173 break; /* empty stdin is OK */
174 die("cannot read mbox %s", file);
177 while (!file_done) {
178 sprintf(name, "%s/%0*d", dir, nr_prec, ++nr);
179 file_done = split_one(f, name, allow_bare);
182 if (f != stdin)
183 fclose(f);
186 printf("%d\n", nr);
187 return 0;