2015-04-30 8:51 GMT+03:00 Martin Natano <nat...@natano.net>:
> grep reads from standard input when no files are specified. It also does
> so when -R is used, which doesn't really make sense. I think using the
> current working directory as a fallback when no directories are
> specified would make sense. POSIX says "If no file operands are
> specified, the standard input shall be used.", but -R is an extension
> to POSIX, so I guess it is not bound by that.
>
> Far too often I've started a grep -R and waiting for the output, only to
> recognize minutes later, that I forgot to add the '.' at the end of the
> command and it is reading from stdin.
>
> Any thoughts? OK?
>
> cheers,
> natano
>
> Index: grep.1
> ===================================================================
> RCS file: /cvs/src/usr.bin/grep/grep.1,v
> retrieving revision 1.43
> diff -u -p -r1.43 grep.1
> --- grep.1      13 Jan 2015 04:45:34 -0000      1.43
> +++ grep.1      29 Apr 2015 20:09:15 -0000
> @@ -238,6 +238,7 @@ will only search a file until a match ha
>  making searches potentially less expensive.
>  .It Fl R
>  Recursively search subdirectories listed.
> +If no directories are specified, the current working directory is used.
>  .It Fl s
>  Silent mode.
>  Nonexistent and unreadable files are ignored
> Index: grep.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/grep/grep.c,v
> retrieving revision 1.50
> diff -u -p -r1.50 grep.c
> --- grep.c      16 Mar 2015 13:27:59 -0000      1.50
> +++ grep.c      29 Apr 2015 20:09:15 -0000
> @@ -481,14 +481,17 @@ main(int argc, char *argv[])
>         if ((argc == 0 || argc == 1) && !Rflag && !Hflag)
>                 hflag = 1;
>
> -       if (argc == 0)
> -               exit(!procfile(NULL));
>
> -       if (Rflag)
> -               c = grep_tree(argv);
> -       else
> +       if (Rflag) {
> +               char *cwd[] = {".", NULL};
> +               c = grep_tree(argc ? argv : cwd);
> +       } else {
> +               if (argc == 0)
> +                       exit(!procfile(NULL));
> +
>                 for (c = 0; argc--; ++argv)
>                         c += procfile(*argv);
> +       }
>
>         exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
>  }

The -R is not "go through directories listed" option; it's a "go
through directories instead of ignoring them, if they are listed on
the command line". It's perfectly acceptable to list only files on
command line and still use -R option; it'll be just a no-op in this
case. Same applies to "no files listed on command line": -R is just a
no-op here.

Maybe some sentences in manual page could be reworded to make this
more clear, though.

--
  WBR,
  Vadim Zhukov

Reply via email to