FRIGN | 05996b9 | 2015-10-01 00:59:40 +0200 | [diff] [blame] | 1 | /* See LICENSE file for copyright and license details. */ |
| 2 | #include <errno.h> |
| 3 | #include <unistd.h> |
| 4 | #include <limits.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "util.h" |
| 9 | |
| 10 | struct var { |
| 11 | const char *k; |
| 12 | long v; |
| 13 | }; |
| 14 | |
Evan Gates | de28c8b | 2016-10-03 15:55:22 -0700 | [diff] [blame] | 15 | #include "getconf.h" |
Roberto E. Vargas Caballero | 136f012 | 2015-12-14 21:22:43 +0100 | [diff] [blame] | 16 | |
FRIGN | 05996b9 | 2015-10-01 00:59:40 +0200 | [diff] [blame] | 17 | void |
| 18 | usage(void) |
| 19 | { |
sin | c0eafd4 | 2015-12-15 08:43:55 +0000 | [diff] [blame] | 20 | eprintf("usage: %s [-v spec] var [path]\n", argv0); |
FRIGN | 05996b9 | 2015-10-01 00:59:40 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | int |
| 24 | main(int argc, char *argv[]) |
| 25 | { |
| 26 | size_t len; |
| 27 | long res; |
| 28 | int i; |
| 29 | char *str; |
| 30 | |
| 31 | ARGBEGIN { |
| 32 | case 'v': |
| 33 | /* ignore */ |
sin | 3423752 | 2015-10-04 16:48:05 +0100 | [diff] [blame] | 34 | EARGF(usage()); |
FRIGN | 05996b9 | 2015-10-01 00:59:40 +0200 | [diff] [blame] | 35 | break; |
Mattias Andrée | d301322 | 2017-01-27 00:41:50 +0100 | [diff] [blame] | 36 | default: |
| 37 | usage(); |
| 38 | break; |
sin | 2366164 | 2015-11-01 10:16:49 +0000 | [diff] [blame] | 39 | } ARGEND |
FRIGN | 05996b9 | 2015-10-01 00:59:40 +0200 | [diff] [blame] | 40 | |
| 41 | if (argc == 1) { |
| 42 | /* sysconf */ |
| 43 | for (i = 0; i < LEN(sysconf_l); i++) { |
| 44 | if (strcmp(argv[0], sysconf_l[i].k)) |
| 45 | continue; |
| 46 | errno = 0; |
| 47 | if ((res = sysconf(sysconf_l[i].v)) < 0) { |
| 48 | if (errno) |
| 49 | eprintf("sysconf %ld:", sysconf_l[i].v); |
| 50 | puts("undefined"); |
| 51 | } else { |
| 52 | printf("%ld\n", res); |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | /* confstr */ |
| 57 | for (i = 0; i < LEN(confstr_l); i++) { |
| 58 | if (strcmp(argv[0], confstr_l[i].k)) |
| 59 | continue; |
| 60 | errno = 0; |
| 61 | if (!(len = confstr(confstr_l[i].v, NULL, 0))) { |
| 62 | if (errno) |
| 63 | eprintf("confstr %ld:", confstr_l[i].v); |
| 64 | puts("undefined"); |
| 65 | } else { |
| 66 | str = emalloc(len); |
| 67 | errno = 0; |
| 68 | if (!confstr(confstr_l[i].v, str, len)) { |
| 69 | if (errno) |
| 70 | eprintf("confstr %ld:", confstr_l[i].v); |
| 71 | puts("undefined"); |
| 72 | } else { |
| 73 | puts(str); |
| 74 | } |
| 75 | free(str); |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | /* limits */ |
| 80 | for (i = 0; i < LEN(limits_l); i++) { |
| 81 | if (strcmp(argv[0], limits_l[i].k)) |
| 82 | continue; |
| 83 | printf("%ld\n", limits_l[i].v); |
| 84 | return 0; |
| 85 | } |
| 86 | } else if (argc == 2) { |
| 87 | /* pathconf */ |
| 88 | for (i = 0; i < LEN(pathconf_l); i++) { |
| 89 | if (strcmp(argv[0], pathconf_l[i].k)) |
| 90 | continue; |
| 91 | errno = 0; |
| 92 | if ((res = pathconf(argv[1], pathconf_l[i].v)) < 0) { |
| 93 | if (errno) |
| 94 | eprintf("pathconf %ld:", pathconf_l[i].v); |
| 95 | puts("undefined"); |
| 96 | } else { |
| 97 | printf("%ld\n", res); |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | } else { |
| 102 | usage(); |
| 103 | } |
| 104 | |
| 105 | puts("undefined"); |
| 106 | |
| 107 | return 1; |
| 108 | } |