cat,nl: fix handling of open errors

$ cat -n does_not_exist; echo $?
cat: does_not_exist: No such file or directory
1

function                                             old     new   delta
print_numbered_lines                                 118     129     +11
nl_main                                              196     201      +5
cat_main                                             421     425      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 20/0)               Total: 20 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-11-29 11:44:10 +01:00
parent 75a1c87357
commit c100535571
4 changed files with 18 additions and 7 deletions

View File

@ -195,6 +195,7 @@ int cat_main(int argc UNUSED_PARAM, char **argv)
# define CAT_OPT_b (1<<1) # define CAT_OPT_b (1<<1)
if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */ if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */
struct number_state ns; struct number_state ns;
int exitcode;
ns.width = 6; ns.width = 6;
ns.start = 1; ns.start = 1;
@ -203,10 +204,11 @@ int cat_main(int argc UNUSED_PARAM, char **argv)
ns.empty_str = "\n"; ns.empty_str = "\n";
ns.all = !(opts & CAT_OPT_b); /* -n without -b */ ns.all = !(opts & CAT_OPT_b); /* -n without -b */
ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */ ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */
exitcode = EXIT_SUCCESS;
do { do {
print_numbered_lines(&ns, *argv); exitcode |= print_numbered_lines(&ns, *argv);
} while (*++argv); } while (*++argv);
fflush_stdout_and_exit(EXIT_SUCCESS); fflush_stdout_and_exit(exitcode);
} }
/*opts >>= 2;*/ /*opts >>= 2;*/
#endif #endif

View File

@ -58,6 +58,8 @@ int nl_main(int argc UNUSED_PARAM, char **argv)
"number-width\0" Required_argument "w" "number-width\0" Required_argument "w"
; ;
#endif #endif
int exitcode;
ns.width = 6; ns.width = 6;
ns.start = 1; ns.start = 1;
ns.inc = 1; ns.inc = 1;
@ -72,9 +74,10 @@ int nl_main(int argc UNUSED_PARAM, char **argv)
if (!*argv) if (!*argv)
*--argv = (char*)"-"; *--argv = (char*)"-";
exitcode = EXIT_SUCCESS;
do { do {
print_numbered_lines(&ns, *argv); exitcode |= print_numbered_lines(&ns, *argv);
} while (*++argv); } while (*++argv);
fflush_stdout_and_exit(EXIT_SUCCESS); fflush_stdout_and_exit(exitcode);
} }

View File

@ -1386,7 +1386,7 @@ struct number_state {
const char *empty_str; const char *empty_str;
smallint all, nonempty; smallint all, nonempty;
}; };
void print_numbered_lines(struct number_state *ns, const char *filename) FAST_FUNC; int print_numbered_lines(struct number_state *ns, const char *filename) FAST_FUNC;
/* Networking */ /* Networking */

View File

@ -8,12 +8,16 @@
#include "libbb.h" #include "libbb.h"
void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename) int FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename)
{ {
FILE *fp = fopen_or_warn_stdin(filename); FILE *fp = fopen_or_warn_stdin(filename);
unsigned N = ns->start; unsigned N;
char *line; char *line;
if (!fp)
return EXIT_FAILURE;
N = ns->start;
while ((line = xmalloc_fgetline(fp)) != NULL) { while ((line = xmalloc_fgetline(fp)) != NULL) {
if (ns->all if (ns->all
|| (ns->nonempty && line[0]) || (ns->nonempty && line[0])
@ -27,4 +31,6 @@ void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filenam
ns->start = N; ns->start = N;
fclose(fp); fclose(fp);
return EXIT_SUCCESS;
} }