1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/*
* Copy me if you can.
* by 20h
*/
#define _BSD_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <X11/Xlib.h>
char *tzberlin = "Europe/Berlin";
static Display *dpy;
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if (ret == NULL) {
perror("malloc");
exit(1);
}
va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}
void
settz(char *tzname)
{
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);
}
void
setstatus(char *str)
{
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
char *
loadavg(void)
{
double avgs[3];
if (getloadavg(avgs, 3) < 0)
return smprintf("");
return smprintf("%.2f", avgs[1]);
}
char *
execscript(char *cmd)
{
FILE *fp;
char retval[1025], *rv;
memset(retval, 0, sizeof(retval));
fp = popen(cmd, "r");
if (fp == NULL)
return smprintf("");
rv = fgets(retval, sizeof(retval), fp);
pclose(fp);
if (rv == NULL)
return smprintf("");
retval[strlen(retval)-1] = '\0';
return smprintf("%s", retval);
}
int
main(void)
{
char *status;
char *avg;
char *tmbln;
char *ips;
char *track;
char *partition;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
return 1;
}
for (;;sleep(1)) {
avg = loadavg();
tmbln = mktimes("%a %d %b %H:%M", tzberlin);
ips = execscript("ip a | grep 'inet ' | grep -v 127.0.0.1 | tr -s ' ' | cut -f3 -d' '");
track = execscript("mpc -h core-a.lan | head -n 1 | grep -v volume || echo");
partition = execscript("df -lh | grep cryptroot | tr -s ' ' | cut -d ' ' -f 4");
status = smprintf(" %s | %s | %s | %s | %s ",
track, ips, avg, partition, tmbln);
setstatus(status);
free(avg);
free(tmbln);
free(ips);
free(track);
free(partition);
free(status);
}
XCloseDisplay(dpy);
return 0;
}
|