pathconf.c 3.8 KB

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