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.k...@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
@@ -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


Reply via email to