Remove all bashisms; drop grml-muttng
[grml-scripts-core.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  *******************************************************************************/
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #define LINE_LEN 10
14
15 static int cpu_cur_frequency(void)
16 {
17         FILE *fp;
18         char puffer[LINE_LEN];
19         char *nl;
20         fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
21         if(!fp) {
22                 printf("[ cpufreq n/a ]\n");
23                 return 1;
24         }
25         else {
26                 while(fgets(puffer, LINE_LEN, fp)){
27                         if ((nl = strchr(puffer,'\n')))
28                             *nl = 0;
29                         int value = atoi(puffer);
30                         value /= 1000;
31                         fprintf(stdout, "%u", value);
32                 }
33         }
34         fclose(fp);
35
36         return 0;
37 }
38
39 static int cpu_max_frequency(void)
40 {
41         FILE *fp;
42         char puffer[LINE_LEN];
43         fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
44         if(!fp) {
45                 return 2;
46         }
47         else {
48                 while(fgets(puffer, LINE_LEN, fp)){
49                         int value = atoi(puffer);
50                         value /= 1000;
51                         fprintf(stdout, "%u\n", value);
52                 }
53         }
54         fclose(fp);
55
56         return 0;
57 }
58
59 // function inspired by cpufreq-info.c of cpufrequtils-0.4 by Dominik Brodowski
60 static int count_cpus(void)
61 {
62         FILE *fp;
63         char value[LINE_LEN];
64         unsigned int ret = 0;
65         unsigned int cpunr = 0;
66
67         fp = fopen("/proc/stat", "r");
68         // assume "1" cpu if we can't count it
69         if(!fp) {
70                 return 1;
71         }
72
73         while (!feof(fp)) {
74                 fgets(value, LINE_LEN, fp);
75                 if (strlen(value) < (LINE_LEN - 2))
76                         continue;
77                 if (strstr(value, "cpu "))
78                         continue;
79                 if (sscanf(value, "cpu%d ", &cpunr) != 1)
80                         continue;
81                 if (cpunr > ret)
82                         ret = cpunr;
83         }
84         fclose(fp);
85
86         return (ret+1);
87 }
88
89 int main()
90 {
91         int cpus;
92         int ret;
93
94         cpus = count_cpus();
95         if (cpus != 1){
96                 printf("%d * ", cpus);
97         }
98
99         ret = cpu_cur_frequency();
100         if (!ret) {
101                 printf(" / ");
102                 ret = cpu_max_frequency();
103         }
104         return (ret);
105 }
106
107 /* EOF */