summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorcheloha <cheloha@openbsd.org>2020-12-04 01:42:05 +0000
committercheloha <cheloha@openbsd.org>2020-12-04 01:42:05 +0000
commit2d7eb85656337cac88f09f1628b941f37e8f7b4a (patch)
tree00ffa426f1556c0e82af1a410fa0e28ef4e92414 /bin
parentadd mvkpcie; (diff)
downloadwireguard-openbsd-2d7eb85656337cac88f09f1628b941f37e8f7b4a.tar.xz
wireguard-openbsd-2d7eb85656337cac88f09f1628b941f37e8f7b4a.zip
cat(1): simplify argv processing loops
cook_args() and raw_args() do some peculiar things in order to avoid calling cook_buf() and raw_cat(), respectively, in more than one place. The result is a convoluted. If we isolate the special cases from the normal case and just call these functions in multiple places the loops will be easier to read. Three things: 1. Pull the no-args case out of the loop. If *argv is NULL when we get into the argv processing function we just want to operate on the standard input and return early. It makes no sense to handle this case *in* the loop. 2. Isolate the "-" case from the filename case. If *argv is "-" we want to operate on the standard input. We can then do any stdin-specific cleanup within the same branch, which makes it easier to understand both the "-" case and the normal filename case. This also allows us to remove the 'filename' intermediate variable from both argv processing functions. 3. While we're here, use a for-loop and iterate argv in the loop header. Now argv is incremented in one place. ok martijn@
Diffstat (limited to 'bin')
-rw-r--r--bin/cat/cat.c75
1 files changed, 36 insertions, 39 deletions
diff --git a/bin/cat/cat.c b/bin/cat/cat.c
index a602e0f1581..fa35136a250 100644
--- a/bin/cat/cat.c
+++ b/bin/cat/cat.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cat.c,v 1.28 2020/12/03 22:37:12 cheloha Exp $ */
+/* $OpenBSD: cat.c,v 1.29 2020/12/04 01:42:05 cheloha Exp $ */
/* $NetBSD: cat.c,v 1.11 1995/09/07 06:12:54 jtc Exp $ */
/*
@@ -107,29 +107,27 @@ main(int argc, char *argv[])
void
cook_args(char **argv)
{
- char *filename;
FILE *fp;
- fp = stdin;
- filename = "stdin";
- do {
- if (*argv) {
- if (!strcmp(*argv, "-"))
- fp = stdin;
- else if ((fp = fopen(*argv, "r")) == NULL) {
- warn("%s", *argv);
- rval = 1;
- ++argv;
- continue;
- }
- filename = *argv++;
+ if (*argv == NULL) {
+ cook_buf(stdin, "stdin");
+ return;
+ }
+
+ for (; *argv != NULL; argv++) {
+ if (!strcmp(*argv, "-")) {
+ cook_buf(stdin, "stdin");
+ clearerr(stdin);
+ continue;
+ }
+ if ((fp = fopen(*argv, "r")) == NULL) {
+ warn("%s", *argv);
+ rval = 1;
+ continue;
}
- cook_buf(fp, filename);
- if (fp == stdin)
- clearerr(fp);
- else
- (void)fclose(fp);
- } while (*argv);
+ cook_buf(fp, *argv);
+ fclose(fp);
+ }
}
void
@@ -198,27 +196,26 @@ cook_buf(FILE *fp, const char *filename)
void
raw_args(char **argv)
{
- char *filename;
int fd;
- fd = fileno(stdin);
- filename = "stdin";
- do {
- if (*argv) {
- if (!strcmp(*argv, "-"))
- fd = fileno(stdin);
- else if ((fd = open(*argv, O_RDONLY, 0)) == -1) {
- warn("%s", *argv);
- rval = 1;
- ++argv;
- continue;
- }
- filename = *argv++;
+ 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, 0)) == -1) {
+ warn("%s", *argv);
+ rval = 1;
+ continue;
}
- raw_cat(fd, filename);
- if (fd != fileno(stdin))
- (void)close(fd);
- } while (*argv);
+ raw_cat(fd, *argv);
+ close(fd);
+ }
}
void