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.
 
 
 
 

136 linhas
2.7 KiB

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