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