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 #include <sys/types.h> 32 33 #include <err.h> 34 #include <errno.h> 35 #include <stdbool.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <sysexits.h> 39 #include <unistd.h> 40 41 #include "getconf.h" 42 43 static void do_allsys(void); 44 static void do_allpath(const char *path); 45 static void do_confstr(const char *name, int key); 46 static void do_sysconf(const char *name, int key); 47 static void do_pathconf(const char *name, int key, const char *path); 48 49 static void 50 usage(void) 51 { 52 fprintf(stderr, 53 "usage: getconf -a [pathname]\n" 54 " getconf [-v prog_env] system_var\n" 55 " getconf [-v prog_env] path_var pathname\n"); 56 exit(EX_USAGE); 57 } 58 59 int 60 main(int argc, char **argv) 61 { 62 bool aflag; 63 int c, key, valid; 64 const char *name, *vflag, *alt_path; 65 intmax_t limitval; 66 uintmax_t ulimitval; 67 68 aflag = false; 69 vflag = NULL; 70 while ((c = getopt(argc, argv, "av:")) != -1) { 71 switch (c) { 72 case 'a': 73 aflag = true; 74 break; 75 case 'v': 76 vflag = optarg; 77 break; 78 79 default: 80 usage(); 81 } 82 } 83 84 if (aflag) { 85 if (vflag != NULL) 86 usage(); 87 if (argv[optind] == NULL) 88 do_allsys(); 89 else 90 do_allpath(argv[optind]); 91 return (0); 92 } 93 94 if ((name = argv[optind]) == NULL) 95 usage(); 96 97 if (vflag != NULL) { 98 if ((valid = find_progenv(vflag, &alt_path)) == 0) 99 errx(EX_USAGE, "invalid programming environment %s", 100 vflag); 101 if (valid > 0 && alt_path != NULL) { 102 if (argv[optind + 1] == NULL) 103 execl(alt_path, "getconf", argv[optind], 104 (char *)NULL); 105 else 106 execl(alt_path, "getconf", argv[optind], 107 argv[optind + 1], (char *)NULL); 108 109 err(EX_OSERR, "execl: %s", alt_path); 110 } 111 if (valid < 0) 112 errx(EX_UNAVAILABLE, "environment %s is not available", 113 vflag); 114 } 115 116 if (argv[optind + 1] == NULL) { /* confstr or sysconf */ 117 if ((valid = find_unsigned_limit(name, &ulimitval)) != 0) { 118 if (valid > 0) 119 printf("%" PRIuMAX "\n", ulimitval); 120 else 121 printf("undefined\n"); 122 return 0; 123 } 124 if ((valid = find_limit(name, &limitval)) != 0) { 125 if (valid > 0) 126 printf("%" PRIdMAX "\n", limitval); 127 else 128 printf("undefined\n"); 129 130 return 0; 131 } 132 if ((valid = find_confstr(name, &key)) != 0) { 133 if (valid > 0) 134 do_confstr(name, key); 135 else 136 printf("undefined\n"); 137 } else { 138 valid = find_sysconf(name, &key); 139 if (valid > 0) { 140 do_sysconf(name, key); 141 } else if (valid < 0) { 142 printf("undefined\n"); 143 } else 144 errx(EX_USAGE, 145 "no such configuration parameter `%s'", 146 name); 147 } 148 } else { 149 valid = find_pathconf(name, &key); 150 if (valid != 0) { 151 if (valid > 0) 152 do_pathconf(name, key, argv[optind + 1]); 153 else 154 printf("undefined\n"); 155 } else 156 errx(EX_USAGE, 157 "no such path configuration parameter `%s'", 158 name); 159 } 160 return 0; 161 } 162 163 static void 164 do_onestr(const char *name, int key) 165 { 166 size_t len; 167 168 errno = 0; 169 len = confstr(key, 0, 0); 170 if (len == 0 && errno != 0) { 171 warn("confstr: %s", name); 172 return; 173 } 174 printf("%s: ", name); 175 if (len == 0) 176 printf("undefined\n"); 177 else { 178 char buf[len + 1]; 179 180 confstr(key, buf, len); 181 printf("%s\n", buf); 182 } 183 } 184 185 static void 186 do_onesys(const char *name, int key) 187 { 188 long value; 189 190 errno = 0; 191 value = sysconf(key); 192 if (value == -1 && errno != 0) { 193 warn("sysconf: %s", name); 194 return; 195 } 196 printf("%s: ", name); 197 if (value == -1) 198 printf("undefined\n"); 199 else 200 printf("%ld\n", value); 201 } 202 203 static void 204 do_allsys(void) 205 { 206 207 foreach_confstr(do_onestr); 208 foreach_sysconf(do_onesys); 209 } 210 211 static void 212 do_onepath(const char *name, int key, const char *path) 213 { 214 long value; 215 216 errno = 0; 217 value = pathconf(path, key); 218 if (value == -1 && errno != EINVAL && errno != 0) 219 warn("pathconf: %s", name); 220 printf("%s: ", name); 221 if (value == -1) 222 printf("undefined\n"); 223 else 224 printf("%ld\n", value); 225 } 226 227 static void 228 do_allpath(const char *path) 229 { 230 231 foreach_pathconf(do_onepath, path); 232 } 233 234 static void 235 do_confstr(const char *name, int key) 236 { 237 size_t len; 238 int savederr; 239 240 savederr = errno; 241 errno = 0; 242 len = confstr(key, 0, 0); 243 if (len == 0) { 244 if (errno) 245 err(EX_OSERR, "confstr: %s", name); 246 else 247 printf("undefined\n"); 248 } else { 249 char buf[len + 1]; 250 251 confstr(key, buf, len); 252 printf("%s\n", buf); 253 } 254 errno = savederr; 255 } 256 257 static void 258 do_sysconf(const char *name, int key) 259 { 260 long value; 261 262 errno = 0; 263 value = sysconf(key); 264 if (value == -1 && errno != 0) 265 err(EX_OSERR, "sysconf: %s", name); 266 else if (value == -1) 267 printf("undefined\n"); 268 else 269 printf("%ld\n", value); 270 } 271 272 static void 273 do_pathconf(const char *name, int key, const char *path) 274 { 275 long value; 276 277 errno = 0; 278 value = pathconf(path, key); 279 if (value == -1 && errno != 0) 280 err(EX_OSERR, "pathconf: %s", name); 281 else if (value == -1) 282 printf("undefined\n"); 283 else 284 printf("%ld\n", value); 285 } 286 287