logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

df.c (9351B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #define _DEFAULT_SOURCE // mntent in glibc 2.19+
  6. #include "../lib/humanize.h"
  7. #include <ctype.h> // iscntrl, isspace
  8. #include <errno.h> // errno
  9. #include <mntent.h>
  10. #include <stdbool.h>
  11. #include <stdio.h> // printf
  12. #include <stdlib.h> // abort, exit
  13. #include <string.h> // strerror
  14. #include <sys/stat.h> // stat, dev_t
  15. #include <sys/statvfs.h>
  16. #include <unistd.h> // getopt
  17. // Grabbed from OpenRC rc-functions.sh
  18. // clang-format off
  19. static const char *net_fs_list[] = {
  20. "afs", "ceph", "cifs", "coda", "davfs", "fuse", "fuse.glusterfs",
  21. "fuse.sshfs", "gfs", "glusterfs", "lustre", "ncpfs", "nfs", "nfs4",
  22. "ocfs2", "shfs", "smbfs"
  23. };
  24. // clang-format on
  25. blksize_t forced_bsize = 0;
  26. const char *argv0 = "df";
  27. // Replaces control characters and whitespaces with '?' in-place (no allocation)
  28. static void
  29. static_escape(char *str)
  30. {
  31. for(size_t i = 0; i < strlen(str); i++)
  32. if(iscntrl(str[i]) || isspace(str[i])) str[i] = '?';
  33. }
  34. int
  35. main(int argc, char *argv[])
  36. {
  37. bool opt_P = false, opt_h = false, opt_a = false, opt_T = false, opt_l = false, opt_i = false;
  38. int fs_col_width = 20;
  39. size_t excluded_count = 0;
  40. char *excluded[4096];
  41. size_t only_count = 0;
  42. char *only[4096];
  43. int c = -1;
  44. while((c = getopt(argc, argv, ":ahilPkTt:x:")) != -1)
  45. {
  46. switch(c)
  47. {
  48. case 'a':
  49. opt_a = true;
  50. break;
  51. case 'P':
  52. if(forced_bsize == 0) forced_bsize = 512;
  53. opt_P = true;
  54. break;
  55. case 'k':
  56. forced_bsize = 1024;
  57. break;
  58. case 'h':
  59. opt_h = true;
  60. fs_col_width = 30;
  61. break;
  62. case 'i':
  63. opt_i = true;
  64. break;
  65. case 'l':
  66. opt_l = true;
  67. break;
  68. case 'T':
  69. opt_T = true;
  70. break;
  71. case 't':
  72. only[only_count++] = optarg;
  73. break;
  74. case 'x':
  75. excluded[excluded_count++] = optarg;
  76. break;
  77. case ':':
  78. fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt);
  79. return 1;
  80. case '?':
  81. fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt);
  82. return 1;
  83. }
  84. }
  85. if(opt_P && opt_T)
  86. {
  87. fprintf(stderr, "%s: error: Options -P and -T are incompatible\n", argv0);
  88. return 1;
  89. }
  90. if(opt_P) opt_h = false;
  91. argc -= optind;
  92. argv += optind;
  93. int args_left = argc != 0 ? argc : 1;
  94. bool tty_out = isatty(1);
  95. int col_width = tty_out ? 10 : 0;
  96. if(!tty_out)
  97. {
  98. errno = 0;
  99. fs_col_width = 0;
  100. }
  101. dev_t *arg_devs = NULL;
  102. if(argc > 0)
  103. {
  104. arg_devs = calloc(argc, sizeof(dev_t));
  105. if(arg_devs == NULL)
  106. {
  107. fprintf(stderr, "%s: error: Failed to allocate arg_devs array: %s\n", argv0, strerror(errno));
  108. return 1;
  109. }
  110. }
  111. for(int i = 0; i < argc; i++)
  112. {
  113. struct stat file_stats;
  114. if(stat(argv[i], &file_stats) != 0)
  115. {
  116. fprintf(stderr,
  117. "%s: error: Failed getting status for file '%s': %s\n",
  118. argv0,
  119. argv[i],
  120. strerror(errno));
  121. goto error;
  122. }
  123. arg_devs[i] = file_stats.st_dev;
  124. }
  125. // Begin: Print header
  126. printf("%-*s ", fs_col_width, "Filesystem");
  127. if(opt_T) printf("%*s ", col_width, "Type");
  128. if(opt_i)
  129. {
  130. printf("%*s %*s %*s ", col_width, "Inodes", col_width, "Iused", col_width, "IFree");
  131. if(opt_P)
  132. printf("IUse%% Mounted on\n");
  133. else
  134. printf("IUse%% Mountpoint\n");
  135. }
  136. else
  137. {
  138. if(forced_bsize != 0)
  139. printf("%zd-blocks ", forced_bsize);
  140. else
  141. printf("%*s ", col_width, "Total");
  142. printf("%*s %*s ", col_width, "Used", col_width, "Available");
  143. if(opt_P)
  144. printf("Capacity Mounted on\n");
  145. else
  146. printf("Use%% Mountpoint\n");
  147. }
  148. // End: Print header
  149. FILE *mounted = setmntent(MOUNTED, "r");
  150. if(mounted == NULL)
  151. {
  152. fprintf(stderr,
  153. "%s: error: Failed opening setmntent(\"" MOUNTED "\", \"r\"): %s\n",
  154. argv0,
  155. strerror(errno));
  156. goto error;
  157. }
  158. // FIXME: Fix maximum number of mount entries / devices
  159. dev_t devices[4096];
  160. size_t devices_found = 0;
  161. // Even with argc>0 we still need to go over mntent for the filesystem mountpoint and type
  162. while(args_left > 0)
  163. {
  164. struct mntent *mntent = getmntent(mounted);
  165. if(mntent == NULL) break;
  166. if(excluded_count > 0)
  167. {
  168. bool exclude = false;
  169. for(size_t i = 0; i < excluded_count; i++)
  170. if(strcmp(excluded[i], mntent->mnt_type) == 0)
  171. {
  172. exclude = true;
  173. break;
  174. }
  175. if(exclude) continue;
  176. }
  177. if(only_count > 0)
  178. {
  179. bool include = false;
  180. for(size_t i = 0; i < only_count; i++)
  181. if(strcmp(only[i], mntent->mnt_type) == 0)
  182. {
  183. include = true;
  184. break;
  185. }
  186. if(!include) continue;
  187. }
  188. if(opt_l)
  189. {
  190. bool remote = false;
  191. for(size_t i = 0; i < sizeof(net_fs_list) / sizeof(char *); i++)
  192. if(strcmp(net_fs_list[i], mntent->mnt_type) == 0)
  193. {
  194. remote = true;
  195. break;
  196. }
  197. if(remote) continue;
  198. }
  199. struct stat file_stats;
  200. if(!opt_a || argc > 0)
  201. {
  202. if(stat(mntent->mnt_dir, &file_stats) != 0)
  203. {
  204. fprintf(stderr,
  205. "%s: warning: Failed getting status for file '%s': %s\n",
  206. argv0,
  207. mntent->mnt_dir,
  208. strerror(errno));
  209. errno = 0;
  210. }
  211. }
  212. if(argc > 0)
  213. {
  214. bool found = false;
  215. for(int i = 0; i < argc; i++)
  216. if(arg_devs[i] == file_stats.st_dev)
  217. {
  218. found = true;
  219. break;
  220. }
  221. if(!found) continue;
  222. args_left--;
  223. }
  224. if(!opt_a)
  225. {
  226. bool dupe = false;
  227. for(size_t i = 0; i < devices_found; i++)
  228. if(devices[i] == file_stats.st_dev)
  229. {
  230. dupe = true;
  231. break;
  232. }
  233. if(dupe) continue;
  234. if(devices_found >= 4096)
  235. fprintf(stderr,
  236. "%s: warning: Reached maximum amount of devices which can be deduplicated\n",
  237. argv0);
  238. devices[devices_found++] = file_stats.st_dev;
  239. }
  240. // Note: musl prior to 1.2.5 has broken getmntent when octal sequences and carriage return is used
  241. // https://git.musl-libc.org/cgit/musl/commit/src/misc/mntent.c?id=f314e133929b6379eccc632bef32eaebb66a7335
  242. // https://git.musl-libc.org/cgit/musl/commit/src/misc/mntent.c?id=ee1d39bc1573c1ae49ee6b658938b56bbef95a6c
  243. struct statvfs stats;
  244. if(statvfs(mntent->mnt_dir, &stats) != 0)
  245. {
  246. fprintf(stderr,
  247. "%s: warning: Failed getting statistics for filesystem '%s': %s\n",
  248. argv0,
  249. mntent->mnt_dir,
  250. strerror(errno));
  251. errno = 0;
  252. static_escape(mntent->mnt_fsname);
  253. static_escape(mntent->mnt_dir);
  254. if(opt_a)
  255. {
  256. printf("%-*s ", fs_col_width, mntent->mnt_fsname);
  257. if(opt_T) printf("%*s ", col_width, mntent->mnt_type);
  258. printf("%*s ", col_width, "-"); // total
  259. printf("%*s ", col_width, "-"); // used
  260. printf("%*s ", col_width, "-"); // free
  261. printf("%*s ", tty_out ? 3 : 0, "-"); // percent
  262. printf("%s\n", mntent->mnt_dir);
  263. }
  264. continue;
  265. }
  266. // Skip null filesystems
  267. if(!opt_a && stats.f_blocks == 0) continue;
  268. // Needs to be done after calling statvfs(3) and stat(3)
  269. static_escape(mntent->mnt_fsname);
  270. static_escape(mntent->mnt_dir);
  271. printf("%-*s ", fs_col_width, mntent->mnt_fsname);
  272. if(opt_T) printf("%*s ", col_width, mntent->mnt_type);
  273. if(opt_i)
  274. {
  275. fsfilcnt_t used = stats.f_files - stats.f_ffree;
  276. fsfilcnt_t percent = (used / stats.f_files) * 100;
  277. if(opt_h && !opt_P)
  278. {
  279. struct si_scale total_scl = dtosi(stats.f_files, false);
  280. struct si_scale used_scl = dtosi(used, false);
  281. struct si_scale free_scl = dtosi(stats.f_ffree, false);
  282. int width_num = tty_out ? col_width - 1 : 0;
  283. int width_pre = tty_out ? 1 : 0;
  284. printf("%*.2f%-*s ", width_num, total_scl.number, width_pre, total_scl.prefix);
  285. printf("%*.2f%-*s ", width_num, used_scl.number, width_pre, used_scl.prefix);
  286. printf("%*.2f%-*s ", width_num, free_scl.number, width_pre, free_scl.prefix);
  287. }
  288. else
  289. {
  290. printf("%*zd ", col_width, stats.f_files);
  291. printf("%*zd ", col_width, used);
  292. printf("%*zd ", col_width, stats.f_ffree);
  293. }
  294. printf("%*zd%% ", tty_out ? 4 : 0, percent);
  295. printf("%s\n", mntent->mnt_dir);
  296. continue;
  297. }
  298. off_t percent = 0;
  299. off_t total = stats.f_frsize * (stats.f_blocks != 0 ? stats.f_blocks : 1);
  300. off_t free = stats.f_bfree * (stats.f_bsize != 0 ? stats.f_bsize : 1);
  301. off_t used = total - free;
  302. if(used + free)
  303. {
  304. percent = (used * 100) / (used + free);
  305. if(used * 100 != percent * (used + free)) percent++;
  306. }
  307. if(forced_bsize != 0)
  308. {
  309. total /= forced_bsize;
  310. free /= forced_bsize;
  311. used /= forced_bsize;
  312. }
  313. if(opt_h && !opt_P)
  314. {
  315. struct si_scale total_scl = dtosi(total, true);
  316. struct si_scale used_scl = dtosi(used, true);
  317. struct si_scale free_scl = dtosi(free, true);
  318. int width_num = tty_out ? col_width - 3 : 0;
  319. int width_pre = tty_out ? 3 : 0;
  320. printf("%*.2f%-*s ", width_num, total_scl.number, width_pre, total_scl.prefix);
  321. printf("%*.2f%-*s ", width_num, used_scl.number, width_pre, used_scl.prefix);
  322. printf("%*.2f%-*s ", width_num, free_scl.number, width_pre, free_scl.prefix);
  323. }
  324. else
  325. {
  326. printf("%*zd ", col_width, total);
  327. printf("%*zd ", col_width, used);
  328. printf("%*zd ", col_width, free);
  329. }
  330. printf("%*zd%% ", tty_out ? 3 : 0, percent);
  331. printf("%s\n", mntent->mnt_dir);
  332. }
  333. endmntent(mounted);
  334. if(argc > 0) free(arg_devs);
  335. return 0;
  336. error:
  337. if(argc > 0) free(arg_devs);
  338. return 1;
  339. }