Skip to content

Commit

Permalink
Exit with a non-zero exit status if a given file does not exist
Browse files Browse the repository at this point in the history
Sadly, the diff is quite large as we will have to change the API
of `blaze822_loop` to differentiate the number of processed messages
and a status code indicating a missing mail. Note that currently
execution is not aborted upon encountering the first non-existent
mail; hence, we can't do both via the return value.

Surprisingly, very few utilities actually use the number of processed
messages as return by `blaze822_loop`. Therefore, we could reduce the
diff quite a bit by differentiating the two use cases through separate
function (e.g. `blaze822_loop` and `blaze822_loop_num`), which would
allow the API of blaze822_loop to remain largely unchanged.

The new `blaze822_loop` API would also allow us to improve error
handling for blaze822_seq_next but I haven't done this so far.

There are also some tests, but these should be expanded.

Fixes leahneukirchen#264
  • Loading branch information
nmeum committed Oct 8, 2024
1 parent c087993 commit 900863b
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 80 deletions.
4 changes: 2 additions & 2 deletions blaze822.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ struct blaze822_seq_iter {
};

char *blaze822_seq_next(char *map, char *range, struct blaze822_seq_iter *iter);
long blaze822_loop(int, char **, void (*)(char *));
long blaze822_loop1(char *arg, void (*cb)(char *));
int blaze822_loop(long *num, int, char **, void (*)(char *));
int blaze822_loop1(long *num, char *arg, void (*cb)(char *));
char *blaze822_home_file(char *basename);

// filter.c
Expand Down
6 changes: 2 additions & 4 deletions maddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ main(int argc, char *argv[])
xpledge("stdio rpath", "");

if (argc == optind && isatty(0))
blaze822_loop1(":", addr);
return blaze822_loop1(NULL, ":", addr);
else
blaze822_loop(argc-optind, argv+optind, addr);

return 0;
return blaze822_loop(NULL, argc-optind, argv+optind, addr);
}
4 changes: 2 additions & 2 deletions magrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ main(int argc, char *argv[])
}

if (argc == optind && isatty(0))
blaze822_loop1(":", magrep);
blaze822_loop1(NULL, ":", magrep);
else
blaze822_loop(argc-optind, argv+optind, magrep);
blaze822_loop(NULL, argc-optind, argv+optind, magrep);

if (cflag && !qflag && !mflag)
printf("%ld\n", matches);
Expand Down
4 changes: 1 addition & 3 deletions mdeliver.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,7 @@ main(int argc, char *argv[])
if (argc == optind + 1 && isatty(0))
goto usage;
else
blaze822_loop(argc - 1 - optind, argv + optind, refile);

return 0;
return blaze822_loop(NULL, argc - 1 - optind, argv + optind, refile);
}

int c;
Expand Down
4 changes: 2 additions & 2 deletions mexport.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ main(int argc, char *argv[])
xpledge("stdio rpath", "");

if (argc == optind && isatty(0))
blaze822_loop1(":", export);
status |= blaze822_loop1(NULL, ":", export);
else
blaze822_loop(argc-optind, argv+optind, export);
status |= blaze822_loop(NULL, argc-optind, argv+optind, export);

return status;
}
20 changes: 9 additions & 11 deletions mflag.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,29 @@ main(int argc, char *argv[])

if (vflag) {
if (argc == optind && !isatty(0)) {
blaze822_loop(0, 0, flag); // read from stdin
return 0;
return blaze822_loop(NULL, 0, 0, flag); // read from stdin
}

args = calloc(argsalloc, sizeof (char *));
if (!args)
exit(-1);

int status;
if (argc == optind)
blaze822_loop1(".", add);
status = blaze822_loop1(NULL, ".", add);
else
blaze822_loop(argc-optind, argv+optind, add);
status = blaze822_loop(NULL, argc-optind, argv+optind, add);

if (isatty(0))
blaze822_loop1(":", flag);
status |= blaze822_loop1(NULL, ":", flag);
else
blaze822_loop(0, 0, flag);
status |= blaze822_loop(NULL, 0, 0, flag);

return 0;
return status;
}

if (argc == optind && isatty(0))
blaze822_loop1(".", flag);
return blaze822_loop1(NULL, ".", flag);
else
blaze822_loop(argc-optind, argv+optind, flag);

return 0;
return blaze822_loop(NULL, argc-optind, argv+optind, flag);
}
4 changes: 2 additions & 2 deletions mhdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ main(int argc, char *argv[])
xpledge("stdio rpath", "");

if (argc == optind && isatty(0))
blaze822_loop1(".", header);
status |= blaze822_loop1(NULL, ".", header);
else
blaze822_loop(argc-optind, argv+optind, header);
status |= blaze822_loop(NULL, argc-optind, argv+optind, header);

return status;
}
2 changes: 1 addition & 1 deletion minc.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ main(int argc, char *argv[])
if (optind == argc) {
if (isatty(0))
goto usage;
blaze822_loop(0, 0, inc);
status = blaze822_loop(NULL, 0, 0, inc);
} else {
for (i = optind; i < argc; i++)
inc(argv[i]);
Expand Down
5 changes: 3 additions & 2 deletions mlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ main(int argc, char *argv[])
flagsum++;
}

int status = 0;
if (optind == argc) {
if (isatty(0))
goto usage;
blaze822_loop(0, 0, listarg);
status = blaze822_loop(NULL, 0, 0, listarg);
} else {
for (i = optind; i < argc; i++)
listarg(argv[i]);
Expand All @@ -300,5 +301,5 @@ main(int argc, char *argv[])
tunseen, tflagged, tcount);
}

return 0;
return status;
}
7 changes: 4 additions & 3 deletions mpick.c
Original file line number Diff line number Diff line change
Expand Up @@ -1464,11 +1464,12 @@ main(int argc, char *argv[])

xpledge("stdio rpath wpath cpath proc exec", 0);

int status;
void (*cb)(char *) = need_thr ? collect : oneline;
if (argc == optind && isatty(0))
i = blaze822_loop1(":", cb);
status = blaze822_loop1(&i, ":", cb);
else
i = blaze822_loop(argc-optind, argv+optind, cb);
status = blaze822_loop(&i, argc-optind, argv+optind, cb);

/* print and free last thread */
if (Tflag && thr)
Expand All @@ -1488,5 +1489,5 @@ main(int argc, char *argv[])
free(files);
}

return 0;
return status;
}
12 changes: 6 additions & 6 deletions mscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,9 @@ main(int argc, char *argv[])

if (nflag) {
if (argc == optind && isatty(0))
blaze822_loop1(":", numline);
return blaze822_loop1(NULL, ":", numline);
else
blaze822_loop(argc-optind, argv+optind, numline);
return 0;
return blaze822_loop(NULL, argc-optind, argv+optind, numline);
}

now = time(0);
Expand Down Expand Up @@ -623,16 +622,17 @@ main(int argc, char *argv[])
}

long i;
int status;
if (argc == optind && isatty(0))
i = blaze822_loop1(":", oneline);
status = blaze822_loop1(&i, ":", oneline);
else
i = blaze822_loop(argc-optind, argv+optind, oneline);
status = blaze822_loop(&i, argc-optind, argv+optind, oneline);

if (pager_pid > 0)
pipeclose(pager_pid);

if (vflag)
fprintf(stderr, "%ld mails scanned\n", i);

return 0;
return status;
}
6 changes: 2 additions & 4 deletions msed.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ main(int argc, char *argv[])
optind++;

if (argc == optind && isatty(0))
blaze822_loop1(".", sed);
return blaze822_loop1(NULL, ".", sed);
else
blaze822_loop(argc-optind, argv+optind, sed);

return 0;
return blaze822_loop(NULL, argc-optind, argv+optind, sed);
}
9 changes: 5 additions & 4 deletions mseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,13 @@ main(int argc, char *argv[])

xpledge("stdio rpath wpath cpath", "");

if (cflag)
blaze822_loop1(cflag, overridecur);
if (cflag) {
if (blaze822_loop1(NULL, cflag, overridecur))
return 1;
}

if (Cflag) {
blaze822_loop1(Cflag, setcur);
return 0;
return blaze822_loop1(NULL, Cflag, setcur);
}

if (Sflag && optind != argc) {
Expand Down
21 changes: 11 additions & 10 deletions mshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,13 @@ extract_cb(char *file)
blaze822_walk_mime(msg, 0, extract_mime);
}

void
int
extract(char *file, int argc, char **argv, int use_stdout)
{
extract_argc = argc;
extract_argv = argv;
extract_stdout = use_stdout;
blaze822_loop1(file, extract_cb);
return blaze822_loop1(NULL, file, extract_cb);
}

static char *newcur;
Expand Down Expand Up @@ -832,21 +832,22 @@ main(int argc, char *argv[])
}
}

int status = 0;
if (xflag) { // extract
xpledge("stdio rpath wpath cpath", NULL);
extract(xflag, argc-optind, argv+optind, 0);
status = extract(xflag, argc-optind, argv+optind, 0);
} else if (Oflag) { // extract to stdout
xpledge("stdio rpath", NULL);
extract(Oflag, argc-optind, argv+optind, 1);
status = extract(Oflag, argc-optind, argv+optind, 1);
} else if (tflag) { // list
xpledge("stdio rpath", NULL);
if (argc == optind && isatty(0))
blaze822_loop1(".", list);
status = blaze822_loop1(NULL, ".", list);
else
blaze822_loop(argc-optind, argv+optind, list);
status = blaze822_loop(NULL, argc-optind, argv+optind, list);
} else if (Rflag) { // render for reply
xpledge("stdio rpath", NULL);
blaze822_loop(argc-optind, argv+optind, reply);
status = blaze822_loop(NULL, argc-optind, argv+optind, reply);
} else { // show
/* XXX pledge: still r/w on the whole file-system + fork/exec */
if (!(qflag || rflag || Fflag)) {
Expand All @@ -857,9 +858,9 @@ main(int argc, char *argv[])
filters = blaze822(f);
}
if (argc == optind && isatty(0))
blaze822_loop1(".", show);
status = blaze822_loop1(NULL, ".", show);
else
blaze822_loop(argc-optind, argv+optind, show);
status = blaze822_loop(NULL, argc-optind, argv+optind, show);
if (!nflag) // don't set cur
if (newcur)
blaze822_seq_setcur(newcur);
Expand All @@ -875,5 +876,5 @@ main(int argc, char *argv[])
return 1;
}

return 0;
return status;
}
7 changes: 4 additions & 3 deletions msort.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,11 @@ main(int argc, char *argv[])
if (!mails)
exit(-1);

int status;
if (argc == optind && isatty(0))
blaze822_loop1(":", add);
status = blaze822_loop1(NULL, ":", add);
else
blaze822_loop(argc-optind, argv+optind, add);
status = blaze822_loop(NULL, argc-optind, argv+optind, add);

qsort(mails, idx, sizeof (struct mail), order);

Expand All @@ -337,5 +338,5 @@ main(int argc, char *argv[])
for (i = 0; i < idx; i++)
printf("%s\n", mails[i].file);

return 0;
return status;
}
9 changes: 5 additions & 4 deletions mthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ main(int argc, char *argv[])

while ((c = getopt(argc, argv, "S:prv")) != -1)
switch (c) {
case 'S': blaze822_loop1(optarg, thread); break;
case 'S': blaze822_loop1(NULL, optarg, thread); break;
case 'v': vflag = 1; break;
case 'p': pflag = 1; break;
case 'r': rflag = 1; break;
Expand All @@ -432,10 +432,11 @@ main(int argc, char *argv[])

optional = 0;

int status;
if (argc == optind && isatty(0))
blaze822_loop1(":", thread);
status = blaze822_loop1(NULL, ":", thread);
else
blaze822_loop(argc-optind, argv+optind, thread);
status = blaze822_loop(NULL, argc-optind, argv+optind, thread);

// the tree of all toplevel threads has depth -1,
// so toplevel threads have depth 0.
Expand All @@ -446,5 +447,5 @@ main(int argc, char *argv[])
sort_tree(top, -1);
print_tree(top, -1);

return 0;
return status;
}
Loading

0 comments on commit 900863b

Please sign in to comment.