fpathconf.c 4.9 KB

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