summaryrefslogtreecommitdiff
path: root/sutils/0003-volume.c-take-some-care.patch
blob: 1ad3c28f63a9247a736ae8230f3e34bcd9bf49c4 (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
47
48
49
50
51
52
53
54
55
From 9c99628bb8a13ce825cf6ee7a3544a40b5527068 Mon Sep 17 00:00:00 2001
From: John Vogel <jvogel4@stny.rr.com>
Date: Mon, 9 Oct 2023 10:36:06 -0400
Subject: [PATCH 3/4] volume.c: take some care

Use strcpy instead of sprintf.
Be more careful with the printf format.
---
 volume.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/volume.c b/volume.c
index 8117279..b0c49ee 100644
--- a/volume.c
+++ b/volume.c
@@ -25,6 +25,8 @@ int put_infos(int vol_min, int vol_max,
     int volume;
     int volume_percent;
     bool mute_switch;
+    bool print_switch_state;
+    bool print_volume_percent;
     char switch_state[4];
 
     snd_hctl_elem_read(volume_elem, volume_ctl);
@@ -36,16 +38,21 @@ int put_infos(int vol_min, int vol_max,
     volume_percent = (int)(100.0f * ((float)volume - vol_min) / (vol_max - vol_min));
 
     if (mute_switch == true)
-        sprintf(switch_state, "on");
+        strcpy(switch_state, "on");
     else
-        sprintf(switch_state, "off");
+        strcpy(switch_state, "off");
 
-    if (strstr(format, "%s") == NULL)
-        printf(format, volume_percent);
-    else if (strstr(format, "%i") == NULL)
+    print_switch_state = (strstr(format, "%s") == NULL) ? false : true;
+    print_volume_percent = (strstr(format, "%i") == NULL) ? false : true;
+
+    if (print_switch_state && print_volume_percent)
+        printf(format, switch_state, volume_percent);
+    else if (print_switch_state)
         printf(format, switch_state);
+    else if (print_volume_percent)
+        printf(format, volume_percent);
     else
-        printf(format, switch_state, volume_percent);
+        exit(EXIT_FAILURE);
     printf("\n");
     fflush(stdout);
 
-- 
2.42.0