pathconf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (C) 1991,1995,1996,1998,2000,2001,2003
  2. 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. /* pathconf -- adjusted for busybox */
  17. /* It would be great it this could be implemented using fpathconf,
  18. * but that doesn't work out very well (think FIFOs and sockets) */
  19. #include <errno.h>
  20. #include <stddef.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #include <sys/statfs.h>
  26. extern __typeof(statfs) __libc_statfs;
  27. /* The Linux kernel headers mention this as a kind of generic value. */
  28. #ifndef LINK_MAX
  29. # define LINK_MAX 127
  30. #endif
  31. /* Get file-specific information about PATH. */
  32. long int
  33. pathconf (const char *path, int name)
  34. {
  35. if (path[0] == '\0')
  36. {
  37. __set_errno (ENOENT);
  38. return -1;
  39. }
  40. switch (name)
  41. {
  42. default:
  43. __set_errno (EINVAL);
  44. return -1;
  45. case _PC_LINK_MAX:
  46. #ifdef LINK_MAX
  47. return LINK_MAX;
  48. #else
  49. return -1;
  50. #endif
  51. case _PC_MAX_CANON:
  52. #ifdef MAX_CANON
  53. return MAX_CANON;
  54. #else
  55. return -1;
  56. #endif
  57. case _PC_MAX_INPUT:
  58. #ifdef MAX_INPUT
  59. return MAX_INPUT;
  60. #else
  61. return -1;
  62. #endif
  63. case _PC_NAME_MAX:
  64. #ifdef NAME_MAX
  65. {
  66. struct statfs buf;
  67. int save_errno = errno;
  68. if (__libc_statfs (path, &buf) < 0)
  69. {
  70. if (errno == ENOSYS)
  71. {
  72. errno = save_errno;
  73. return NAME_MAX;
  74. }
  75. return -1;
  76. }
  77. else
  78. {
  79. #ifdef _STATFS_F_NAMELEN
  80. return buf.f_namelen;
  81. #else
  82. # ifdef _STATFS_F_NAME_MAX
  83. return buf.f_name_max;
  84. # else
  85. return NAME_MAX;
  86. # endif
  87. #endif
  88. }
  89. }
  90. #else
  91. return -1;
  92. #endif
  93. case _PC_PATH_MAX:
  94. #ifdef PATH_MAX
  95. return PATH_MAX;
  96. #else
  97. return -1;
  98. #endif
  99. case _PC_PIPE_BUF:
  100. #ifdef PIPE_BUF
  101. return PIPE_BUF;
  102. #else
  103. return -1;
  104. #endif
  105. case _PC_CHOWN_RESTRICTED:
  106. #ifdef _POSIX_CHOWN_RESTRICTED
  107. return _POSIX_CHOWN_RESTRICTED;
  108. #else
  109. return -1;
  110. #endif
  111. case _PC_NO_TRUNC:
  112. #ifdef _POSIX_NO_TRUNC
  113. return _POSIX_NO_TRUNC;
  114. #else
  115. return -1;
  116. #endif
  117. case _PC_VDISABLE:
  118. #ifdef _POSIX_VDISABLE
  119. return _POSIX_VDISABLE;
  120. #else
  121. return -1;
  122. #endif
  123. case _PC_SYNC_IO:
  124. #ifdef _POSIX_SYNC_IO
  125. return _POSIX_SYNC_IO;
  126. #else
  127. return -1;
  128. #endif
  129. case _PC_ASYNC_IO:
  130. #if defined _POSIX_ASYNC_IO && defined __UCLIBC_HAS_LFS__
  131. {
  132. /* AIO is only allowed on regular files and block devices. */
  133. struct stat st;
  134. if (stat (path, &st) < 0
  135. || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
  136. return -1;
  137. else
  138. return 1;
  139. }
  140. #else
  141. return -1;
  142. #endif
  143. case _PC_PRIO_IO:
  144. #ifdef _POSIX_PRIO_IO
  145. return _POSIX_PRIO_IO;
  146. #else
  147. return -1;
  148. #endif
  149. case _PC_SOCK_MAXBUF:
  150. #ifdef SOCK_MAXBUF
  151. return SOCK_MAXBUF;
  152. #else
  153. return -1;
  154. #endif
  155. case _PC_FILESIZEBITS:
  156. #ifdef FILESIZEBITS
  157. return FILESIZEBITS;
  158. #else
  159. /* We let platforms with larger file sizes overwrite this value. */
  160. return 32;
  161. #endif
  162. /* Be lazy -- skip these */
  163. case _PC_REC_INCR_XFER_SIZE:
  164. case _PC_REC_MAX_XFER_SIZE:
  165. case _PC_REC_MIN_XFER_SIZE:
  166. case _PC_REC_XFER_ALIGN:
  167. case _PC_ALLOC_SIZE_MIN:
  168. case _PC_SYMLINK_MAX:
  169. return -1;
  170. }
  171. }