slstatus.c 18 KB

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