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