vmware-detect.c: adding header
[grml-scripts.git] / compile / cpu-screen.c
1 /*
2  * Filename:      cpu-screen.c
3  * Purpose:       output current / available cpu frequence (useful for integration into GNU screen)
4  * Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5  * Bug-Reports:   see http://grml.org/bugs/
6  * License:       This file is licensed under the GPL v2.
7  * Latest change: Tue Mar 28 11:01:43 CEST 2006 [mika]
8  *******************************************************************************/
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #define LINE_LEN 10
15
16 static int cpu_cur_frequency(void)
17 {
18         FILE *fp;
19         char puffer[LINE_LEN];
20         char *nl;
21         fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
22         if(!fp) {
23                 printf("[ cpufreq n/a ]\n");
24                 return 1;
25         }
26         else {
27                 while(fgets(puffer, LINE_LEN, fp)){
28                         if ((nl = strchr(puffer,'\n')))
29                             *nl = 0;
30                         int value = atoi(puffer);
31                         value /= 1000;
32                         fprintf(stdout, "%u", value);
33                 }
34         }
35         fclose(fp);
36
37         return 0;
38 }
39
40 static int cpu_max_frequency(void)
41 {
42         FILE *fp;
43         char puffer[LINE_LEN];
44         fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
45         if(!fp) {
46                 return 2;
47         }
48         else {
49                 while(fgets(puffer, LINE_LEN, fp)){
50                         int value = atoi(puffer);
51                         value /= 1000;
52                         fprintf(stdout, "%u\n", value);
53                 }
54         }
55         fclose(fp);
56
57         return 0;
58 }
59
60 // function inspired by cpufreq-info.c of cpufrequtils-0.4 by Dominik Brodowski
61 static int count_cpus(void)
62 {
63         FILE *fp;
64         char value[LINE_LEN];
65         unsigned int ret = 0;
66         unsigned int cpunr = 0;
67
68         fp = fopen("/proc/stat", "r");
69         // assume "1" cpu if we can't count it
70         if(!fp) {
71                 return 1;
72         }
73
74         while (!feof(fp)) {
75                 fgets(value, LINE_LEN, fp);
76                 if (strlen(value) < (LINE_LEN - 2))
77                         continue;
78                 if (strstr(value, "cpu "))
79                         continue;
80                 if (sscanf(value, "cpu%d ", &cpunr) != 1)
81                         continue;
82                 if (cpunr > ret)
83                         ret = cpunr;
84         }
85         fclose(fp);
86
87         return (ret+1);
88 }
89
90 int main()
91 {
92         int cpus;
93         int ret;
94
95         cpus = count_cpus();
96         if (cpus != 1){
97                 printf("%d * ", cpus);
98         }
99
100         ret = cpu_cur_frequency();
101         if (!ret) {
102                 printf(" / ");
103                 ret = cpu_max_frequency();
104         }
105         return (ret);
106 }
107
108 /* EOF */