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 <stdio.h> 38 #include <stdlib.h> 39 #include <sysexits.h> 40 #include <unistd.h> 41 42 #include "getconf.h" 43 44 static void do_confstr(const char *name, int key); 45 static void do_sysconf(const char *name, int key); 46 static void do_pathconf(const char *name, int key, const char *path); 47 48 static void 49 usage(void) 50 { 51 fprintf(stderr, "usage:\n" 52 "\tgetconf [-v prog_model] system_var\n" 53 "\tgetconf [-v prog_model] path_var pathname\n"); 54 exit(EX_USAGE); 55 } 56 57 int 58 main(int argc, char **argv) 59 { 60 int c; 61 const char *name, *vflag; 62 int key; 63 64 vflag = 0; 65 66 while ((c = getopt(argc, argv, "v:")) != -1) { 67 switch (c) { 68 case 'v': 69 vflag = optarg; 70 break; 71 72 default: 73 usage(); 74 } 75 } 76 77 if (vflag) 78 warnx("-v %s ignored", vflag); 79 80 /* No arguments... */ 81 if ((name = argv[optind]) == 0) 82 usage(); 83 84 if (argv[optind + 1] == 0) { /* confstr or sysconf */ 85 key = find_confstr(name); 86 if (key >= 0) { 87 do_confstr(name, key); 88 } else { 89 key = find_sysconf(name); 90 if (key >= 0) 91 do_sysconf(name, key); 92 else 93 errx(EX_USAGE, 94 "no such configuration parameter `%s'", 95 name); 96 } 97 } else { 98 key = find_pathconf(name); 99 if (key >= 0) 100 do_pathconf(name, key, argv[optind + 1]); 101 else 102 errx(EX_USAGE, 103 "no such path configuration parameter `%s'", 104 name); 105 } 106 return 0; 107 } 108 109 static void 110 do_confstr(const char *name, int key) 111 { 112 char *buf; 113 size_t len; 114 115 len = confstr(key, 0, 0); 116 if (len == (size_t)-1) 117 err(EX_OSERR, "confstr: %s", name); 118 119 if (len == 0) { 120 printf("undefined\n"); 121 } else { 122 buf = alloca(len); 123 confstr(key, buf, len); 124 printf("%s\n", buf); 125 } 126 } 127 128 static void 129 do_sysconf(const char *name, int key) 130 { 131 long value; 132 133 errno = 0; 134 value = sysconf(key); 135 if (value == -1 && errno != 0) 136 err(EX_OSERR, "sysconf: %s", name); 137 else if (value == -1) 138 printf("undefined\n"); 139 else 140 printf("%ld\n", value); 141 } 142 143 static void 144 do_pathconf(const char *name, int key, const char *path) 145 { 146 long value; 147 148 errno = 0; 149 value = pathconf(path, key); 150 if (value == -1 && errno != 0) 151 err(EX_OSERR, "pathconf: %s", name); 152 else if (value == -1) 153 printf("undefined\n"); 154 else 155 printf("%ld\n", value); 156 } 157