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