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