My slstatus configuration
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

863 líneas
17 KiB

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