slstatus.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* See LICENSE file for copyright and license details. */
  2. #include <err.h>
  3. #include <fcntl.h>
  4. #include <ifaddrs.h>
  5. #include <limits.h>
  6. #include <linux/wireless.h>
  7. #include <locale.h>
  8. #include <netdb.h>
  9. #include <pwd.h>
  10. #include <signal.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/stat.h>
  17. #include <sys/statvfs.h>
  18. #include <sys/socket.h>
  19. #include <sys/soundcard.h>
  20. #include <sys/sysinfo.h>
  21. #include <sys/types.h>
  22. #include <sys/utsname.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include <X11/Xlib.h>
  26. #include "arg.h"
  27. struct arg {
  28. const char *(*func)();
  29. const char *fmt;
  30. const char *args;
  31. };
  32. static const char *bprintf(const char *fmt, ...);
  33. static const char *battery_perc(const char *bat);
  34. static const char *battery_power(const char *bat);
  35. static const char *battery_state(const char *bat);
  36. static const char *cpu_freq(void);
  37. static const char *cpu_perc(void);
  38. static const char *datetime(const char *fmt);
  39. static const char *disk_free(const char *mnt);
  40. static const char *disk_perc(const char *mnt);
  41. static const char *disk_total(const char *mnt);
  42. static const char *disk_used(const char *mnt);
  43. static const char *entropy(void);
  44. static const char *gid(void);
  45. static const char *hostname(void);
  46. static const char *ip(const char *iface);
  47. static const char *kernel_release(void);
  48. static const char *keyboard_indicators(void);
  49. static const char *load_avg(void);
  50. static const char *ram_free(void);
  51. static const char *ram_perc(void);
  52. static const char *ram_used(void);
  53. static const char *ram_total(void);
  54. static const char *run_command(const char *cmd);
  55. static const char *swap_free(void);
  56. static const char *swap_perc(void);
  57. static const char *swap_used(void);
  58. static const char *swap_total(void);
  59. static const char *temp(const char *file);
  60. static const char *uid(void);
  61. static const char *uptime(void);
  62. static const char *username(void);
  63. static const char *vol_perc(const char *card);
  64. static const char *wifi_perc(const char *iface);
  65. static const char *wifi_essid(const char *iface);
  66. static void sighandler(const int signo);
  67. static void usage(const int eval);
  68. char *argv0;
  69. static unsigned short int delay = 0;
  70. static unsigned short int done;
  71. static unsigned short int dflag, oflag, nflag;
  72. static Display *dpy;
  73. #include "config.h"
  74. static char buf[MAXLEN];
  75. static const char *
  76. bprintf(const char *fmt, ...)
  77. {
  78. va_list ap;
  79. size_t len;
  80. va_start(ap, fmt);
  81. len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
  82. va_end(ap);
  83. if (len >= sizeof(buf))
  84. buf[sizeof(buf)-1] = '\0';
  85. return buf;
  86. }
  87. static const char *
  88. battery_perc(const char *bat)
  89. {
  90. int perc;
  91. char path[PATH_MAX];
  92. FILE *fp;
  93. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
  94. fp = fopen(path, "r");
  95. if (fp == NULL) {
  96. warn("Failed to open file %s", path);
  97. return UNKNOWN_STR;
  98. }
  99. fscanf(fp, "%i", &perc);
  100. fclose(fp);
  101. return bprintf("%d", perc);
  102. }
  103. static const char *
  104. battery_power(const char *bat)
  105. {
  106. char path[PATH_MAX];
  107. FILE *fp;
  108. int watts;
  109. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now");
  110. fp = fopen(path, "r");
  111. if (fp == NULL) {
  112. warn("Failed to open file %s", path);
  113. return UNKNOWN_STR;
  114. }
  115. fscanf(fp, "%i", &watts);
  116. fclose(fp);
  117. return bprintf("%d", (watts + 500000) / 1000000);
  118. }
  119. static const char *
  120. battery_state(const char *bat)
  121. {
  122. char path[PATH_MAX];
  123. char state[12];
  124. FILE *fp;
  125. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
  126. fp = fopen(path, "r");
  127. if (fp == NULL) {
  128. warn("Failed to open file %s", path);
  129. return UNKNOWN_STR;
  130. }
  131. fscanf(fp, "%12s", state);
  132. fclose(fp);
  133. if (strcmp(state, "Charging") == 0) {
  134. return "+";
  135. } else if (strcmp(state, "Discharging") == 0) {
  136. return "-";
  137. } else if (strcmp(state, "Full") == 0) {
  138. return "=";
  139. } else if (strcmp(state, "Unknown") == 0) {
  140. return "/";
  141. } else {
  142. return "?";
  143. }
  144. }
  145. static const char *
  146. cpu_freq(void)
  147. {
  148. int freq;
  149. FILE *fp;
  150. fp = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
  151. if (fp == NULL) {
  152. warn("Failed to open file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
  153. return UNKNOWN_STR;
  154. }
  155. fscanf(fp, "%i", &freq);
  156. fclose(fp);
  157. return bprintf("%d", (freq + 500) / 1000);
  158. }
  159. static const char *
  160. cpu_perc(void)
  161. {
  162. int perc;
  163. long double a[4], b[4];
  164. FILE *fp;
  165. fp = fopen("/proc/stat", "r");
  166. if (fp == NULL) {
  167. warn("Failed to open file /proc/stat");
  168. return UNKNOWN_STR;
  169. }
  170. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
  171. fclose(fp);
  172. delay++;
  173. sleep(delay);
  174. fp = fopen("/proc/stat", "r");
  175. if (fp == NULL) {
  176. warn("Failed to open file /proc/stat");
  177. return UNKNOWN_STR;
  178. }
  179. fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
  180. fclose(fp);
  181. perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
  182. return bprintf("%d", perc);
  183. }
  184. static const char *
  185. datetime(const char *fmt)
  186. {
  187. time_t t;
  188. t = time(NULL);
  189. if (strftime(buf, sizeof(buf), fmt, localtime(&t)) == 0)
  190. return UNKNOWN_STR;
  191. return buf;
  192. }
  193. static const char *
  194. disk_free(const char *mnt)
  195. {
  196. struct statvfs fs;
  197. if (statvfs(mnt, &fs) < 0) {
  198. warn("Failed to get filesystem info");
  199. return UNKNOWN_STR;
  200. }
  201. return bprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
  202. }
  203. static const char *
  204. disk_perc(const char *mnt)
  205. {
  206. int perc;
  207. struct statvfs fs;
  208. if (statvfs(mnt, &fs) < 0) {
  209. warn("Failed to get filesystem info");
  210. return UNKNOWN_STR;
  211. }
  212. perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
  213. return bprintf("%d", perc);
  214. }
  215. static const char *
  216. disk_total(const char *mnt)
  217. {
  218. struct statvfs fs;
  219. if (statvfs(mnt, &fs) < 0) {
  220. warn("Failed to get filesystem info");
  221. return UNKNOWN_STR;
  222. }
  223. return bprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
  224. }
  225. static const char *
  226. disk_used(const char *mnt)
  227. {
  228. struct statvfs fs;
  229. if (statvfs(mnt, &fs) < 0) {
  230. warn("Failed to get filesystem info");
  231. return UNKNOWN_STR;
  232. }
  233. return bprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
  234. }
  235. static const char *
  236. entropy(void)
  237. {
  238. int num;
  239. FILE *fp;
  240. fp= fopen("/proc/sys/kernel/random/entropy_avail", "r");
  241. if (fp == NULL) {
  242. warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
  243. return UNKNOWN_STR;
  244. }
  245. fscanf(fp, "%d", &num);
  246. fclose(fp);
  247. return bprintf("%d", num);
  248. }
  249. static const char *
  250. gid(void)
  251. {
  252. return bprintf("%d", getgid());
  253. }
  254. static const char *
  255. hostname(void)
  256. {
  257. if (gethostname(buf, sizeof(buf)) == -1) {
  258. warn("hostname");
  259. return UNKNOWN_STR;
  260. }
  261. return buf;
  262. }
  263. static const char *
  264. ip(const char *iface)
  265. {
  266. struct ifaddrs *ifaddr, *ifa;
  267. int s;
  268. char host[NI_MAXHOST];
  269. if (getifaddrs(&ifaddr) == -1) {
  270. warn("Failed to get IP address for interface %s", iface);
  271. return UNKNOWN_STR;
  272. }
  273. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  274. if (ifa->ifa_addr == NULL) {
  275. continue;
  276. }
  277. s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  278. if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
  279. if (s != 0) {
  280. warnx("Failed to get IP address for interface %s", iface);
  281. return UNKNOWN_STR;
  282. }
  283. return bprintf("%s", host);
  284. }
  285. }
  286. freeifaddrs(ifaddr);
  287. return UNKNOWN_STR;
  288. }
  289. static const char *
  290. kernel_release(void)
  291. {
  292. struct utsname udata;
  293. if (uname(&udata) < 0) {
  294. return UNKNOWN_STR;
  295. }
  296. return bprintf("%s", udata.release);
  297. }
  298. static const char *
  299. keyboard_indicators(void)
  300. {
  301. Display *dpy = XOpenDisplay(NULL);
  302. XKeyboardState state;
  303. XGetKeyboardControl(dpy, &state);
  304. XCloseDisplay(dpy);
  305. switch (state.led_mask) {
  306. case 1:
  307. return "c";
  308. case 2:
  309. return "n";
  310. case 3:
  311. return "cn";
  312. default:
  313. return "";
  314. }
  315. }
  316. static const char *
  317. load_avg(void)
  318. {
  319. double avgs[3];
  320. if (getloadavg(avgs, 3) < 0) {
  321. warnx("Failed to get the load avg");
  322. return UNKNOWN_STR;
  323. }
  324. return bprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
  325. }
  326. static const char *
  327. ram_free(void)
  328. {
  329. long free;
  330. FILE *fp;
  331. fp = fopen("/proc/meminfo", "r");
  332. if (fp == NULL) {
  333. warn("Failed to open file /proc/meminfo");
  334. return UNKNOWN_STR;
  335. }
  336. fscanf(fp, "MemFree: %ld kB\n", &free);
  337. fclose(fp);
  338. return bprintf("%f", (float)free / 1024 / 1024);
  339. }
  340. static const char *
  341. ram_perc(void)
  342. {
  343. long total, free, buffers, cached;
  344. FILE *fp;
  345. fp = fopen("/proc/meminfo", "r");
  346. if (fp == NULL) {
  347. warn("Failed to open file /proc/meminfo");
  348. return UNKNOWN_STR;
  349. }
  350. fscanf(fp, "MemTotal: %ld kB\n", &total);
  351. fscanf(fp, "MemFree: %ld kB\n", &free);
  352. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  353. fscanf(fp, "Cached: %ld kB\n", &cached);
  354. fclose(fp);
  355. return bprintf("%d", 100 * ((total - free) - (buffers + cached)) / total);
  356. }
  357. static const char *
  358. ram_total(void)
  359. {
  360. long total;
  361. FILE *fp;
  362. fp = fopen("/proc/meminfo", "r");
  363. if (fp == NULL) {
  364. warn("Failed to open file /proc/meminfo");
  365. return UNKNOWN_STR;
  366. }
  367. fscanf(fp, "MemTotal: %ld kB\n", &total);
  368. fclose(fp);
  369. return bprintf("%f", (float)total / 1024 / 1024);
  370. }
  371. static const char *
  372. ram_used(void)
  373. {
  374. long free, total, buffers, cached;
  375. FILE *fp;
  376. fp = fopen("/proc/meminfo", "r");
  377. if (fp == NULL) {
  378. warn("Failed to open file /proc/meminfo");
  379. return UNKNOWN_STR;
  380. }
  381. fscanf(fp, "MemTotal: %ld kB\n", &total);
  382. fscanf(fp, "MemFree: %ld kB\n", &free);
  383. fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
  384. fscanf(fp, "Cached: %ld kB\n", &cached);
  385. fclose(fp);
  386. return bprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024);
  387. }
  388. static const char *
  389. run_command(const char *cmd)
  390. {
  391. char *nlptr;
  392. FILE *fp;
  393. fp = popen(cmd, "r");
  394. if (fp == NULL) {
  395. warn("Failed to get command output for %s", cmd);
  396. return UNKNOWN_STR;
  397. }
  398. fgets(buf, sizeof(buf) - 1, fp);
  399. pclose(fp);
  400. if ((nlptr = strrchr(buf, '\n')) != NULL)
  401. nlptr[0] = '\0';
  402. return buf[0] ? buf : UNKNOWN_STR;
  403. }
  404. static const char *
  405. swap_free(void)
  406. {
  407. long total, free;
  408. FILE *fp;
  409. size_t bytes_read;
  410. char *match;
  411. fp = fopen("/proc/meminfo", "r");
  412. if (fp == NULL) {
  413. warn("Failed to open file /proc/meminfo");
  414. return UNKNOWN_STR;
  415. }
  416. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  417. warn("swap_free: read error");
  418. fclose(fp);
  419. return UNKNOWN_STR;
  420. }
  421. fclose(fp);
  422. if ((match = strstr(buf, "SwapTotal")) == NULL)
  423. return UNKNOWN_STR;
  424. sscanf(match, "SwapTotal: %ld kB\n", &total);
  425. if ((match = strstr(buf, "SwapFree")) == NULL)
  426. return UNKNOWN_STR;
  427. sscanf(match, "SwapFree: %ld kB\n", &free);
  428. return bprintf("%f", (float)free / 1024 / 1024);
  429. }
  430. static const char *
  431. swap_perc(void)
  432. {
  433. long total, free, cached;
  434. FILE *fp;
  435. size_t bytes_read;
  436. char *match;
  437. fp = fopen("/proc/meminfo", "r");
  438. if (fp == NULL) {
  439. warn("Failed to open file /proc/meminfo");
  440. return UNKNOWN_STR;
  441. }
  442. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  443. warn("swap_perc: read error");
  444. fclose(fp);
  445. return UNKNOWN_STR;
  446. }
  447. fclose(fp);
  448. if ((match = strstr(buf, "SwapTotal")) == NULL)
  449. return UNKNOWN_STR;
  450. sscanf(match, "SwapTotal: %ld kB\n", &total);
  451. if ((match = strstr(buf, "SwapCached")) == NULL)
  452. return UNKNOWN_STR;
  453. sscanf(match, "SwapCached: %ld kB\n", &cached);
  454. if ((match = strstr(buf, "SwapFree")) == NULL)
  455. return UNKNOWN_STR;
  456. sscanf(match, "SwapFree: %ld kB\n", &free);
  457. return bprintf("%d", 100 * (total - free - cached) / total);
  458. }
  459. static const char *
  460. swap_total(void)
  461. {
  462. long total;
  463. FILE *fp;
  464. size_t bytes_read;
  465. char *match;
  466. fp = fopen("/proc/meminfo", "r");
  467. if (fp == NULL) {
  468. warn("Failed to open file /proc/meminfo");
  469. return UNKNOWN_STR;
  470. }
  471. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  472. warn("swap_total: read error");
  473. fclose(fp);
  474. return UNKNOWN_STR;
  475. }
  476. fclose(fp);
  477. if ((match = strstr(buf, "SwapTotal")) == NULL)
  478. return UNKNOWN_STR;
  479. sscanf(match, "SwapTotal: %ld kB\n", &total);
  480. return bprintf("%f", (float)total / 1024 / 1024);
  481. }
  482. static const char *
  483. swap_used(void)
  484. {
  485. long total, free, cached;
  486. FILE *fp;
  487. size_t bytes_read;
  488. char *match;
  489. fp = fopen("/proc/meminfo", "r");
  490. if (fp == NULL) {
  491. warn("Failed to open file /proc/meminfo");
  492. return UNKNOWN_STR;
  493. }
  494. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  495. warn("swap_used: read error");
  496. fclose(fp);
  497. return UNKNOWN_STR;
  498. }
  499. fclose(fp);
  500. if ((match = strstr(buf, "SwapTotal")) == NULL)
  501. return UNKNOWN_STR;
  502. sscanf(match, "SwapTotal: %ld kB\n", &total);
  503. if ((match = strstr(buf, "SwapCached")) == NULL)
  504. return UNKNOWN_STR;
  505. sscanf(match, "SwapCached: %ld kB\n", &cached);
  506. if ((match = strstr(buf, "SwapFree")) == NULL)
  507. return UNKNOWN_STR;
  508. sscanf(match, "SwapFree: %ld kB\n", &free);
  509. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  510. }
  511. static const char *
  512. temp(const char *file)
  513. {
  514. int temp;
  515. FILE *fp;
  516. fp = fopen(file, "r");
  517. if (fp == NULL) {
  518. warn("Failed to open file %s", file);
  519. return UNKNOWN_STR;
  520. }
  521. fscanf(fp, "%d", &temp);
  522. fclose(fp);
  523. return bprintf("%d", temp / 1000);
  524. }
  525. static const char *
  526. uptime(void)
  527. {
  528. struct sysinfo info;
  529. int h = 0;
  530. int m = 0;
  531. sysinfo(&info);
  532. h = info.uptime / 3600;
  533. m = (info.uptime - h * 3600 ) / 60;
  534. return bprintf("%dh %dm", h, m);
  535. }
  536. static const char *
  537. username(void)
  538. {
  539. struct passwd *pw = getpwuid(geteuid());
  540. if (pw == NULL) {
  541. warn("Failed to get username");
  542. return UNKNOWN_STR;
  543. }
  544. return bprintf("%s", pw->pw_name);
  545. }
  546. static const char *
  547. uid(void)
  548. {
  549. return bprintf("%d", geteuid());
  550. }
  551. static const char *
  552. vol_perc(const char *card)
  553. {
  554. unsigned int i;
  555. int v, afd, devmask;
  556. char *vnames[] = SOUND_DEVICE_NAMES;
  557. afd = open(card, O_RDONLY | O_NONBLOCK);
  558. if (afd == -1) {
  559. warn("Cannot open %s", card);
  560. return UNKNOWN_STR;
  561. }
  562. if (ioctl(afd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
  563. warn("Cannot get volume for %s", card);
  564. close(afd);
  565. return UNKNOWN_STR;
  566. }
  567. for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
  568. if (devmask & (1 << i) && !strcmp("vol", vnames[i])) {
  569. if (ioctl(afd, MIXER_READ(i), &v) == -1) {
  570. warn("vol_perc: ioctl");
  571. close(afd);
  572. return UNKNOWN_STR;
  573. }
  574. }
  575. }
  576. close(afd);
  577. return bprintf("%d", v & 0xff);
  578. }
  579. static const char *
  580. wifi_perc(const char *iface)
  581. {
  582. int perc;
  583. char *datastart;
  584. char path[PATH_MAX];
  585. char status[5];
  586. FILE *fp;
  587. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
  588. fp = fopen(path, "r");
  589. if (fp == NULL) {
  590. warn("Failed to open file %s", path);
  591. return UNKNOWN_STR;
  592. }
  593. fgets(status, 5, fp);
  594. fclose(fp);
  595. if(strcmp(status, "up\n") != 0) {
  596. return UNKNOWN_STR;
  597. }
  598. fp = fopen("/proc/net/wireless", "r");
  599. if (fp == NULL) {
  600. warn("Failed to open file /proc/net/wireless");
  601. return UNKNOWN_STR;
  602. }
  603. fgets(buf, sizeof(buf) - 1, fp);
  604. fgets(buf, sizeof(buf) - 1, fp);
  605. fgets(buf, sizeof(buf) - 1, fp);
  606. fclose(fp);
  607. if ((datastart = strstr(buf, iface)) == NULL)
  608. return UNKNOWN_STR;
  609. datastart = (datastart+(strlen(iface)+1));
  610. sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
  611. return bprintf("%d", perc);
  612. }
  613. static const char *
  614. wifi_essid(const char *iface)
  615. {
  616. static char id[IW_ESSID_MAX_SIZE+1];
  617. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  618. struct iwreq wreq;
  619. memset(&wreq, 0, sizeof(struct iwreq));
  620. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  621. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  622. if (sockfd == -1) {
  623. warn("Failed to get ESSID for interface %s", iface);
  624. return UNKNOWN_STR;
  625. }
  626. wreq.u.essid.pointer = id;
  627. if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
  628. warn("Failed to get ESSID for interface %s", iface);
  629. return UNKNOWN_STR;
  630. }
  631. close(sockfd);
  632. if (strcmp(id, "") == 0)
  633. return UNKNOWN_STR;
  634. else
  635. return id;
  636. }
  637. static void
  638. sighandler(const int signo)
  639. {
  640. if (signo == SIGTERM || signo == SIGINT) {
  641. done = 1;
  642. }
  643. }
  644. static void
  645. usage(const int eval)
  646. {
  647. fprintf(stderr, "usage: %s [-d] [-o] [-n] [-v] [-h]\n", argv0);
  648. exit(eval);
  649. }
  650. int
  651. main(int argc, char *argv[])
  652. {
  653. unsigned short int i;
  654. char status_string[MAXLEN];
  655. char *element;
  656. struct arg argument;
  657. struct sigaction act;
  658. size_t len;
  659. ARGBEGIN {
  660. case 'd':
  661. dflag = 1;
  662. break;
  663. case 'o':
  664. oflag = 1;
  665. break;
  666. case 'n':
  667. nflag = 1;
  668. break;
  669. case 'v':
  670. printf("slstatus (C) 2016-2017 slstatus engineers\n");
  671. return 0;
  672. case 'h':
  673. usage(0);
  674. default:
  675. usage(1);
  676. } ARGEND
  677. if ((dflag && oflag) || (dflag && nflag) || (oflag && nflag)) {
  678. usage(1);
  679. }
  680. if (dflag && daemon(1, 1) < 0) {
  681. err(1, "daemon");
  682. }
  683. memset(&act, 0, sizeof(act));
  684. act.sa_handler = sighandler;
  685. sigaction(SIGINT, &act, 0);
  686. sigaction(SIGTERM, &act, 0);
  687. if (!oflag) {
  688. dpy = XOpenDisplay(NULL);
  689. }
  690. setlocale(LC_ALL, "");
  691. while (!done) {
  692. status_string[0] = '\0';
  693. for (element = status_string, i = len = 0;
  694. i < sizeof(args) / sizeof(args[0]);
  695. ++i, element += len) {
  696. argument = args[i];
  697. len = snprintf(element, sizeof(status_string)-1 - len,
  698. argument.fmt,
  699. argument.func(argument.args));
  700. if (len >= sizeof(status_string)) {
  701. status_string[sizeof(status_string)-1] = '\0';
  702. break;
  703. }
  704. }
  705. if (oflag) {
  706. printf("%s\n", status_string);
  707. } else if (nflag) {
  708. printf("%s\n", status_string);
  709. done = 1;
  710. } else {
  711. XStoreName(dpy, DefaultRootWindow(dpy), status_string);
  712. XSync(dpy, False);
  713. }
  714. if ((UPDATE_INTERVAL - delay) <= 0) {
  715. delay = 0;
  716. continue;
  717. } else {
  718. sleep(UPDATE_INTERVAL - delay);
  719. delay = 0;
  720. }
  721. }
  722. if (!oflag) {
  723. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  724. XCloseDisplay(dpy);
  725. }
  726. return 0;
  727. }