summaryrefslogtreecommitdiff
path: root/mailsplit.c
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2005-10-06 22:55:43 (GMT)
committerJunio C Hamano <junkio@cox.net>2005-10-06 22:55:43 (GMT)
commit8b73edf498adf2895af7ff9c750283cf9325a632 (patch)
tree8cf0bb652ffde36e57bca5d6f33287305a61bb74 /mailsplit.c
parent54ba6013b4eaff0d96f9ab96cf225ca392b08a30 (diff)
downloadgit-8b73edf498adf2895af7ff9c750283cf9325a632.zip
git-8b73edf498adf2895af7ff9c750283cf9325a632.tar.gz
git-8b73edf498adf2895af7ff9c750283cf9325a632.tar.bz2
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 <junkio@cox.net>
Diffstat (limited to 'mailsplit.c')
-rw-r--r--mailsplit.c175
1 files changed, 91 insertions, 84 deletions
diff --git a/mailsplit.c b/mailsplit.c
index bd7c611..7981f87 100644
--- a/mailsplit.c
+++ b/mailsplit.c
@@ -14,25 +14,10 @@
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
+#include "cache.h"
-static int usage(void)
-{
- fprintf(stderr, "git-mailsplit [-d<prec>] <mbox> <directory>\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<prec>] [<mbox>] <directory>";
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;
+ }
}