X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=mailsplit.c;h=70a569c12a9435627e5b61e887ea4f8d3ccfe7dd;hb=1a82e79315ed633f6b0b1fc4076054950c5380d3;hp=a3238c20da27c3348d406066b595cd085e600558;hpb=597c9cc540c2ca5d0b0eeaa3f453dffa14afab6d;p=git.git diff --git a/mailsplit.c b/mailsplit.c index a3238c20..70a569c1 100644 --- a/mailsplit.c +++ b/mailsplit.c @@ -9,30 +9,12 @@ #include #include #include -#include #include #include -#include -#include +#include "cache.h" -static int usage(void) -{ - fprintf(stderr, "mailsplit \n"); - exit(1); -} - -static int linelen(const char *map, unsigned long size) -{ - int len = 0, c; - - do { - c = *map; - map++; - size--; - len++; - } while (size && c != '\n'); - return len; -} +static const char git_mailsplit_usage[] = +"git-mailsplit [-d] [-f] [-b] -o ..."; static int is_from_line(const char *line, int len) { @@ -65,81 +47,142 @@ static int is_from_line(const char *line, int len) return 1; } -static int parse_email(const void *map, unsigned long size) +/* Could be as small as 64, enough to hold a Unix "From " line. */ +static char buf[4096]; + +/* Called with the first line (potentially partial) + * already in buf[] -- normally that should begin with + * the Unix "From " line. Write it into the specified + * file. + */ +static int split_one(FILE *mbox, const char *name, int allow_bare) { - unsigned long offset; + FILE *output = NULL; + int len = strlen(buf); + int fd; + int status = 0; + int is_bare = !is_from_line(buf, len); - if (size < 6 || memcmp("From ", map, 5)) + if (is_bare && !allow_bare) goto corrupt; - /* Make sure we don't trigger on this first line */ - map++; size--; offset=1; + fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666); + if (fd < 0) + die("cannot open output file %s", name); + output = fdopen(fd, "w"); - /* - * Search for a line beginning with "From ", and - * having something that looks like a date format. + /* Copy it out, while searching for a line that begins with + * "From " and having something that looks like a date format. */ - do { - int len = linelen(map, size); - if (is_from_line(map, len)) - return offset; - map += len; - size -= len; - offset += len; - } while (size); - return offset; - -corrupt: + for (;;) { + int is_partial = (buf[len-1] != '\n'); + + if (fputs(buf, output) == EOF) + die("cannot write output"); + + if (fgets(buf, sizeof(buf), mbox) == NULL) { + if (feof(mbox)) { + status = 1; + break; + } + die("cannot read mbox"); + } + len = strlen(buf); + if (!is_partial && !is_bare && is_from_line(buf, len)) + break; /* done with one message */ + } + fclose(output); + return status; + + corrupt: + if (output) + fclose(output); + unlink(name); fprintf(stderr, "corrupt mailbox\n"); exit(1); } -int main(int argc, char **argv) +int main(int argc, const char **argv) { - int fd, nr; - struct stat st; - unsigned long size; - void *map; - - if (argc != 3) - usage(); - fd = open(argv[1], O_RDONLY); - if (fd < 0) { - perror(argv[1]); - exit(1); - } - if (chdir(argv[2]) < 0) - usage(); - if (fstat(fd, &st) < 0) { - perror("stat"); - exit(1); + int nr = 0, nr_prec = 4; + int allow_bare = 0; + const char *dir = NULL; + const char **argp; + static const char *stdin_only[] = { "-", NULL }; + char *name; + + for (argp = argv+1; *argp; argp++) { + const char *arg = *argp; + + if (arg[0] != '-') + break; + /* do flags here */ + if ( arg[1] == 'd' ) { + nr_prec = strtol(arg+2, NULL, 10); + if (nr_prec < 3 || 10 <= nr_prec) + usage(git_mailsplit_usage); + continue; + } else if ( arg[1] == 'f' ) { + nr = strtol(arg+2, NULL, 10); + } else if ( arg[1] == 'b' && !arg[2] ) { + allow_bare = 1; + } else if ( arg[1] == 'o' && arg[2] ) { + dir = arg+2; + } else if ( arg[1] == '-' && !arg[2] ) { + argp++; /* -- marks end of options */ + break; + } else { + die("unknown option: %s", arg); + } } - size = st.st_size; - map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); - if (map == MAP_FAILED) { - perror("mmap"); - close(fd); - exit(1); + + if ( !dir ) { + /* Backwards compatibility: if no -o specified, accept + or just */ + switch (argc - (argp-argv)) { + case 1: + dir = argp[0]; + argp = stdin_only; + break; + case 2: + stdin_only[0] = argp[0]; + dir = argp[1]; + argp = stdin_only; + break; + default: + usage(git_mailsplit_usage); + } + } else { + /* New usage: if no more argument, parse stdin */ + if ( !*argp ) + argp = stdin_only; } - close(fd); - nr = 0; - do { - char name[10]; - unsigned long len = parse_email(map, size); - assert(len <= size); - sprintf(name, "%04d", ++nr); - fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600); - if (fd < 0) { - perror(name); - exit(1); + + name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr)); + + while (*argp) { + const char *file = *argp++; + FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r"); + int file_done = 0; + + if ( !f ) + die ("cannot open mbox %s", file); + + if (fgets(buf, sizeof(buf), f) == NULL) { + if (f == stdin) + break; /* empty stdin is OK */ + die("cannot read mbox %s", file); } - if (write(fd, map, len) != len) { - perror("write"); - exit(1); + + while (!file_done) { + sprintf(name, "%s/%0*d", dir, nr_prec, ++nr); + file_done = split_one(f, name, allow_bare); } - close(fd); - map += len; - size -= len; - } while (size > 0); + + if (f != stdin) + fclose(f); + } + + printf("%d\n", nr); return 0; }