fpathconf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifndef __USE_FILE_OFFSET64
  23. extern int fstatfs (int __fildes, struct statfs *__buf)
  24. __THROW __nonnull ((2));
  25. #else
  26. # ifdef __REDIRECT_NTH
  27. extern int __REDIRECT_NTH (fstatfs, (int __fildes, struct statfs *__buf),
  28. fstatfs64) __nonnull ((2));
  29. # else
  30. # define fstatfs fstatfs64
  31. # endif
  32. #endif
  33. extern __typeof(fstatfs) __libc_fstatfs;
  34. /* The Linux kernel headers mention this as a kind of generic value. */
  35. #define LINUX_LINK_MAX 127
  36. /* Get file-specific information about descriptor FD. */
  37. long int fpathconf(int fd, int name)
  38. {
  39. if (fd < 0)
  40. {
  41. __set_errno (EBADF);
  42. return -1;
  43. }
  44. if (name == _PC_LINK_MAX)
  45. {
  46. /* Cut some corners */
  47. #if 0
  48. struct statfs fsbuf;
  49. /* Determine the filesystem type. */
  50. if (__libc_fstatfs (fd, &fsbuf) < 0)
  51. {
  52. if (errno == ENOSYS)
  53. /* not possible, return the default value. */
  54. return LINUX_LINK_MAX;
  55. /* Some error occured. */
  56. return -1;
  57. }
  58. switch (fsbuf.f_type)
  59. {
  60. case EXT2_SUPER_MAGIC:
  61. return EXT2_LINK_MAX;
  62. case MINIX_SUPER_MAGIC:
  63. case MINIX_SUPER_MAGIC2:
  64. return MINIX_LINK_MAX;
  65. case MINIX2_SUPER_MAGIC:
  66. case MINIX2_SUPER_MAGIC2:
  67. return MINIX2_LINK_MAX;
  68. case XENIX_SUPER_MAGIC:
  69. return XENIX_LINK_MAX;
  70. case SYSV4_SUPER_MAGIC:
  71. case SYSV2_SUPER_MAGIC:
  72. return SYSV_LINK_MAX;
  73. case COH_SUPER_MAGIC:
  74. return COH_LINK_MAX;
  75. case UFS_MAGIC:
  76. case UFS_CIGAM:
  77. return UFS_LINK_MAX;
  78. case REISERFS_SUPER_MAGIC:
  79. return REISERFS_LINK_MAX;
  80. default:
  81. return LINUX_LINK_MAX;
  82. }
  83. #else
  84. return LINUX_LINK_MAX;
  85. #endif
  86. }
  87. switch (name)
  88. {
  89. default:
  90. __set_errno (EINVAL);
  91. return -1;
  92. case _PC_MAX_CANON:
  93. #ifdef MAX_CANON
  94. return MAX_CANON;
  95. #else
  96. return -1;
  97. #endif
  98. case _PC_MAX_INPUT:
  99. #ifdef MAX_INPUT
  100. return MAX_INPUT;
  101. #else
  102. return -1;
  103. #endif
  104. case _PC_NAME_MAX:
  105. #ifdef NAME_MAX
  106. {
  107. struct statfs buf;
  108. int save_errno = errno;
  109. if (__libc_fstatfs (fd, &buf) < 0)
  110. {
  111. if (errno == ENOSYS)
  112. {
  113. errno = save_errno;
  114. return NAME_MAX;
  115. }
  116. return -1;
  117. }
  118. else
  119. {
  120. #ifdef _STATFS_F_NAMELEN
  121. return buf.f_namelen;
  122. #else
  123. # ifdef _STATFS_F_NAME_MAX
  124. return buf.f_name_max;
  125. # else
  126. return NAME_MAX;
  127. # endif
  128. #endif
  129. }
  130. }
  131. #else
  132. return -1;
  133. #endif
  134. case _PC_PATH_MAX:
  135. #ifdef PATH_MAX
  136. return PATH_MAX;
  137. #else
  138. return -1;
  139. #endif
  140. case _PC_PIPE_BUF:
  141. #ifdef PIPE_BUF
  142. return PIPE_BUF;
  143. #else
  144. return -1;
  145. #endif
  146. case _PC_CHOWN_RESTRICTED:
  147. #ifdef _POSIX_CHOWN_RESTRICTED
  148. return _POSIX_CHOWN_RESTRICTED;
  149. #else
  150. return -1;
  151. #endif
  152. case _PC_NO_TRUNC:
  153. #ifdef _POSIX_NO_TRUNC
  154. return _POSIX_NO_TRUNC;
  155. #else
  156. return -1;
  157. #endif
  158. case _PC_VDISABLE:
  159. #ifdef _POSIX_VDISABLE
  160. return _POSIX_VDISABLE;
  161. #else
  162. return -1;
  163. #endif
  164. case _PC_SYNC_IO:
  165. #ifdef _POSIX_SYNC_IO
  166. return _POSIX_SYNC_IO;
  167. #else
  168. return -1;
  169. #endif
  170. case _PC_ASYNC_IO:
  171. #if defined _POSIX_ASYNC_IO && defined __UCLIBC_HAS_LFS__
  172. {
  173. /* AIO is only allowed on regular files and block devices. */
  174. struct stat st;
  175. if (fstat (fd, &st) < 0 || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
  176. return -1;
  177. else
  178. return 1;
  179. }
  180. #else
  181. return -1;
  182. #endif
  183. case _PC_PRIO_IO:
  184. #ifdef _POSIX_PRIO_IO
  185. return _POSIX_PRIO_IO;
  186. #else
  187. return -1;
  188. #endif
  189. case _PC_SOCK_MAXBUF:
  190. #ifdef SOCK_MAXBUF
  191. return SOCK_MAXBUF;
  192. #else
  193. return -1;
  194. #endif
  195. case _PC_FILESIZEBITS:
  196. #ifdef FILESIZEBITS
  197. return FILESIZEBITS;
  198. #else
  199. /* We let platforms with larger file sizes overwrite this value. */
  200. return 32;
  201. #endif
  202. /* Be lazy -- skip these */
  203. case _PC_REC_INCR_XFER_SIZE:
  204. case _PC_REC_MAX_XFER_SIZE:
  205. case _PC_REC_MIN_XFER_SIZE:
  206. case _PC_REC_XFER_ALIGN:
  207. case _PC_ALLOC_SIZE_MIN:
  208. case _PC_SYMLINK_MAX:
  209. return -1;
  210. }
  211. }