Re: [dev] [PATCH 1/2] slstatus: allow pscanf to continue on the same file

From: Laslo Hunhold <dev_AT_frign.de>
Date: Fri, 15 Sep 2017 20:00:26 +0200

On Fri, 15 Sep 2017 19:50:47 +0200
Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be> wrote:

Dear Kurt,

> strtok() inspired me to let pscanf continue on the same file.
> Only if a new filename was specified (the default case), the pending
> open file is closed.
>
> Signed-off-by: Kurt Van Dijck <dev.kurt_AT_vandijck-laurijssen.be>
> ---
> slstatus.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/slstatus.c b/slstatus.c
> index ef79b03..79faeb3 100644
> --- a/slstatus.c
> +++ b/slstatus.c
> _AT_@ -100,18 +100,21 @@ bprintf(const char *fmt, ...)
> int
> pscanf(const char *path, const char *fmt, ...)
> {
> - FILE *fp;
> + static FILE *fp;
> va_list ap;
> int n;
>
> - if (!(fp = fopen(path, "r"))) {
> - warn("fopen %s: %s\n", path, strerror(errno));
> - return -1;
> + if (path) {
> + if (fp)
> + fclose(fp);
> + if (!(fp = fopen(path, "r"))) {
> + warn("fopen %s: %s\n", path, strerror
> (errno));
> + return -1;
> + }
> }
> va_start(ap, fmt);
> n = vfscanf(fp, fmt, ap);
> va_end(ap);
> - fclose(fp);
>
> return (n == EOF) ? -1 : n;
> }

I think this just makes the code more complicated, as pscanf()
is now no longer preëmtive. Especially with languages like C, one
should avoid such things in my opinion.

With best regards

Laslo Hunhold

-- 
Laslo Hunhold <dev_AT_frign.de>
Received on Fri Sep 15 2017 - 20:00:26 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 15 2017 - 20:12:30 CEST