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