From: girst Date: Wed, 8 Feb 2017 13:24:59 +0000 (+0100) Subject: cleaned up debugcode X-Git-Url: https://git.gir.st/forkaftergrep.git/commitdiff_plain/ff6565ef2736b58f149c31cffdf1f8b0cf076d9c cleaned up debugcode --- diff --git a/Makefile b/Makefile index 3186a28..afaf910 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: fag -fag: +fag: fag.c fag.h gcc -std=c99 -Wall -Werror -Wextra fag.c -o fag clean: diff --git a/fag.c b/fag.c index e0218c3..aed1836 100644 --- a/fag.c +++ b/fag.c @@ -13,14 +13,14 @@ #include #include "fag.h" -struct opt opts = {0, 0, NULL, NULL, STDOUT_FILENO}; +struct opt opts = {0, 0, 0, NULL, NULL, STDOUT_FILENO}; int main (int argc, char** argv) { int opt; opterr = 0; /* the `+' forces getopt to stop at the first non-option */ - while ((opt = getopt (argc, argv, "+t:k::ehv")) != -1) { + while ((opt = getopt (argc, argv, "+t:k::eVhv")) != -1) { switch (opt) { case 't': opts.timeout = atoi (optarg); @@ -31,6 +31,9 @@ int main (int argc, char** argv) { case 'e': opts.stream = STDERR_FILENO; break; + case 'V': + opts.verbose = 1; + break; case 'h': fprintf (stderr, VERTEXT USAGE "Options:\n" @@ -69,10 +72,6 @@ int main (int argc, char** argv) { } int fork_after_grep (struct opt opts) { - printf ("timeout:\t%d\n" "kill_sig:\t%d\n" "pattern: \t%s\n" "stream: \t%d\n" "program: \t" - , opts.timeout, opts.kill_sig, opts.pattern, opts.stream); - for (char** p = opts.argv; *p;) printf ("%s ", *p++); putchar ('\n'); - int pipefd[2]; pid_t cpid; int status; @@ -116,6 +115,7 @@ int fork_after_grep (struct opt opts) { for (;;) { usleep (20000); + memset (buf, 0, BUF_SIZE); nbytes = read (pipefd[0], buf, BUF_SIZE); if (nbytes == -1) { switch (errno) { @@ -136,6 +136,9 @@ int fork_after_grep (struct opt opts) { } return EX_UNAVAILABLE; } + if (op.verbose) { + fputs (buf, opts.stream==STDERR_FILENO?stderr:stdout); + } if (strstr (buf, opts.pattern) != NULL) { printf ("%d\n", cpid); /* create a new child to keep pipe alive (will exit with exec'd program) */ @@ -143,12 +146,10 @@ int fork_after_grep (struct opt opts) { while (kill(cpid, 0) != -1 && errno != ESRCH ) sleep (1); close (pipefd[0]); close (pipefd[1]); -//close(0);close(1);close(2); _exit(0); } close (pipefd[0]); close (pipefd[1]); -//close(0);close(1);close(2); return EX_OK; } diff --git a/fag.h b/fag.h index cda06cf..80d0118 100644 --- a/fag.h +++ b/fag.h @@ -15,6 +15,7 @@ struct opt { int timeout; int kill_sig; + int verbose; char* pattern; char** argv; int stream;