My slstatus configuration
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

218 linhas
3.7 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 void
  111. getstats(int *total, int *used)
  112. {
  113. struct swapent *sep, *fsep;
  114. int rnswap, nswap, i;
  115. nswap = swapctl(SWAP_NSWAP, 0, 0);
  116. if (nswap < 1) {
  117. warn("swaptctl 'SWAP_NSWAP':");
  118. }
  119. fsep = sep = calloc(nswap, sizeof(*sep));
  120. if (!sep) {
  121. warn("calloc 'nswap':");
  122. }
  123. rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
  124. if (rnswap < 0) {
  125. warn("swapctl 'SWAP_STATA':");
  126. }
  127. if (nswap != rnswap) {
  128. warn("getstats: SWAP_STATS != SWAP_NSWAP");
  129. }
  130. *total = 0;
  131. *used = 0;
  132. for (i = 0; i < rnswap; i++) {
  133. *total += sep->se_nblks >> 1;
  134. *used += sep->se_inuse >> 1;
  135. }
  136. free(fsep);
  137. }
  138. const char *
  139. swap_free(void)
  140. {
  141. int total, used;
  142. getstats(&total, &used);
  143. return fmt_human((total - used) * 1024, 1024);
  144. }
  145. const char *
  146. swap_perc(void)
  147. {
  148. int total, used;
  149. getstats(&total, &used);
  150. if (total == 0) {
  151. return NULL;
  152. }
  153. return bprintf("%d", 100 * used / total);
  154. }
  155. const char *
  156. swap_total(void)
  157. {
  158. int total, used;
  159. getstats(&total, &used);
  160. return fmt_human(total * 1024, 1024);
  161. }
  162. const char *
  163. swap_used(void)
  164. {
  165. int total, used;
  166. getstats(&total, &used);
  167. return fmt_human(used * 1024, 1024);
  168. }
  169. #endif