My slstatus configuration
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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