55#include "./include/color.h"
66
77int
8- cpu_info ()
8+ cpu_info ()
99{
10+ #define ITER (x ) (for (int i = 0; i < x; i++) \
11+ while ((c = fgetc(cpu)) != '\n' && c != EOF))
1012
1113 char brand [10 ]; // cpu brand
1214 char lineup [10 ]; // lineup (Ryzen, Core, Xeon, Epyc, etc)
1315 char sublineup [10 ]; // sublineup (5, 7, 9 or i5 i7 i9 etc)
1416 char model_num [10 ]; // model number (3600, 9900k, 3900X, etc)
15- char freq [10 ]; //cpu frequency
16- char threads [10 ]; //cpu threads
17+ #ifdef CPU_FREQUENCY
18+ char freq [10 ]; // cpu frequency
19+ #endif /* CPU_FREQUENCY */
20+ #ifdef CPU_THREADS
21+ char threads [10 ]; // cpu threads
22+ #endif /* CPU_THREADS */
1723#ifdef CPU_TEMP
1824 float TEMP ;
19- #endif
25+ FILE * cpu3 = fopen ("/sys/class/hwmon/hwmon1/temp3_input" , "r" );
26+ #endif /* CPU_TEMP */
2027
2128 int c ;
2229
@@ -26,48 +33,56 @@ cpu_info()
2633 exit (EXIT_FAILURE );
2734 }
2835 // this long line is used to skip lines.
29- for (int i = 0 ; i < 4 ; i ++ ) while ((c = fgetc (cpu )) != '\n' && c != EOF );
36+ //for (int i = 0; i < 4; i++)
37+ //while ((c = fgetc(cpu)) != '\n' && c != EOF);
38+ ITER (4 );
3039
3140 fscanf (cpu , "%*9s %*9s \t: %9s %9s %9s %9s" , \
3241 brand , lineup , sublineup , model_num );
33- for (int i = 0 ; i < 3 ; i ++ ) while ((c = fgetc (cpu )) != '\n' && c != EOF );
34-
42+ #if defined(CPU_FREQUENCY ) || defined(CPU_THREADS ) || defined(CPU_TEMP )
43+ /*for (int i = 0; i < 3; i++)
44+ while ((c = fgetc(cpu)) != '\n' && c != EOF);*/
45+ ITER (3 );
3546
47+ #ifdef CPU_FREQUENCY
3648 fscanf (cpu , "%*9s %*9s \t: %9s" , freq );
37- for (int i = 0 ; i < 3 ; i ++ ) while ((c = fgetc (cpu )) != '\n' && c != EOF );
38-
49+ #endif /* CPU_FREQUENCY */
50+ /*for (int i = 0; i < 3; i++)
51+ while ((c = fgetc(cpu)) != '\n' && c != EOF);*/
52+ ITER (3 );
3953
54+ #ifdef CPU_THREADS
4055 fscanf (cpu , "%*9s \t: %9s" , threads );
56+ #endif /* CPU_THREADS */
57+ #endif /* CPU_FREQUENCY || CPU_THREADS || CPU_TEMP */
4158 fclose (cpu );
4259#ifdef CPU_TEMP
43- FILE * cpu3 = fopen ("/sys/class/hwmon/hwmon1/temp3_input" , "r" );
4460
45- if (cpu3 == NULL ) TEMP = 0. ;
46-
47- else {
61+ if (cpu3 == NULL ) {
62+ TEMP = 0. ;
63+ } else {
4864 char line1_value [100 ];
65+ int x2 = 0 ;
4966 fscanf (cpu3 , "%99s" , line1_value );
5067 fclose (cpu3 );
51- int x2 ;
5268 sscanf (line1_value , "%d" , & x2 );
53- TEMP = (x2 /1000. );
69+ TEMP = (x2 /1000. );
5470 }
55- #endif
56-
71+ #endif /* CPU_TEMP */
5772
5873 printf ("%sCPU:\033[0m %s %s %s %s" ,\
5974 color_distro (), brand , lineup , sublineup , model_num );
6075
6176
6277#ifdef CPU_THREADS
6378 printf (" (%s)" , threads );
64- #endif
79+ #endif /* CPU_THREADS */
6580#ifdef CPU_FREQUENCY
6681 printf (" @ %sMHz" , freq );
67- #endif
82+ #endif /* CPU_FREQUENCY */
6883#ifdef CPU_TEMP
6984 printf (" (%.1f°C)" , TEMP );
70- #endif
85+ #endif /* CPU_TEMP */
7186printf ("\n" );
7287 return EXIT_SUCCESS ;
7388}
0 commit comments