lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | /* pathconf -- adjusted for busybox */ |
| 21 | |
| 22 | /* It would be great it this could be implemented using fpathconf, |
| 23 | * but that doesn't work out very well (think FIFOs and sockets) */ |
| 24 | |
| 25 | #include <errno.h> |
| 26 | #include <stddef.h> |
| 27 | #include <unistd.h> |
| 28 | #include <limits.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/statfs.h> |
| 32 | |
| 33 | extern __typeof(statfs) __libc_statfs; |
| 34 | |
| 35 | |
| 36 | /* The Linux kernel headers mention this as a kind of generic value. */ |
| 37 | #ifndef LINK_MAX |
| 38 | # define LINK_MAX 127 |
| 39 | #endif |
| 40 | |
| 41 | /* Get file-specific information about PATH. */ |
| 42 | long int |
| 43 | pathconf (const char *path, int name) |
| 44 | { |
| 45 | if (path[0] == '\0') |
| 46 | { |
| 47 | __set_errno (ENOENT); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | switch (name) |
| 52 | { |
| 53 | default: |
| 54 | __set_errno (EINVAL); |
| 55 | return -1; |
| 56 | |
| 57 | case _PC_LINK_MAX: |
| 58 | #ifdef LINK_MAX |
| 59 | return LINK_MAX; |
| 60 | #else |
| 61 | return -1; |
| 62 | #endif |
| 63 | |
| 64 | case _PC_MAX_CANON: |
| 65 | #ifdef MAX_CANON |
| 66 | return MAX_CANON; |
| 67 | #else |
| 68 | return -1; |
| 69 | #endif |
| 70 | |
| 71 | case _PC_MAX_INPUT: |
| 72 | #ifdef MAX_INPUT |
| 73 | return MAX_INPUT; |
| 74 | #else |
| 75 | return -1; |
| 76 | #endif |
| 77 | |
| 78 | case _PC_NAME_MAX: |
| 79 | #ifdef NAME_MAX |
| 80 | { |
| 81 | struct statfs buf; |
| 82 | int save_errno = errno; |
| 83 | |
| 84 | if (__libc_statfs (path, &buf) < 0) |
| 85 | { |
| 86 | if (errno == ENOSYS) |
| 87 | { |
| 88 | errno = save_errno; |
| 89 | return NAME_MAX; |
| 90 | } |
| 91 | return -1; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | #ifdef _STATFS_F_NAMELEN |
| 96 | return buf.f_namelen; |
| 97 | #else |
| 98 | # ifdef _STATFS_F_NAME_MAX |
| 99 | return buf.f_name_max; |
| 100 | # else |
| 101 | return NAME_MAX; |
| 102 | # endif |
| 103 | #endif |
| 104 | } |
| 105 | } |
| 106 | #else |
| 107 | return -1; |
| 108 | #endif |
| 109 | |
| 110 | case _PC_PATH_MAX: |
| 111 | #ifdef PATH_MAX |
| 112 | return PATH_MAX; |
| 113 | #else |
| 114 | return -1; |
| 115 | #endif |
| 116 | |
| 117 | case _PC_PIPE_BUF: |
| 118 | #ifdef PIPE_BUF |
| 119 | return PIPE_BUF; |
| 120 | #else |
| 121 | return -1; |
| 122 | #endif |
| 123 | |
| 124 | case _PC_CHOWN_RESTRICTED: |
| 125 | #ifdef _POSIX_CHOWN_RESTRICTED |
| 126 | return _POSIX_CHOWN_RESTRICTED; |
| 127 | #else |
| 128 | return -1; |
| 129 | #endif |
| 130 | |
| 131 | case _PC_NO_TRUNC: |
| 132 | #ifdef _POSIX_NO_TRUNC |
| 133 | return _POSIX_NO_TRUNC; |
| 134 | #else |
| 135 | return -1; |
| 136 | #endif |
| 137 | |
| 138 | case _PC_VDISABLE: |
| 139 | #ifdef _POSIX_VDISABLE |
| 140 | return _POSIX_VDISABLE; |
| 141 | #else |
| 142 | return -1; |
| 143 | #endif |
| 144 | |
| 145 | case _PC_SYNC_IO: |
| 146 | #ifdef _POSIX_SYNC_IO |
| 147 | return _POSIX_SYNC_IO; |
| 148 | #else |
| 149 | return -1; |
| 150 | #endif |
| 151 | |
| 152 | case _PC_ASYNC_IO: |
| 153 | #if defined _POSIX_ASYNC_IO && defined __UCLIBC_HAS_LFS__ |
| 154 | { |
| 155 | /* AIO is only allowed on regular files and block devices. */ |
| 156 | struct stat st; |
| 157 | |
| 158 | if (stat (path, &st) < 0 |
| 159 | || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode))) |
| 160 | return -1; |
| 161 | else |
| 162 | return 1; |
| 163 | } |
| 164 | #else |
| 165 | return -1; |
| 166 | #endif |
| 167 | |
| 168 | case _PC_PRIO_IO: |
| 169 | #ifdef _POSIX_PRIO_IO |
| 170 | return _POSIX_PRIO_IO; |
| 171 | #else |
| 172 | return -1; |
| 173 | #endif |
| 174 | |
| 175 | case _PC_SOCK_MAXBUF: |
| 176 | #ifdef SOCK_MAXBUF |
| 177 | return SOCK_MAXBUF; |
| 178 | #else |
| 179 | return -1; |
| 180 | #endif |
| 181 | |
| 182 | case _PC_FILESIZEBITS: |
| 183 | #ifdef FILESIZEBITS |
| 184 | return FILESIZEBITS; |
| 185 | #else |
| 186 | /* We let platforms with larger file sizes overwrite this value. */ |
| 187 | return 32; |
| 188 | #endif |
| 189 | |
| 190 | /* Be lazy -- skip these */ |
| 191 | case _PC_REC_INCR_XFER_SIZE: |
| 192 | case _PC_REC_MAX_XFER_SIZE: |
| 193 | case _PC_REC_MIN_XFER_SIZE: |
| 194 | case _PC_REC_XFER_ALIGN: |
| 195 | case _PC_ALLOC_SIZE_MIN: |
| 196 | case _PC_SYMLINK_MAX: |
| 197 | return -1; |
| 198 | } |
| 199 | } |