mailsplit.con commit Merge branch 'jc/status' (94c6eb3)
   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 "cache.h"
  15
  16static const char git_mailsplit_usage[] =
  17"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
  18
  19static int is_from_line(const char *line, int len)
  20{
  21        const char *colon;
  22
  23        if (len < 20 || memcmp("From ", line, 5))
  24                return 0;
  25
  26        colon = line + len - 2;
  27        line += 5;
  28        for (;;) {
  29                if (colon < line)
  30                        return 0;
  31                if (*--colon == ':')
  32                        break;
  33        }
  34
  35        if (!isdigit(colon[-4]) ||
  36            !isdigit(colon[-2]) ||
  37            !isdigit(colon[-1]) ||
  38            !isdigit(colon[ 1]) ||
  39            !isdigit(colon[ 2]))
  40                return 0;
  41
  42        /* year */
  43        if (strtol(colon+3, NULL, 10) <= 90)
  44                return 0;
  45
  46        /* Ok, close enough */
  47        return 1;
  48}
  49
  50/* Could be as small as 64, enough to hold a Unix "From " line. */
  51static char buf[4096];
  52
  53/* Called with the first line (potentially partial)
  54 * already in buf[] -- normally that should begin with
  55 * the Unix "From " line.  Write it into the specified
  56 * file.
  57 */
  58static int split_one(FILE *mbox, const char *name, int allow_bare)
  59{
  60        FILE *output = NULL;
  61        int len = strlen(buf);
  62        int fd;
  63        int status = 0;
  64        int is_bare = !is_from_line(buf, len);
  65
  66        if (is_bare && !allow_bare)
  67                goto corrupt;
  68
  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");
  73
  74        /* Copy it out, while searching for a line that begins with
  75         * "From " and having something that looks like a date format.
  76         */
  77        for (;;) {
  78                int is_partial = (buf[len-1] != '\n');
  79
  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_bare && 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);
 101        fprintf(stderr, "corrupt mailbox\n");
 102        exit(1);
 103}
 104
 105int main(int argc, const char **argv)
 106{
 107        int nr = 0, nr_prec = 4;
 108        int allow_bare = 0;
 109        const char *dir = NULL;
 110        const char **argp;
 111        static const char *stdin_only[] = { "-", NULL };
 112        char *name;
 113
 114        for (argp = argv+1; *argp; argp++) {
 115                const char *arg = *argp;
 116
 117                if (arg[0] != '-')
 118                        break;
 119                /* do flags here */
 120                if ( arg[1] == 'd' ) {
 121                        nr_prec = strtol(arg+2, NULL, 10);
 122                        if (nr_prec < 3 || 10 <= nr_prec)
 123                                usage(git_mailsplit_usage);
 124                        continue;
 125                } else if ( arg[1] == 'f' ) {
 126                        nr = strtol(arg+2, NULL, 10);
 127                } else if ( arg[1] == 'b' && !arg[2] ) {
 128                        allow_bare = 1;
 129                } else if ( arg[1] == 'o' && arg[2] ) {
 130                        dir = arg+2;
 131                } else if ( arg[1] == '-' && !arg[2] ) {
 132                        argp++; /* -- marks end of options */
 133                        break;
 134                } else {
 135                        die("unknown option: %s", arg);
 136                }
 137        }
 138
 139        if ( !dir ) {
 140                /* Backwards compatibility: if no -o specified, accept
 141                   <mbox> <dir> or just <dir> */
 142                switch (argc - (argp-argv)) {
 143                case 1:
 144                        dir = argp[0];
 145                        argp = stdin_only;
 146                        break;
 147                case 2:
 148                        stdin_only[0] = argp[0];
 149                        dir = argp[1];
 150                        argp = stdin_only;
 151                        break;
 152                default:
 153                        usage(git_mailsplit_usage);
 154                }
 155        } else {
 156                /* New usage: if no more argument, parse stdin */
 157                if ( !*argp )
 158                        argp = stdin_only;
 159        }
 160
 161        name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr));
 162
 163        while (*argp) {
 164                const char *file = *argp++;
 165                FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "rt");
 166                int file_done = 0;
 167
 168                if ( !f )
 169                        die ("cannot open mbox %s", file);
 170
 171                if (fgets(buf, sizeof(buf), f) == NULL) {
 172                        if (f == stdin)
 173                                break; /* empty stdin is OK */
 174                        die("cannot read mbox %s", file);
 175                }
 176
 177                while (!file_done) {
 178                        sprintf(name, "%s/%0*d", dir, nr_prec, ++nr);
 179                        file_done = split_one(f, name, allow_bare);
 180                }
 181
 182                if (f != stdin)
 183                        fclose(f);
 184        }
 185
 186        printf("%d\n", nr);
 187        return 0;
 188}