From: Jan Stancek <jstancek@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] lib: add tst_read_meminfo / tst_get_avail_mem
Date: Wed, 13 Jul 2016 10:13:13 -0400 (EDT) [thread overview]
Message-ID: <937821381.4475496.1468419193289.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <20160713135428.GD2232@rei.scz.novell.com>
----- Original Message -----
> From: "Cyril Hrubis" <chrubis@suse.cz>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: "Li Wang" <liwang@redhat.com>, 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
>
next prev parent reply other threads:[~2016-07-13 14:13 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-14 11:27 [LTP] [PATCH] lib: add tst_read_meminfo / tst_get_avail_mem Jan Stancek
2016-06-14 14:37 ` Li Wang
2016-06-15 7:34 ` Jan Stancek
2016-06-15 12:51 ` Jan Stancek
2016-06-16 5:25 ` Li Wang
2016-06-16 7:08 ` Jan Stancek
2016-06-16 7:32 ` Li Wang
2016-07-13 9:35 ` Li Wang
2016-07-13 13:12 ` Jan Stancek
2016-07-13 13:54 ` Cyril Hrubis
2016-07-13 14:13 ` Jan Stancek [this message]
2016-07-13 14:23 ` Cyril Hrubis
2016-07-14 8:03 ` Li Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=937821381.4475496.1468419193289.JavaMail.zimbra@redhat.com \
--to=jstancek@redhat.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox