1
0

statvfs.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <sys/statvfs.h>
  2. #include <sys/statfs.h>
  3. #include "syscall.h"
  4. static int __statfs(const char *path, struct statfs *buf)
  5. {
  6. *buf = (struct statfs){0};
  7. #ifdef SYS_statfs64
  8. return syscall(SYS_statfs64, path, sizeof *buf, buf);
  9. #else
  10. return syscall(SYS_statfs, path, buf);
  11. #endif
  12. }
  13. static int __fstatfs(int fd, struct statfs *buf)
  14. {
  15. *buf = (struct statfs){0};
  16. #ifdef SYS_fstatfs64
  17. return syscall(SYS_fstatfs64, fd, sizeof *buf, buf);
  18. #else
  19. return syscall(SYS_fstatfs, fd, buf);
  20. #endif
  21. }
  22. weak_alias(__statfs, statfs);
  23. weak_alias(__fstatfs, fstatfs);
  24. static void fixup(struct statvfs *out, const struct statfs *in)
  25. {
  26. *out = (struct statvfs){0};
  27. out->f_bsize = in->f_bsize;
  28. out->f_frsize = in->f_frsize ? in->f_frsize : in->f_bsize;
  29. out->f_blocks = in->f_blocks;
  30. out->f_bfree = in->f_bfree;
  31. out->f_bavail = in->f_bavail;
  32. out->f_files = in->f_files;
  33. out->f_ffree = in->f_ffree;
  34. out->f_favail = in->f_ffree;
  35. out->f_fsid = in->f_fsid.__val[0];
  36. out->f_flag = in->f_flags;
  37. out->f_namemax = in->f_namelen;
  38. }
  39. int statvfs(const char *restrict path, struct statvfs *restrict buf)
  40. {
  41. struct statfs kbuf;
  42. if (__statfs(path, &kbuf)<0) return -1;
  43. fixup(buf, &kbuf);
  44. return 0;
  45. }
  46. int fstatvfs(int fd, struct statvfs *buf)
  47. {
  48. struct statfs kbuf;
  49. if (__fstatfs(fd, &kbuf)<0) return -1;
  50. fixup(buf, &kbuf);
  51. return 0;
  52. }
  53. weak_alias(statvfs, statvfs64);
  54. weak_alias(statfs, statfs64);
  55. weak_alias(fstatvfs, fstatvfs64);
  56. weak_alias(fstatfs, fstatfs64);