* dwmstatus for OpenBSD
       
   URI git clone git://git.codevoid.de/dwmstatus-sdk
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
   DIR commit aacfb04966639f402407af14bb8f421c09238f0b
   DIR parent c891fa7936514f8933b0bd23dadac36149d67f19
   URI Author: Stefan Hagen <github@textmail.me>
       Date:   Sun, 20 May 2018 22:33:25 +0200
       
       Retab (argh, tabs)
       
       Diffstat:
         M dwmstatus.c                         |     256 ++++++++++++++++---------------
       
       1 file changed, 129 insertions(+), 127 deletions(-)
       ---
   DIR diff --git a/dwmstatus.c b/dwmstatus.c
       @@ -1,6 +1,8 @@
        /*
         * Copy me if you can.
         * by 20h
       + *
       + * done. ~sdk
         */
        
        #define _BSD_SOURCE
       @@ -18,37 +20,37 @@
        
        #include <X11/Xlib.h>
        
       -#define ZEROC 2731
       -#define CONVC(x) ((x)-ZEROC) / 10
       -
       +// conversation presets
       +#define ZEROCTOC(x) ((x)-2731) / 10
       +#define BTOMB(x) ((x)*1048576
       +#define BTOGB(x) ((x)*1073741824
        
        char *tzberlin = "Europe/Berlin";
        
        static Display *dpy;
        
       -
        char *
        smprintf(char *fmt, ...)
        {
       -        va_list fmtargs;
       -        char *ret;
       -        int len;
       +    va_list fmtargs;
       +    char *ret;
       +    int len;
        
       -        va_start(fmtargs, fmt);
       -        len = vsnprintf(NULL, 0, fmt, fmtargs);
       -        va_end(fmtargs);
       +    va_start(fmtargs, fmt);
       +    len = vsnprintf(NULL, 0, fmt, fmtargs);
       +    va_end(fmtargs);
        
       -        ret = malloc(++len);
       -        if (ret == NULL) {
       -                perror("malloc");
       -                exit(1);
       -        }
       +    ret = malloc(++len);
       +    if (ret == NULL) {
       +        perror("malloc");
       +        exit(1);
       +    }
        
       -        va_start(fmtargs, fmt);
       -        vsnprintf(ret, len, fmt, fmtargs);
       -        va_end(fmtargs);
       +    va_start(fmtargs, fmt);
       +    vsnprintf(ret, len, fmt, fmtargs);
       +    va_end(fmtargs);
        
       -        return ret;
       +    return ret;
        }
        
        char *
       @@ -75,163 +77,163 @@ getsysctl(char *input_str) {
        void
        settz(char *tzname)
        {
       -        setenv("TZ", tzname, 1);
       +    setenv("TZ", tzname, 1);
        }
        
        char *
        mktimes(char *fmt, char *tzname)
        {
       -        char buf[129];
       -        time_t tim;
       -        struct tm *timtm;
       -
       -        settz(tzname);
       -        tim = time(NULL);
       -        timtm = localtime(&tim);
       -        if (timtm == NULL)
       -                return smprintf("");
       -
       -        if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
       -                fprintf(stderr, "strftime == 0\n");
       -                return smprintf("");
       -        }
       -
       -        return smprintf("%s", buf);
       +    char buf[129];
       +    time_t tim;
       +    struct tm *timtm;
       +
       +    settz(tzname);
       +    tim = time(NULL);
       +    timtm = localtime(&tim);
       +    if (timtm == NULL)
       +        return smprintf("");
       +
       +    if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
       +        fprintf(stderr, "strftime == 0\n");
       +        return smprintf("");
       +    }
       +
       +    return smprintf("%s", buf);
        }
        
        void
        setstatus(char *str)
        {
       -        XStoreName(dpy, DefaultRootWindow(dpy), str);
       -        XSync(dpy, False);
       +    XStoreName(dpy, DefaultRootWindow(dpy), str);
       +    XSync(dpy, False);
        }
        
        char *
        loadavg(void)
        {
       -        double avgs[3];
       +    double avgs[3];
        
       -        if (getloadavg(avgs, 3) < 0)
       -                return smprintf("");
       +    if (getloadavg(avgs, 3) < 0)
       +        return smprintf("");
        
       -        return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
       +    return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
        }
        
        char *
        readfile(char *base, char *file)
        {
       -        char *path, line[513];
       -        FILE *fd;
       +    char *path, line[513];
       +    FILE *fd;
        
       -        memset(line, 0, sizeof(line));
       +    memset(line, 0, sizeof(line));
        
       -        path = smprintf("%s/%s", base, file);
       -        fd = fopen(path, "r");
       -        free(path);
       -        if (fd == NULL)
       -                return NULL;
       +    path = smprintf("%s/%s", base, file);
       +    fd = fopen(path, "r");
       +    free(path);
       +    if (fd == NULL)
       +        return NULL;
        
       -        if (fgets(line, sizeof(line)-1, fd) == NULL)
       -                return NULL;
       -        fclose(fd);
       +    if (fgets(line, sizeof(line)-1, fd) == NULL)
       +        return NULL;
       +    fclose(fd);
        
       -        return smprintf("%s", line);
       +    return smprintf("%s", line);
        }
        
        char *
        getbattery(char *base)
        {
       -        char *co, status;
       -        int descap, remcap;
       -
       -        descap = -1;
       -        remcap = -1;
       -
       -        co = readfile(base, "present");
       -        if (co == NULL)
       -                return smprintf("");
       -        if (co[0] != '1') {
       -                free(co);
       -                return smprintf("not present");
       -        }
       -        free(co);
       -
       -        co = readfile(base, "charge_full_design");
       -        if (co == NULL) {
       -                co = readfile(base, "energy_full_design");
       -                if (co == NULL)
       -                        return smprintf("");
       -        }
       -        sscanf(co, "%d", &descap);
       -        free(co);
       -
       -        co = readfile(base, "charge_now");
       -        if (co == NULL) {
       -                co = readfile(base, "energy_now");
       -                if (co == NULL)
       -                        return smprintf("");
       -        }
       -        sscanf(co, "%d", &remcap);
       -        free(co);
       -
       -        co = readfile(base, "status");
       -        if (!strncmp(co, "Discharging", 11)) {
       -                status = '-';
       -        } else if(!strncmp(co, "Charging", 8)) {
       -                status = '+';
       -        } else {
       -                status = '?';
       -        }
       -
       -        if (remcap < 0 || descap < 0)
       -                return smprintf("invalid");
       -
       -        return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status);
       +    char *co, status;
       +    int descap, remcap;
       +
       +    descap = -1;
       +    remcap = -1;
       +
       +    co = readfile(base, "present");
       +    if (co == NULL)
       +        return smprintf("");
       +    if (co[0] != '1') {
       +        free(co);
       +        return smprintf("not present");
       +    }
       +    free(co);
       +
       +    co = readfile(base, "charge_full_design");
       +    if (co == NULL) {
       +        co = readfile(base, "energy_full_design");
       +        if (co == NULL)
       +            return smprintf("");
       +    }
       +    sscanf(co, "%d", &descap);
       +    free(co);
       +
       +    co = readfile(base, "charge_now");
       +    if (co == NULL) {
       +        co = readfile(base, "energy_now");
       +        if (co == NULL)
       +            return smprintf("");
       +    }
       +    sscanf(co, "%d", &remcap);
       +    free(co);
       +
       +    co = readfile(base, "status");
       +    if (!strncmp(co, "Discharging", 11)) {
       +        status = '-';
       +    } else if(!strncmp(co, "Charging", 8)) {
       +        status = '+';
       +    } else {
       +        status = '?';
       +    }
       +
       +    if (remcap < 0 || descap < 0)
       +        return smprintf("invalid");
       +
       +    return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status);
        }
        
        char *
        gettemperature(char *base, char *sensor)
        {
       -        char *co;
       +    char *co;
        
       -        co = readfile(base, sensor);
       -        if (co == NULL)
       -                return smprintf("");
       -        return smprintf("%02.0f°C", atof(co) / 1000);
       +    co = readfile(base, sensor);
       +    if (co == NULL)
       +        return smprintf("");
       +    return smprintf("%02.0f°C", atof(co) / 1000);
        }
        
        int
        main(void)
        {
       -        char *status;
       -        char *datetime;
       -        char *cputemp;
       -        char *battery;
       +    char *status;
       +    char *datetime;
       +    char *cputemp;
       +    char *battery;
        
       -        if (!(dpy = XOpenDisplay(NULL))) {
       -                fprintf(stderr, "dwmstatus: cannot open display.\n");
       -                return 1;
       -        }
       +    if (!(dpy = XOpenDisplay(NULL))) {
       +        fprintf(stderr, "dwmstatus: cannot open display.\n");
       +        return 1;
       +    }
        
       -        for (;;sleep(60)) {
       -                datetime = mktimes("%d.%m.%Y %H:%M", tzberlin);
       +    for (;;sleep(60)) {
       +        datetime = mktimes("%d.%m.%Y %H:%M", tzberlin);
        
       -                cputemp  = smprintf("%i", CONVC((int)getsysctl("hw.acpi.thermal.tz0.temperature")));
       -                battery  = smprintf("%i", (int)getsysctl("hw.acpi.battery.life"));
       +        cputemp  = smprintf("%i", ZEROCTOC((int)getsysctl("hw.acpi.thermal.tz0.temperature")));
       +        battery  = smprintf("%i", (int)getsysctl("hw.acpi.battery.life"));
        
       -                status   = smprintf(" CPU:%s°C BAT:%s%% | %s %s",
       -                                cputemp, battery, datetime);
       +        status   = smprintf(" CPU:%s°C BAT:%s%% | %s %s",
       +                cputemp, battery, datetime);
        
       -                setstatus(status);
       +        setstatus(status);
        
       -                free(battery);
       -                free(cputemp);
       -                free(datetime);
       -                free(status);
       -        }
       +        free(battery);
       +        free(cputemp);
       +        free(datetime);
       +        free(status);
       +    }
        
       -        XCloseDisplay(dpy);
       +    XCloseDisplay(dpy);
        
       -        return 0;
       +    return 0;
        }