summaryrefslogtreecommitdiff
path: root/sutils/0004-battery.c-be-more-careful-with-the-printf-format.patch
blob: 7fc8ee36269ca9fbd8e5f90c5b3b913e8713af2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
From d3b11514c22943587e76def34c48655c78cd9610 Mon Sep 17 00:00:00 2001
From: John Vogel <jvogel4@stny.rr.com>
Date: Mon, 9 Oct 2023 10:39:19 -0400
Subject: [PATCH 4/4] battery.c: be more careful with the printf format

---
 battery.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/battery.c b/battery.c
index 20307e5..0770619 100644
--- a/battery.c
+++ b/battery.c
@@ -26,6 +26,7 @@ int put_infos(char *path, char *format)
     char status[MAXLEN] = {0};
     int capacity = -1;
     bool found_status = false, found_capacity = false;
+    bool print_status = false, print_capacity = false;
     while (fgets(line, sizeof(line), bf) != NULL) {
         char *key = strtok(line, TOKSEP);
         if (key != NULL) { 
@@ -43,12 +44,17 @@ int put_infos(char *path, char *format)
         fprintf(stderr, "The battery information is missing.\n");
         return EXIT_FAILURE;
     } else {
-        if (strstr(format, "%s") == NULL)
-            printf(format, capacity);
-        else if (strstr(format, "%i") == NULL)
+        print_status = (strstr(format, "%s") == NULL) ? false : true;
+        print_capacity = (strstr(format, "%i") == NULL) ? false : true;
+
+        if (print_status && print_capacity)
+            printf(format, status, capacity);
+        else if (print_status)
             printf(format, status);
+        else if (print_capacity)
+            printf(format, capacity);
         else
-            printf(format, status, capacity);
+            return EXIT_FAILURE;
         printf("\n");
         fflush(stdout);
     }
-- 
2.42.0