[pmg-devel] [PATCH pmg-log-tracker 2/8] add -i option for specifying an external file

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Feb 20 20:11:27 CET 2019


On 2/20/19 2:10 PM, Dominik Csapak wrote:
> where '-' is stdin
> 
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
>  pmg-log-tracker.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/pmg-log-tracker.c b/pmg-log-tracker.c
> index 8d5e02f..e0f03f9 100644
> --- a/pmg-log-tracker.c
> +++ b/pmg-log-tracker.c
> @@ -1766,6 +1766,7 @@ main (int argc, char * const argv[])
>  {
>    char linebuf[linebufsize];
>    char *line;
> +  char *inputfile = NULL;
>  
>    const char *text;
>    const char *idx1;
> @@ -1799,7 +1800,7 @@ main (int argc, char * const argv[])
>      exit (-1);
>    }
>  
> -  while ((opt = getopt (argc, argv, "f:t:s:e:h:m:q:x:l:vgn")) != -1) {
> +  while ((opt = getopt (argc, argv, "f:t:s:e:h:m:q:x:i:l:vgn")) != -1) {
>      if (opt == 'f') {
>        parser->from = epool_strdup (&parser->ep, optarg);
>      } else if (opt == 't') {
> @@ -1832,6 +1833,8 @@ main (int argc, char * const argv[])
>        }
>      } else if (opt == 'x') {
>        parser->strmatch = epool_strdup (&parser->ep, optarg);
> +    } else if (opt == 'i') {
> +      inputfile = optarg;
>      } else if (opt == 'l') {
>        char *l;
>        parser->limit = strtoul (optarg, &l, 0);
> @@ -1911,7 +1914,9 @@ main (int argc, char * const argv[])
>    }
>  
>    int filecount;
> -  if ((filecount = parser_count_files (parser)) <= 0) {
> +  if (inputfile) {
> +      filecount = 1;
> +  } else if ((filecount = parser_count_files (parser)) <= 0) {
>      fprintf (stderr, "unable to access log files\n");
>      exit (-1);
>    }
> @@ -1960,7 +1965,14 @@ main (int argc, char * const argv[])
>      cur_year = parser->year[i];
>  
>      if (i <= 1) {
> -      if (!(stream = (gpointer) fopen (logfiles[i], "r"))) continue;
> +      if (inputfile && *inputfile == '-') {

what if my inputfile only starts with - ? maybe check length too...

> +	stream = (gpointer) stdin;
> +      } else if (inputfile) {
> +	if (!(stream = (gpointer) fopen (inputfile, "r"))) {
> +	  fprintf(stderr, "unable to open log file\n");
> +	  exit (-1);
> +	}
> +      } else if (!(stream = (gpointer) fopen (logfiles[i], "r"))) continue;
>      } else {
>        if (!(stream = (gpointer) gzopen (logfiles[i], "r"))) continue;
>      }
> 




More information about the pmg-devel mailing list