Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <stdio.h> |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 14 | #include <assert.h> |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 15 | #include "cache.h" |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 16 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 17 | static const char git_mailsplit_usage[] = |
| 18 | "git-mailsplit [-d<prec>] [<mbox>] <directory>"; |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 19 | |
| 20 | static int is_from_line(const char *line, int len) |
| 21 | { |
| 22 | const char *colon; |
| 23 | |
| 24 | if (len < 20 || memcmp("From ", line, 5)) |
| 25 | return 0; |
| 26 | |
| 27 | colon = line + len - 2; |
| 28 | line += 5; |
| 29 | for (;;) { |
| 30 | if (colon < line) |
| 31 | return 0; |
| 32 | if (*--colon == ':') |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | if (!isdigit(colon[-4]) || |
| 37 | !isdigit(colon[-2]) || |
| 38 | !isdigit(colon[-1]) || |
| 39 | !isdigit(colon[ 1]) || |
| 40 | !isdigit(colon[ 2])) |
| 41 | return 0; |
| 42 | |
| 43 | /* year */ |
| 44 | if (strtol(colon+3, NULL, 10) <= 90) |
| 45 | return 0; |
| 46 | |
| 47 | /* Ok, close enough */ |
| 48 | return 1; |
| 49 | } |
| 50 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 51 | /* Could be as small as 64, enough to hold a Unix "From " line. */ |
| 52 | static char buf[4096]; |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 53 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 54 | /* Called with the first line (potentially partial) |
| 55 | * already in buf[] -- normally that should begin with |
| 56 | * the Unix "From " line. Write it into the specified |
| 57 | * file. |
| 58 | */ |
| 59 | static int split_one(FILE *mbox, const char *name) |
| 60 | { |
| 61 | FILE *output = NULL; |
| 62 | int len = strlen(buf); |
| 63 | int fd; |
| 64 | int status = 0; |
| 65 | |
| 66 | if (!is_from_line(buf, len)) |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 67 | goto corrupt; |
| 68 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 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"); |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 73 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 74 | /* Copy it out, while searching for a line that begins with |
| 75 | * "From " and having something that looks like a date format. |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 76 | */ |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 77 | for (;;) { |
| 78 | int is_partial = (buf[len-1] != '\n'); |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 79 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 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); |
| 91 | if (!is_partial && is_from_line(buf, len)) |
| 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); |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 101 | fprintf(stderr, "corrupt mailbox\n"); |
| 102 | exit(1); |
| 103 | } |
| 104 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 105 | int main(int argc, const char **argv) |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 106 | { |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 107 | int i, nr, nr_prec = 4; |
| 108 | FILE *mbox = NULL; |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 109 | |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 110 | for (i = 1; i < argc; i++) { |
| 111 | const char *arg = argv[i]; |
| 112 | |
| 113 | if (arg[0] != '-') |
| 114 | break; |
| 115 | /* do flags here */ |
| 116 | if (!strncmp(arg, "-d", 2)) { |
| 117 | nr_prec = strtol(arg + 2, NULL, 10); |
| 118 | if (nr_prec < 3 || 10 <= nr_prec) |
| 119 | usage(git_mailsplit_usage); |
| 120 | continue; |
| 121 | } |
Junio C Hamano | e11fc02 | 2005-10-06 14:25:52 -0700 | [diff] [blame] | 122 | } |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 123 | |
| 124 | /* Either one remaining arg (dir), or two (mbox and dir) */ |
| 125 | switch (argc - i) { |
| 126 | case 1: |
| 127 | mbox = stdin; |
| 128 | break; |
| 129 | case 2: |
| 130 | if ((mbox = fopen(argv[i], "r")) == NULL) |
| 131 | die("cannot open mbox %s for reading", argv[i]); |
| 132 | break; |
| 133 | default: |
| 134 | usage(git_mailsplit_usage); |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 135 | } |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 136 | if (chdir(argv[argc - 1]) < 0) |
| 137 | usage(git_mailsplit_usage); |
| 138 | |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 139 | nr = 0; |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 140 | if (fgets(buf, sizeof(buf), mbox) == NULL) |
| 141 | die("cannot read mbox"); |
| 142 | |
| 143 | for (;;) { |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 144 | char name[10]; |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 145 | |
Junio C Hamano | e11fc02 | 2005-10-06 14:25:52 -0700 | [diff] [blame] | 146 | sprintf(name, "%0*d", nr_prec, ++nr); |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 147 | switch (split_one(mbox, name)) { |
| 148 | case 0: |
| 149 | break; |
| 150 | case 1: |
| 151 | printf("%d\n", nr); |
| 152 | return 0; |
| 153 | default: |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 154 | exit(1); |
| 155 | } |
Junio C Hamano | 8b73edf | 2005-10-06 15:55:43 -0700 | [diff] [blame] | 156 | } |
Linus Torvalds | 2744b23 | 2005-04-11 23:46:50 -0700 | [diff] [blame] | 157 | } |