fpathconf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* fpathconf -- adjusted for busybox
  2. Copyright (C) 1991,95,96,98,99,2000,2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <limits.h>
  18. #include <stddef.h>
  19. #include <fcntl.h>
  20. #include <sys/stat.h>
  21. #include <sys/statfs.h>
  22. #define STAT stat64
  23. #define FSTAT fstat64
  24. #ifndef __USE_FILE_OFFSET64
  25. extern int fstatfs (int __fildes, struct statfs *__buf)
  26. __THROW __nonnull ((2));
  27. #else
  28. # ifdef __REDIRECT_NTH
  29. extern int __REDIRECT_NTH (fstatfs, (int __fildes, struct statfs *__buf),
  30. fstatfs64) __nonnull ((2));
  31. # else
  32. # define fstatfs fstatfs64
  33. # endif
  34. #endif
  35. extern __typeof(fstatfs) __libc_fstatfs;
  36. /* The Linux kernel headers mention this as a kind of generic value. */
  37. #define LINUX_LINK_MAX 127
  38. /* Get file-specific information about descriptor FD. */
  39. long int fpathconf(int fd, int name)
  40. {
  41. if (fd < 0)
  42. {
  43. __set_errno (EBADF);
  44. return -1;
  45. }
  46. if (name == _PC_LINK_MAX)
  47. {
  48. /* Cut some corners */
  49. #if 0
  50. struct statfs fsbuf;
  51. /* Determine the filesystem type. */
  52. if (__libc_fstatfs (fd, &fsbuf) < 0)
  53. {
  54. if (errno == ENOSYS)
  55. /* not possible, return the default value. */
  56. return LINUX_LINK_MAX;
  57. /* Some error occured. */
  58. return -1;
  59. }
  60. switch (fsbuf.f_type)
  61. {
  62. case EXT2_SUPER_MAGIC:
  63. return EXT2_LINK_MAX;
  64. case MINIX_SUPER_MAGIC:
  65. case MINIX_SUPER_MAGIC2:
  66. return MINIX_LINK_MAX;
  67. case MINIX2_SUPER_MAGIC:
  68. case MINIX2_SUPER_MAGIC2:
  69. return MINIX2_LINK_MAX;
  70. case XENIX_SUPER_MAGIC:
  71. return XENIX_LINK_MAX;
  72. case SYSV4_SUPER_MAGIC:
  73. case SYSV2_SUPER_MAGIC:
  74. return SYSV_LINK_MAX;
  75. case COH_SUPER_MAGIC:
  76. return COH_LINK_MAX;
  77. case UFS_MAGIC:
  78. case UFS_CIGAM:
  79. return UFS_LINK_MAX;
  80. case REISERFS_SUPER_MAGIC:
  81. return REISERFS_LINK_MAX;
  82. default:
  83. return LINUX_LINK_MAX;
  84. }
  85. #else
  86. return LINUX_LINK_MAX;
  87. #endif
  88. }
  89. switch (name)
  90. {
  91. default:
  92. __set_errno (EINVAL);
  93. return -1;
  94. case _PC_MAX_CANON:
  95. #ifdef MAX_CANON
  96. return MAX_CANON;
  97. #else
  98. return -1;
  99. #endif
  100. case _PC_MAX_INPUT:
  101. #ifdef MAX_INPUT
  102. return MAX_INPUT;
  103. #else
  104. return -1;
  105. #endif
  106. case _PC_NAME_MAX:
  107. #ifdef NAME_MAX
  108. {
  109. struct statfs buf;
  110. int save_errno = errno;
  111. if (__libc_fstatfs (fd, &buf) < 0)
  112. {
  113. if (errno == ENOSYS)
  114. {
  115. errno = save_errno;
  116. return NAME_MAX;
  117. }
  118. return -1;
  119. }
  120. else
  121. {
  122. #ifdef _STATFS_F_NAMELEN
  123. return buf.f_namelen;
  124. #else
  125. # ifdef _STATFS_F_NAME_MAX
  126. return buf.f_name_max;
  127. # else
  128. return NAME_MAX;
  129. # endif
  130. #endif
  131. }
  132. }
  133. #else
  134. return -1;
  135. #endif
  136. case _PC_PATH_MAX:
  137. #ifdef PATH_MAX
  138. return PATH_MAX;
  139. #else
  140. return -1;
  141. #endif
  142. case _PC_PIPE_BUF:
  143. #ifdef PIPE_BUF
  144. return PIPE_BUF;
  145. #else
  146. return -1;
  147. #endif
  148. case _PC_CHOWN_RESTRICTED:
  149. #ifdef _POSIX_CHOWN_RESTRICTED
  150. return _POSIX_CHOWN_RESTRICTED;
  151. #else
  152. return -1;
  153. #endif
  154. case _PC_NO_TRUNC:
  155. #ifdef _POSIX_NO_TRUNC
  156. return _POSIX_NO_TRUNC;
  157. #else
  158. return -1;
  159. #endif
  160. case _PC_VDISABLE:
  161. #ifdef _POSIX_VDISABLE
  162. return _POSIX_VDISABLE;
  163. #else
  164. return -1;
  165. #endif
  166. case _PC_SYNC_IO:
  167. #ifdef _POSIX_SYNC_IO
  168. return _POSIX_SYNC_IO;
  169. #else
  170. return -1;
  171. #endif
  172. case _PC_ASYNC_IO:
  173. #if defined _POSIX_ASYNC_IO
  174. {
  175. /* AIO is only allowed on regular files and block devices. */
  176. struct STAT st;
  177. if (FSTAT (fd, &st) < 0 || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
  178. return -1;
  179. else
  180. return 1;
  181. }
  182. #else
  183. return -1;
  184. #endif
  185. case _PC_PRIO_IO:
  186. #ifdef _POSIX_PRIO_IO
  187. return _POSIX_PRIO_IO;
  188. #else
  189. return -1;
  190. #endif
  191. case _PC_SOCK_MAXBUF:
  192. #ifdef SOCK_MAXBUF
  193. return SOCK_MAXBUF;
  194. #else
  195. return -1;
  196. #endif
  197. case _PC_FILESIZEBITS:
  198. #ifdef FILESIZEBITS
  199. return FILESIZEBITS;
  200. #else
  201. /* We let platforms with larger file sizes overwrite this value. */
  202. return 32;
  203. #endif
  204. /* Be lazy -- skip these */
  205. case _PC_REC_INCR_XFER_SIZE:
  206. case _PC_REC_MAX_XFER_SIZE:
  207. case _PC_REC_MIN_XFER_SIZE:
  208. case _PC_REC_XFER_ALIGN:
  209. case _PC_ALLOC_SIZE_MIN:
  210. case _PC_SYMLINK_MAX:
  211. return -1;
  212. }
  213. }