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