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, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 /* pathconf -- adjusted for busybox */
20 
21 /* It would be great it this could be implemented using fpathconf,
22  * but that doesn't work out very well (think FIFOs and sockets) */
23 
24 #include <errno.h>
25 #include <stddef.h>
26 #include <unistd.h>
27 #include <limits.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/statfs.h>
31 
32 extern __typeof(statfs) __libc_statfs;
33 
34 
35 /* The Linux kernel headers mention this as a kind of generic value. */
36 #ifndef LINK_MAX
37 # define LINK_MAX 127
38 #endif
39 
40 /* Get file-specific information about PATH.  */
41 long int
pathconf(const char * path,int name)42 pathconf (const char *path, int name)
43 {
44   if (path[0] == '\0')
45     {
46       __set_errno (ENOENT);
47       return -1;
48     }
49 
50   switch (name)
51     {
52     default:
53       __set_errno (EINVAL);
54       return -1;
55 
56     case _PC_LINK_MAX:
57 #ifdef	LINK_MAX
58       return LINK_MAX;
59 #else
60       return -1;
61 #endif
62 
63     case _PC_MAX_CANON:
64 #ifdef	MAX_CANON
65       return MAX_CANON;
66 #else
67       return -1;
68 #endif
69 
70     case _PC_MAX_INPUT:
71 #ifdef	MAX_INPUT
72       return MAX_INPUT;
73 #else
74       return -1;
75 #endif
76 
77     case _PC_NAME_MAX:
78 #ifdef	NAME_MAX
79       {
80 	struct statfs buf;
81 	int save_errno = errno;
82 
83 	if (__libc_statfs (path, &buf) < 0)
84 	  {
85 	    if (errno == ENOSYS)
86 	      {
87 		errno = save_errno;
88 		return NAME_MAX;
89 	      }
90 	    return -1;
91 	  }
92 	else
93 	  {
94 #ifdef _STATFS_F_NAMELEN
95 	    return buf.f_namelen;
96 #else
97 # ifdef _STATFS_F_NAME_MAX
98 	    return buf.f_name_max;
99 # else
100 	    return NAME_MAX;
101 # endif
102 #endif
103 	  }
104       }
105 #else
106       return -1;
107 #endif
108 
109     case _PC_PATH_MAX:
110 #ifdef	PATH_MAX
111       return PATH_MAX;
112 #else
113       return -1;
114 #endif
115 
116     case _PC_PIPE_BUF:
117 #ifdef	PIPE_BUF
118       return PIPE_BUF;
119 #else
120       return -1;
121 #endif
122 
123     case _PC_CHOWN_RESTRICTED:
124 #ifdef	_POSIX_CHOWN_RESTRICTED
125       return _POSIX_CHOWN_RESTRICTED;
126 #else
127       return -1;
128 #endif
129 
130     case _PC_NO_TRUNC:
131 #ifdef	_POSIX_NO_TRUNC
132       return _POSIX_NO_TRUNC;
133 #else
134       return -1;
135 #endif
136 
137     case _PC_VDISABLE:
138 #ifdef	_POSIX_VDISABLE
139       return _POSIX_VDISABLE;
140 #else
141       return -1;
142 #endif
143 
144     case _PC_SYNC_IO:
145 #ifdef	_POSIX_SYNC_IO
146       return _POSIX_SYNC_IO;
147 #else
148       return -1;
149 #endif
150 
151     case _PC_ASYNC_IO:
152 #if defined _POSIX_ASYNC_IO
153       {
154 	/* AIO is only allowed on regular files and block devices.  */
155 	struct stat st;
156 
157 	if (stat (path, &st) < 0
158 	    || (! S_ISREG (st.st_mode) && ! S_ISBLK (st.st_mode)))
159 	  return -1;
160 	else
161 	  return 1;
162       }
163 #else
164       return -1;
165 #endif
166 
167     case _PC_PRIO_IO:
168 #ifdef	_POSIX_PRIO_IO
169       return _POSIX_PRIO_IO;
170 #else
171       return -1;
172 #endif
173 
174     case _PC_SOCK_MAXBUF:
175 #ifdef	SOCK_MAXBUF
176       return SOCK_MAXBUF;
177 #else
178       return -1;
179 #endif
180 
181     case _PC_FILESIZEBITS:
182 #ifdef FILESIZEBITS
183       return FILESIZEBITS;
184 #else
185       /* We let platforms with larger file sizes overwrite this value.  */
186       return 32;
187 #endif
188 
189         /* Be lazy -- skip these */
190     case _PC_REC_INCR_XFER_SIZE:
191     case _PC_REC_MAX_XFER_SIZE:
192     case _PC_REC_MIN_XFER_SIZE:
193     case _PC_REC_XFER_ALIGN:
194     case _PC_ALLOC_SIZE_MIN:
195     case _PC_SYMLINK_MAX:
196       return -1;
197     }
198 }
199