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 static const char git_mailsplit_usage
[] =
11 "git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
13 static int is_from_line(const char *line
, int len
)
17 if (len
< 20 || memcmp("From ", line
, 5))
20 colon
= line
+ len
- 2;
29 if (!isdigit(colon
[-4]) ||
30 !isdigit(colon
[-2]) ||
31 !isdigit(colon
[-1]) ||
32 !isdigit(colon
[ 1]) ||
37 if (strtol(colon
+3, NULL
, 10) <= 90)
40 /* Ok, close enough */
44 /* Could be as small as 64, enough to hold a Unix "From " line. */
45 static char buf
[4096];
47 /* Called with the first line (potentially partial)
48 * already in buf[] -- normally that should begin with
49 * the Unix "From " line. Write it into the specified
52 static int split_one(FILE *mbox
, const char *name
, int allow_bare
)
55 int len
= strlen(buf
);
58 int is_bare
= !is_from_line(buf
, len
);
60 if (is_bare
&& !allow_bare
)
63 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
65 die("cannot open output file %s", name
);
66 output
= fdopen(fd
, "w");
68 /* Copy it out, while searching for a line that begins with
69 * "From " and having something that looks like a date format.
72 int is_partial
= (buf
[len
-1] != '\n');
74 if (fputs(buf
, output
) == EOF
)
75 die("cannot write output");
77 if (fgets(buf
, sizeof(buf
), mbox
) == NULL
) {
82 die("cannot read mbox");
85 if (!is_partial
&& !is_bare
&& is_from_line(buf
, len
))
86 break; /* done with one message */
95 fprintf(stderr
, "corrupt mailbox\n");
99 int split_mbox(const char **mbox
, const char *dir
, int allow_bare
, int nr_prec
, int skip
)
101 char *name
= xmalloc(strlen(dir
) + 2 + 3 * sizeof(skip
));
105 const char *file
= *mbox
++;
106 FILE *f
= !strcmp(file
, "-") ? stdin
: fopen(file
, "r");
110 error("cannot open mbox %s", file
);
114 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
116 break; /* empty stdin is OK */
117 error("cannot read mbox %s", file
);
122 sprintf(name
, "%s/%0*d", dir
, nr_prec
, ++skip
);
123 file_done
= split_one(f
, name
, allow_bare
);
134 int cmd_mailsplit(int argc
, const char **argv
, const char *prefix
)
136 int nr
= 0, nr_prec
= 4, ret
;
138 const char *dir
= NULL
;
140 static const char *stdin_only
[] = { "-", NULL
};
142 for (argp
= argv
+1; *argp
; argp
++) {
143 const char *arg
= *argp
;
148 if ( arg
[1] == 'd' ) {
149 nr_prec
= strtol(arg
+2, NULL
, 10);
150 if (nr_prec
< 3 || 10 <= nr_prec
)
151 usage(git_mailsplit_usage
);
153 } else if ( arg
[1] == 'f' ) {
154 nr
= strtol(arg
+2, NULL
, 10);
155 } else if ( arg
[1] == 'b' && !arg
[2] ) {
157 } else if ( arg
[1] == 'o' && arg
[2] ) {
159 } else if ( arg
[1] == '-' && !arg
[2] ) {
160 argp
++; /* -- marks end of options */
163 die("unknown option: %s", arg
);
168 /* Backwards compatibility: if no -o specified, accept
169 <mbox> <dir> or just <dir> */
170 switch (argc
- (argp
-argv
)) {
176 stdin_only
[0] = argp
[0];
181 usage(git_mailsplit_usage
);
184 /* New usage: if no more argument, parse stdin */
189 ret
= split_mbox(argp
, dir
, allow_bare
, nr_prec
, nr
);