summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>2011-03-19 19:33:15 +0100
committerJunio C Hamano <gitster@pobox.com>2011-03-19 21:46:52 -0700
commitc41dd2fd7d3a16e6f0b1629d688bee3240db496c (patch)
tree77348580064caf5b871624a98f007450627bb42d /builtin
parentaf4c62ae88d403a417ba7c2b879eca10e7bff8f4 (diff)
downloadgit-c41dd2fd7d3a16e6f0b1629d688bee3240db496c.tar.gz
grep: read patterns from stdin with -f -
Support the well-know convention of reading standard input instead of a named file if "-" (dash) is specified. GNU grep does the same. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/grep.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/builtin/grep.c b/builtin/grep.c
index eaf8560a52..0bf8c0116a 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -659,11 +659,12 @@ static int context_callback(const struct option *opt, const char *arg,
static int file_callback(const struct option *opt, const char *arg, int unset)
{
struct grep_opt *grep_opt = opt->value;
+ int from_stdin = !strcmp(arg, "-");
FILE *patterns;
int lno = 0;
struct strbuf sb = STRBUF_INIT;
- patterns = fopen(arg, "r");
+ patterns = from_stdin ? stdin : fopen(arg, "r");
if (!patterns)
die_errno("cannot open '%s'", arg);
while (strbuf_getline(&sb, patterns, '\n') == 0) {
@@ -677,7 +678,8 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
s = strbuf_detach(&sb, &len);
append_grep_pat(grep_opt, s, len, arg, ++lno, GREP_PATTERN);
}
- fclose(patterns);
+ if (!from_stdin)
+ fclose(patterns);
strbuf_release(&sb);
return 0;
}