18c6bd995SGarrett Wollman /* 28c6bd995SGarrett Wollman * Copyright 2000 Massachusetts Institute of Technology 38c6bd995SGarrett Wollman * 48c6bd995SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 58c6bd995SGarrett Wollman * its documentation for any purpose and without fee is hereby 68c6bd995SGarrett Wollman * granted, provided that both the above copyright notice and this 78c6bd995SGarrett Wollman * permission notice appear in all copies, that both the above 88c6bd995SGarrett Wollman * copyright notice and this permission notice appear in all 98c6bd995SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 108c6bd995SGarrett Wollman * in advertising or publicity pertaining to distribution of the 118c6bd995SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 128c6bd995SGarrett Wollman * no representations about the suitability of this software for any 138c6bd995SGarrett Wollman * purpose. It is provided "as is" without express or implied 148c6bd995SGarrett Wollman * warranty. 158c6bd995SGarrett Wollman * 168c6bd995SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 178c6bd995SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 188c6bd995SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 198c6bd995SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 208c6bd995SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 218c6bd995SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 228c6bd995SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 238c6bd995SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 248c6bd995SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 258c6bd995SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 268c6bd995SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278c6bd995SGarrett Wollman * SUCH DAMAGE. 288c6bd995SGarrett Wollman */ 298c6bd995SGarrett Wollman 30e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 31e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 32e026a48cSDavid E. O'Brien 338c6bd995SGarrett Wollman #include <sys/types.h> 348c6bd995SGarrett Wollman 358c6bd995SGarrett Wollman #include <err.h> 368c6bd995SGarrett Wollman #include <errno.h> 378c6bd995SGarrett Wollman #include <stdio.h> 388c6bd995SGarrett Wollman #include <stdlib.h> 398c6bd995SGarrett Wollman #include <sysexits.h> 408c6bd995SGarrett Wollman #include <unistd.h> 418c6bd995SGarrett Wollman 428c6bd995SGarrett Wollman #include "getconf.h" 438c6bd995SGarrett Wollman 448c6bd995SGarrett Wollman static void do_confstr(const char *name, int key); 458c6bd995SGarrett Wollman static void do_sysconf(const char *name, int key); 468c6bd995SGarrett Wollman static void do_pathconf(const char *name, int key, const char *path); 478c6bd995SGarrett Wollman 488c6bd995SGarrett Wollman static void 498c6bd995SGarrett Wollman usage(void) 508c6bd995SGarrett Wollman { 517007f3d6STim J. Robbins fprintf(stderr, 527007f3d6STim J. Robbins "usage: getconf [-v prog_env] system_var\n" 537007f3d6STim J. Robbins " getconf [-v prog_env] path_var pathname\n"); 548c6bd995SGarrett Wollman exit(EX_USAGE); 558c6bd995SGarrett Wollman } 568c6bd995SGarrett Wollman 578c6bd995SGarrett Wollman int 588c6bd995SGarrett Wollman main(int argc, char **argv) 598c6bd995SGarrett Wollman { 60e9cfb9aeSGarrett Wollman int c, key, valid; 61e9cfb9aeSGarrett Wollman const char *name, *vflag, *alt_path; 62e9cfb9aeSGarrett Wollman intmax_t limitval; 638c6bd995SGarrett Wollman 64e9cfb9aeSGarrett Wollman vflag = NULL; 658c6bd995SGarrett Wollman while ((c = getopt(argc, argv, "v:")) != -1) { 668c6bd995SGarrett Wollman switch (c) { 678c6bd995SGarrett Wollman case 'v': 688c6bd995SGarrett Wollman vflag = optarg; 698c6bd995SGarrett Wollman break; 708c6bd995SGarrett Wollman 718c6bd995SGarrett Wollman default: 728c6bd995SGarrett Wollman usage(); 738c6bd995SGarrett Wollman } 748c6bd995SGarrett Wollman } 758c6bd995SGarrett Wollman 76e9cfb9aeSGarrett Wollman if ((name = argv[optind]) == NULL) 778c6bd995SGarrett Wollman usage(); 788c6bd995SGarrett Wollman 79e9cfb9aeSGarrett Wollman if (vflag != NULL) { 80e9cfb9aeSGarrett Wollman if ((valid = find_progenv(vflag, &alt_path)) == 0) 81e9cfb9aeSGarrett Wollman errx(EX_USAGE, "invalid programming environment %s", 82e9cfb9aeSGarrett Wollman vflag); 83e9cfb9aeSGarrett Wollman if (valid > 0 && alt_path != NULL) { 84e9cfb9aeSGarrett Wollman if (argv[optind + 1] == NULL) 85e9cfb9aeSGarrett Wollman execl(alt_path, "getconf", argv[optind], 86e9cfb9aeSGarrett Wollman (char *)NULL); 878c6bd995SGarrett Wollman else 88e9cfb9aeSGarrett Wollman execl(alt_path, "getconf", argv[optind], 89e9cfb9aeSGarrett Wollman argv[optind + 1], (char *)NULL); 90e9cfb9aeSGarrett Wollman 91e9cfb9aeSGarrett Wollman err(EX_OSERR, "execl: %s", alt_path); 92e9cfb9aeSGarrett Wollman } 93e9cfb9aeSGarrett Wollman if (valid < 0) 94e9cfb9aeSGarrett Wollman errx(EX_UNAVAILABLE, "environment %s is not available", 95e9cfb9aeSGarrett Wollman vflag); 96e9cfb9aeSGarrett Wollman } 97e9cfb9aeSGarrett Wollman 98e9cfb9aeSGarrett Wollman if (argv[optind + 1] == NULL) { /* confstr or sysconf */ 99e9cfb9aeSGarrett Wollman if ((valid = find_limit(name, &limitval)) != 0) { 100e9cfb9aeSGarrett Wollman if (valid > 0) 10111e3dcb6SGarrett Wollman printf("%" PRIdMAX "\n", limitval); 102e9cfb9aeSGarrett Wollman else 103e9cfb9aeSGarrett Wollman printf("undefined\n"); 104e9cfb9aeSGarrett Wollman 105e9cfb9aeSGarrett Wollman return 0; 106e9cfb9aeSGarrett Wollman } 107e9cfb9aeSGarrett Wollman if ((valid = find_confstr(name, &key)) != 0) { 108e9cfb9aeSGarrett Wollman if (valid > 0) 109e9cfb9aeSGarrett Wollman do_confstr(name, key); 110e9cfb9aeSGarrett Wollman else 111e9cfb9aeSGarrett Wollman printf("undefined\n"); 112e9cfb9aeSGarrett Wollman } else { 113e9cfb9aeSGarrett Wollman valid = find_sysconf(name, &key); 114e9cfb9aeSGarrett Wollman if (valid > 0) { 115e9cfb9aeSGarrett Wollman do_sysconf(name, key); 116e9cfb9aeSGarrett Wollman } else if (valid < 0) { 117e9cfb9aeSGarrett Wollman printf("undefined\n"); 118e9cfb9aeSGarrett Wollman } else 1198c6bd995SGarrett Wollman errx(EX_USAGE, 1208c6bd995SGarrett Wollman "no such configuration parameter `%s'", 1218c6bd995SGarrett Wollman name); 1228c6bd995SGarrett Wollman } 1238c6bd995SGarrett Wollman } else { 124e9cfb9aeSGarrett Wollman valid = find_pathconf(name, &key); 125e9cfb9aeSGarrett Wollman if (valid != 0) { 126e9cfb9aeSGarrett Wollman if (valid > 0) 1278c6bd995SGarrett Wollman do_pathconf(name, key, argv[optind + 1]); 1288c6bd995SGarrett Wollman else 129e9cfb9aeSGarrett Wollman printf("undefined\n"); 130e9cfb9aeSGarrett Wollman } else 1318c6bd995SGarrett Wollman errx(EX_USAGE, 1328c6bd995SGarrett Wollman "no such path configuration parameter `%s'", 1338c6bd995SGarrett Wollman name); 1348c6bd995SGarrett Wollman } 1358c6bd995SGarrett Wollman return 0; 1368c6bd995SGarrett Wollman } 1378c6bd995SGarrett Wollman 1388c6bd995SGarrett Wollman static void 1398c6bd995SGarrett Wollman do_confstr(const char *name, int key) 1408c6bd995SGarrett Wollman { 1418c6bd995SGarrett Wollman size_t len; 14237716191SMaxim Konovalov int savederr; 1438c6bd995SGarrett Wollman 14437716191SMaxim Konovalov savederr = errno; 14537716191SMaxim Konovalov errno = 0; 1468c6bd995SGarrett Wollman len = confstr(key, 0, 0); 14737716191SMaxim Konovalov if (len == 0) { 14837716191SMaxim Konovalov if (errno) 1498c6bd995SGarrett Wollman err(EX_OSERR, "confstr: %s", name); 15037716191SMaxim Konovalov else 1518c6bd995SGarrett Wollman printf("undefined\n"); 15237716191SMaxim Konovalov } else { 153a272cd3aSMark Murray char buf[len + 1]; 154a272cd3aSMark Murray 1558c6bd995SGarrett Wollman confstr(key, buf, len); 1568c6bd995SGarrett Wollman printf("%s\n", buf); 1578c6bd995SGarrett Wollman } 15837716191SMaxim Konovalov errno = savederr; 1598c6bd995SGarrett Wollman } 1608c6bd995SGarrett Wollman 1618c6bd995SGarrett Wollman static void 1628c6bd995SGarrett Wollman do_sysconf(const char *name, int key) 1638c6bd995SGarrett Wollman { 1648c6bd995SGarrett Wollman long value; 1658c6bd995SGarrett Wollman 1668c6bd995SGarrett Wollman errno = 0; 1678c6bd995SGarrett Wollman value = sysconf(key); 1688c6bd995SGarrett Wollman if (value == -1 && errno != 0) 1698c6bd995SGarrett Wollman err(EX_OSERR, "sysconf: %s", name); 1708c6bd995SGarrett Wollman else if (value == -1) 1718c6bd995SGarrett Wollman printf("undefined\n"); 1728c6bd995SGarrett Wollman else 1738c6bd995SGarrett Wollman printf("%ld\n", value); 1748c6bd995SGarrett Wollman } 1758c6bd995SGarrett Wollman 1768c6bd995SGarrett Wollman static void 1778c6bd995SGarrett Wollman do_pathconf(const char *name, int key, const char *path) 1788c6bd995SGarrett Wollman { 1798c6bd995SGarrett Wollman long value; 1808c6bd995SGarrett Wollman 1818c6bd995SGarrett Wollman errno = 0; 1828c6bd995SGarrett Wollman value = pathconf(path, key); 1838c6bd995SGarrett Wollman if (value == -1 && errno != 0) 1848c6bd995SGarrett Wollman err(EX_OSERR, "pathconf: %s", name); 1858c6bd995SGarrett Wollman else if (value == -1) 1868c6bd995SGarrett Wollman printf("undefined\n"); 1878c6bd995SGarrett Wollman else 1888c6bd995SGarrett Wollman printf("%ld\n", value); 1898c6bd995SGarrett Wollman } 190*b4b4b530SBaptiste Daroussin 191