pathconf.c 4.7 KB

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