diff options
author | Scott Soule Cheloha <cheloha@cvs.openbsd.org> | 2022-02-09 01:56:29 +0000 |
---|---|---|
committer | Scott Soule Cheloha <cheloha@cvs.openbsd.org> | 2022-02-09 01:56:29 +0000 |
commit | fed07de9140c3c7c87d45acf4692f8b4e3c48c93 (patch) | |
tree | ea9c424a3fa880e4e9828f00019ffd7056e8f9f3 /bin | |
parent | 74e9118891273829fb1bb9de2373e38b9ec5d14c (diff) |
cat(1): refactor cook_args()/raw_args() into single function, cat_file()
- Combine the open/close portions of cook_args()/raw_args() into a single
function, cat_file().
- Push the flag-checking conditional in main() down into cat_file().
- Pull the argv loop in cat_file() up into main().
These changes -- especially pulling the argv look up into main() --
will allow us to drop the "rpath" promise in a single spot in a
subsequent patch.
Tweaked by mestre@. Descriptor leak in earlier version spotted by
Matthew Martin.
Thread: https://marc.info/?l=openbsd-tech&m=163941848104274&w=2
No objections on tech@ after several weeks.
Diffstat (limited to 'bin')
-rw-r--r-- | bin/cat/cat.c | 79 |
1 files changed, 32 insertions, 47 deletions
diff --git a/bin/cat/cat.c b/bin/cat/cat.c index 9dbda66186c..129c3696870 100644 --- a/bin/cat/cat.c +++ b/bin/cat/cat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cat.c,v 1.32 2021/10/24 21:24:21 deraadt Exp $ */ +/* $OpenBSD: cat.c,v 1.33 2022/02/09 01:56:28 cheloha Exp $ */ /* $NetBSD: cat.c,v 1.11 1995/09/07 06:12:54 jtc Exp $ */ /* @@ -50,9 +50,8 @@ int bflag, eflag, nflag, sflag, tflag, vflag; int rval; -void cook_args(char *argv[]); +void cat_file(const char *); void cook_buf(FILE *, const char *); -void raw_args(char *argv[]); void raw_cat(int, const char *); int @@ -92,40 +91,51 @@ main(int argc, char *argv[]) return 1; } } + argc -= optind; argv += optind; - if (bflag || eflag || nflag || sflag || tflag || vflag) - cook_args(argv); - else - raw_args(argv); + if (argc == 0) { + cat_file(NULL); + } else { + for (; *argv != NULL; argv++) + cat_file(*argv); + } if (fclose(stdout)) err(1, "stdout"); return rval; } void -cook_args(char **argv) +cat_file(const char *path) { FILE *fp; + int fd; - if (*argv == NULL) { - cook_buf(stdin, "stdin"); - return; - } - - for (; *argv != NULL; argv++) { - if (!strcmp(*argv, "-")) { + if (bflag || eflag || nflag || sflag || tflag || vflag) { + if (path == NULL || strcmp(path, "-") == 0) { cook_buf(stdin, "stdin"); clearerr(stdin); - continue; + } else { + if ((fp = fopen(path, "r")) == NULL) { + warn("%s", path); + rval = 1; + return; + } + cook_buf(fp, path); + fclose(fp); } - if ((fp = fopen(*argv, "r")) == NULL) { - warn("%s", *argv); - rval = 1; - continue; + } else { + if (path == NULL || strcmp(path, "-") == 0) { + raw_cat(STDIN_FILENO, "stdin"); + } else { + if ((fd = open(path, O_RDONLY)) == -1) { + warn("%s", path); + rval = 1; + return; + } + raw_cat(fd, path); + close(fd); } - cook_buf(fp, *argv); - fclose(fp); } } @@ -194,31 +204,6 @@ cook_buf(FILE *fp, const char *filename) } void -raw_args(char **argv) -{ - int fd; - - if (*argv == NULL) { - raw_cat(fileno(stdin), "stdin"); - return; - } - - for (; *argv != NULL; argv++) { - if (!strcmp(*argv, "-")) { - raw_cat(fileno(stdin), "stdin"); - continue; - } - if ((fd = open(*argv, O_RDONLY)) == -1) { - warn("%s", *argv); - rval = 1; - continue; - } - raw_cat(fd, *argv); - close(fd); - } -} - -void raw_cat(int rfd, const char *filename) { int wfd; |