On Tue, Feb 01, 2022 at 01:33:19PM -0700, Todd C. Miller wrote:
> On Tue, 01 Feb 2022 09:59:51 -0600, Scott Cheloha wrote:
>
> > To recap:
> >
> > - Refactor the open/close portions of cook_args() and raw_args() into a
> > single function, cat_file().
> >
> > - Push the flag-check branch in main() down into cat_file().
> >
> > - Pull the argv loop in cat_file() up into main().
> >
> > Once we've done that, we can then:
> >
> > - Drop the "rpath" promise in the no-file case in main().
> >
> > Any objections?
>
> No objection from me.
Whoops, introduced a bug in that patch. Matthew Martin points out
off-list that STDIN_FILENO isn't necessarily the standard input. Like
this:
$ cat <&- foo
So, I guess we just double up on the calls to cook_buf() and
raw_cat(). This is closer to how the code was before. Not thrilled,
but I don't see a simpler way to do it.
Index: cat.c
===================================================================
RCS file: /cvs/src/bin/cat/cat.c,v
retrieving revision 1.32
diff -u -p -r1.32 cat.c
--- cat.c 24 Oct 2021 21:24:21 -0000 1.32
+++ cat.c 1 Feb 2022 23:22:19 -0000
@@ -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,54 @@ 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) {
+ if (pledge("stdio", NULL) == -1)
+ err(1, "pledge");
+
+ 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);
}
}
@@ -191,31 +204,6 @@ cook_buf(FILE *fp, const char *filename)
}
if (ferror(stdout))
err(1, "stdout");
-}
-
-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