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

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

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;
 }
-- 
1.8.5.rc3
Received on Fri Sep 15 2017 - 19:50:47 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 15 2017 - 20:00:35 CEST