Interpret "-" in filenames as standard input

This commit is contained in:
Pierre Pronchery 2017-01-31 22:14:30 +01:00
parent 77b2d9ec05
commit e31adeb8c9

View File

@ -86,24 +86,31 @@ static int _grep_files(Prefs * prefs, regex_t * reg, int filec, char * filev[])
{ {
int ret = 1; int ret = 1;
int i; int i;
char const * filename;
FILE * fp; FILE * fp;
char buf[128]; char buf[128];
for(i = 0; i < filec; i++) for(i = 0; i < filec; i++)
{ {
if((fp = fopen(filev[i], "r")) == NULL) if(strcmp(filev[i], "-") == 0)
{
filename = NULL;
fp = stdin;
}
else if((fp = fopen(filev[i], "r")) == NULL)
{ {
snprintf(buf, sizeof(buf), "%s: %s", filev[i], snprintf(buf, sizeof(buf), "%s: %s", filev[i],
strerror(errno)); strerror(errno));
ret = _grep_error(buf, 2); ret = _grep_error(buf, 2);
continue; continue;
} }
if(_grep_stream(prefs, reg, fp, (filec > 1) ? filev[i] : NULL) else
== 0 && ret == 1) filename = (filec == 1) ? "" : filev[i];
if(_grep_stream(prefs, reg, fp, filename) == 0 && ret == 1)
ret = 0; ret = 0;
if(fclose(fp) != 0) if(filename != NULL && fclose(fp) != 0)
{ {
snprintf(buf, sizeof(buf), "%s: %s", filev[i], snprintf(buf, sizeof(buf), "%s: %s", filename,
strerror(errno)); strerror(errno));
ret = _grep_error(buf, 2); ret = _grep_error(buf, 2);
} }
@ -125,7 +132,7 @@ static int _grep_stream(Prefs * prefs, regex_t * reg, FILE * fp,
{ {
if(prefs != NULL && !(*prefs & GREP_PREFS_q)) if(prefs != NULL && !(*prefs & GREP_PREFS_q))
{ {
if(filename != NULL) if(filename != NULL && filename[0] != '\0')
printf("%s:", filename); printf("%s:", filename);
if(prefs != NULL && *prefs & GREP_PREFS_n) if(prefs != NULL && *prefs & GREP_PREFS_n)
printf("%zd:", line); printf("%zd:", line);