fpathconf.c 4.8 KB

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