bsd-statvfs.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2008,2014 Darren Tucker <dtucker@zip.com.au>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  14. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "includes.h"
  17. #if !defined(HAVE_STATVFS) || !defined(HAVE_FSTATVFS)
  18. #include <sys/param.h>
  19. #ifdef HAVE_SYS_MOUNT_H
  20. # include <sys/mount.h>
  21. #endif
  22. #include <errno.h>
  23. #ifndef MNAMELEN
  24. # define MNAMELEN 32
  25. #endif
  26. #ifdef HAVE_STRUCT_STATFS_F_FILES
  27. # define HAVE_STRUCT_STATFS
  28. #endif
  29. #ifdef HAVE_STRUCT_STATFS
  30. static void
  31. copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from)
  32. {
  33. to->f_bsize = from->f_bsize;
  34. to->f_frsize = from->f_bsize; /* no exact equivalent */
  35. to->f_blocks = from->f_blocks;
  36. to->f_bfree = from->f_bfree;
  37. to->f_bavail = from->f_bavail;
  38. to->f_files = from->f_files;
  39. to->f_ffree = from->f_ffree;
  40. to->f_favail = from->f_ffree; /* no exact equivalent */
  41. to->f_fsid = 0; /* XXX fix me */
  42. #ifdef HAVE_STRUCT_STATFS_F_FLAGS
  43. to->f_flag = from->f_flags;
  44. #else
  45. to->f_flag = 0;
  46. #endif
  47. to->f_namemax = MNAMELEN;
  48. }
  49. #endif
  50. # ifndef HAVE_STATVFS
  51. int statvfs(const char *path, struct statvfs *buf)
  52. {
  53. # if defined(HAVE_STATFS) && defined(HAVE_STRUCT_STATFS)
  54. struct statfs fs;
  55. memset(&fs, 0, sizeof(fs));
  56. if (statfs(path, &fs) == -1)
  57. return -1;
  58. copy_statfs_to_statvfs(buf, &fs);
  59. return 0;
  60. # else
  61. errno = ENOSYS;
  62. return -1;
  63. # endif
  64. }
  65. # endif
  66. # ifndef HAVE_FSTATVFS
  67. int fstatvfs(int fd, struct statvfs *buf)
  68. {
  69. # if defined(HAVE_FSTATFS) && defined(HAVE_STRUCT_STATFS)
  70. struct statfs fs;
  71. memset(&fs, 0, sizeof(fs));
  72. if (fstatfs(fd, &fs) == -1)
  73. return -1;
  74. copy_statfs_to_statvfs(buf, &fs);
  75. return 0;
  76. # else
  77. errno = ENOSYS;
  78. return -1;
  79. # endif
  80. }
  81. # endif
  82. #endif