fpathconf.c 4.6 KB

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