My slstatus configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

123 行
2.5 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. 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. /* unimplemented */
  96. #endif