pathconf.c 3.9 KB

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