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