1 /* 2 * Copyright 2000 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/types.h> 34 35 #include <err.h> 36 #include <errno.h> 37 #include <stdbool.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <sysexits.h> 41 #include <unistd.h> 42 43 #include "getconf.h" 44 45 static void do_allsys(void); 46 static void do_allpath(const char *path); 47 static void do_confstr(const char *name, int key); 48 static void do_sysconf(const char *name, int key); 49 static void do_pathconf(const char *name, int key, const char *path); 50 51 static void 52 usage(void) 53 { 54 fprintf(stderr, 55 "usage: getconf -a [pathname]\n" 56 " getconf [-v prog_env] system_var\n" 57 " getconf [-v prog_env] path_var pathname\n"); 58 exit(EX_USAGE); 59 } 60 61 int 62 main(int argc, char **argv) 63 { 64 bool aflag; 65 int c, key, valid; 66 const char *name, *vflag, *alt_path; 67 intmax_t limitval; 68 69 aflag = false; 70 vflag = NULL; 71 while ((c = getopt(argc, argv, "av:")) != -1) { 72 switch (c) { 73 case 'a': 74 aflag = true; 75 break; 76 case 'v': 77 vflag = optarg; 78 break; 79 80 default: 81 usage(); 82 } 83 } 84 85 if (aflag) { 86 if (vflag != NULL) 87 usage(); 88 if (argv[optind] == NULL) 89 do_allsys(); 90 else 91 do_allpath(argv[optind]); 92 return (0); 93 } 94 95 if ((name = argv[optind]) == NULL) 96 usage(); 97 98 if (vflag != NULL) { 99 if ((valid = find_progenv(vflag, &alt_path)) == 0) 100 errx(EX_USAGE, "invalid programming environment %s", 101 vflag); 102 if (valid > 0 && alt_path != NULL) { 103 if (argv[optind + 1] == NULL) 104 execl(alt_path, "getconf", argv[optind], 105 (char *)NULL); 106 else 107 execl(alt_path, "getconf", argv[optind], 108 argv[optind + 1], (char *)NULL); 109 110 err(EX_OSERR, "execl: %s", alt_path); 111 } 112 if (valid < 0) 113 errx(EX_UNAVAILABLE, "environment %s is not available", 114 vflag); 115 } 116 117 if (argv[optind + 1] == NULL) { /* confstr or sysconf */ 118 if ((valid = find_limit(name, &limitval)) != 0) { 119 if (valid > 0) 120 printf("%" PRIdMAX "\n", limitval); 121 else 122 printf("undefined\n"); 123 124 return 0; 125 } 126 if ((valid = find_confstr(name, &key)) != 0) { 127 if (valid > 0) 128 do_confstr(name, key); 129 else 130 printf("undefined\n"); 131 } else { 132 valid = find_sysconf(name, &key); 133 if (valid > 0) { 134 do_sysconf(name, key); 135 } else if (valid < 0) { 136 printf("undefined\n"); 137 } else 138 errx(EX_USAGE, 139 "no such configuration parameter `%s'", 140 name); 141 } 142 } else { 143 valid = find_pathconf(name, &key); 144 if (valid != 0) { 145 if (valid > 0) 146 do_pathconf(name, key, argv[optind + 1]); 147 else 148 printf("undefined\n"); 149 } else 150 errx(EX_USAGE, 151 "no such path configuration parameter `%s'", 152 name); 153 } 154 return 0; 155 } 156 157 static void 158 do_onestr(const char *name, int key) 159 { 160 size_t len; 161 162 errno = 0; 163 len = confstr(key, 0, 0); 164 if (len == 0 && errno != 0) { 165 warn("confstr: %s", name); 166 return; 167 } 168 printf("%s: ", name); 169 if (len == 0) 170 printf("undefined\n"); 171 else { 172 char buf[len + 1]; 173 174 confstr(key, buf, len); 175 printf("%s\n", buf); 176 } 177 } 178 179 static void 180 do_onesys(const char *name, int key) 181 { 182 long value; 183 184 errno = 0; 185 value = sysconf(key); 186 if (value == -1 && errno != 0) { 187 warn("sysconf: %s", name); 188 return; 189 } 190 printf("%s: ", name); 191 if (value == -1) 192 printf("undefined\n"); 193 else 194 printf("%ld\n", value); 195 } 196 197 static void 198 do_allsys(void) 199 { 200 201 foreach_confstr(do_onestr); 202 foreach_sysconf(do_onesys); 203 } 204 205 static void 206 do_onepath(const char *name, int key, const char *path) 207 { 208 long value; 209 210 errno = 0; 211 value = pathconf(path, key); 212 if (value == -1 && errno != EINVAL && errno != 0) 213 warn("pathconf: %s", name); 214 printf("%s: ", name); 215 if (value == -1) 216 printf("undefined\n"); 217 else 218 printf("%ld\n", value); 219 } 220 221 static void 222 do_allpath(const char *path) 223 { 224 225 foreach_pathconf(do_onepath, path); 226 } 227 228 static void 229 do_confstr(const char *name, int key) 230 { 231 size_t len; 232 int savederr; 233 234 savederr = errno; 235 errno = 0; 236 len = confstr(key, 0, 0); 237 if (len == 0) { 238 if (errno) 239 err(EX_OSERR, "confstr: %s", name); 240 else 241 printf("undefined\n"); 242 } else { 243 char buf[len + 1]; 244 245 confstr(key, buf, len); 246 printf("%s\n", buf); 247 } 248 errno = savederr; 249 } 250 251 static void 252 do_sysconf(const char *name, int key) 253 { 254 long value; 255 256 errno = 0; 257 value = sysconf(key); 258 if (value == -1 && errno != 0) 259 err(EX_OSERR, "sysconf: %s", name); 260 else if (value == -1) 261 printf("undefined\n"); 262 else 263 printf("%ld\n", value); 264 } 265 266 static void 267 do_pathconf(const char *name, int key, const char *path) 268 { 269 long value; 270 271 errno = 0; 272 value = pathconf(path, key); 273 if (value == -1 && errno != 0) 274 err(EX_OSERR, "pathconf: %s", name); 275 else if (value == -1) 276 printf("undefined\n"); 277 else 278 printf("%ld\n", value); 279 } 280 281