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