]> git.sur5r.net Git - i3/i3status/blob - i3status.c
Display 0 instead of negative values for remaining time
[i3/i3status] / i3status.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3status – Generates a status line for dzen2 or wmii
5  *
6  *
7  * Copyright © 2008-2009 Michael Stapelberg and contributors
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * * Redistributions of source code must retain the above copyright notice, this
14  *   list of conditions and the following disclaimer.
15  *
16  * * Redistributions in binary form must reproduce the above copyright notice, this
17  *   list of conditions and the following disclaimer in the documentation and/or other
18  *   materials provided with the distribution.
19  *
20  * * Neither the name of Michael Stapelberg nor the names of contributors
21  *   may be used to endorse or promote products derived from this software without
22  *   specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27  * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33  * DAMAGE.
34  *
35  */
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <time.h>
42 #include <stdbool.h>
43 #include <stdarg.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <limits.h>
47 #include <ctype.h>
48 #include <net/if.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <glob.h>
54 #include <dirent.h>
55 #include <getopt.h>
56
57 #include "queue.h"
58
59 #ifdef LINUX
60 #include <linux/ethtool.h>
61 #include <linux/sockios.h>
62 #else
63 /* TODO: correctly check for *BSD */
64 #include <sys/param.h>
65 #include <sys/sysctl.h>
66 #include <sys/resource.h>
67 #endif
68
69 #include "i3status.h"
70
71 #define BAR "^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)"
72
73 struct battery {
74         const char *path;
75         SIMPLEQ_ENTRY(battery) batteries;
76 };
77
78 SIMPLEQ_HEAD(battery_head, battery) batteries;
79
80 /* socket file descriptor for general purposes */
81 static int general_socket;
82
83 static const char *wlan_interface;
84 static const char *eth_interface;
85 static char *wmii_path;
86 static const char *time_format;
87 static const char *battery_path;
88 static bool use_colors;
89 static bool get_ethspeed;
90 static const char *wmii_normcolors = "#222222 #333333";
91 static char order[MAX_ORDER][2];
92 static const char **run_watches;
93 static unsigned int num_run_watches;
94 static unsigned int interval = 1;
95
96 static int max(int a, int b) {
97         return (a > b ? a : b);
98 }
99
100 /*
101  * This function just concats two strings in place, it should only be used
102  * for concatting order to the name of a file or concatting color codes.
103  * Otherwise, the buffer size would have to be increased.
104  *
105  */
106 static char *concat(const char *str1, const char *str2) {
107         static char concatbuf[32];
108         (void)snprintf(concatbuf, sizeof(concatbuf), "%s%s", str1, str2);
109         return concatbuf;
110 }
111
112 /*
113  * Returns the correct color format for dzen (^fg(color)) or wmii (color <normcolors>)
114  *
115  */
116 static char *color(const char *colorstr) {
117         static char colorbuf[32];
118 #ifdef DZEN
119         (void)snprintf(colorbuf, sizeof(colorbuf), "^fg(%s)", colorstr);
120 #else
121         (void)snprintf(colorbuf, sizeof(colorbuf), "%s %s ", colorstr, wmii_normcolors);
122 #endif
123         return colorbuf;
124 }
125
126 /*
127  * Cleans wmii's /rbar directory by deleting all regular files
128  *
129  */
130 static void cleanup_rbar_dir() {
131 #ifdef DZEN
132         return;
133 #endif
134         struct dirent *ent;
135         DIR *dir;
136         char pathbuf[strlen(wmii_path)+256+1];
137
138         if ((dir = opendir(wmii_path)) == NULL)
139                 exit(EXIT_FAILURE);
140
141         while ((ent = readdir(dir)) != NULL) {
142                 if (ent->d_type == DT_REG) {
143                         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, ent->d_name);
144                         if (unlink(pathbuf) == -1)
145                                 exit(EXIT_FAILURE);
146                 }
147         }
148
149         (void)closedir(dir);
150 }
151
152 /*
153  * Creates the specified file in wmii's /rbar directory with
154  * correct modes and initializes colors if colormode is enabled
155  *
156  */
157 static void create_file(const char *name) {
158 #ifdef DZEN
159         return;
160 #endif
161         char pathbuf[strlen(wmii_path)+256+1];
162         int fd;
163         int flags = O_CREAT | O_WRONLY;
164         struct stat statbuf;
165
166         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
167
168         /* Overwrite file's contents if it exists */
169         if (stat(pathbuf, &statbuf) >= 0)
170                 flags |= O_TRUNC;
171
172         if ((fd = open(pathbuf, flags, S_IRUSR | S_IWUSR)) < 0)
173                 exit(EXIT_FAILURE);
174         if (use_colors) {
175                 char *tmp = color("#888888");
176                 if (write(fd, tmp, strlen(tmp)) != (ssize_t)strlen(tmp))
177                         exit(EXIT_FAILURE);
178         }
179         (void)close(fd);
180 }
181
182 /*
183  * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
184  * cleans up all files and creates the needed files
185  *
186  */
187 static void setup(void) {
188         unsigned int i;
189         char pathbuf[512];
190
191 #ifndef DZEN
192         struct stat statbuf;
193         /* Wait until wmii_path/rbar exists */
194         for (; stat(wmii_path, &statbuf) < 0; sleep(interval));
195 #endif
196
197         cleanup_rbar_dir();
198         if (wlan_interface)
199                 create_file(concat(order[ORDER_WLAN],"wlan"));
200         if (eth_interface)
201                 create_file(concat(order[ORDER_ETH],"eth"));
202         if (battery_path)
203                 create_file(concat(order[ORDER_BATTERY],"battery"));
204         create_file(concat(order[ORDER_LOAD],"load"));
205         if (time_format)
206                 create_file(concat(order[ORDER_TIME],"time"));
207         for (i = 0; i < num_run_watches; i += 2) {
208                 snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
209                 create_file(pathbuf);
210         }
211 }
212
213 /*
214  * Writes the given message in the corresponding file in wmii's /rbar directory
215  *
216  */
217 static void write_to_statusbar(const char *name, const char *message, bool final_entry) {
218 #ifdef DZEN
219         if (final_entry) {
220                 (void)printf("%s^p(6)\n", message);
221                 fflush(stdout);
222                 return;
223         }
224         (void)printf("%s" BAR, message);
225         return;
226 #endif
227
228         char pathbuf[strlen(wmii_path)+256+1];
229         int fd;
230
231         (void)snprintf(pathbuf, sizeof(pathbuf), "%s%s", wmii_path, name);
232         if ((fd = open(pathbuf, O_RDWR)) == -1) {
233                 /* Try to re-setup stuff and just continue */
234                 setup();
235                 return;
236         }
237         if (write(fd, message, strlen(message)) != (ssize_t)strlen(message))
238                 exit(EXIT_FAILURE);
239         (void)close(fd);
240 }
241
242 /*
243  * Writes an errormessage to statusbar
244  *
245  */
246 static void write_error_to_statusbar(const char *message) {
247         cleanup_rbar_dir();
248         create_file("error");
249         write_to_statusbar("error", message, true);
250 }
251
252 /*
253  * Write errormessage to statusbar and exit
254  *
255  */
256 void die(const char *fmt, ...) {
257         if (wmii_path != NULL) {
258                 char buffer[512];
259                 va_list ap;
260                 va_start(ap, fmt);
261                 (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
262                 va_end(ap);
263
264                 write_error_to_statusbar(buffer);
265         }
266         exit(EXIT_FAILURE);
267 }
268
269 /*
270  * Skip the given character for exactly 'amount' times, returns
271  * a pointer to the first non-'character' character in 'input'.
272  *
273  */
274 static char *skip_character(char *input, char character, int amount) {
275         char *walk;
276         size_t len = strlen(input);
277         int blanks = 0;
278
279         for (walk = input; ((size_t)(walk - input) < len) && (blanks < amount); walk++)
280                 if (*walk == character)
281                         blanks++;
282
283         return (walk == input ? walk : walk-1);
284 }
285
286 /*
287  * Get battery information from /sys. Note that it uses the design capacity to
288  * calculate the percentage, not the last full capacity, so you can see how
289  * worn off your battery is.
290  *
291  */
292 static char *get_battery_info(const char *path) {
293         char buf[1024];
294         static char part[512];
295         char *walk, *last;
296         int fd;
297         int full_design = -1,
298             remaining = -1,
299             present_rate = -1;
300         charging_status_t status = CS_DISCHARGING;
301
302         if ((fd = open(path, O_RDONLY)) == -1)
303                 return "No battery found";
304
305         memset(part, 0, sizeof(part));
306         (void)read(fd, buf, sizeof(buf));
307         for (walk = buf, last = buf; (walk-buf) < 1024; walk++)
308                 if (*walk == '=') {
309                         if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN") ||
310                             BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN"))
311                                 full_design = atoi(walk+1);
312                         else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
313                                  BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
314                                 remaining = atoi(walk+1);
315                         else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
316                                 present_rate = atoi(walk+1);
317                         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
318                                 status = CS_CHARGING;
319                         else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
320                                 status = CS_FULL;
321                 } else if (*walk == '\n')
322                         last = walk+1;
323         (void)close(fd);
324
325         if ((full_design == 1) || (remaining == -1))
326                 return part;
327
328         if (present_rate > 0) {
329                 float remaining_time;
330                 int seconds, hours, minutes;
331                 if (status == CS_CHARGING)
332                         remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
333                 else if (status == CS_DISCHARGING)
334                         remaining_time = ((float)remaining / (float)present_rate);
335                 else remaining_time = 0;
336
337                 seconds = (int)(remaining_time * 3600.0);
338                 hours = seconds / 3600;
339                 seconds -= (hours * 3600);
340                 minutes = seconds / 60;
341                 seconds -= (minutes * 60);
342
343                 (void)snprintf(part, sizeof(part), "%s %.02f%% %02d:%02d:%02d",
344                         (status == CS_CHARGING ? "CHR" :
345                          (status == CS_DISCHARGING ? "BAT" : "FULL")),
346                         (((float)remaining / (float)full_design) * 100),
347                         max(hours, 0), max(minutes, 0), max(seconds, 0));
348         } else {
349                 (void)snprintf(part, sizeof(part), "%s %.02f%%",
350                         (status == CS_CHARGING ? "CHR" :
351                          (status == CS_DISCHARGING ? "BAT" : "FULL")),
352                         (((float)remaining / (float)full_design) * 100));
353         }
354         return part;
355 }
356
357 /*
358  * Return the IP address for the given interface or "no IP" if the
359  * interface is up and running but hasn't got an IP address yet
360  *
361  */
362 static const char *get_ip_address(const char *interface) {
363         static char part[512];
364         struct ifreq ifr;
365         struct sockaddr_in addr;
366         socklen_t len = sizeof(struct sockaddr_in);
367         memset(part, 0, sizeof(part));
368
369         /* First check if the interface is running */
370         (void)strcpy(ifr.ifr_name, interface);
371         if (ioctl(general_socket, SIOCGIFFLAGS, &ifr) < 0 ||
372             !(ifr.ifr_flags & IFF_RUNNING))
373                 return NULL;
374
375         /* Interface is up, get the IP address */
376         (void)strcpy(ifr.ifr_name, interface);
377         ifr.ifr_addr.sa_family = AF_INET;
378         if (ioctl(general_socket, SIOCGIFADDR, &ifr) < 0)
379                 return "no IP";
380
381         memcpy(&addr, &ifr.ifr_addr, len);
382         (void)inet_ntop(AF_INET, &addr.sin_addr.s_addr, part, len);
383         if (strlen(part) == 0)
384                 (void)snprintf(part, sizeof(part), "no IP");
385
386         return part;
387 }
388
389 /*
390  * Just parses /proc/net/wireless looking for lines beginning with
391  * wlan_interface, extracting the quality of the link and adding the
392  * current IP address of wlan_interface.
393  *
394  */
395 static char *get_wireless_info() {
396         char buf[1024];
397         static char part[512];
398         char *interfaces;
399         int fd;
400         memset(buf, 0, sizeof(buf));
401         memset(part, 0, sizeof(part));
402
403         if ((fd = open("/proc/net/wireless", O_RDONLY)) == -1)
404                 die("Could not open /proc/net/wireless\n");
405         (void)read(fd, buf, sizeof(buf));
406         (void)close(fd);
407
408         interfaces = skip_character(buf, '\n', 1) + 1;
409         while ((interfaces = skip_character(interfaces, '\n', 1)+1) < buf+strlen(buf)) {
410                 while (isspace((int)*interfaces))
411                         interfaces++;
412                 if (!BEGINS_WITH(interfaces, wlan_interface))
413                         continue;
414                 int quality;
415                 if (sscanf(interfaces, "%*[^:]: 0000 %d", &quality) != 1)
416                         continue;
417                 if ((quality == UCHAR_MAX) || (quality == 0)) {
418                         if (use_colors)
419                                 (void)snprintf(part, sizeof(part), "%sW: down", color("#FF0000"));
420                         else (void)snprintf(part, sizeof(part), "W: down");
421                 } else (void)snprintf(part, sizeof(part), "%sW: (%03d%%) %s",
422                                 color("#00FF00"), quality, get_ip_address(wlan_interface));
423                 return part;
424         }
425
426         return part;
427 }
428
429 /*
430  * Combines ethernet IP addresses and speed (if requested) for displaying
431  *
432  */
433 static char *get_eth_info() {
434         static char part[512];
435         const char *ip_address = get_ip_address(eth_interface);
436         int ethspeed = 0;
437
438         if (get_ethspeed) {
439 #ifdef LINUX
440                 /* This code path requires root privileges */
441                 struct ifreq ifr;
442                 struct ethtool_cmd ecmd;
443
444                 ecmd.cmd = ETHTOOL_GSET;
445                 (void)memset(&ifr, 0, sizeof(ifr));
446                 ifr.ifr_data = (caddr_t)&ecmd;
447                 (void)strcpy(ifr.ifr_name, eth_interface);
448                 if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0)
449                         ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
450                 else get_ethspeed = false;
451 #endif
452         }
453
454         if (ip_address == NULL)
455                 (void)snprintf(part, sizeof(part), "E: down");
456         else {
457                 if (get_ethspeed)
458                         (void)snprintf(part, sizeof(part), "E: %s (%d Mbit/s)", ip_address, ethspeed);
459                 else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
460         }
461
462         return part;
463 }
464
465 /*
466  * Checks if the PID in path is still valid by checking:
467  *  (Linux) if /proc/<pid> exists
468  *  (NetBSD) if sysctl returns process infos for this pid
469  *
470  */
471 static bool process_runs(const char *path) {
472         char pidbuf[16];
473         static glob_t globbuf;
474         int fd;
475         memset(pidbuf, 0, sizeof(pidbuf));
476
477         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
478                 die("glob() failed\n");
479         fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY);
480         globfree(&globbuf);
481         if (fd < 0)
482                 return false;
483         (void)read(fd, pidbuf, sizeof(pidbuf));
484         (void)close(fd);
485
486 #ifdef LINUX
487         struct stat statbuf;
488         char procbuf[512];
489         (void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10));
490         return (stat(procbuf, &statbuf) >= 0);
491 #else
492         /* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */
493         struct kinfo_proc info;
494         size_t length = sizeof(struct kinfo_proc);
495         int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) };
496         if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
497                 return false;
498         return (length != 0);
499 #endif
500 }
501
502 /*
503  * Reads the configuration from the given file
504  *
505  */
506 static int load_configuration(const char *configfile) {
507         #define OPT(x) else if (strcasecmp(dest_name, x) == 0)
508
509         /* check if the file exists */
510         struct stat buf;
511         if (stat(configfile, &buf) < 0)
512                 return -1;
513
514         int result = 0;
515         FILE *handle = fopen(configfile, "r");
516         if (handle == NULL)
517                 die("Could not open configfile\n");
518         char dest_name[512], dest_value[512], whole_buffer[1026];
519
520         while (!feof(handle)) {
521                 char *ret;
522                 if ((ret = fgets(whole_buffer, 1024, handle)) == whole_buffer) {
523                         /* sscanf implicitly strips whitespace */
524                         if (sscanf(whole_buffer, "%s %[^\n]", dest_name, dest_value) < 1)
525                                 continue;
526                 } else if (ret != NULL)
527                         die("Could not read line in configuration file\n");
528
529                 /* skip comments and empty lines */
530                 if (dest_name[0] == '#' || strlen(dest_name) < 3)
531                         continue;
532
533                 OPT("wlan")
534                         wlan_interface = strdup(dest_value);
535                 OPT("eth")
536                         eth_interface = strdup(dest_value);
537                 OPT("time_format")
538                         time_format = strdup(dest_value);
539                 OPT("battery_path") {
540                         struct battery *new = calloc(1, sizeof(struct battery));
541                         if (new == NULL)
542                                 die("Could not allocate memory\n");
543                         new->path = strdup(dest_value);
544                         SIMPLEQ_INSERT_TAIL(&batteries, new, batteries);
545                 } OPT("color")
546                         use_colors = true;
547                 OPT("get_ethspeed")
548                         get_ethspeed = true;
549                 OPT("normcolors")
550                         wmii_normcolors = strdup(dest_value);
551                 OPT("interval")
552                         interval = atoi(dest_value);
553                 OPT("wmii_path")
554                 {
555 #ifndef DZEN
556                         static glob_t globbuf;
557                         struct stat stbuf;
558                         if (glob(dest_value, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
559                                 die("glob() failed\n");
560                         wmii_path = strdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : dest_value);
561                         globfree(&globbuf);
562
563                         if ((stat(wmii_path, &stbuf)) == -1) {
564                                 fprintf(stderr, "Warning: wmii_path contains an invalid path\n");
565                                 free(wmii_path);
566                                 wmii_path = strdup(dest_value);
567                         }
568                         if (wmii_path[strlen(wmii_path)-1] != '/')
569                                 die("wmii_path is not terminated by /\n");
570 #endif
571                 }
572                 OPT("run_watch")
573                 {
574                         char *name = strdup(dest_value);
575                         char *path = name;
576                         while (*path != ' ')
577                                 path++;
578                         *(path++) = '\0';
579                         num_run_watches += 2;
580                         run_watches = realloc(run_watches, sizeof(char*) * num_run_watches);
581                         run_watches[num_run_watches-2] = name;
582                         run_watches[num_run_watches-1] = path;
583                 }
584                 OPT("order")
585                 {
586                         #define SET_ORDER(opt, idx) { if (strcasecmp(token, opt) == 0) sprintf(order[idx], "%d", c++); }
587                         char *walk, *token;
588                         int c = 0;
589                         walk = token = dest_value;
590                         while (*walk != '\0') {
591                                 while ((*walk != ',') && (*walk != '\0'))
592                                         walk++;
593                                 *(walk++) = '\0';
594                                 SET_ORDER("run", ORDER_RUN);
595                                 SET_ORDER("wlan", ORDER_WLAN);
596                                 SET_ORDER("eth", ORDER_ETH);
597                                 SET_ORDER("battery", ORDER_BATTERY);
598                                 SET_ORDER("load", ORDER_LOAD);
599                                 SET_ORDER("time", ORDER_TIME);
600                                 token = walk;
601                                 while (isspace((int)(*token)))
602                                         token++;
603                         }
604                 }
605                 else
606                 {
607                         result = -2;
608                         die("Unknown configfile option: %s\n", dest_name);
609                 }
610         }
611         fclose(handle);
612
613 #ifndef DZEN
614         if (wmii_path == NULL)
615                 exit(EXIT_FAILURE);
616 #endif
617
618         return result;
619 }
620
621 int main(int argc, char *argv[]) {
622         char part[512],
623              pathbuf[512];
624         unsigned int i;
625
626         char *configfile = PREFIX "/etc/i3status.conf";
627         int o, option_index = 0;
628         struct option long_options[] = {
629                 {"config", required_argument, 0, 'c'},
630                 {"help", no_argument, 0, 'h'},
631                 {0, 0, 0, 0}
632         };
633
634         SIMPLEQ_INIT(&batteries);
635
636         while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
637                 if ((char)o == 'c')
638                         configfile = optarg;
639                 else if ((char)o == 'h') {
640                         printf("i3status (c) 2008-2009 Michael Stapelberg\n"
641                                 "Syntax: %s [-c <configfile>]\n", argv[0]);
642                         return 0;
643                 }
644
645         if (load_configuration(configfile) < 0)
646                 return EXIT_FAILURE;
647
648         setup();
649
650         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
651                 die("Could not create socket\n");
652
653         while (1) {
654                 for (i = 0; i < num_run_watches; i += 2) {
655                         bool running = process_runs(run_watches[i+1]);
656                         if (use_colors)
657                                 snprintf(part, sizeof(part), "%s%s: %s",
658                                         (running ? color("#00FF00") : color("#FF0000")),
659                                         run_watches[i],
660                                         (running ? "yes" : "no"));
661                         else snprintf(part, sizeof(part), "%s: %s", run_watches[i], (running ? "yes" : "no"));
662                         snprintf(pathbuf, sizeof(pathbuf), "%s%s", order[ORDER_RUN], run_watches[i]);
663                         write_to_statusbar(pathbuf, part, false);
664                 }
665
666                 if (wlan_interface)
667                         write_to_statusbar(concat(order[ORDER_WLAN], "wlan"), get_wireless_info(), false);
668                 if (eth_interface)
669                         write_to_statusbar(concat(order[ORDER_ETH], "eth"), get_eth_info(), false);
670                 struct battery *current_battery;
671                 SIMPLEQ_FOREACH(current_battery, &batteries, batteries) {
672                         write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info(current_battery->path), false);
673                 }
674
675                 /* Get load */
676 #ifdef LINUX
677                 int load_avg;
678                 if ((load_avg = open("/proc/loadavg", O_RDONLY)) == -1)
679                         die("Could not open /proc/loadavg\n");
680                 (void)read(load_avg, part, sizeof(part));
681                 (void)close(load_avg);
682                 *skip_character(part, ' ', 3) = '\0';
683 #else
684                 /* TODO: correctly check for NetBSD, check if it works the same on *BSD */
685                 struct loadavg load;
686                 size_t length = sizeof(struct loadavg);
687                 int mib[2] = { CTL_VM, VM_LOADAVG };
688                 if (sysctl(mib, 2, &load, &length, NULL, 0) < 0)
689                         die("Could not sysctl({ CTL_VM, VM_LOADAVG })\n");
690                 double scale = load.fscale;
691                 (void)snprintf(part, sizeof(part), "%.02f %.02f %.02f",
692                                 (double)load.ldavg[0] / scale,
693                                 (double)load.ldavg[1] / scale,
694                                 (double)load.ldavg[2] / scale);
695 #endif
696                 write_to_statusbar(concat(order[ORDER_LOAD], "load"), part, !time_format);
697
698                 if (time_format) {
699                         /* Get date & time */
700                         time_t current_time = time(NULL);
701                         struct tm *current_tm = localtime(&current_time);
702                         (void)strftime(part, sizeof(part), time_format, current_tm);
703                         write_to_statusbar(concat(order[ORDER_TIME], "time"), part, true);
704                 }
705
706                 sleep(interval);
707         }
708 }