27
27
*/
28
28
29
29
30
+ #include <stdarg.h>
30
31
#include <stdio.h>
31
32
#include <stdlib.h>
32
33
#include <unistd.h>
@@ -61,6 +62,29 @@ t_sensors* sensors = NULL;
61
62
t_fans * fans = NULL ;
62
63
63
64
65
+ static char * smprintf (const char * fmt , ...) __attribute__((format (printf , 1 , 2 )));
66
+ static char * smprintf (const char * fmt , ...)
67
+ {
68
+ char * buf ;
69
+ int cnt ;
70
+ va_list ap ;
71
+
72
+ // find buffer length
73
+ va_start (ap , fmt );
74
+ cnt = vsnprintf (NULL , 0 , fmt , ap );
75
+ va_end (ap );
76
+ if (cnt < 0 ) {
77
+ return NULL ;
78
+ }
79
+
80
+ // create and write to buffer
81
+ buf = malloc (cnt + 1 );
82
+ va_start (ap , fmt );
83
+ vsnprintf (buf , cnt + 1 , fmt , ap );
84
+ va_end (ap );
85
+ return buf ;
86
+ }
87
+
64
88
bool is_legacy_sensors_path ()
65
89
{
66
90
struct utsname kernel ;
@@ -147,9 +171,7 @@ t_sensors *retrieve_sensors()
147
171
148
172
if (errno == EISDIR ) {
149
173
150
- path_begin = (char * ) malloc (sizeof ( char ) * (strlen (hwmon_path ) + strlen ("/temp" ) + 1 ));
151
- strcpy (path_begin , hwmon_path );
152
- strcat (path_begin , "/temp" );
174
+ path_begin = smprintf ("%s/temp" , hwmon_path );
153
175
154
176
if (verbose ) {
155
177
printf ("Found hwmon path at %s\n" , path_begin );
@@ -167,28 +189,17 @@ t_sensors *retrieve_sensors()
167
189
168
190
const char * path_end = "_input" ;
169
191
170
- int path_size = strlen (path_begin ) + strlen (path_end ) + 2 ;
171
- char number [2 ];
172
- sprintf (number ,"%d" ,0 );
173
-
174
192
int sensors_found = 0 ;
175
193
176
194
int counter = 0 ;
177
195
for (counter = 0 ; counter < 10 ; counter ++ ) {
178
- path = (char * ) malloc (sizeof ( char ) * path_size );
179
-
180
- sprintf (number ,"%d" ,counter );
181
- path [0 ] = '\0' ;
182
- strncat ( path , path_begin , strlen (path_begin ) );
183
- strncat ( path , number , strlen (number ) );
184
- strncat ( path , path_end , strlen (path_begin ) );
196
+ path = smprintf ("%s%d%s" , path_begin , counter , path_end );
185
197
186
198
FILE * file = fopen (path , "r" );
187
199
188
200
if (file != NULL ) {
189
201
s = (t_sensors * ) malloc ( sizeof ( t_sensors ) );
190
- s -> path = (char * ) malloc (sizeof ( char ) * path_size );
191
- strcpy (s -> path , path );
202
+ s -> path = strdup (path );
192
203
fscanf (file , "%d" , & s -> temperature );
193
204
194
205
if (sensors_head == NULL ) {
@@ -248,39 +259,20 @@ t_fans *retrieve_fans()
248
259
const char * path_output_end = "_output" ;
249
260
const char * path_man_end = "_manual" ;
250
261
251
- int path_min_size = strlen (path_begin ) + strlen (path_output_end ) + 2 ;
252
- int path_man_size = strlen (path_begin ) + strlen (path_man_end ) + 2 ;
253
- char number [2 ];
254
- sprintf (number ,"%d" ,0 );
255
-
256
262
int counter = 0 ;
257
263
int fans_found = 0 ;
258
264
259
265
for (counter = 0 ; counter < 10 ; counter ++ ) {
260
266
261
- path_output = (char * ) malloc (sizeof ( char ) * path_min_size );
262
- path_output [0 ] = '\0' ;
263
- path_manual = (char * ) malloc (sizeof ( char ) * path_man_size );
264
- path_manual [0 ] = '\0' ;
265
- sprintf (number ,"%d" ,counter );
266
-
267
- strncat ( path_output , path_begin , strlen (path_begin ) );
268
- strncat ( path_output , number , strlen (number ) );
269
- strncat ( path_output , path_output_end , strlen (path_begin ) );
270
-
271
- strncat ( path_manual , path_begin , strlen (path_begin ) );
272
- strncat ( path_manual , number , strlen (number ) );
273
- strncat ( path_manual , path_man_end , strlen (path_begin ) );
274
-
267
+ path_output = smprintf ("%s%d%s" , path_begin , counter , path_output_end );
268
+ path_manual = smprintf ("%s%d%s" , path_begin , counter , path_man_end );
275
269
276
270
FILE * file = fopen (path_output , "w" );
277
271
278
272
if (file != NULL ) {
279
273
fan = (t_fans * ) malloc ( sizeof ( t_fans ) );
280
- fan -> fan_output_path = (char * ) malloc (sizeof ( char ) * path_min_size );
281
- fan -> fan_manual_path = (char * ) malloc (sizeof ( char ) * path_man_size );
282
- strcpy (fan -> fan_output_path , path_output );
283
- strcpy (fan -> fan_manual_path , path_manual );
274
+ fan -> fan_output_path = strdup (path_output );
275
+ fan -> fan_manual_path = strdup (path_manual );
284
276
285
277
if (fans_head == NULL ) {
286
278
fans_head = fan ;
0 commit comments