From 17713e672236fe0f612a64cd16d01ff129f4759d Mon Sep 17 00:00:00 2001
From: PA4WDH
Date: Sun, 21 May 2023 11:43:49 +0200
Subject: Tidy up cpufreq.c

---
 cpufreq.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/cpufreq.c b/cpufreq.c
index 75eb774..1eab630 100644
--- a/cpufreq.c
+++ b/cpufreq.c
@@ -8,14 +8,20 @@
 
 #define DIR_BUF_SIZE 1024
 
+// Validate if we found a file named cpu[0-9][0-9
 int cpufreq_is_cpu(char *name)
 {
  if (strncmp(name,"cpu",3)!=0) return 0;
  if ((name[3]<'0') || (name[3]>'9')) return 0;
+ if (name[4]==0) return 1;
 
- return 1;
+ if ((name[4]<'0') || (name[4]>'9')) return 0;
+ if (name[5]==0) return 1;
+
+ return 0;
 }
 
+// Set a cpufreq parameter to a value
 int cpufreq_set_str(char *parameter,char *value,int checkdelay)
 {
  int fd;
@@ -37,6 +43,7 @@ int cpufreq_set_str(char *parameter,char *value,int checkdelay)
  struct linux_dirent *d;
  size_t bpos;
 
+// Open the CPU directory in sysfs
  fd=open("/sys/devices/system/cpu",O_RDONLY|O_DIRECTORY);
  if (fd==-1)
  {
@@ -46,18 +53,26 @@ int cpufreq_set_str(char *parameter,char *value,int checkdelay)
 
  while(1)
  {
+// Iterate over SYS_getdents calls
   nread=syscall(SYS_getdents,fd,buf,DIR_BUF_SIZE);
+
+// If we can't read return an error
   if (nread==-1)
   {
    DEBUG1_CPUFREQ("Unable to find CPU's\n");
    close(fd);
    return -1;
   }
+
+// If there are no entries left we're done
   if (nread==0) break;
 
+// Iterate over returned file info
   for(bpos=0;bpos<nread;)
   {
    d=(struct linux_dirent *)(buf+bpos);
+
+// If it's a CPU, set our parameter
    if (cpufreq_is_cpu(d->d_name))
    {
     DEBUG2_CPUFREQ("Found CPU %s\n",d->d_name);
@@ -77,6 +92,8 @@ int cpufreq_set_str(char *parameter,char *value,int checkdelay)
  return done;
 }
 
+// This is just a wrapper around cpufreq_set_str with long int to string
+// convertion
 int cpufreq_set_long_int(char *parameter,long int value,int checkdelay)
 {
  char buf[255];
-- 
cgit v1.2.3