lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1991-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 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 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <stddef.h> |
| 20 | #include <unistd.h> |
| 21 | #include <limits.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/statfs.h> |
| 24 | #include <sys/statvfs.h> |
| 25 | |
| 26 | |
| 27 | /* Get file-specific information about descriptor FD. */ |
| 28 | long int |
| 29 | __fpathconf (fd, name) |
| 30 | int fd; |
| 31 | int name; |
| 32 | { |
| 33 | if (fd < 0) |
| 34 | { |
| 35 | __set_errno (EBADF); |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | switch (name) |
| 40 | { |
| 41 | default: |
| 42 | __set_errno (EINVAL); |
| 43 | return -1; |
| 44 | |
| 45 | case _PC_LINK_MAX: |
| 46 | #ifdef LINK_MAX |
| 47 | return LINK_MAX; |
| 48 | #else |
| 49 | return -1; |
| 50 | #endif |
| 51 | |
| 52 | case _PC_MAX_CANON: |
| 53 | #ifdef MAX_CANON |
| 54 | return MAX_CANON; |
| 55 | #else |
| 56 | return -1; |
| 57 | #endif |
| 58 | |
| 59 | case _PC_MAX_INPUT: |
| 60 | #ifdef MAX_INPUT |
| 61 | return MAX_INPUT; |
| 62 | #else |
| 63 | return -1; |
| 64 | #endif |
| 65 | |
| 66 | case _PC_NAME_MAX: |
| 67 | #ifdef NAME_MAX |
| 68 | { |
| 69 | struct statfs buf; |
| 70 | int save_errno = errno; |
| 71 | |
| 72 | if (__fstatfs (fd, &buf) < 0) |
| 73 | { |
| 74 | if (errno == ENOSYS) |
| 75 | { |
| 76 | __set_errno (save_errno); |
| 77 | return NAME_MAX; |
| 78 | } |
| 79 | else if (errno == ENODEV) |
| 80 | __set_errno (EINVAL); |
| 81 | |
| 82 | return -1; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | #ifdef _STATFS_F_NAMELEN |
| 87 | return buf.f_namelen; |
| 88 | #else |
| 89 | # ifdef _STATFS_F_NAME_MAX |
| 90 | return buf.f_name_max; |
| 91 | # else |
| 92 | return NAME_MAX; |
| 93 | # endif |
| 94 | #endif |
| 95 | } |
| 96 | } |
| 97 | #else |
| 98 | return -1; |
| 99 | #endif |
| 100 | |
| 101 | case _PC_PATH_MAX: |
| 102 | #ifdef PATH_MAX |
| 103 | return PATH_MAX; |
| 104 | #else |
| 105 | return -1; |
| 106 | #endif |
| 107 | |
| 108 | case _PC_PIPE_BUF: |
| 109 | #ifdef PIPE_BUF |
| 110 | return PIPE_BUF; |
| 111 | #else |
| 112 | return -1; |
| 113 | #endif |
| 114 | |
| 115 | case _PC_CHOWN_RESTRICTED: |
| 116 | #ifdef _POSIX_CHOWN_RESTRICTED |
| 117 | return _POSIX_CHOWN_RESTRICTED; |
| 118 | #else |
| 119 | return -1; |
| 120 | #endif |
| 121 | |
| 122 | case _PC_NO_TRUNC: |
| 123 | #ifdef _POSIX_NO_TRUNC |
| 124 | return _POSIX_NO_TRUNC; |
| 125 | #else |
| 126 | return -1; |
| 127 | #endif |
| 128 | |
| 129 | case _PC_VDISABLE: |
| 130 | #ifdef _POSIX_VDISABLE |
| 131 | return _POSIX_VDISABLE; |
| 132 | #else |
| 133 | return -1; |
| 134 | #endif |
| 135 | |
| 136 | case _PC_SYNC_IO: |
| 137 | #ifdef _POSIX_SYNC_IO |
| 138 | return _POSIX_SYNC_IO; |
| 139 | #else |
| 140 | return -1; |
| 141 | #endif |
| 142 | |
| 143 | case _PC_ASYNC_IO: |
| 144 | #ifdef _POSIX_ASYNC_IO |
| 145 | { |
| 146 | /* AIO is only allowed on regular files and block devices. */ |
| 147 | struct stat64 st; |
| 148 | |
| 149 | if (__fxstat64 (_STAT_VER, fd, &st) < 0 |
| 150 | || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode))) |
| 151 | return -1; |
| 152 | else |
| 153 | return 1; |
| 154 | } |
| 155 | #else |
| 156 | return -1; |
| 157 | #endif |
| 158 | |
| 159 | case _PC_PRIO_IO: |
| 160 | #ifdef _POSIX_PRIO_IO |
| 161 | return _POSIX_PRIO_IO; |
| 162 | #else |
| 163 | return -1; |
| 164 | #endif |
| 165 | |
| 166 | case _PC_SOCK_MAXBUF: |
| 167 | #ifdef SOCK_MAXBUF |
| 168 | return SOCK_MAXBUF; |
| 169 | #else |
| 170 | return -1; |
| 171 | #endif |
| 172 | |
| 173 | case _PC_FILESIZEBITS: |
| 174 | #ifdef FILESIZEBITS |
| 175 | return FILESIZEBITS; |
| 176 | #else |
| 177 | /* We let platforms with larger file sizes overwrite this value. */ |
| 178 | return 32; |
| 179 | #endif |
| 180 | |
| 181 | case _PC_REC_INCR_XFER_SIZE: |
| 182 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 183 | What is incremented? */ |
| 184 | return -1; |
| 185 | |
| 186 | case _PC_REC_MAX_XFER_SIZE: |
| 187 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 188 | In general there is no top limit of the number of bytes which |
| 189 | case be transported at once. */ |
| 190 | return -1; |
| 191 | |
| 192 | case _PC_REC_MIN_XFER_SIZE: |
| 193 | { |
| 194 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 195 | I assume this is the block size of the filesystem. */ |
| 196 | struct statvfs64 sv; |
| 197 | |
| 198 | if (__fstatvfs64 (fd, &sv) < 0) |
| 199 | return -1; |
| 200 | return sv.f_bsize; |
| 201 | } |
| 202 | |
| 203 | case _PC_REC_XFER_ALIGN: |
| 204 | { |
| 205 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 206 | I assume that the number should reflect the minimal block |
| 207 | alignment. */ |
| 208 | struct statvfs64 sv; |
| 209 | |
| 210 | if (__fstatvfs64 (fd, &sv) < 0) |
| 211 | return -1; |
| 212 | return sv.f_frsize; |
| 213 | } |
| 214 | |
| 215 | case _PC_ALLOC_SIZE_MIN: |
| 216 | { |
| 217 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 218 | I assume that the number should reflect the minimal block |
| 219 | alignment. */ |
| 220 | struct statvfs64 sv; |
| 221 | |
| 222 | if (__fstatvfs64 (fd, &sv) < 0) |
| 223 | return -1; |
| 224 | return sv.f_frsize; |
| 225 | } |
| 226 | |
| 227 | case _PC_SYMLINK_MAX: |
| 228 | /* In general there are no limits. If a system has one it should |
| 229 | overwrite this case. */ |
| 230 | return -1; |
| 231 | |
| 232 | case _PC_2_SYMLINKS: |
| 233 | /* Unix systems generally have symlinks. */ |
| 234 | return 1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #undef __fpathconf |
| 239 | weak_alias (__fpathconf, fpathconf) |