From: girst Date: Sat, 12 Aug 2017 13:38:48 +0000 (+0200) Subject: fix grep bug (match found on exit); found new one X-Git-Url: https://git.gir.st/forkaftergrep.git/commitdiff_plain/b98f2a916dbd5d5f9960f095b4ac315679eed77e fix grep bug (match found on exit); found new one not closing grep's stderr fixed it. go figure. --- diff --git a/fag.1 b/fag.1 index 0c67e21..08a3a6a 100644 --- a/fag.1 +++ b/fag.1 @@ -66,9 +66,7 @@ if the error occured from within or in case the chid process exits prematurely, its exit code is inherited. Notably, \fI69\fP is returned when the timeout is reached. .SH BUGS .SS Known Bugs -if grep exits with a code > 0, fag should terminate with grep's exit code and an error message. instead, fag exits as if a match was found. -.PP -Sometimes, stdin behaves strange after the program terminates. +if grep gets killed (e.g. `killall grep'), fag should terminate. .SS Reporting Bugs Please report bugs and patches to the issue tracker at https://github.com/girst/forkaftergrep/. .SH NOTES diff --git a/fag.c b/fag.c index cbf886d..e8670b2 100644 --- a/fag.c +++ b/fag.c @@ -1,5 +1,6 @@ /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */ -//TODO: if grep exits with an error, fag thinks a match was found +//TODO: grep is missing `-e' and `-f' options +//TODO: killall grep not recognized #define _XOPEN_SOURCE 500 #define _DEFAULT_SOURCE @@ -154,10 +155,9 @@ int fork_after_grep (struct opt opts) { dup2 (grep_pipefd[0], STDIN_FILENO); close (grep_pipefd[0]); - close (STDERR_FILENO); close (STDOUT_FILENO); - execlp ("grep", "grep", opts.grepopt, opts.pattern, NULL); + execlp ("grep", "grep", opts.grepopt, "--", opts.pattern, NULL); fprintf (stderr, "exec error (grep): %s", strerror (errno)); _exit (EX_SOFTWARE); } else { @@ -194,7 +194,6 @@ int fork_after_grep (struct opt opts) { write(grep_pipefd[1], buf, nbytes); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */ } - // TODO: exits with `0' even if `grep' exits with code > 0 ! if (waitpid (grep_cpid, &grep_status, WNOHANG) > 0 && WIFEXITED (grep_status)) { close (grep_pipefd[1]);