1 /* $NetBSD: pathconf.c,v 1.5 2000/04/14 06:03:40 simonb 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.5 2000/04/14 06:03:40 simonb 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 char *path; 90 int ch; 91 92 while ((ch = getopt(argc, argv, "Aan")) != -1) { 93 switch (ch) { 94 95 case 'A': 96 Aflag = 1; 97 break; 98 99 case 'a': 100 aflag = 1; 101 break; 102 103 case 'n': 104 nflag = 1; 105 break; 106 107 default: 108 usage(); 109 } 110 } 111 argc -= optind; 112 argv += optind; 113 114 if (argc == 0) 115 usage(); 116 path = *argv++; 117 if (strcmp(path, "-") == 0) 118 stdinflag = 1; 119 argc--; 120 if (Aflag || aflag) { 121 listall(path, &pclist); 122 exit(0); 123 } 124 if (argc == 0) 125 usage(); 126 while (argc-- > 0) 127 parse(path, *argv, 1); 128 exit(0); 129 } 130 131 /* 132 * List all variables known to the system. 133 */ 134 listall(path, lp) 135 char *path; 136 struct list *lp; 137 { 138 int lvl2; 139 140 if (lp->list == 0) 141 return; 142 for (lvl2 = 0; lvl2 < lp->size; lvl2++) { 143 if (lp->list[lvl2].ctl_name == 0) 144 continue; 145 parse(path, lp->list[lvl2].ctl_name, Aflag); 146 } 147 } 148 149 /* 150 * Parse a name into an index. 151 * Lookup and print out the attribute if it exists. 152 */ 153 parse(pathname, string, flags) 154 char *pathname; 155 char *string; 156 int flags; 157 { 158 int indx, value; 159 char *bufp, buf[BUFSIZ]; 160 161 bufp = buf; 162 snprintf(buf, BUFSIZ, "%s", string); 163 if ((indx = findname(string, "top", &bufp, &pclist)) == -1) 164 return; 165 if (bufp) { 166 fprintf(stderr, "name %s in %s is unknown\n", *bufp, string); 167 return; 168 } 169 if (stdinflag) 170 value = fpathconf(0, indx); 171 else 172 value = pathconf(pathname, indx); 173 if (value == -1) { 174 if (flags == 0) 175 return; 176 switch (errno) { 177 case EOPNOTSUPP: 178 fprintf(stderr, "%s: value is not available\n", string); 179 return; 180 case ENOTDIR: 181 fprintf(stderr, "%s: specification is incomplete\n", 182 string); 183 return; 184 case ENOMEM: 185 fprintf(stderr, "%s: type is unknown to this program\n", 186 string); 187 return; 188 default: 189 perror(string); 190 return; 191 } 192 } 193 if (!nflag) 194 fprintf(stdout, "%s = ", string); 195 fprintf(stdout, "%d\n", value); 196 } 197 198 /* 199 * Scan a list of names searching for a particular name. 200 */ 201 findname(string, level, bufp, namelist) 202 char *string; 203 char *level; 204 char **bufp; 205 struct list *namelist; 206 { 207 char *name; 208 int i; 209 210 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) { 211 fprintf(stderr, "%s: incomplete specification\n", string); 212 return (-1); 213 } 214 for (i = 0; i < namelist->size; i++) 215 if (namelist->list[i].ctl_name != NULL && 216 strcmp(name, namelist->list[i].ctl_name) == 0) 217 break; 218 if (i == namelist->size) { 219 fprintf(stderr, "%s level name %s in %s is invalid\n", 220 level, name, string); 221 return (-1); 222 } 223 return (i); 224 } 225 226 usage() 227 { 228 229 (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n", 230 "pathname [-n] variable ...", 231 "pathname [-n] -a", "pathname [-n] -A"); 232 exit(1); 233 } 234