wmmisc: Add error handling for fscanf.
[dockapps.git] / wmmisc / src / dockapp_utils.c
blob4e8405a594e542dd12f0116030743b3f604893f0
1 /*
2 * wmmisc - WindowMaker Dockapp for monitoring misc. information.
3 * Copyright (C) 2003-2006 Jesse S. (luxorfalls@sbcglobal.net)
5 * wmmisc is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * wmmisc is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with wmmisc; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <bits/types/time_t.h>
22 #include <paths.h>
23 #include <stdio.h>
24 #include <sys/stat.h>
25 #include <utmp.h>
27 #include "dockapp_utils.h"
29 static dockapp_proc_t dockapp_proc;
31 dockapp_proc_t
32 dockapp_utils_get_proc( void )
34 FILE* pla_buf = NULL;
35 int p_load[2];
36 int p_run = 0;
37 int p_total = 0;
38 int count;
39 FILE* put_buf = NULL;
40 float p_uptime = 0.00;
42 dockapp_utils_get_users();
44 pla_buf = fopen( "/proc/loadavg", "r" );
46 if ( NULL != pla_buf )
48 count = fscanf( pla_buf,
49 "%d.%d %*s %*s %d/%d %*s",
50 &p_load[0],
51 &p_load[1],
52 &p_run,
53 &p_total );
54 fclose( pla_buf );
56 if (count == 4) {
57 dockapp_proc.total = p_total;
58 dockapp_proc.running = p_run;
59 dockapp_proc.load[0] = p_load[0];
60 dockapp_proc.load[1] = p_load[1];
61 } else {
62 dockapp_proc.total = 0;
63 dockapp_proc.running = 0;
64 dockapp_proc.load[0] = 0;
65 dockapp_proc.load[1] = 0;
68 else
70 dockapp_proc.total = 0;
71 dockapp_proc.running = 0;
72 dockapp_proc.load[0] = 0;
73 dockapp_proc.load[1] = 0;
76 put_buf = fopen( "/proc/uptime", "r" );
78 if ( NULL != put_buf )
80 count = fscanf( put_buf, "%f %*s", &p_uptime );
81 fclose( put_buf );
83 if (count == 1) {
84 dockapp_proc.jiffies = p_uptime;
85 dockapp_proc.seconds = GET_SECS( p_uptime );
86 dockapp_proc.minutes = GET_MINS( p_uptime );
87 dockapp_proc.hours = GET_HRS( p_uptime );
88 dockapp_proc.days = GET_DAYS( p_uptime );
89 dockapp_proc.weeks = GET_WEEKS( p_uptime );
90 } else {
91 dockapp_proc.jiffies = 0.00;
92 dockapp_proc.seconds = 0;
93 dockapp_proc.minutes = 0;
94 dockapp_proc.hours = 0;
95 dockapp_proc.days = 0;
96 dockapp_proc.weeks = 0;
99 #if 0
100 printf( "Uptime: %.2d:%.2d:%.2d, %d day%c, %.3d week%c\n",
101 dockapp_proc.hours,
102 dockapp_proc.minutes,
103 dockapp_proc.seconds,
104 dockapp_proc.days,
105 ( ( dockapp_proc.days > 1 || !dockapp_proc.days ) ? 's' : '\0' ),
106 dockapp_proc.weeks,
107 ( ( dockapp_proc.weeks > 1 || !dockapp_proc.weeks ) ? 's' : '\0' ) );
108 #endif
110 else
112 dockapp_proc.jiffies = 0.00;
113 dockapp_proc.seconds = 0;
114 dockapp_proc.minutes = 0;
115 dockapp_proc.hours = 0;
116 dockapp_proc.days = 0;
117 dockapp_proc.weeks = 0;
120 return dockapp_proc;
124 * Get the number of users logged onto the system.
126 * This code was taken from gkrellm and modified for
127 * this program.
130 void
131 dockapp_utils_get_users( void )
133 struct utmp* ut;
134 struct stat stt;
135 static time_t utmp_mtime;
136 int p_users = 0;
138 if ( 0 == stat( _PATH_UTMP, &stt ) && stt.st_mtime != utmp_mtime )
140 setutent();
142 ut = getutent();
144 while ( NULL != ut )
146 if ( USER_PROCESS == ut->ut_type && '\0' != ut->ut_name[0] )
148 ++p_users;
151 ut = getutent();
154 endutent();
156 utmp_mtime = stt.st_mtime;
157 dockapp_proc.users = p_users;
162 dockapp_utils_get_char( char d_char )
164 int n_char = 0;
166 switch ( d_char )
168 case 'a':
169 case 'A':
171 n_char = 0;
172 break;
175 case 'b':
176 case 'B':
178 n_char = 1;
179 break;
182 case 'c':
183 case 'C':
185 n_char = 2;
186 break;
189 case 'd':
190 case 'D':
192 n_char = 3;
193 break;
196 case 'e':
197 case 'E':
199 n_char = 4;
200 break;
203 case 'f':
204 case 'F':
206 n_char = 5;
207 break;
210 case 'g':
211 case 'G':
213 n_char = 6;
214 break;
217 case 'h':
218 case 'H':
220 n_char = 7;
221 break;
224 case 'i':
225 case 'I':
227 n_char = 8;
228 break;
231 case 'j':
232 case 'J':
234 n_char = 9;
235 break;
238 case 'k':
239 case 'K':
241 n_char = 10;
242 break;
245 case 'l':
246 case 'L':
248 n_char = 11;
249 break;
252 case 'm':
253 case 'M':
255 n_char = 12;
256 break;
259 case 'n':
260 case 'N':
262 n_char = 13;
263 break;
266 case 'o':
267 case 'O':
269 n_char = 14;
270 break;
273 case 'p':
274 case 'P':
276 n_char = 15;
277 break;
280 case 'q':
281 case 'Q':
283 n_char = 16;
284 break;
287 case 'r':
288 case 'R':
290 n_char = 17;
291 break;
294 case 's':
295 case 'S':
297 n_char = 18;
298 break;
301 case 't':
302 case 'T':
304 n_char = 19;
305 break;
308 case 'u':
309 case 'U':
311 n_char = 20;
312 break;
315 case 'v':
316 case 'V':
318 n_char = 21;
319 break;
322 case 'w':
323 case 'W':
325 n_char = 22;
326 break;
329 case 'x':
330 case 'X':
332 n_char = 23;
333 break;
336 case 'y':
337 case 'Y':
339 n_char = 24;
340 break;
343 case 'z':
344 case 'Z':
346 n_char = 25;
347 break;
350 default:
352 n_char = 26; /* Blank. */
353 break;
357 return n_char;