1 /* Linux specific extensions to pathconf.
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 /* Modified for newlib July 19, 2002 - Jeff Johnston */
21 
22 #include <errno.h>
23 #include <stddef.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/statfs.h>
29 #include <sys/statvfs.h>
30 #include <machine/weakalias.h>
31 #include "linux_fsinfo.h"
32 
33 /* The Linux kernel header mentioned this as a kind of generic value.  */
34 #define LINUX_LINK_MAX	127
35 
36 static long int posix_pathconf (const char *path, int name);
37 
38 
39 /* Get file-specific information about descriptor FD.  */
40 long int
__pathconf(path,name)41 __pathconf (path, name)
42      const char *path;
43      int name;
44 {
45   if (name == _PC_LINK_MAX)
46     {
47       struct statfs fsbuf;
48 
49       /* Determine the filesystem type.  */
50       if (__statfs (path, &fsbuf) < 0)
51 	{
52 	  if (errno == ENOSYS)
53 	    /* not possible, return the default value.  */
54 	    return LINUX_LINK_MAX;
55 
56 	  /* Some error occured.  */
57           return -1;
58 	}
59 
60       switch (fsbuf.f_type)
61 	{
62 	case EXT2_SUPER_MAGIC:
63 	  return EXT2_LINK_MAX;
64 
65 	case MINIX_SUPER_MAGIC:
66 	case MINIX_SUPER_MAGIC2:
67 	  return MINIX_LINK_MAX;
68 
69 	case MINIX2_SUPER_MAGIC:
70 	case MINIX2_SUPER_MAGIC2:
71 	  return MINIX2_LINK_MAX;
72 
73 	case XENIX_SUPER_MAGIC:
74 	  return XENIX_LINK_MAX;
75 
76 	case SYSV4_SUPER_MAGIC:
77 	case SYSV2_SUPER_MAGIC:
78 	  return SYSV_LINK_MAX;
79 
80 	case COH_SUPER_MAGIC:
81 	  return COH_LINK_MAX;
82 
83 	case UFS_MAGIC:
84 	case UFS_CIGAM:
85 	  return UFS_LINK_MAX;
86 
87 	case REISERFS_SUPER_MAGIC:
88 	  return REISERFS_LINK_MAX;
89 
90 	default:
91 	  return LINUX_LINK_MAX;
92 	}
93     }
94 
95   return posix_pathconf (path, name);
96 }
97 
98 /* Get file-specific information about PATH.  */
99 static long int
posix_pathconf(const char * path,int name)100 posix_pathconf (const char *path, int name)
101 {
102   if (path[0] == '\0')
103     {
104       __set_errno (ENOENT);
105       return -1;
106     }
107 
108   switch (name)
109     {
110     default:
111       __set_errno (EINVAL);
112       return -1;
113 
114     case _PC_LINK_MAX:
115 #ifdef	LINK_MAX
116       return LINK_MAX;
117 #else
118       return -1;
119 #endif
120 
121     case _PC_MAX_CANON:
122 #ifdef	MAX_CANON
123       return MAX_CANON;
124 #else
125       return -1;
126 #endif
127 
128     case _PC_MAX_INPUT:
129 #ifdef	MAX_INPUT
130       return MAX_INPUT;
131 #else
132       return -1;
133 #endif
134 
135     case _PC_NAME_MAX:
136 #ifdef	NAME_MAX
137       {
138 	struct statfs buf;
139 	int save_errno = errno;
140 
141 	if (__statfs (path, &buf) < 0)
142 	  {
143 	    if (errno == ENOSYS)
144 	      {
145 		errno = save_errno;
146 		return NAME_MAX;
147 	      }
148 	    return -1;
149 	  }
150 	else
151 	  {
152 #ifdef _STATFS_F_NAMELEN
153 	    return buf.f_namelen;
154 #else
155 # ifdef _STATFS_F_NAME_MAX
156 	    return buf.f_name_max;
157 # else
158 	    return NAME_MAX;
159 # endif
160 #endif
161 	  }
162       }
163 #else
164       return -1;
165 #endif
166 
167     case _PC_PATH_MAX:
168 #ifdef	PATH_MAX
169       return PATH_MAX;
170 #else
171       return -1;
172 #endif
173 
174     case _PC_PIPE_BUF:
175 #ifdef	PIPE_BUF
176       return PIPE_BUF;
177 #else
178       return -1;
179 #endif
180 
181     case _PC_CHOWN_RESTRICTED:
182 #ifdef	_POSIX_CHOWN_RESTRICTED
183       return _POSIX_CHOWN_RESTRICTED;
184 #else
185       return -1;
186 #endif
187 
188     case _PC_NO_TRUNC:
189 #ifdef	_POSIX_NO_TRUNC
190       return _POSIX_NO_TRUNC;
191 #else
192       return -1;
193 #endif
194 
195     case _PC_VDISABLE:
196 #ifdef	_POSIX_VDISABLE
197       return _POSIX_VDISABLE;
198 #else
199       return -1;
200 #endif
201 
202     case _PC_SYNC_IO:
203 #ifdef	_POSIX_SYNC_IO
204       return _POSIX_SYNC_IO;
205 #else
206       return -1;
207 #endif
208 
209     case _PC_ASYNC_IO:
210 #ifdef	_POSIX_ASYNC_IO
211       {
212 	/* AIO is only allowed on regular files and block devices.  */
213 	struct stat64 st;
214 
215 	if (stat64 (path, &st) < 0
216 	    || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
217 	  return -1;
218 	else
219 	  return 1;
220       }
221 #else
222       return -1;
223 #endif
224 
225     case _PC_PRIO_IO:
226 #ifdef	_POSIX_PRIO_IO
227       return _POSIX_PRIO_IO;
228 #else
229       return -1;
230 #endif
231 
232     case _PC_SOCK_MAXBUF:
233 #ifdef	SOCK_MAXBUF
234       return SOCK_MAXBUF;
235 #else
236       return -1;
237 #endif
238 
239     case _PC_FILESIZEBITS:
240 #ifdef FILESIZEBITS
241       return FILESIZEBITS;
242 #else
243       /* We let platforms with larger file sizes overwrite this value.  */
244       return 32;
245 #endif
246 
247     case _PC_REC_INCR_XFER_SIZE:
248       /* XXX It is not entirely clear what the limit is supposed to do.
249 	 What is incremented?  */
250       return -1;
251 
252     case _PC_REC_MAX_XFER_SIZE:
253       /* XXX It is not entirely clear what the limit is supposed to do.
254 	 In general there is no top limit of the number of bytes which
255 	 case be transported at once.  */
256       return -1;
257 
258     case _PC_REC_MIN_XFER_SIZE:
259       {
260 	/* XXX It is not entirely clear what the limit is supposed to do.
261 	   I assume this is the block size of the filesystem.  */
262 	struct statvfs64 sv;
263 
264 	if (__statvfs64 (path, &sv) < 0)
265 	  return -1;
266 	return sv.f_bsize;
267       }
268 
269     case _PC_REC_XFER_ALIGN:
270       {
271 	/* XXX It is not entirely clear what the limit is supposed to do.
272 	   I assume that the number should reflect the minimal block
273 	   alignment.  */
274 	struct statvfs64 sv;
275 
276 	if (__statvfs64 (path, &sv) < 0)
277 	  return -1;
278 	return sv.f_frsize;
279       }
280 
281     case _PC_ALLOC_SIZE_MIN:
282       {
283 	/* XXX It is not entirely clear what the limit is supposed to do.
284 	   I assume that the number should reflect the minimal block
285 	   alignment.  */
286 	struct statvfs64 sv;
287 
288 	if (__statvfs64 (path, &sv) < 0)
289 	  return -1;
290 	return sv.f_frsize;
291       }
292 
293     case _PC_SYMLINK_MAX:
294       /* In general there are no limits.  If a system has one it should
295 	 overwrite this case.  */
296       return -1;
297     }
298 }
299 
300 weak_alias (__pathconf, pathconf)
301