diff options
Diffstat (limited to 'status.c')
| -rw-r--r-- | status.c | 100 |
1 files changed, 63 insertions, 37 deletions
@@ -1,17 +1,22 @@ #include <errno.h> #include <ifaddrs.h> #include <linux/wireless.h> -//#include <net/if.h> +#include <linux/net.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#define __USE_MISC #include <sys/time.h> #include <sys/ioctl.h> #include <sys/types.h> -#include <time.h> +#include <sys/socket.h> #include <unistd.h> +#include <time.h> +#include <netdb.h> +#include <netinet/in.h> +#include <arpa/inet.h> #define ELEMENT_SEPERATOR " " @@ -32,9 +37,9 @@ struct element { void date(char* buf) { time_t now = time(NULL); struct tm tm; - localtime_r(&now, &tm); + tm = *localtime(&now); - strftime(buf, ELEMENT_STRBUF_SZ, "%F %R", &tm); + strftime(buf, ELEMENT_STRBUF_SZ, "%Y-%m-%d %H:%M", &tm); } @@ -70,14 +75,17 @@ struct battery_status get_battery_status(const char* buf) { char charge_path[128]; char capacity_path[128]; - memset(charge_path, 0, 128); - memset(capacity_path, 0, 128); + char charge_str[512]; + char capacity_str[512]; int charge = 0; int capacity = 0; - char charge_str[512]; - char capacity_str[512]; + FILE* bat_charge; + FILE* bat_capacity; + + memset(charge_path, 0, 128); + memset(capacity_path, 0, 128); strcat(charge_path, path_prefix); strcat(charge_path, buf); @@ -87,16 +95,16 @@ struct battery_status get_battery_status(const char* buf) { strcat(capacity_path, buf); strcat(capacity_path, "/energy_full"); - FILE* bat_charge = fopen(charge_path, "r"); - FILE* bat_capacity = fopen(capacity_path, "r"); + bat_charge = fopen(charge_path, "r"); + bat_capacity = fopen(capacity_path, "r"); if (!bat_charge) { printf("%d: \"%s\" %s\n", errno, charge_path, strerror(errno)); - return (struct battery_status){.status = bat_unknown, .charge = -1}; + return (struct battery_status){bat_unknown, -1}; } if (!bat_capacity) { printf("%d: \"%s\" %s\n", errno, capacity_path, strerror(errno)); - return (struct battery_status){.status = bat_unknown, .charge = -1}; + return (struct battery_status){bat_unknown, -1}; } fread(charge_str, sizeof(char), 512, bat_charge); @@ -108,7 +116,7 @@ struct battery_status get_battery_status(const char* buf) { charge = atoi(charge_str); capacity = atoi(capacity_str); - return (struct battery_status){.status = bat_unknown, .charge = (float)charge / capacity}; + return (struct battery_status){bat_unknown, (float)charge / capacity}; } void get_battery1_status(char* buf) { @@ -139,7 +147,7 @@ void get_all_bat_status(char* buf) { int batlvl = (int)(s.charge*100.f) / 10; char* batlvl_icon = battery_level_icon[batlvl]; - //snprintf(buf, ELEMENT_STRBUF_SZ, "%.1f%%", 100.f * s.charge); + /*snprintf(buf, ELEMENT_STRBUF_SZ, "%.1f%%", 100.f * s.charge);*/ snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge); } @@ -150,16 +158,15 @@ void get_net_link_status(char* buf) { getifaddrs(&if_addr); - for (struct ifaddrs *ifa = if_addr; ifa != NULL; ifa = ifa->ifa_next) { - // Ignore loopback interface flag + /* Ignore loopback interface flag */ if (ifa->ifa_flags & IFF_LOOPBACK) continue; - // Ignore interfaces without an address + /* Ignore interfaces without an address */ if (ifa->ifa_addr == NULL) continue; - // Ignore everythings not an IPv4 link - // (AF_PACKET might be usefull if you want to display tx) + /* Ignore everythings not an IPv4 link */ + /* (AF_PACKET might be usefull if you want to display tx) */ if (ifa->ifa_addr->sa_family != AF_INET) continue; if (ifa->ifa_flags & IFF_UP) { @@ -167,8 +174,17 @@ void get_net_link_status(char* buf) { size_t l = strlen(ifa->ifa_name); struct iwreq wreq; int sock; + const size_t ipv6_charlen = 8*4+7; + char address[ipv6_charlen + 1]; // in case of ipv6, plus null-byte char id[IW_ESSID_MAX_SIZE+1]; + /* Get the IP address */ + memset(address, 0, ipv6_charlen + 1); + /* Use sa_family instead of hardcoding AF_INET, just in case we get ip6'ed */ + /* Does inet_ntop add a null-byte? */ + inet_ntop(ifa->ifa_addr->sa_family, (void*)&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, address, ipv6_charlen); + + /* Get the SSID */ memset(id, 0, IW_ESSID_MAX_SIZE+1); memset(&wreq, 0, sizeof(struct iwreq)); sock = socket(AF_INET, SOCK_DGRAM, 0); @@ -181,6 +197,8 @@ void get_net_link_status(char* buf) { ioctl(sock, SIOCGIWESSID, &wreq); + + strncpy(buf + n, ifa->ifa_name, l); size_t ssid_len = strlen(id); @@ -191,42 +209,47 @@ void get_net_link_status(char* buf) { l += ssid_len; buf[n + l + 2] = ')'; } + n += l; + + strcat(buf + n, address); + n += strlen(address); - strcat(buf + n + l, " "); - n += l + 2; + strcat(buf + n, " "); + + n += 2; } } } static struct element statusbar[] = { - //{get_battery0_status, {60,0}, 0}, - //{get_battery1_status, {60,0}, 0}, - {get_net_link_status, {5,0}, 0}, - {get_all_bat_status, {60,0}, 0}, - {date, {2,500000}, 0}, + {.f = get_net_link_status, .fire_interval = {5,0}, .fire_previous = {0}, .buf = {0} }, + {.f = get_all_bat_status, .fire_interval = {60,0}, .fire_previous = {0}, .buf = {0} }, + {.f = date, .fire_interval = {2,500000}, .fire_previous = {0}, .buf = {0} }, + }; -int main() { +int main(void) { const int num_elems = sizeof(statusbar)/sizeof(statusbar[0]); struct timeval now; - const struct timeval one_minute = {.tv_sec = 60, .tv_usec = 0}; + const struct timeval one_minute = {60, 0}; while (true) { gettimeofday(&now, NULL); + unsigned i; - // Stall updating for at most 1 minute + /* Stall updating for at most 1 minute */ struct timeval next_update; timeradd(&now, &one_minute, &next_update); - for (unsigned i = 0; i < num_elems; i++) { + for (i = 0; i < num_elems; i++) { struct timeval next_fire; timeradd(&statusbar[i].fire_previous, &statusbar[i].fire_interval, &next_fire); if (timercmp(&next_fire, &now, >)) { - // Check if this is the next to-be-updated element + /* Check if this is the next to-be-updated element */ if (timercmp(&next_fire, &next_update, <)) { next_update = next_fire; } @@ -238,28 +261,31 @@ int main() { statusbar[i].f(statusbar[i].buf); statusbar[i].fire_previous = now; - // Check if this element needs to be refreshed next, again + /* Check if this element needs to be refreshed next, again */ timeradd(&statusbar[i].fire_previous, &statusbar[i].fire_interval, &next_fire); if (timercmp(&next_fire, &next_update, <)) { next_update = next_fire; } - //printf("[%ld.%ld] %s\n", statusbar[i].fire_interval.tv_sec, statusbar[i].fire_interval.tv_usec, (char*)statusbar[i].buf); + /* printf("[%ld.%ld] %s\n", + * statusbar[i].fire_interval.tv_sec, + * statusbar[i].fire_interval.tv_usec, + * (char*)statusbar[i].buf); + */ } - unsigned c = 0; char buf[STATUS_STRBUF_SZ]; memset(buf, 0, STATUS_STRBUF_SZ); - for (unsigned i = 0; i < num_elems; i++) { + for (i = 0; i < num_elems; i++) { strcat(buf, statusbar[i].buf); if (i != num_elems - 1) strcat(buf, ELEMENT_SEPERATOR); } - //strcat(buf, "\0"); + /* strcat(buf, "\0"); */ - fputs(buf, stdout); + puts(buf); fflush(stdout); timersub(&next_update, &now, &next_update); |
