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