My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

137 lines
2.8 KiB

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