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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* See LICENSE file for copyright and license details. */
  2. #if defined(__linux__)
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "../util.h"
  7. const char *
  8. swap_free(void)
  9. {
  10. long total, free;
  11. FILE *fp;
  12. size_t bytes_read;
  13. char *match;
  14. fp = fopen("/proc/meminfo", "r");
  15. if (fp == NULL) {
  16. fprintf(stderr, "fopen '/proc/meminfo': %s\n",
  17. strerror(errno));
  18. return NULL;
  19. }
  20. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
  21. fp)) == 0) {
  22. fprintf(stderr, "fread '/proc/meminfo': %s\n",
  23. strerror(errno));
  24. fclose(fp);
  25. return NULL;
  26. }
  27. fclose(fp);
  28. if ((match = strstr(buf, "SwapTotal")) == NULL)
  29. return NULL;
  30. sscanf(match, "SwapTotal: %ld kB\n", &total);
  31. if ((match = strstr(buf, "SwapFree")) == NULL)
  32. return NULL;
  33. sscanf(match, "SwapFree: %ld kB\n", &free);
  34. return bprintf("%f", (float)free / 1024 / 1024);
  35. }
  36. const char *
  37. swap_perc(void)
  38. {
  39. long total, free, cached;
  40. FILE *fp;
  41. size_t bytes_read;
  42. char *match;
  43. fp = fopen("/proc/meminfo", "r");
  44. if (fp == NULL) {
  45. fprintf(stderr, "fopen '/proc/meminfo': %s\n",
  46. strerror(errno));
  47. return NULL;
  48. }
  49. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
  50. fp)) == 0) {
  51. fprintf(stderr, "fread '/proc/meminfo': %s\n",
  52. strerror(errno));
  53. fclose(fp);
  54. return NULL;
  55. }
  56. fclose(fp);
  57. if ((match = strstr(buf, "SwapTotal")) == NULL)
  58. return NULL;
  59. sscanf(match, "SwapTotal: %ld kB\n", &total);
  60. if ((match = strstr(buf, "SwapCached")) == NULL)
  61. return NULL;
  62. sscanf(match, "SwapCached: %ld kB\n", &cached);
  63. if ((match = strstr(buf, "SwapFree")) == NULL)
  64. return NULL;
  65. sscanf(match, "SwapFree: %ld kB\n", &free);
  66. return bprintf("%d", 100 * (total - free - cached) / total);
  67. }
  68. const char *
  69. swap_total(void)
  70. {
  71. long total;
  72. FILE *fp;
  73. size_t bytes_read;
  74. char *match;
  75. fp = fopen("/proc/meminfo", "r");
  76. if (fp == NULL) {
  77. fprintf(stderr, "fopen '/proc/meminfo': %s\n",
  78. strerror(errno));
  79. return NULL;
  80. }
  81. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
  82. fp)) == 0) {
  83. fprintf(stderr, "fread '/proc/meminfo': %s\n",
  84. strerror(errno));
  85. fclose(fp);
  86. return NULL;
  87. }
  88. fclose(fp);
  89. if ((match = strstr(buf, "SwapTotal")) == NULL)
  90. return NULL;
  91. sscanf(match, "SwapTotal: %ld kB\n", &total);
  92. return bprintf("%f", (float)total / 1024 / 1024);
  93. }
  94. const char *
  95. swap_used(void)
  96. {
  97. long total, free, cached;
  98. FILE *fp;
  99. size_t bytes_read;
  100. char *match;
  101. fp = fopen("/proc/meminfo", "r");
  102. if (fp == NULL) {
  103. fprintf(stderr, "fopen '/proc/meminfo': %s\n",
  104. strerror(errno));
  105. return NULL;
  106. }
  107. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
  108. fp)) == 0) {
  109. fprintf(stderr, "fread '/proc/meminfo': %s\n",
  110. strerror(errno));
  111. fclose(fp);
  112. return NULL;
  113. }
  114. fclose(fp);
  115. if ((match = strstr(buf, "SwapTotal")) == NULL)
  116. return NULL;
  117. sscanf(match, "SwapTotal: %ld kB\n", &total);
  118. if ((match = strstr(buf, "SwapCached")) == NULL)
  119. return NULL;
  120. sscanf(match, "SwapCached: %ld kB\n", &cached);
  121. if ((match = strstr(buf, "SwapFree")) == NULL)
  122. return NULL;
  123. sscanf(match, "SwapFree: %ld kB\n", &free);
  124. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  125. }
  126. #elif defined(__OpenBSD__)
  127. /* unimplemented */
  128. #endif