My slstatus configuration
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

6 лет назад
6 лет назад
6 лет назад
6 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. fprintf(stderr, "fopen '%s': %s\n", path,
  14. strerror(errno));
  15. return 0;
  16. }
  17. if ((bytes_read = fread(buf, sizeof(char), bufsiz, fp)) == 0) {
  18. fprintf(stderr, "fread '%s': %s\n", path,
  19. strerror(errno));
  20. fclose(fp);
  21. return 0;
  22. }
  23. fclose(fp);
  24. buf[bytes_read] = '\0';
  25. return bytes_read;
  26. }
  27. const char *
  28. swap_free(void)
  29. {
  30. long total, free;
  31. char *match;
  32. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  33. return NULL;
  34. }
  35. if ((match = strstr(buf, "SwapTotal")) == NULL)
  36. return NULL;
  37. sscanf(match, "SwapTotal: %ld kB\n", &total);
  38. if ((match = strstr(buf, "SwapFree")) == NULL)
  39. return NULL;
  40. sscanf(match, "SwapFree: %ld kB\n", &free);
  41. return bprintf("%f", (float)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")) == NULL)
  52. return NULL;
  53. sscanf(match, "SwapTotal: %ld kB\n", &total);
  54. if ((match = strstr(buf, "SwapCached")) == NULL)
  55. return NULL;
  56. sscanf(match, "SwapCached: %ld kB\n", &cached);
  57. if ((match = strstr(buf, "SwapFree")) == NULL)
  58. return NULL;
  59. sscanf(match, "SwapFree: %ld kB\n", &free);
  60. return bprintf("%d", 100 * (total - free - cached) / total);
  61. }
  62. const char *
  63. swap_total(void)
  64. {
  65. long total;
  66. char *match;
  67. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  68. return NULL;
  69. }
  70. if ((match = strstr(buf, "SwapTotal")) == NULL)
  71. return NULL;
  72. sscanf(match, "SwapTotal: %ld kB\n", &total);
  73. return bprintf("%f", (float)total / 1024 / 1024);
  74. }
  75. const char *
  76. swap_used(void)
  77. {
  78. long total, free, cached;
  79. char *match;
  80. if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
  81. return NULL;
  82. }
  83. if ((match = strstr(buf, "SwapTotal")) == NULL)
  84. return NULL;
  85. sscanf(match, "SwapTotal: %ld kB\n", &total);
  86. if ((match = strstr(buf, "SwapCached")) == NULL)
  87. return NULL;
  88. sscanf(match, "SwapCached: %ld kB\n", &cached);
  89. if ((match = strstr(buf, "SwapFree")) == NULL)
  90. return NULL;
  91. sscanf(match, "SwapFree: %ld kB\n", &free);
  92. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  93. }
  94. #elif defined(__OpenBSD__)
  95. #include <stdlib.h>
  96. #include <sys/param.h> /* dbtob */
  97. #include <sys/swap.h>
  98. #include <sys/types.h>
  99. #include <unistd.h>
  100. #define dbtoqb(b) dbtob((int64_t)(b))
  101. static void
  102. getstats(int *total, int *used)
  103. {
  104. struct swapent *sep, *fsep;
  105. int rnswap, nswap, i;
  106. nswap = swapctl(SWAP_NSWAP, 0, 0);
  107. if (nswap < 1)
  108. fprintf(stderr, "swaptctl 'SWAP_NSWAP': %s\n", strerror(errno));
  109. fsep = sep = calloc(nswap, sizeof(*sep));
  110. if (sep == NULL)
  111. fprintf(stderr, "calloc 'nswap': %s\n", strerror(errno));
  112. rnswap = swapctl(SWAP_STATS, (void *)sep, nswap);
  113. if (rnswap < 0)
  114. fprintf(stderr, "swapctl 'SWAP_STATA': %s\n", strerror(errno));
  115. if (nswap != rnswap)
  116. fprintf(stderr, "SWAP_STATS != SWAP_NSWAP\n");
  117. *total = 0;
  118. *used = 0;
  119. for (i = 0; i < rnswap; i++) {
  120. *total += dbtoqb(sep->se_nblks);
  121. *used += dbtoqb(sep->se_inuse);
  122. }
  123. free(fsep);
  124. }
  125. const char *
  126. swap_free(void)
  127. {
  128. int total, used;
  129. getstats(&total, &used);
  130. return bprintf("%f", (float)(total - used) / 1024 / 1024 / 1024);
  131. }
  132. const char *
  133. swap_perc(void)
  134. {
  135. int total, used;
  136. getstats(&total, &used);
  137. return bprintf("%d", 100 * used / total);
  138. }
  139. const char *
  140. swap_total(void)
  141. {
  142. int total, used;
  143. getstats(&total, &used);
  144. return bprintf("%f", (float)total / 1024 / 1024 / 1024);
  145. }
  146. const char *
  147. swap_used(void)
  148. {
  149. int total, used;
  150. getstats(&total, &used);
  151. return bprintf("%f", (float)used / 1024 / 1024 / 1024);
  152. }
  153. #endif