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.
 
 
 
 

139 lines
3.0 KiB

  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", strerror(errno));
  17. return NULL;
  18. }
  19. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  20. fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
  21. fclose(fp);
  22. return NULL;
  23. }
  24. fclose(fp);
  25. if ((match = strstr(buf, "SwapTotal")) == NULL)
  26. return NULL;
  27. sscanf(match, "SwapTotal: %ld kB\n", &total);
  28. if ((match = strstr(buf, "SwapFree")) == NULL)
  29. return NULL;
  30. sscanf(match, "SwapFree: %ld kB\n", &free);
  31. return bprintf("%f", (float)free / 1024 / 1024);
  32. }
  33. const char *
  34. swap_perc(void)
  35. {
  36. long total, free, cached;
  37. FILE *fp;
  38. size_t bytes_read;
  39. char *match;
  40. fp = fopen("/proc/meminfo", "r");
  41. if (fp == NULL) {
  42. fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
  43. return NULL;
  44. }
  45. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  46. fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
  47. fclose(fp);
  48. return NULL;
  49. }
  50. fclose(fp);
  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. FILE *fp;
  67. size_t bytes_read;
  68. char *match;
  69. fp = fopen("/proc/meminfo", "r");
  70. if (fp == NULL) {
  71. fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
  72. return NULL;
  73. }
  74. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  75. fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
  76. fclose(fp);
  77. return NULL;
  78. }
  79. fclose(fp);
  80. if ((match = strstr(buf, "SwapTotal")) == NULL)
  81. return NULL;
  82. sscanf(match, "SwapTotal: %ld kB\n", &total);
  83. return bprintf("%f", (float)total / 1024 / 1024);
  84. }
  85. const char *
  86. swap_used(void)
  87. {
  88. long total, free, cached;
  89. FILE *fp;
  90. size_t bytes_read;
  91. char *match;
  92. fp = fopen("/proc/meminfo", "r");
  93. if (fp == NULL) {
  94. fprintf(stderr, "fopen '/proc/meminfo': %s\n", strerror(errno));
  95. return NULL;
  96. }
  97. if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) == 0) {
  98. fprintf(stderr, "fread '/proc/meminfo': %s\n", strerror(errno));
  99. fclose(fp);
  100. return NULL;
  101. }
  102. fclose(fp);
  103. if ((match = strstr(buf, "SwapTotal")) == NULL)
  104. return NULL;
  105. sscanf(match, "SwapTotal: %ld kB\n", &total);
  106. if ((match = strstr(buf, "SwapCached")) == NULL)
  107. return NULL;
  108. sscanf(match, "SwapCached: %ld kB\n", &cached);
  109. if ((match = strstr(buf, "SwapFree")) == NULL)
  110. return NULL;
  111. sscanf(match, "SwapFree: %ld kB\n", &free);
  112. return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
  113. }
  114. #endif