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.
 
 
 
 

226 righe
3.8 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "../util.h"
  6. #if defined(__linux__)
  7. static size_t
  8. pread(const char *path, char *buf, size_t bufsiz)
  9. {
  10. FILE *fp;
  11. size_t bytes_read;
  12. if (!(fp = fopen(path, "r"))) {
  13. warn("fopen '%s':", path);
  14. return 0;
  15. }
  16. if (!(bytes_read = fread(buf, sizeof(char), bufsiz, fp))) {
  17. warn("fread '%s':", path);
  18. fclose(fp);
  19. return 0;
  20. }
  21. fclose(fp);
  22. buf[bytes_read] = '\0';
  23. return bytes_read;
  24. }
  25. const char *
  26. swap_free(void)
  27. {
  28. long total, free;
  29. char *match;
  30. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  31. return NULL;
  32. }
  33. if (!(match = strstr(buf, "SwapTotal"))) {
  34. return NULL;
  35. }
  36. sscanf(match, "SwapTotal: %ld kB\n", &total);
  37. if (!(match = strstr(buf, "SwapFree"))) {
  38. return NULL;
  39. }
  40. sscanf(match, "SwapFree: %ld kB\n", &free);
  41. return fmt_human(free * 1024, 1024);
  42. }
  43. const char *
  44. swap_perc(void)
  45. {
  46. long total, free, cached;
  47. char *match;
  48. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  49. return NULL;
  50. }
  51. if (!(match = strstr(buf, "SwapTotal"))) {
  52. return NULL;
  53. }
  54. sscanf(match, "SwapTotal: %ld kB\n", &total);
  55. if (!(match = strstr(buf, "SwapCached"))) {
  56. return NULL;
  57. }
  58. sscanf(match, "SwapCached: %ld kB\n", &cached);
  59. if (!(match = strstr(buf, "SwapFree"))) {
  60. return NULL;
  61. }
  62. sscanf(match, "SwapFree: %ld kB\n", &free);
  63. if (total == 0) {
  64. return NULL;
  65. }
  66. return bprintf("%d", 100 * (total - free - cached) / total);
  67. }
  68. const char *
  69. swap_total(void)
  70. {
  71. long total;
  72. char *match;
  73. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  74. return NULL;
  75. }
  76. if (!(match = strstr(buf, "SwapTotal"))) {
  77. return NULL;
  78. }
  79. sscanf(match, "SwapTotal: %ld kB\n", &total);
  80. return fmt_human(total * 1024, 1024);
  81. }
  82. const char *
  83. swap_used(void)
  84. {
  85. long total, free, cached;
  86. char *match;
  87. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  88. return NULL;
  89. }
  90. if (!(match = strstr(buf, "SwapTotal"))) {
  91. return NULL;
  92. }
  93. sscanf(match, "SwapTotal: %ld kB\n", &total);
  94. if (!(match = strstr(buf, "SwapCached"))) {
  95. return NULL;
  96. }
  97. sscanf(match, "SwapCached: %ld kB\n", &cached);
  98. if (!(match = strstr(buf, "SwapFree"))) {
  99. return NULL;
  100. }
  101. sscanf(match, "SwapFree: %ld kB\n", &free);
  102. return fmt_human((total - free - cached) * 1024, 1024);
  103. }
  104. #elif defined(__OpenBSD__)
  105. #include <stdlib.h>
  106. #include <sys/param.h> /* dbtob */
  107. #include <sys/swap.h>
  108. #include <sys/types.h>
  109. #include <unistd.h>
  110. static int
  111. getstats(int *total, int *used)
  112. {
  113. struct swapent *sep, *fsep;
  114. int rnswap, nswap, i;
  115. if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) {
  116. warn("swaptctl 'SWAP_NSWAP':");
  117. return 1;
  118. }
  119. if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) {
  120. warn("calloc 'nswap':");
  121. return 1;
  122. }
  123. if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) {
  124. warn("swapctl 'SWAP_STATA':");
  125. return 1;
  126. }
  127. if (nswap != rnswap) {
  128. warn("getstats: SWAP_STATS != SWAP_NSWAP");
  129. return 1;
  130. }
  131. *total = 0;
  132. *used = 0;
  133. for (i = 0; i < rnswap; i++) {
  134. *total += sep->se_nblks >> 1;
  135. *used += sep->se_inuse >> 1;
  136. }
  137. free(fsep);
  138. return 0;
  139. }
  140. const char *
  141. swap_free(void)
  142. {
  143. int total, used;
  144. if (getstats(&total, &used)) {
  145. return NULL;
  146. }
  147. return fmt_human((total - used) * 1024, 1024);
  148. }
  149. const char *
  150. swap_perc(void)
  151. {
  152. int total, used;
  153. if (getstats(&total, &used)) {
  154. return NULL;
  155. }
  156. if (total == 0) {
  157. return NULL;
  158. }
  159. return bprintf("%d", 100 * used / total);
  160. }
  161. const char *
  162. swap_total(void)
  163. {
  164. int total, used;
  165. if (getstats(&total, &used)) {
  166. return NULL;
  167. }
  168. return fmt_human(total * 1024, 1024);
  169. }
  170. const char *
  171. swap_used(void)
  172. {
  173. int total, used;
  174. if (getstats(&total, &used)) {
  175. return NULL;
  176. }
  177. return fmt_human(used * 1024, 1024);
  178. }
  179. #endif