pathconf.c revision 1.4 1 /* $NetBSD: pathconf.c,v 1.4 2000/01/17 02:31:14 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)pathconf.c 8.1 (Berkeley) 6/6/93";
45 #else
46 static char rcsid[] = "$NetBSD: pathconf.c,v 1.4 2000/01/17 02:31:14 itojun Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #define PC_NAMES { \
60 { 0, 0 }, \
61 { "link_max", CTLTYPE_INT }, \
62 { "max_canon", CTLTYPE_INT }, \
63 { "max_input", CTLTYPE_INT }, \
64 { "name_max", CTLTYPE_INT }, \
65 { "path_max", CTLTYPE_INT }, \
66 { "pipe_buf", CTLTYPE_INT }, \
67 { "chown_restricted", CTLTYPE_INT }, \
68 { "no_trunc", CTLTYPE_INT }, \
69 { "vdisable", CTLTYPE_INT }, \
70 }
71 #define PC_MAXID 10
72
73 struct ctlname pcnames[] = PC_NAMES;
74 char names[BUFSIZ];
75
76 struct list {
77 struct ctlname *list;
78 int size;
79 };
80 struct list pclist = { pcnames, PC_MAXID };
81
82 int Aflag, aflag, nflag, wflag, stdinflag;
83
84 int
85 main(argc, argv)
86 int argc;
87 char *argv[];
88 {
89 extern char *optarg;
90 extern int optind;
91 char *path;
92 int ch;
93
94 while ((ch = getopt(argc, argv, "Aan")) != -1) {
95 switch (ch) {
96
97 case 'A':
98 Aflag = 1;
99 break;
100
101 case 'a':
102 aflag = 1;
103 break;
104
105 case 'n':
106 nflag = 1;
107 break;
108
109 default:
110 usage();
111 }
112 }
113 argc -= optind;
114 argv += optind;
115
116 if (argc == 0)
117 usage();
118 path = *argv++;
119 if (strcmp(path, "-") == 0)
120 stdinflag = 1;
121 argc--;
122 if (Aflag || aflag) {
123 listall(path, &pclist);
124 exit(0);
125 }
126 if (argc == 0)
127 usage();
128 while (argc-- > 0)
129 parse(path, *argv, 1);
130 exit(0);
131 }
132
133 /*
134 * List all variables known to the system.
135 */
136 listall(path, lp)
137 char *path;
138 struct list *lp;
139 {
140 int lvl2;
141
142 if (lp->list == 0)
143 return;
144 for (lvl2 = 0; lvl2 < lp->size; lvl2++) {
145 if (lp->list[lvl2].ctl_name == 0)
146 continue;
147 parse(path, lp->list[lvl2].ctl_name, Aflag);
148 }
149 }
150
151 /*
152 * Parse a name into an index.
153 * Lookup and print out the attribute if it exists.
154 */
155 parse(pathname, string, flags)
156 char *pathname;
157 char *string;
158 int flags;
159 {
160 int indx, value;
161 char *bufp, buf[BUFSIZ];
162
163 bufp = buf;
164 snprintf(buf, BUFSIZ, "%s", string);
165 if ((indx = findname(string, "top", &bufp, &pclist)) == -1)
166 return;
167 if (bufp) {
168 fprintf(stderr, "name %s in %s is unknown\n", *bufp, string);
169 return;
170 }
171 if (stdinflag)
172 value = fpathconf(0, indx);
173 else
174 value = pathconf(pathname, indx);
175 if (value == -1) {
176 if (flags == 0)
177 return;
178 switch (errno) {
179 case EOPNOTSUPP:
180 fprintf(stderr, "%s: value is not available\n", string);
181 return;
182 case ENOTDIR:
183 fprintf(stderr, "%s: specification is incomplete\n",
184 string);
185 return;
186 case ENOMEM:
187 fprintf(stderr, "%s: type is unknown to this program\n",
188 string);
189 return;
190 default:
191 perror(string);
192 return;
193 }
194 }
195 if (!nflag)
196 fprintf(stdout, "%s = ", string);
197 fprintf(stdout, "%d\n", value);
198 }
199
200 /*
201 * Scan a list of names searching for a particular name.
202 */
203 findname(string, level, bufp, namelist)
204 char *string;
205 char *level;
206 char **bufp;
207 struct list *namelist;
208 {
209 char *name;
210 int i;
211
212 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
213 fprintf(stderr, "%s: incomplete specification\n", string);
214 return (-1);
215 }
216 for (i = 0; i < namelist->size; i++)
217 if (namelist->list[i].ctl_name != NULL &&
218 strcmp(name, namelist->list[i].ctl_name) == 0)
219 break;
220 if (i == namelist->size) {
221 fprintf(stderr, "%s level name %s in %s is invalid\n",
222 level, name, string);
223 return (-1);
224 }
225 return (i);
226 }
227
228 usage()
229 {
230
231 (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n",
232 "pathname [-n] variable ...",
233 "pathname [-n] -a", "pathname [-n] -A");
234 exit(1);
235 }
236