slstatus.c 18 KB

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