xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1991-2016 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 (int fd, int name) |
| 30 | { |
| 31 | if (fd < 0) |
| 32 | { |
| 33 | __set_errno (EBADF); |
| 34 | return -1; |
| 35 | } |
| 36 | |
| 37 | switch (name) |
| 38 | { |
| 39 | default: |
| 40 | __set_errno (EINVAL); |
| 41 | return -1; |
| 42 | |
| 43 | case _PC_LINK_MAX: |
| 44 | #ifdef LINK_MAX |
| 45 | return LINK_MAX; |
| 46 | #else |
| 47 | return -1; |
| 48 | #endif |
| 49 | |
| 50 | case _PC_MAX_CANON: |
| 51 | #ifdef MAX_CANON |
| 52 | return MAX_CANON; |
| 53 | #else |
| 54 | return -1; |
| 55 | #endif |
| 56 | |
| 57 | case _PC_MAX_INPUT: |
| 58 | #ifdef MAX_INPUT |
| 59 | return MAX_INPUT; |
| 60 | #else |
| 61 | return -1; |
| 62 | #endif |
| 63 | |
| 64 | case _PC_NAME_MAX: |
| 65 | #ifdef NAME_MAX |
| 66 | { |
| 67 | struct statvfs64 sv; |
| 68 | int save_errno = errno; |
| 69 | |
| 70 | if (__fstatvfs64 (fd, &sv) < 0) |
| 71 | { |
| 72 | if (errno == ENOSYS) |
| 73 | { |
| 74 | __set_errno (save_errno); |
| 75 | return NAME_MAX; |
| 76 | } |
| 77 | else if (errno == ENODEV) |
| 78 | __set_errno (EINVAL); |
| 79 | |
| 80 | return -1; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | return sv.f_namemax; |
| 85 | } |
| 86 | } |
| 87 | #else |
| 88 | return -1; |
| 89 | #endif |
| 90 | |
| 91 | case _PC_PATH_MAX: |
| 92 | #ifdef PATH_MAX |
| 93 | return PATH_MAX; |
| 94 | #else |
| 95 | return -1; |
| 96 | #endif |
| 97 | |
| 98 | case _PC_PIPE_BUF: |
| 99 | #ifdef PIPE_BUF |
| 100 | return PIPE_BUF; |
| 101 | #else |
| 102 | return -1; |
| 103 | #endif |
| 104 | |
| 105 | case _PC_CHOWN_RESTRICTED: |
| 106 | #ifdef _POSIX_CHOWN_RESTRICTED |
| 107 | return _POSIX_CHOWN_RESTRICTED; |
| 108 | #else |
| 109 | return -1; |
| 110 | #endif |
| 111 | |
| 112 | case _PC_NO_TRUNC: |
| 113 | #ifdef _POSIX_NO_TRUNC |
| 114 | return _POSIX_NO_TRUNC; |
| 115 | #else |
| 116 | return -1; |
| 117 | #endif |
| 118 | |
| 119 | case _PC_VDISABLE: |
| 120 | #ifdef _POSIX_VDISABLE |
| 121 | return _POSIX_VDISABLE; |
| 122 | #else |
| 123 | return -1; |
| 124 | #endif |
| 125 | |
| 126 | case _PC_SYNC_IO: |
| 127 | #ifdef _POSIX_SYNC_IO |
| 128 | return _POSIX_SYNC_IO; |
| 129 | #else |
| 130 | return -1; |
| 131 | #endif |
| 132 | |
| 133 | case _PC_ASYNC_IO: |
| 134 | #ifdef _POSIX_ASYNC_IO |
| 135 | { |
| 136 | /* AIO is only allowed on regular files and block devices. */ |
| 137 | struct stat64 st; |
| 138 | |
| 139 | if (__fxstat64 (_STAT_VER, fd, &st) < 0 |
| 140 | || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode))) |
| 141 | return -1; |
| 142 | else |
| 143 | return 1; |
| 144 | } |
| 145 | #else |
| 146 | return -1; |
| 147 | #endif |
| 148 | |
| 149 | case _PC_PRIO_IO: |
| 150 | #ifdef _POSIX_PRIO_IO |
| 151 | return _POSIX_PRIO_IO; |
| 152 | #else |
| 153 | return -1; |
| 154 | #endif |
| 155 | |
| 156 | case _PC_SOCK_MAXBUF: |
| 157 | #ifdef SOCK_MAXBUF |
| 158 | return SOCK_MAXBUF; |
| 159 | #else |
| 160 | return -1; |
| 161 | #endif |
| 162 | |
| 163 | case _PC_FILESIZEBITS: |
| 164 | #ifdef FILESIZEBITS |
| 165 | return FILESIZEBITS; |
| 166 | #else |
| 167 | /* We let platforms with larger file sizes overwrite this value. */ |
| 168 | return 32; |
| 169 | #endif |
| 170 | |
| 171 | case _PC_REC_INCR_XFER_SIZE: |
| 172 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 173 | What is incremented? */ |
| 174 | return -1; |
| 175 | |
| 176 | case _PC_REC_MAX_XFER_SIZE: |
| 177 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 178 | In general there is no top limit of the number of bytes which |
| 179 | case be transported at once. */ |
| 180 | return -1; |
| 181 | |
| 182 | case _PC_REC_MIN_XFER_SIZE: |
| 183 | { |
| 184 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 185 | I assume this is the block size of the filesystem. */ |
| 186 | struct statvfs64 sv; |
| 187 | |
| 188 | if (__fstatvfs64 (fd, &sv) < 0) |
| 189 | return -1; |
| 190 | return sv.f_bsize; |
| 191 | } |
| 192 | |
| 193 | case _PC_REC_XFER_ALIGN: |
| 194 | { |
| 195 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 196 | I assume that the number should reflect the minimal block |
| 197 | alignment. */ |
| 198 | struct statvfs64 sv; |
| 199 | |
| 200 | if (__fstatvfs64 (fd, &sv) < 0) |
| 201 | return -1; |
| 202 | return sv.f_frsize; |
| 203 | } |
| 204 | |
| 205 | case _PC_ALLOC_SIZE_MIN: |
| 206 | { |
| 207 | /* XXX It is not entirely clear what the limit is supposed to do. |
| 208 | I assume that the number should reflect the minimal block |
| 209 | alignment. */ |
| 210 | struct statvfs64 sv; |
| 211 | |
| 212 | if (__fstatvfs64 (fd, &sv) < 0) |
| 213 | return -1; |
| 214 | return sv.f_frsize; |
| 215 | } |
| 216 | |
| 217 | case _PC_SYMLINK_MAX: |
| 218 | /* In general there are no limits. If a system has one it should |
| 219 | overwrite this case. */ |
| 220 | return -1; |
| 221 | |
| 222 | case _PC_2_SYMLINKS: |
| 223 | /* Unix systems generally have symlinks. */ |
| 224 | return 1; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #undef __fpathconf |
| 229 | weak_alias (__fpathconf, fpathconf) |