66#define FF_CPU_MODULE_NAME "CPU"
77#define FF_CPU_NUM_FORMAT_ARGS 15
88
9- static long parseLong (const FFstrbuf * content )
10- {
11- long value ;
12- if (sscanf (content -> chars , "%li" , & value ) != 1 )
13- return -1 ;
14-
15- return value ;
16- }
17-
18- static double parseDouble (const FFstrbuf * content )
19- {
20- double herz ;
21- if (sscanf (content -> chars , "%lf" , & herz ) != 1 )
22- return 0 ;
23-
24- return herz ;
25- }
26-
279static double getGhz (const char * policyFile , const char * cpuFile )
2810{
2911 FFstrbuf content ;
@@ -33,10 +15,14 @@ static double getGhz(const char* policyFile, const char* cpuFile)
3315 if (content .length == 0 )
3416 ffGetFileContent (cpuFile , & content );
3517
36- double herz = parseDouble (& content );
18+ double herz = ffStrbufToDouble (& content );
3719
3820 ffStrbufDestroy (& content );
3921
22+ //ffStrbufToDouble failed
23+ if (herz != herz )
24+ return 0 ;
25+
4026 herz /= 1000.0 ; //to MHz
4127 return herz / 1000.0 ; //to GHz
4228}
@@ -88,7 +74,12 @@ void ffPrintCPU(FFinstance* instance)
8874
8975 fclose (cpuinfo );
9076
91- double procGhz = parseDouble (& procGhzString ) / 1000.0 ; //to GHz
77+ double procGhz = ffStrbufToDouble (& procGhzString );
78+ if (procGhz != procGhz )
79+ procGhz = 0 ; //NaN
80+ else
81+ procGhz /= 1000.0 ; //To GHz
82+
9283 ffStrbufDestroy (& procGhzString );
9384
9485 double biosLimit = getGhz ("/sys/devices/system/cpu/cpufreq/policy0/bios_limit" , "/sys/devices/system/cpu/cpu0/cpufreq/bios_limit" );
@@ -158,13 +149,19 @@ void ffPrintCPU(FFinstance* instance)
158149 FFTempValue * value = ffListGet (& temps -> values , i );
159150
160151 if (
161- ffStrbufFirstIndexS (& value -> name , "cpu" ) < value -> name .length ||
162- ffStrbufCompS (& value -> name , "k10temp" ) == 0 ||
163- ffStrbufCompS (& value -> name , "coretemp" ) == 0
164- ) {
165- cpuTemp = (double ) parseLong (& value -> value ) / 1000.0 ;
166- break ;
167- }
152+ ffStrbufFirstIndexS (& value -> name , "cpu" ) == value -> name .length &&
153+ ffStrbufCompS (& value -> name , "k10temp" ) != 0 &&
154+ ffStrbufCompS (& value -> name , "coretemp" ) != 0
155+ ) continue ;
156+
157+ double temp = ffStrbufToDouble (& value -> value );
158+
159+ //NaN
160+ if (temp != temp )
161+ continue ;
162+
163+ cpuTemp = temp / 1000.0 ;
164+ break ;
168165 }
169166
170167 FFstrbuf cpu ;
0 commit comments