From 8b73edf498adf2895af7ff9c750283cf9325a632 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 6 Oct 2005 15:55:43 -0700 Subject: [PATCH] mailsplit: allow feeding mbox from standard input. When mbox argument is missing, read the mailbox from the standard input. Signed-off-by: Junio C Hamano --- Documentation/git-mailsplit.txt | 7 +- mailsplit.c | 175 +++++++++++++++++--------------- 2 files changed, 96 insertions(+), 86 deletions(-) diff --git a/Documentation/git-mailsplit.txt b/Documentation/git-mailsplit.txt index 66239a8f8..03a947766 100644 --- a/Documentation/git-mailsplit.txt +++ b/Documentation/git-mailsplit.txt @@ -7,7 +7,7 @@ git-mailsplit - Totally braindamaged mbox splitter program. SYNOPSIS -------- -'git-mailsplit' [-d] +'git-mailsplit' [-d] [] DESCRIPTION ----------- @@ -17,7 +17,8 @@ directory so you can process them further from there. OPTIONS ------- :: - Mbox file to split. + Mbox file to split. If not given, the mbox is read from + the standard input. :: Directory in which to place the individual messages. @@ -31,6 +32,8 @@ OPTIONS Author ------ Written by Linus Torvalds +and Junio C Hamano + Documentation -------------- diff --git a/mailsplit.c b/mailsplit.c index bd7c611bb..7981f87a7 100644 --- a/mailsplit.c +++ b/mailsplit.c @@ -14,25 +14,10 @@ #include #include #include +#include "cache.h" -static int usage(void) -{ - fprintf(stderr, "git-mailsplit [-d] \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] [] "; static int is_from_line(const char *line, int len) { @@ -65,88 +50,110 @@ 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) { - unsigned long offset; + FILE *output = NULL; + int len = strlen(buf); + int fd; + int status = 0; - if (size < 6 || memcmp("From ", map, 5)) + if (!is_from_line(buf, len)) 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_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, nr_prec = 4; - struct stat st; - unsigned long size; - void *map; - - if (argc == 4 && !strncmp(argv[1], "-d", 2)) { - nr_prec = strtol(argv[1] + 2, NULL, 10); - if (nr_prec < 3 || 10 <= nr_prec) - usage(); - argc--; argv++; - } - 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 i, nr, nr_prec = 4; + FILE *mbox = NULL; + + for (i = 1; i < argc; i++) { + const char *arg = argv[i]; + + if (arg[0] != '-') + break; + /* do flags here */ + if (!strncmp(arg, "-d", 2)) { + nr_prec = strtol(arg + 2, NULL, 10); + if (nr_prec < 3 || 10 <= nr_prec) + usage(git_mailsplit_usage); + continue; + } } - size = st.st_size; - map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); - if (map == MAP_FAILED) { - perror("mmap"); - close(fd); - exit(1); + + /* Either one remaining arg (dir), or two (mbox and dir) */ + switch (argc - i) { + case 1: + mbox = stdin; + break; + case 2: + if ((mbox = fopen(argv[i], "r")) == NULL) + die("cannot open mbox %s for reading", argv[i]); + break; + default: + usage(git_mailsplit_usage); } - close(fd); + if (chdir(argv[argc - 1]) < 0) + usage(git_mailsplit_usage); + nr = 0; - do { + if (fgets(buf, sizeof(buf), mbox) == NULL) + die("cannot read mbox"); + + for (;;) { char name[10]; - unsigned long len = parse_email(map, size); - assert(len <= size); + sprintf(name, "%0*d", nr_prec, ++nr); - fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666); - if (fd < 0) { - perror(name); - exit(1); - } - if (write(fd, map, len) != len) { - perror("write"); + switch (split_one(mbox, name)) { + case 0: + break; + case 1: + printf("%d\n", nr); + return 0; + default: exit(1); } - close(fd); - map += len; - size -= len; - } while (size > 0); - printf("%d\n", nr); - return 0; + } } -- 2.26.2