99
int split_mbox(const char **mbox, const char *dir, int allow_bare, int nr_prec, int skip)
101
char *name = xmalloc(strlen(dir) + 2 + 3 * sizeof(skip));
100
static int populate_maildir_list(struct path_list *list, const char *path)
105
if ((dir = opendir(path)) == NULL) {
106
error("cannot opendir %s (%s)", path, strerror(errno));
110
while ((dent = readdir(dir)) != NULL) {
111
if (dent->d_name[0] == '.')
113
path_list_insert(dent->d_name, list);
121
static int split_maildir(const char *maildir, const char *dir,
122
int nr_prec, int skip)
125
char curdir[PATH_MAX];
105
const char *file = *mbox++;
106
FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
110
error("cannot open mbox %s", file);
129
struct path_list list = {NULL, 0, 0, 1};
131
snprintf(curdir, sizeof(curdir), "%s/cur", maildir);
132
if (populate_maildir_list(&list, curdir) < 0)
135
for (i = 0; i < list.nr; i++) {
137
snprintf(file, sizeof(file), "%s/%s", curdir, list.items[i].path);
138
f = fopen(file, "r");
140
error("cannot open mail %s (%s)", file, strerror(errno));
114
144
if (fgets(buf, sizeof(buf), f) == NULL) {
116
break; /* empty stdin is OK */
145
error("cannot read mail %s (%s)", file, strerror(errno));
149
sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
150
split_one(f, name, 1);
155
path_list_clear(&list, 1);
162
static int split_mbox(const char *file, const char *dir, int allow_bare,
163
int nr_prec, int skip)
168
FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
172
error("cannot open mbox %s", file);
176
if (fgets(buf, sizeof(buf), f) == NULL) {
177
/* empty stdin is OK */
117
179
error("cannot read mbox %s", file);
122
sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
123
file_done = split_one(f, name, allow_bare);
186
sprintf(name, "%s/%0*d", dir, nr_prec, ++skip);
187
file_done = split_one(f, name, allow_bare);
134
198
int cmd_mailsplit(int argc, const char **argv, const char *prefix)
136
int nr = 0, nr_prec = 4, ret;
200
int nr = 0, nr_prec = 4, num = 0;
137
201
int allow_bare = 0;
138
202
const char *dir = NULL;
139
203
const char **argp;
186
250
argp = stdin_only;
189
ret = split_mbox(argp, dir, allow_bare, nr_prec, nr);
254
const char *arg = *argp++;
258
if (arg[0] == '-' && arg[1] == 0) {
259
ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
261
error("cannot split patches from stdin");
269
if (stat(arg, &argstat) == -1) {
270
error("cannot stat %s (%s)", arg, strerror(errno));
274
if (S_ISDIR(argstat.st_mode))
275
ret = split_maildir(arg, dir, nr_prec, nr);
277
ret = split_mbox(arg, dir, allow_bare, nr_prec, nr);
280
error("cannot split patches from %s", arg);