From 0406047a1c47ea9092b6527fd8f0150d1ed3b1db Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Sun, 28 Jul 2024 16:36:07 +0200 Subject: Move file name matching to separate function --- cputemp.c | 58 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 23 deletions(-) (limited to 'cputemp.c') diff --git a/cputemp.c b/cputemp.c index 4276e54..e30cc31 100644 --- a/cputemp.c +++ b/cputemp.c @@ -46,12 +46,39 @@ void cputemp_match_and_validate(char *name_file,char *input_file,char *sensor_na } } +// Validate if we found a file named temp_label +int cputemp_is_label(char *name) +{ + int count; + int offset; + +// Start with "temp" + if (strncmp(name,"temp",4)!=0) return 0; + +// Must contain an underscore + offset=0; + for(count=5;count<7;count++) + { + if (name[count]=='_') + { + offset=count+1; + break; + } + } + if (offset==0) return 0; + +// Ends with "label" + if (strncmp(name+offset,"label",5)!=0) return 0; + if (name[offset+5]==0) return 1; + + return 0; +} + // Read a single hwmon directory and search for the requested sensor name void cputemp_read_hwmon(char *hwmon,char *sensor_name,struct s_sensor *sensor) { DIR *hwmon_dir; struct dirent *hwmon_dirent; - int count; char name_file[255]; char input_file[255]; int offset; @@ -70,29 +97,14 @@ void cputemp_read_hwmon(char *hwmon,char *sensor_name,struct s_sensor *sensor) { DEBUG3_CPUTEMP("%s\n",hwmon_dirent->d_name); -// We are searching for files named "temp_label" - if (strncmp(hwmon_dirent->d_name,"temp",4)==0) + if (cputemp_is_label(hwmon_dirent->d_name)) { - DEBUG3_CPUTEMP("Found temp entry: %s\n",hwmon_dirent->d_name); - offset=0; - for(count=5;count<7;count++) - { - if (hwmon_dirent->d_name[count]=='_') - { - DEBUG3_CPUTEMP("Underscore at %d\n",count); - offset=count+1; - break; - } - } - if (strncmp(hwmon_dirent->d_name+offset,"label",5)==0) - { - DEBUG2_CPUTEMP("Found label %s\n",hwmon_dirent->d_name); - sprintf(name_file,"%s/%s",hwmon,hwmon_dirent->d_name); - sprintf(input_file,"%s/%s",hwmon,hwmon_dirent->d_name); - offset=strlen(input_file)-5; - strncpy(input_file+offset,"input",5); - cputemp_match_and_validate(name_file,input_file,sensor_name,sensor); - } + DEBUG2_CPUTEMP("Found label %s\n",hwmon_dirent->d_name); + sprintf(name_file,"%s/%s",hwmon,hwmon_dirent->d_name); + sprintf(input_file,"%s/%s",hwmon,hwmon_dirent->d_name); + offset=strlen(input_file)-5; + strncpy(input_file+offset,"input",5); + cputemp_match_and_validate(name_file,input_file,sensor_name,sensor); } hwmon_dirent=readdir(hwmon_dir); } -- cgit v1.2.3