From: Jan Stancek Date: Wed, 13 Jul 2016 10:13:13 -0400 (EDT) Subject: [LTP] [PATCH] lib: add tst_read_meminfo / tst_get_avail_mem In-Reply-To: <20160713135428.GD2232@rei.scz.novell.com> References: <1589618283.6491776.1465976075619.JavaMail.zimbra@redhat.com> <57614F55.9090407@redhat.com> <20160616052544.GA18852@gmail.com> <1809783770.6955910.1466060898915.JavaMail.zimbra@redhat.com> <20160713093505.GA6033@gmail.com> <57863E28.7020309@redhat.com> <20160713135428.GD2232@rei.scz.novell.com> Message-ID: <937821381.4475496.1468419193289.JavaMail.zimbra@redhat.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: ltp@lists.linux.it ----- Original Message ----- > From: "Cyril Hrubis" > To: "Jan Stancek" > Cc: "Li Wang" , ltp@lists.linux.it > Sent: Wednesday, 13 July, 2016 3:54:28 PM > Subject: Re: [LTP] [PATCH] lib: add tst_read_meminfo / tst_get_avail_mem > > Hi! > > Latest idea attached. > > > > It's similar to SAFE_FILE_SCANF, but it scanfs each line and first > > matching all scanf directives wins and terminates search, otherwise > > you get non-zero ret code. For example: > > > > if (SAFE_FILE_LINES_SCANF("/proc/meminfo", "MemFree: %ld", &free)) > > tst_brk(TBROK, "Could not parse MemFree"); > > The API looks good to me. Nice work. > > We should call this version FILE_LINES_SCANF() since the SAFE_ variants > call tst_brkm() on failure. I was hesitant about SAFE_ part, but since it still calls brk on some occasions I left it there. There is also option to remove brkm calls, turn it into tst_file_lines_scanf(), and let user deal with possible errors. > > > diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c > > index dff85cd83fec..db1660b1f684 100644 > > --- a/lib/safe_file_ops.c > > +++ b/lib/safe_file_ops.c > > @@ -169,6 +169,49 @@ void safe_file_scanf(const char *file, const int > > lineno, > > } > > } > > > > + > > +/* > > + * Try to parse each line from file specified by 'path' according > > + * to scanf format 'fmt'. If all fields could be parsed, stop and > > + * return 0, otherwise continue or return 1 if EOF is reached. > > + */ > > +int safe_file_lines_scanf(const char *file, const int lineno, > > + void (*cleanup_fn)(void), > > + const char *path, const char *fmt, ...) > > +{ > > + FILE *fp; > > + int ret = 0; > > + int arg_count = 0; > > + char line[BUFSIZ]; > > + va_list ap; > > + > > + if (!fmt) { > > + tst_brkm(TBROK, cleanup_fn, "pattern is NULL, %s:%d", > > + file, lineno); > > + } > > + > > + fp = fopen(path, "r"); > > + if (fp == NULL) { > > + tst_brkm(TBROK | TERRNO, cleanup_fn, > > + "Failed to open FILE '%s' for reading at %s:%d", > > + path, file, lineno); > > + } > > + > > + arg_count = count_scanf_conversions(fmt); > > + > > + while (fgets(line, BUFSIZ, fp) != NULL) { > > + va_start(ap, fmt); > > + ret = vsscanf(line, fmt, ap); > > + va_end(ap); > > + > > + if (ret == arg_count) > > + break; > > + } > > + fclose(fp); > > I wonder if it would be easier to take the fmt and append "%*[^\n]\n" to > it so that we can use fscanf() directly without the line buffer... > > I guess that both of these are similarily complicated. > > -- > Cyril Hrubis > chrubis@suse.cz >