"Fossies" - the Fresh Open Source Software Archive

Member "lscpu-1.8/lscpu.c" (7 Jun 2006, 5372 Bytes) of package /linux/misc/old/lscpu-1.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lscpu.c" see the Fossies "Dox" file reference documentation.

    1 /* lscpu.c Copyright(C) Jason R Fink  see COPYING file */
    2 #include <errno.h>
    3 #include <stdio.h>
    4 #include <stdlib.h>
    5 #include <string.h>
    6 
    7 #define MAXLEN 1024
    8 
    9 /* the file handles we will be using */
   10 static char *pfh[]=
   11 {
   12     "/proc/cpuinfo",
   13     "/proc/sys/kernel/hostname",
   14     "/proc/sys/kernel/ostype",
   15     "/proc/sys/kernel/osrelease",
   16     "/proc/rtc",
   17     "/proc/driver/rtc",
   18 };
   19 
   20 /* Function Prototypes */
   21 int get_kernel_version(void);
   22 void error_output_mesg(char *locale);
   23 void get_hostname_info(void);
   24 void get_ostype_info(void);
   25 void get_osrelease_info(void);
   26 void get_cpu_info(void);
   27 void get_rtc_info(void);
   28 
   29 /* Using /proc/sys/kernel/osrelease this function returns:
   30     0 if the kernel is a 2.2.X series kernel
   31     1 if the kernel is a 2.3.X series kernel
   32     2 if the not 2.2.X or 2.2.X series kernel
   33     3 if the iteration is missed completely */
   34 int get_kernel_version()
   35 {
   36     FILE *osrfp;
   37     static char osrch[MAXLEN];
   38 
   39     if((osrfp = fopen(pfh[3], "r")) == NULL)
   40         error_output_mesg(pfh[3]);
   41 
   42     while(fgets(osrch, MAXLEN, osrfp) != NULL) {
   43         if(!strncmp(osrch, "2.2", 3)) {
   44             fclose(osrfp);
   45             return 0;
   46         } else if(!strncmp(osrch, "2.3", 3)) {
   47             fclose(osrfp);
   48             return 1;
   49         } else {
   50             fclose(osrfp);
   51             return 2;
   52         }
   53     }
   54 
   55     fclose(osrfp);
   56 
   57     return 3;
   58 }
   59 
   60 /* Simple strerror print function depends upon file handle    
   61    parameter to print out a file not found message */
   62 void error_output_mesg(char *locale)
   63 {
   64     fprintf(stderr, ("%s: %s\n"), locale, strerror(errno));
   65     exit (1);
   66 }
   67 
   68 /* Scans the hostname into a line and prints out the line */
   69 void get_hostname_info()
   70 {
   71     FILE *hostfp;
   72     static char hostch[MAXLEN];
   73     static char hostname[MAXLEN];
   74 
   75     if((hostfp = fopen(pfh[1], "r")) == NULL)
   76         error_output_mesg(pfh[1]);
   77 
   78     while(fgets(hostch, MAXLEN, hostfp) != NULL) {
   79         sscanf(hostch, "%s", hostname);
   80         printf("Processor Information for %s\n", hostname);
   81     }
   82 
   83     fclose(hostfp);
   84 }
   85 
   86 /* Scans the ostype into a line buffer and prints it out */
   87 void get_ostype_info()
   88 {
   89     FILE *osfp;
   90     static char osch[MAXLEN];
   91     static char ostype[MAXLEN];
   92 
   93     if((osfp = fopen(pfh[2], "r")) == NULL) 
   94         error_output_mesg(pfh[2]);
   95 
   96     while(fgets(osch, MAXLEN, osfp) != NULL) {
   97         sscanf(osch, "%s", ostype);
   98         printf("OS: %s", ostype);
   99     }
  100 
  101     fclose(osfp);
  102 }
  103 
  104 /* Scans the osrelease into a line buffer and prints it out */
  105 void get_osrelease_info()
  106 {
  107     FILE *osrfp;
  108     static char osrch[MAXLEN];
  109     static char osrelease[MAXLEN];
  110 
  111     if((osrfp = fopen(pfh[3], "r")) == NULL) 
  112         error_output_mesg(pfh[3]);
  113 
  114     while(fgets(osrch, MAXLEN, osrfp) != NULL) {
  115         sscanf(osrch, "%s", osrelease);
  116         printf(" version %s\n", osrelease);
  117     }
  118 
  119     fclose(osrfp);
  120 }
  121 
  122 /* Scans select information from /proc/cpuinfo
  123    into a set of buffers and prints them to std out
  124    AS EACH LINE is matched */
  125 void get_cpu_info()
  126 {
  127     FILE *cpufp;
  128     static char ch[MAXLEN];
  129     char line[MAXLEN];
  130 
  131     if ((cpufp = fopen(pfh[0], "r")) == NULL) 
  132         error_output_mesg(pfh[0]);
  133 
  134     while (fgets(ch, MAXLEN, cpufp) != NULL) {
  135         if (!strncmp(ch, "processor", 9)) {
  136             sscanf(ch, "%*s %*s %s", line);
  137             printf("CPU %s", line);
  138         } else if (!strncmp(ch, "vendor_id", 9)) {
  139             sscanf(ch,"%*s %*s %s", line);
  140             printf(" is Processor Type: %s ", line);
  141         } else if (!strncmp(ch, "model name", 10)) {
  142             sscanf(ch, "%*s %*s %*s %s", line);
  143             printf(" %s\n", line);
  144         } else if (!strncmp(ch, "cpu MHz", 7)) {
  145             sscanf(ch, "%*s %*s %*s %s", line);
  146             printf("Processor Speed in MHz: %s\n", line);
  147         } else if (!strncmp(ch, "cache size", 10)) {
  148             sscanf(ch, "%*s %*s %*s %s", line);
  149             printf("Processor Cache Size: %s\n", line);
  150         } else if (!strncmp(ch, "bogomips", 8)) {
  151             sscanf(ch, "%*s %*s %s", line);
  152             printf("Processor Speed in Bogomips: %s\n", line);
  153         } 
  154     }
  155     fclose(cpufp);
  156 }
  157 
  158 /* Checks the kernel version using /proc/sys/kernel/osrelease
  159    then based upon that determines where the rtc file is at.
  160    Finally scan and print out select info from the rtc file. */
  161 void get_rtc_info()
  162 {
  163     FILE *rtcfp;
  164     static char ch[MAXLEN]; 
  165     char line[MAXLEN];
  166     char rtcinfo[MAXLEN];
  167     register int kernelv_flag;
  168 
  169     /* set kernel flag */
  170     kernelv_flag = get_kernel_version();
  171 
  172     /* 
  173        Based upon the flag determine where the 
  174        elusive rtc file is residing. If it is
  175        indetermined print an error message
  176        and exit out 
  177      */
  178     if(kernelv_flag == 0) {
  179         strcpy(rtcinfo, pfh[4]);
  180     } else if(kernelv_flag == 1) {
  181         strcpy(rtcinfo, pfh[5]);
  182     } else {
  183         fprintf(stderr, "unknown");
  184         exit(2);    /* slightly higher magnitude */
  185     } 
  186 
  187     if ((rtcfp = fopen(rtcinfo, "r")) == NULL) 
  188         error_output_mesg(rtcinfo);
  189 
  190     /* Just grab stuff from a certain position and dump it to stdout */
  191     while (fgets(ch, MAXLEN, rtcfp) != NULL) {
  192         if (!strncmp(ch, "rtc_time", 8)) {
  193             sscanf(ch, "%*s %*s %s", line);
  194             printf("RTC Current Time: %s\t", line);
  195         } else if (!strncmp(ch, "rtc_date", 8)) {
  196             sscanf(ch, "%*s %*s %s", line);
  197             printf("RTC Date: %s\n", line);
  198         } else if (!strncmp(ch, "periodic_freq", 13)) {
  199             sscanf(ch, "%*s %*s %s", line);
  200             printf("RTC Periodic Frequency: %s\t", line); 
  201         } else if (!strncmp(ch, "batt_status", 11)) {
  202             sscanf(ch, "%*s %*s %s", line);
  203             printf("RTC Battery Status: %s\n", line);
  204         }
  205     }
  206     fclose(rtcfp);
  207 }
  208 
  209 /* Main */
  210 int main(void)
  211 {
  212     get_hostname_info();
  213     get_ostype_info();
  214     get_osrelease_info();
  215     get_cpu_info();
  216     get_rtc_info();
  217 
  218     return 0;
  219 }