fpathconf.c 4.9 KB

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