My slstatus configuration
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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