Commit 04f24daa2f89613d508999fa7f75c59855d724da
1 parent
5a8f93f317
Exists in
master
works
Showing 2 changed files with 36 additions and 37 deletions Side-by-side Diff
.gitignore
View file @
04f24da
1 | +status |
status.c
View file @
04f24da
... | ... | @@ -21,7 +21,7 @@ |
21 | 21 | #define AC_MIB_LEN 3 |
22 | 22 | |
23 | 23 | #define BATBUF_LEN 16 |
24 | -#define DATETIME_LEN 16 | |
24 | +#define DATETIME_LEN 32 | |
25 | 25 | #define TEMPER_LEN 8 |
26 | 26 | #define STATUS_LEN 200 |
27 | 27 | |
28 | 28 | |
... | ... | @@ -34,10 +34,9 @@ |
34 | 34 | XSync(dpy, False); |
35 | 35 | } |
36 | 36 | |
37 | -char * | |
38 | -getdatetime() | |
37 | +int | |
38 | +getdatetime(char *buf) | |
39 | 39 | { |
40 | - char *buf = (char *) datetime_buf; | |
41 | 40 | time_t result; |
42 | 41 | struct tm *resulttm; |
43 | 42 | |
44 | 43 | |
45 | 44 | |
46 | 45 | |
47 | 46 | |
48 | 47 | |
49 | 48 | |
50 | 49 | |
51 | 50 | |
... | ... | @@ -46,40 +45,37 @@ |
46 | 45 | |
47 | 46 | if (resulttm == NULL) { |
48 | 47 | fprintf(stderr, "Error getting localtime.\n"); |
49 | - return ""; | |
48 | + return 1; | |
50 | 49 | } |
51 | 50 | |
52 | 51 | if (!strftime(buf, DATETIME_LEN - 1, "%D | %l:%M:%S%p", resulttm)) { |
53 | - fprintf(stderr, "strftime is 0.\n"); | |
54 | - return ""; | |
52 | + fprintf(stderr, "strftime is 0. Buffer too small?\n"); | |
53 | + return 1; | |
55 | 54 | } |
56 | 55 | |
57 | - return buf; | |
56 | + return 0; | |
58 | 57 | } |
59 | 58 | |
60 | 59 | #ifdef __FreeBSD__ |
61 | -int * | |
60 | +int | |
62 | 61 | init_sysctl(char *name, size_t miblen, int *mib) |
63 | 62 | { |
64 | - sysctlnametomib(name, mib, &miblen); | |
65 | - | |
66 | - return mib; | |
63 | + return sysctlnametomib(name, mib, &miblen); | |
67 | 64 | } |
68 | 65 | #endif |
69 | 66 | |
70 | -char * | |
71 | -gettemp(int *mib) | |
67 | +int | |
68 | +gettemp(int *mib, char *buf) | |
72 | 69 | { |
73 | - char *buf = (char *) temp_buf; | |
74 | 70 | int c_temp; |
75 | 71 | |
76 | 72 | #ifdef __FreeBSD__ |
77 | 73 | int kelvin_temp; |
78 | 74 | size_t len = sizeof(kelvin_temp); |
79 | 75 | |
80 | - if (sysctl(mib, 5, &kelvin_temp, &len, NULL, 0)) { | |
76 | + if (sysctl(mib, TEMP_MIB_LEN, &kelvin_temp, &len, NULL, 0)) { | |
81 | 77 | perror("Failed to get system temperature"); |
82 | - return buf = "-1"; | |
78 | + return -1; | |
83 | 79 | } |
84 | 80 | |
85 | 81 | c_temp = (kelvin_temp - 2732) / 10; |
86 | 82 | |
... | ... | @@ -93,14 +89,14 @@ |
93 | 89 | if (tfile == NULL) { |
94 | 90 | fprintf(stderr, "Could not open file %s: %s\n", |
95 | 91 | F_TMPER, strerror(errno)); |
96 | - return "?"; | |
92 | + return -1; | |
97 | 93 | } |
98 | 94 | |
99 | 95 | |
100 | 96 | if (fscanf(tfile, "%g", &c_temp) == EOF) { |
101 | 97 | fprintf(stderr, "Failed to read temperature\n"); |
102 | 98 | fclose(tfile); |
103 | - return "?"; | |
99 | + return -1; | |
104 | 100 | } |
105 | 101 | |
106 | 102 | fclose(tfile); |
107 | 103 | |
108 | 104 | |
... | ... | @@ -108,13 +104,12 @@ |
108 | 104 | snprintf(buf, TEMPER_LEN, "%dC", c_temp / 1000); |
109 | 105 | #endif |
110 | 106 | |
111 | - return buf; | |
107 | + return 0; | |
112 | 108 | } |
113 | 109 | |
114 | -char * | |
115 | -getbat(int *mib, int *power) | |
110 | +int | |
111 | +getbat(int *mib, int *power, char *buf) | |
116 | 112 | { |
117 | - char *buf = (char *) bat_buf; | |
118 | 113 | |
119 | 114 | #ifdef __linux__ |
120 | 115 | FILE *batstat; |
121 | 116 | |
... | ... | @@ -123,13 +118,13 @@ |
123 | 118 | if ((batstat = fopen(F_BATT, "r")) == NULL) { |
124 | 119 | fprintf(stderr, "Could not open file %s: %s\n", |
125 | 120 | F_BATT, strerror(errno)); |
126 | - return "?"; | |
121 | + return -1; | |
127 | 122 | } |
128 | 123 | |
129 | 124 | if (fscanf(batstat, "%d", &batval) == EOF) { |
130 | 125 | fprintf(stderr, "Failed to read battery status\n"); |
131 | 126 | fclose(batstat); |
132 | - return "?"; | |
127 | + return -1; | |
133 | 128 | } |
134 | 129 | |
135 | 130 | snprintf(buf, BATBUF_LEN, "BAT %d", batval); |
136 | 131 | |
137 | 132 | |
... | ... | @@ -143,12 +138,13 @@ |
143 | 138 | |
144 | 139 | size_t batlife_len = sizeof(batlife); |
145 | 140 | |
146 | - if (sysctl(mib, 4, &batlife, &batlife_len, NULL, 0)) { | |
141 | + if (sysctl(mib, BAT_MIB_LEN, &batlife, &batlife_len, NULL, 0)) { | |
147 | 142 | perror("Failed to get battery status"); |
148 | - return buf = "-1"; | |
143 | + return -1; | |
149 | 144 | } |
150 | 145 | |
151 | - plugged_in_err = sysctl(power, 3, &plugged_in, &batlife_len, NULL, 0); | |
146 | + plugged_in_err = sysctl(power, AC_MIB_LEN, &plugged_in, | |
147 | + &batlife_len, NULL, 0); | |
152 | 148 | |
153 | 149 | if (plugged_in_err || !plugged_in) { |
154 | 150 | snprintf(buf, BATBUF_LEN, "BAT %d", batlife); |
... | ... | @@ -157,7 +153,7 @@ |
157 | 153 | } |
158 | 154 | |
159 | 155 | #endif |
160 | - return buf; | |
156 | + return 0; | |
161 | 157 | } |
162 | 158 | |
163 | 159 | int |
... | ... | @@ -168,7 +164,7 @@ |
168 | 164 | mib_ac[AC_MIB_LEN]; |
169 | 165 | |
170 | 166 | char bat_buf[BATBUF_LEN], |
171 | - datetime_buf[DATETIME_LEN], | |
167 | + datetime[DATETIME_LEN], | |
172 | 168 | temp_buf[TEMPER_LEN], |
173 | 169 | status[STATUS_LEN]; |
174 | 170 | |
175 | 171 | |
176 | 172 | |
177 | 173 | |
... | ... | @@ -178,18 +174,20 @@ |
178 | 174 | } |
179 | 175 | |
180 | 176 | #ifdef __FreeBSD__ |
181 | - mib_bat = init_sysctl("hw.acpi.battery.life", BAT_MIB_LEN, mib_bat); | |
182 | - mib_temp = init_sysctl("hw.acpi.thermal.tz0.temperature", TEMP_MIB_LEN, mib_temp); | |
183 | - mib_ac = init_sysctl("hw.acpi.acline", AC_MIB_LEN, mib_ac); | |
177 | + init_sysctl("hw.acpi.battery.life", BAT_MIB_LEN, mib_bat); | |
178 | + init_sysctl("hw.acpi.thermal.tz0.temperature", TEMP_MIB_LEN, mib_temp); | |
179 | + init_sysctl("hw.acpi.acline", AC_MIB_LEN, mib_ac); | |
184 | 180 | #endif |
185 | 181 | |
186 | - for (;;sleep(1)) { | |
187 | - datetime = getdatetime(); | |
182 | + while (1) { | |
183 | + getdatetime(datetime); | |
184 | + getbat(mib_bat, mib_ac, bat_buf); | |
185 | + gettemp(mib_temp, temp_buf); | |
188 | 186 | |
189 | - snprintf(status, 200, "%s%% | %s | %s", | |
190 | - getbat(mib_bat, mib_ac), gettemp(mib_temp), datetime); | |
187 | + snprintf(status, 200, "%s%% | %s | %s", bat_buf, temp_buf, datetime); | |
191 | 188 | |
192 | 189 | setstatus(status); |
190 | + sleep(1); | |
193 | 191 | } |
194 | 192 | |
195 | 193 | XCloseDisplay(dpy); |