blob: 02b4445a752ac7c086cb52d2baa91ca708d4a657 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* fpathconf -- adjusted for busybox
2 Copyright (C) 1991,95,96,98,99,2000,2001 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#include <errno.h>
21#include <unistd.h>
22#include <limits.h>
23#include <stddef.h>
24#include <fcntl.h>
25#include <sys/stat.h>
26#include <sys/statfs.h>
27
28
29#ifndef __USE_FILE_OFFSET64
30extern int fstatfs (int __fildes, struct statfs *__buf)
31 __THROW __nonnull ((2));
32#else
33# ifdef __REDIRECT_NTH
34 extern int __REDIRECT_NTH (fstatfs, (int __fildes, struct statfs *__buf),
35 fstatfs64) __nonnull ((2));
36# else
37# define fstatfs fstatfs64
38# endif
39#endif
40
41extern __typeof(fstatfs) __libc_fstatfs;
42
43/* The Linux kernel headers mention this as a kind of generic value. */
44#define LINUX_LINK_MAX 127
45
46
47/* Get file-specific information about descriptor FD. */
48long int fpathconf(int fd, int name)
49{
50 if (fd < 0)
51 {
52 __set_errno (EBADF);
53 return -1;
54 }
55
56 if (name == _PC_LINK_MAX)
57 {
58 /* Cut some corners */
59#if 0
60 struct statfs fsbuf;
61
62 /* Determine the filesystem type. */
63 if (__libc_fstatfs (fd, &fsbuf) < 0)
64 {
65 if (errno == ENOSYS)
66 /* not possible, return the default value. */
67 return LINUX_LINK_MAX;
68
69 /* Some error occured. */
70 return -1;
71 }
72
73 switch (fsbuf.f_type)
74 {
75 case EXT2_SUPER_MAGIC:
76 return EXT2_LINK_MAX;
77
78 case MINIX_SUPER_MAGIC:
79 case MINIX_SUPER_MAGIC2:
80 return MINIX_LINK_MAX;
81
82 case MINIX2_SUPER_MAGIC:
83 case MINIX2_SUPER_MAGIC2:
84 return MINIX2_LINK_MAX;
85
86 case XENIX_SUPER_MAGIC:
87 return XENIX_LINK_MAX;
88
89 case SYSV4_SUPER_MAGIC:
90 case SYSV2_SUPER_MAGIC:
91 return SYSV_LINK_MAX;
92
93 case COH_SUPER_MAGIC:
94 return COH_LINK_MAX;
95
96 case UFS_MAGIC:
97 case UFS_CIGAM:
98 return UFS_LINK_MAX;
99
100 case REISERFS_SUPER_MAGIC:
101 return REISERFS_LINK_MAX;
102
103 default:
104 return LINUX_LINK_MAX;
105 }
106#else
107 return LINUX_LINK_MAX;
108#endif
109 }
110
111 switch (name)
112 {
113 default:
114 __set_errno (EINVAL);
115 return -1;
116
117 case _PC_MAX_CANON:
118#ifdef MAX_CANON
119 return MAX_CANON;
120#else
121 return -1;
122#endif
123
124 case _PC_MAX_INPUT:
125#ifdef MAX_INPUT
126 return MAX_INPUT;
127#else
128 return -1;
129#endif
130
131 case _PC_NAME_MAX:
132#ifdef NAME_MAX
133 {
134 struct statfs buf;
135 int save_errno = errno;
136
137 if (__libc_fstatfs (fd, &buf) < 0)
138 {
139 if (errno == ENOSYS)
140 {
141 errno = save_errno;
142 return NAME_MAX;
143 }
144 return -1;
145 }
146 else
147 {
148#ifdef _STATFS_F_NAMELEN
149 return buf.f_namelen;
150#else
151# ifdef _STATFS_F_NAME_MAX
152 return buf.f_name_max;
153# else
154 return NAME_MAX;
155# endif
156#endif
157 }
158 }
159#else
160 return -1;
161#endif
162
163 case _PC_PATH_MAX:
164#ifdef PATH_MAX
165 return PATH_MAX;
166#else
167 return -1;
168#endif
169
170 case _PC_PIPE_BUF:
171#ifdef PIPE_BUF
172 return PIPE_BUF;
173#else
174 return -1;
175#endif
176
177 case _PC_CHOWN_RESTRICTED:
178#ifdef _POSIX_CHOWN_RESTRICTED
179 return _POSIX_CHOWN_RESTRICTED;
180#else
181 return -1;
182#endif
183
184 case _PC_NO_TRUNC:
185#ifdef _POSIX_NO_TRUNC
186 return _POSIX_NO_TRUNC;
187#else
188 return -1;
189#endif
190
191 case _PC_VDISABLE:
192#ifdef _POSIX_VDISABLE
193 return _POSIX_VDISABLE;
194#else
195 return -1;
196#endif
197
198 case _PC_SYNC_IO:
199#ifdef _POSIX_SYNC_IO
200 return _POSIX_SYNC_IO;
201#else
202 return -1;
203#endif
204
205 case _PC_ASYNC_IO:
206#if defined _POSIX_ASYNC_IO && defined __UCLIBC_HAS_LFS__
207 {
208 /* AIO is only allowed on regular files and block devices. */
209 struct stat st;
210
211 if (fstat (fd, &st) < 0 || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
212 return -1;
213 else
214 return 1;
215 }
216#else
217 return -1;
218#endif
219
220 case _PC_PRIO_IO:
221#ifdef _POSIX_PRIO_IO
222 return _POSIX_PRIO_IO;
223#else
224 return -1;
225#endif
226
227 case _PC_SOCK_MAXBUF:
228#ifdef SOCK_MAXBUF
229 return SOCK_MAXBUF;
230#else
231 return -1;
232#endif
233
234 case _PC_FILESIZEBITS:
235#ifdef FILESIZEBITS
236 return FILESIZEBITS;
237#else
238 /* We let platforms with larger file sizes overwrite this value. */
239 return 32;
240#endif
241
242 /* Be lazy -- skip these */
243 case _PC_REC_INCR_XFER_SIZE:
244 case _PC_REC_MAX_XFER_SIZE:
245 case _PC_REC_MIN_XFER_SIZE:
246 case _PC_REC_XFER_ALIGN:
247 case _PC_ALLOC_SIZE_MIN:
248 case _PC_SYMLINK_MAX:
249 return -1;
250 }
251
252}
253