]> git.sur5r.net Git - i3/i3status/commitdiff
Document everything, wrap comments at 80 characters, handle socket() return code
authorMichael Stapelberg <michael+git@stapelberg.de>
Sun, 25 Jan 2009 17:57:54 +0000 (18:57 +0100)
committerMichael Stapelberg <michael+git@stapelberg.de>
Sun, 25 Jan 2009 17:57:54 +0000 (18:57 +0100)
wmiistatus.c

index 1c9360f77ea72ad9373461fec68df3c76e3679f3..afd3965938b465d7e40a6e622e729e57e0e9d63e 100644 (file)
@@ -2,7 +2,7 @@
  * Generates a status line for use with wmii or other minimal window managers
  *
  *
  * Generates a status line for use with wmii or other minimal window managers
  *
  *
- * Copyright (c) 2008 Michael Stapelberg and contributors
+ * Copyright (c) 2008-2009 Michael Stapelberg and contributors
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without modification,
@@ -121,6 +121,11 @@ static void create_file(const char *name) {
        (void)close(fd);
 }
 
        (void)close(fd);
 }
 
+/*
+ * Waits until wmii_path/rbar exists (= the filesystem gets mounted),
+ * cleans up all files and creates the needed files
+ *
+ */
 static void setup(void) {
        unsigned int i;
        struct stat statbuf;
 static void setup(void) {
        unsigned int i;
        struct stat statbuf;
@@ -191,6 +196,11 @@ void die(const char *fmt, ...) {
        exit(EXIT_FAILURE);
 }
 
        exit(EXIT_FAILURE);
 }
 
+/*
+ * Skip the given character for maximum 'amount' times, returns
+ * a pointer to the first non-'character' character in 'input'.
+ *
+ */
 static char *skip_character(char *input, char character, int amount) {
        char *walk;
        size_t len = strlen(input);
 static char *skip_character(char *input, char character, int amount) {
        char *walk;
        size_t len = strlen(input);
@@ -204,8 +214,9 @@ static char *skip_character(char *input, char character, int amount) {
 }
 
 /*
 }
 
 /*
- * Get battery information from /sys. Note that it uses the design capacity to calculate the percentage,
- * not the full capacity.
+ * Get battery information from /sys. Note that it uses the design capacity to
+ * calculate the percentage, not the last full capacity, so you can see how
+ * worn off your battery is.
  *
  */
 static char *get_battery_info() {
  *
  */
 static char *get_battery_info() {
@@ -317,13 +328,19 @@ static char *get_wireless_info() {
        return part;
 }
 
        return part;
 }
 
+/*
+ * Return the IP address for the given interface or "no IP" if the
+ * interface is up and running but hasn't got an IP address yet
+ *
+ */
 static const char *get_ip_address(const char *interface) {
        static char part[512];
        struct ifreq ifr;
        int fd;
        memset(part, 0, sizeof(part));
 
 static const char *get_ip_address(const char *interface) {
        static char part[512];
        struct ifreq ifr;
        int fd;
        memset(part, 0, sizeof(part));
 
-       fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+               die("Could not create socket");
 
        strcpy(ifr.ifr_name, interface);
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
 
        strcpy(ifr.ifr_name, interface);
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
@@ -335,7 +352,7 @@ static const char *get_ip_address(const char *interface) {
                return NULL;
        }
 
                return NULL;
        }
 
-       strcpy(ifr.ifr_name, interface);
+       (void)strcpy(ifr.ifr_name, interface);
        ifr.ifr_addr.sa_family = AF_INET;
        if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
                struct sockaddr_in addr;
        ifr.ifr_addr.sa_family = AF_INET;
        if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
                struct sockaddr_in addr;
@@ -349,12 +366,17 @@ static const char *get_ip_address(const char *interface) {
        return part;
 }
 
        return part;
 }
 
+/*
+ * Combines ethernet IP addresses and speed (if requested) for displaying
+ *
+ */
 static char *get_eth_info() {
        static char part[512];
        const char *ip_address = get_ip_address(eth_interface);
        int ethspeed = 0;
 
        if (get_ethspeed) {
 static char *get_eth_info() {
        static char part[512];
        const char *ip_address = get_ip_address(eth_interface);
        int ethspeed = 0;
 
        if (get_ethspeed) {
+               /* This code path requires root privileges */
                struct ifreq ifr;
                struct ethtool_cmd ecmd;
                int fd, err;
                struct ifreq ifr;
                struct ethtool_cmd ecmd;
                int fd, err;