├── .gitignore ├── Makefile ├── README.md ├── LICENSE ├── getconf.1 └── getconf.c /.gitignore: -------------------------------------------------------------------------------- 1 | getconf 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += -Wall -Wextra -Werror 2 | PREFIX ?= /usr/local 3 | 4 | getconf: getconf.c 5 | 6 | clean: 7 | rm -f getconf 8 | 9 | install: getconf 10 | mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1 $(DESTDIR)$(PREFIX)/bin 11 | install -m700 getconf $(DESTDIR)$(PREFIX)/bin/ 12 | install -m600 getconf.1 $(DESTDIR)$(PREFIX)/share/man/man1/ 13 | 14 | uninstall: 15 | rm -f $(PREFIX)/bin/getconf $(PREFIX)/share/man/man1/getconf.1 16 | 17 | .PHONY: clean install uninstall 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # getconf 2 | A simple standalone [getconf(1)](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getconf.html) implementation created to be used in [Termux](https://termux.com). 3 | 4 | # Portability 5 | This implementation should be portable across different Unix-like systems. Contributions to support more are welcome. 6 | 7 | # Origins 8 | Taken from http://git.alpinelinux.org/cgit/aports/tree/main/musl/getconf.c and adopted for building on Android. 9 | 10 | Manpage taken from http://cvsweb.netbsd.org/bsdweb.cgi/src/usr.bin/getconf/getconf.1?only_with_tag=MAIN. 11 | 12 | # Authors 13 | Maintained by Fredrik Fornwall ([@fornwall](https://github.com/fornwall)) and [these fine people](https://github.com/termux/getconf/graphs/contributors) have contributed. 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 1996, 1998 The NetBSD Foundation, Inc. 4 | Copyright (c) 2017, Fredrik Fornwall. 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /getconf.1: -------------------------------------------------------------------------------- 1 | .\" $NetBSD: getconf.1,v 1.13 2014/04/13 01:45:34 snj Exp $ 2 | .\" 3 | .\" Copyright (c) 1996 The NetBSD Foundation, Inc. 4 | .\" All rights reserved. 5 | .\" 6 | .\" This code is derived from software contributed to The NetBSD Foundation 7 | .\" by J.T. Conklin. 8 | .\" 9 | .\" Redistribution and use in source and binary forms, with or without 10 | .\" modification, are permitted provided that the following conditions 11 | .\" are met: 12 | .\" 1. Redistributions of source code must retain the above copyright 13 | .\" notice, this list of conditions and the following disclaimer. 14 | .\" 2. Redistributions in binary form must reproduce the above copyright 15 | .\" notice, this list of conditions and the following disclaimer in the 16 | .\" documentation and/or other materials provided with the distribution. 17 | .\" 18 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 | .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 | .\" POSSIBILITY OF SUCH DAMAGE. 29 | .\" 30 | .Dd August 9, 2011 31 | .Dt GETCONF 1 32 | .Os 33 | .Sh NAME 34 | .Nm getconf 35 | .Nd get configuration values 36 | .Sh SYNOPSIS 37 | .Nm 38 | .Ar system_var 39 | .Nm 40 | .Fl a 41 | .Nm 42 | .Ar path_var 43 | .Ar pathname 44 | .Nm 45 | .Fl a 46 | .Ar pathname 47 | .Sh DESCRIPTION 48 | The 49 | .Nm 50 | utility writes the current value of a configurable system limit or 51 | option variable to the standard output. 52 | .Pp 53 | The 54 | .Ar system_var 55 | argument specifies the system variable to be queried. 56 | The names of the system variables are from 57 | .Xr sysconf 3 58 | with the leading 59 | .Dq Li _SC_ 60 | removed. 61 | .Pp 62 | The 63 | .Ar path_var 64 | argument specifies the pathname variable to be queried for the specified 65 | .Ar pathname 66 | argument. 67 | The names of the pathname variables are from 68 | .Xr pathconf 2 69 | with the leading 70 | .Dq Li _PC_ 71 | removed. 72 | .Pp 73 | When invoked with the option 74 | .Fl a , 75 | .Nm 76 | writes a list of all applicable variables and their values to the 77 | standard output, in the format 78 | .Do 79 | .Va name 80 | = 81 | .Va value 82 | .Dc . 83 | .Sh EXIT STATUS 84 | .Ex -std 85 | .Sh SEE ALSO 86 | .Xr pathconf 2 , 87 | .Xr confstr 3 , 88 | .Xr limits 3 , 89 | .Xr sysconf 3 90 | .Sh STANDARDS 91 | The 92 | .Nm 93 | utility conforms to 94 | .St -p1003.2-92 . 95 | -------------------------------------------------------------------------------- /getconf.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc. 3 | * All rights reserved. 4 | * 5 | * This code is derived from software contributed to The NetBSD Foundation 6 | * by J.T. Conklin. 7 | * 8 | * Mostly rewritten to be used in Alpine Linux (with musl c-library) 9 | * by Timo Teräs. 10 | * 11 | * Maintained for use in Termux (and more) by Fredrik Fornwall. 12 | * 13 | * Redistribution and use in source and binary forms, with or without 14 | * modification, are permitted provided that the following conditions 15 | * are met: 16 | * 1. Redistributions of source code must retain the above copyright 17 | * notice, this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright 19 | * notice, this list of conditions and the following disclaimer in the 20 | * documentation and/or other materials provided with the distribution. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #define _GNU_SOURCE 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | struct conf_variable { 47 | const char *name; 48 | enum { SYSCONF, CONFSTR, PATHCONF, CONSTANT, UCONSTANT, NUM_TYPES } type; 49 | long value; 50 | }; 51 | 52 | static const struct conf_variable conf_table[] = { 53 | #ifdef _CS_PATH 54 | { "PATH", CONFSTR, _CS_PATH }, 55 | #endif 56 | 57 | /* Utility Limit Minimum Values */ 58 | #ifdef _POSIX2_BC_BASE_MAX 59 | { "POSIX2_BC_BASE_MAX", CONSTANT, _POSIX2_BC_BASE_MAX }, 60 | #endif 61 | #ifdef _POSIX2_BC_DIM_MAX 62 | { "POSIX2_BC_DIM_MAX", CONSTANT, _POSIX2_BC_DIM_MAX }, 63 | #endif 64 | #ifdef _POSIX2_BC_SCALE_MAX 65 | { "POSIX2_BC_SCALE_MAX", CONSTANT, _POSIX2_BC_SCALE_MAX }, 66 | #endif 67 | #ifdef _POSIX2_BC_STRING_MAX 68 | { "POSIX2_BC_STRING_MAX", CONSTANT, _POSIX2_BC_STRING_MAX }, 69 | #endif 70 | #ifdef _POSIX2_COLL_WEIGHTS_MAX 71 | { "POSIX2_COLL_WEIGHTS_MAX", CONSTANT, _POSIX2_COLL_WEIGHTS_MAX }, 72 | #endif 73 | #ifdef _POSIX2_EXPR_NEST_MAX 74 | { "POSIX2_EXPR_NEST_MAX", CONSTANT, _POSIX2_EXPR_NEST_MAX }, 75 | #endif 76 | { "POSIX2_LINE_MAX", CONSTANT, _POSIX2_LINE_MAX }, 77 | #ifdef _POSIX2_RE_DUP_MAX 78 | { "POSIX2_RE_DUP_MAX", CONSTANT, _POSIX2_RE_DUP_MAX }, 79 | #endif 80 | #ifdef _POSIX2_VERSION 81 | { "POSIX2_VERSION", CONSTANT, _POSIX2_VERSION }, 82 | #endif 83 | 84 | /* POSIX.1 Minimum Values */ 85 | #ifdef _POSIX_AIO_LISTIO_MAX 86 | { "_POSIX_AIO_LISTIO_MAX", CONSTANT, _POSIX_AIO_LISTIO_MAX }, 87 | #endif 88 | #ifdef _POSIX_AIO_MAX 89 | { "_POSIX_AIO_MAX", CONSTANT, _POSIX_AIO_MAX }, 90 | #endif 91 | { "_POSIX_ARG_MAX", CONSTANT, _POSIX_ARG_MAX }, 92 | { "_POSIX_CHILD_MAX", CONSTANT, _POSIX_CHILD_MAX }, 93 | { "_POSIX_LINK_MAX", CONSTANT, _POSIX_LINK_MAX }, 94 | { "_POSIX_MAX_CANON", CONSTANT, _POSIX_MAX_CANON }, 95 | { "_POSIX_MAX_INPUT", CONSTANT, _POSIX_MAX_INPUT }, 96 | #ifdef _POSIX_MQ_OPEN_MAX 97 | { "_POSIX_MQ_OPEN_MAX", CONSTANT, _POSIX_MQ_OPEN_MAX }, 98 | #endif 99 | #ifdef _POSIX_MQ_PRIO_MAX 100 | { "_POSIX_MQ_PRIO_MAX", CONSTANT, _POSIX_MQ_PRIO_MAX }, 101 | #endif 102 | { "_POSIX_NAME_MAX", CONSTANT, _POSIX_NAME_MAX }, 103 | { "_POSIX_NGROUPS_MAX", CONSTANT, _POSIX_NGROUPS_MAX }, 104 | { "_POSIX_OPEN_MAX", CONSTANT, _POSIX_OPEN_MAX }, 105 | { "_POSIX_PATH_MAX", CONSTANT, _POSIX_PATH_MAX }, 106 | { "_POSIX_PIPE_BUF", CONSTANT, _POSIX_PIPE_BUF }, 107 | { "_POSIX_SSIZE_MAX", CONSTANT, _POSIX_SSIZE_MAX }, 108 | { "_POSIX_STREAM_MAX", CONSTANT, _POSIX_STREAM_MAX }, 109 | { "_POSIX_TZNAME_MAX", CONSTANT, _POSIX_TZNAME_MAX }, 110 | 111 | /* Symbolic Utility Limits */ 112 | #ifdef _SC_BC_BASE_MAX 113 | { "BC_BASE_MAX", SYSCONF, _SC_BC_BASE_MAX }, 114 | #endif 115 | #ifdef _SC_BC_DIM_MAX 116 | { "BC_DIM_MAX", SYSCONF, _SC_BC_DIM_MAX }, 117 | #endif 118 | #ifdef _SC_BC_SCALE_MAX 119 | { "BC_SCALE_MAX", SYSCONF, _SC_BC_SCALE_MAX }, 120 | #endif 121 | #ifdef _SC_BC_STRING_MAX 122 | { "BC_STRING_MAX", SYSCONF, _SC_BC_STRING_MAX }, 123 | #endif 124 | #ifdef _SC_COLL_WEIGHTS_MAX 125 | { "COLL_WEIGHTS_MAX", SYSCONF, _SC_COLL_WEIGHTS_MAX }, 126 | #endif 127 | #ifdef _SC_EXPR_NEST_MAX 128 | { "EXPR_NEST_MAX", SYSCONF, _SC_EXPR_NEST_MAX }, 129 | #endif 130 | #ifdef _SC_LINE_MAX 131 | { "LINE_MAX", SYSCONF, _SC_LINE_MAX }, 132 | #endif 133 | #ifdef _SC_RE_DUP_MAX 134 | { "RE_DUP_MAX", SYSCONF, _SC_RE_DUP_MAX }, 135 | #endif 136 | 137 | /* Optional Facility Configuration Values */ 138 | #ifdef _SC_2_C_BIND 139 | { "_POSIX2_C_BIND", SYSCONF, _SC_2_C_BIND }, 140 | #endif 141 | #ifdef _SC_2_C_DEV 142 | { "POSIX2_C_DEV", SYSCONF, _SC_2_C_DEV }, 143 | #endif 144 | #ifdef _SC_2_C_CHAR_TERM 145 | { "POSIX2_CHAR_TERM", SYSCONF, _SC_2_CHAR_TERM }, 146 | #endif 147 | #ifdef _SC_2_C_FORT_DEV 148 | { "POSIX2_FORT_DEV", SYSCONF, _SC_2_FORT_DEV }, 149 | #endif 150 | #ifdef _SC_2_C_FORT_RUN 151 | { "POSIX2_FORT_RUN", SYSCONF, _SC_2_FORT_RUN }, 152 | #endif 153 | #ifdef _SC_2_C_LOCALEDEF 154 | { "POSIX2_LOCALEDEF", SYSCONF, _SC_2_LOCALEDEF }, 155 | #endif 156 | #ifdef _SC_2_SW_DEV 157 | { "POSIX2_SW_DEV", SYSCONF, _SC_2_SW_DEV }, 158 | #endif 159 | #ifdef _SC_2_UPE 160 | { "POSIX2_UPE", SYSCONF, _SC_2_UPE }, 161 | #endif 162 | 163 | /* POSIX.1 Configurable System Variables */ 164 | #ifdef _SC_AIO_LISTIO_MAX 165 | { "AIO_LISTIO_MAX", SYSCONF, _SC_AIO_LISTIO_MAX }, 166 | #endif 167 | #ifdef _SC_AIO_MAX 168 | { "AIO_MAX", SYSCONF, _SC_AIO_MAX }, 169 | #endif 170 | { "ARG_MAX", SYSCONF, _SC_ARG_MAX }, 171 | { "CHILD_MAX", SYSCONF, _SC_CHILD_MAX }, 172 | { "CLK_TCK", SYSCONF, _SC_CLK_TCK }, 173 | #ifdef _SC_MQ_OPEN_MAX 174 | { "MQ_OPEN_MAX", SYSCONF, _SC_MQ_OPEN_MAX }, 175 | #endif 176 | #ifdef _SC_MQ_PRIO_MAX 177 | { "MQ_PRIO_MAX", SYSCONF, _SC_MQ_PRIO_MAX }, 178 | #endif 179 | { "NGROUPS_MAX", SYSCONF, _SC_NGROUPS_MAX }, 180 | { "OPEN_MAX", SYSCONF, _SC_OPEN_MAX }, 181 | { "STREAM_MAX", SYSCONF, _SC_STREAM_MAX }, 182 | { "TZNAME_MAX", SYSCONF, _SC_TZNAME_MAX }, 183 | { "_POSIX_JOB_CONTROL", SYSCONF, _SC_JOB_CONTROL }, 184 | { "_POSIX_SAVED_IDS", SYSCONF, _SC_SAVED_IDS }, 185 | { "_POSIX_VERSION", SYSCONF, _SC_VERSION }, 186 | 187 | { "LINK_MAX", PATHCONF, _PC_LINK_MAX }, 188 | { "MAX_CANON", PATHCONF, _PC_MAX_CANON }, 189 | { "MAX_INPUT", PATHCONF, _PC_MAX_INPUT }, 190 | { "NAME_MAX", PATHCONF, _PC_NAME_MAX }, 191 | { "PATH_MAX", PATHCONF, _PC_PATH_MAX }, 192 | { "PIPE_BUF", PATHCONF, _PC_PIPE_BUF }, 193 | { "_POSIX_CHOWN_RESTRICTED", PATHCONF, _PC_CHOWN_RESTRICTED }, 194 | { "_POSIX_NO_TRUNC", PATHCONF, _PC_NO_TRUNC }, 195 | { "_POSIX_VDISABLE", PATHCONF, _PC_VDISABLE }, 196 | 197 | /* POSIX.1b Configurable System Variables */ 198 | { "PAGESIZE", SYSCONF, _SC_PAGESIZE }, 199 | #ifdef _SC_ASYNCHRONOUS_IO 200 | { "_POSIX_ASYNCHRONOUS_IO", SYSCONF, _SC_ASYNCHRONOUS_IO }, 201 | #endif 202 | #ifdef _SC_FSYNC 203 | { "_POSIX_FSYNC", SYSCONF, _SC_FSYNC }, 204 | #endif 205 | { "_POSIX_MAPPED_FILES", SYSCONF, _SC_MAPPED_FILES }, 206 | #ifdef _SC_MEMLOCK 207 | { "_POSIX_MEMLOCK", SYSCONF, _SC_MEMLOCK }, 208 | #endif 209 | #ifdef _SC_MEMLOCK_RANGE 210 | { "_POSIX_MEMLOCK_RANGE", SYSCONF, _SC_MEMLOCK_RANGE }, 211 | #endif 212 | { "_POSIX_MEMORY_PROTECTION", SYSCONF, _SC_MEMORY_PROTECTION }, 213 | #ifdef _SC_MESSAGE_PASSING 214 | { "_POSIX_MESSAGE_PASSING", SYSCONF, _SC_MESSAGE_PASSING }, 215 | #endif 216 | { "_POSIX_MONOTONIC_CLOCK", SYSCONF, _SC_MONOTONIC_CLOCK }, 217 | #ifdef _SC_PRIORITY_SCHEDULING 218 | { "_POSIX_PRIORITY_SCHEDULING", SYSCONF, _SC_PRIORITY_SCHEDULING }, 219 | #endif 220 | { "_POSIX_SEMAPHORES", SYSCONF, _SC_SEMAPHORES }, 221 | #ifdef _SC_SHARED_MEMORY_OBJECTS 222 | { "_POSIX_SHARED_MEMORY_OBJECTS", SYSCONF, _SC_SHARED_MEMORY_OBJECTS }, 223 | #endif 224 | #ifdef _SC_SYNCHRONIZED_IO 225 | { "_POSIX_SYNCHRONIZED_IO", SYSCONF, _SC_SYNCHRONIZED_IO }, 226 | #endif 227 | { "_POSIX_TIMERS", SYSCONF, _SC_TIMERS }, 228 | 229 | { "_POSIX_SYNC_IO", PATHCONF, _PC_SYNC_IO }, 230 | 231 | /* POSIX.1c Configurable System Variables */ 232 | #ifdef _SC_LOGIN_NAME_MAX 233 | { "LOGIN_NAME_MAX", SYSCONF, _SC_LOGIN_NAME_MAX }, 234 | #endif 235 | { "_POSIX_THREADS", SYSCONF, _SC_THREADS }, 236 | 237 | /* POSIX.1j Configurable System Variables */ 238 | #ifdef _SC_BARRIERS 239 | { "_POSIX_BARRIERS", SYSCONF, _SC_BARRIERS }, 240 | #endif 241 | #ifdef _SC_READER_WRITER_LOCKS 242 | { "_POSIX_READER_WRITER_LOCKS", SYSCONF, _SC_READER_WRITER_LOCKS }, 243 | #endif 244 | #ifdef _SC_SPIN_LOCKS 245 | { "_POSIX_SPIN_LOCKS", SYSCONF, _SC_SPIN_LOCKS }, 246 | #endif 247 | 248 | /* XPG4.2 Configurable System Variables */ 249 | { "IOV_MAX", SYSCONF, _SC_IOV_MAX }, 250 | { "PAGE_SIZE", SYSCONF, _SC_PAGE_SIZE }, 251 | #ifdef _SC_XOPEN_SHM 252 | { "_XOPEN_SHM", SYSCONF, _SC_XOPEN_SHM }, 253 | #endif 254 | 255 | /* X/Open CAE Spec. Issue 5 Version 2 Configurable System Variables */ 256 | { "FILESIZEBITS", PATHCONF, _PC_FILESIZEBITS }, 257 | 258 | /* POSIX.1-2001 XSI Option Group Configurable System Variables */ 259 | { "ATEXIT_MAX", SYSCONF, _SC_ATEXIT_MAX }, 260 | 261 | /* POSIX.1-2001 TSF Configurable System Variables */ 262 | { "GETGR_R_SIZE_MAX", SYSCONF, _SC_GETGR_R_SIZE_MAX }, 263 | { "GETPW_R_SIZE_MAX", SYSCONF, _SC_GETPW_R_SIZE_MAX }, 264 | 265 | /* Commonly provided extensions */ 266 | { "_PHYS_PAGES", SYSCONF, _SC_PHYS_PAGES }, 267 | #ifdef _SC_AVPHYS_PAGES 268 | { "_AVPHYS_PAGES", SYSCONF, _SC_AVPHYS_PAGES }, 269 | #endif 270 | { "_NPROCESSORS_CONF", SYSCONF, _SC_NPROCESSORS_CONF }, 271 | { "_NPROCESSORS_ONLN", SYSCONF, _SC_NPROCESSORS_ONLN }, 272 | 273 | /* Data type related extensions */ 274 | { "CHAR_BIT", CONSTANT, CHAR_BIT }, 275 | { "CHAR_MAX", CONSTANT, CHAR_MAX }, 276 | { "CHAR_MIN", CONSTANT, CHAR_MIN }, 277 | { "INT_MAX", CONSTANT, INT_MAX }, 278 | { "INT_MIN", CONSTANT, INT_MIN }, 279 | #ifdef LONG_BIT 280 | { "LONG_BIT", CONSTANT, LONG_BIT }, 281 | #endif 282 | { "LONG_MAX", CONSTANT, LONG_MAX }, 283 | { "LONG_MIN", CONSTANT, LONG_MIN }, 284 | { "SCHAR_MAX", CONSTANT, SCHAR_MAX }, 285 | { "SCHAR_MIN", CONSTANT, SCHAR_MIN }, 286 | { "SHRT_MAX", CONSTANT, SHRT_MAX }, 287 | { "SHRT_MIN", CONSTANT, SHRT_MIN }, 288 | { "SSIZE_MAX", CONSTANT, SSIZE_MAX }, 289 | { "UCHAR_MAX", UCONSTANT, (long) UCHAR_MAX }, 290 | { "UINT_MAX", UCONSTANT, (long) UINT_MAX }, 291 | { "ULONG_MAX", UCONSTANT, (long) ULONG_MAX }, 292 | { "USHRT_MAX", UCONSTANT, (long) USHRT_MAX }, 293 | { "WORD_BIT", CONSTANT, sizeof(int)*8 }, 294 | 295 | { NULL, CONSTANT, 0L } 296 | }; 297 | 298 | static int all = 0; 299 | 300 | static void usage(const char *p) 301 | { 302 | (void)fprintf(stderr, "Usage: %s system_var\n" 303 | " %s -a\n" 304 | " %s path_var pathname\n" 305 | " %s -a pathname\n", p, p, p, p); 306 | exit(EXIT_FAILURE); 307 | } 308 | 309 | static void print_long(const char *name, long val) 310 | { 311 | if (all) printf("%s = %ld\n", name, val); 312 | else printf("%ld\n", val); 313 | } 314 | 315 | static void print_ulong(const char *name, unsigned long val) 316 | { 317 | if (all) printf("%s = %lu\n", name, val); 318 | else printf("%lu\n", val); 319 | } 320 | 321 | static void print_string(const char *name, const char *val) 322 | { 323 | if (all) printf("%s = %s\n", name, val); 324 | else printf("%s\n", val); 325 | } 326 | 327 | static int print_constant(const struct conf_variable *cp, const char *pathname __attribute__ ((unused))) 328 | { 329 | print_long(cp->name, cp->value); 330 | return 0; 331 | } 332 | 333 | static int print_uconstant(const struct conf_variable *cp, const char *pathname __attribute__ ((unused))) 334 | { 335 | print_ulong(cp->name, (unsigned long) cp->value); 336 | return 0; 337 | } 338 | 339 | static int print_sysconf(const struct conf_variable *cp, const char *pathname __attribute__ ((unused))) 340 | { 341 | long val; 342 | 343 | errno = 0; 344 | if ((val = sysconf((int)cp->value)) == -1) { 345 | if (errno != 0) err(EXIT_FAILURE, "sysconf(%ld)", cp->value); 346 | return -1; 347 | } 348 | print_long(cp->name, val); 349 | return 0; 350 | } 351 | 352 | #ifndef __ANDROID__ 353 | static int print_confstr(struct conf_variable const* cp, char const* pathname __attribute__ ((unused))) 354 | { 355 | size_t len; 356 | char *val; 357 | 358 | errno = 0; 359 | if ((len = confstr((int)cp->value, NULL, 0)) == 0) goto error; 360 | if ((val = malloc(len)) == NULL) err(EXIT_FAILURE, "Can't allocate %zu bytes", len); 361 | errno = 0; 362 | if (confstr((int)cp->value, val, len) == 0) goto error; 363 | print_string(cp->name, val); 364 | free(val); 365 | return 0; 366 | error: 367 | if (errno != EINVAL) err(EXIT_FAILURE, "confstr(%ld)", cp->value); 368 | return -1; 369 | } 370 | #endif 371 | 372 | static int print_pathconf(const struct conf_variable *cp, const char *pathname) 373 | { 374 | long val; 375 | 376 | errno = 0; 377 | if ((val = pathconf(pathname, (int)cp->value)) == -1) { 378 | if (all && errno == EINVAL) return 0; 379 | if (errno != 0) err(EXIT_FAILURE, "pathconf(%s, %ld)", pathname, cp->value); 380 | return -1; 381 | } 382 | print_long(cp->name, val); 383 | return 0; 384 | } 385 | 386 | typedef int (*handler_t)(const struct conf_variable *cp, const char *pathname); 387 | static const handler_t type_handlers[NUM_TYPES] = { 388 | [SYSCONF] = print_sysconf, 389 | #ifndef __ANDROID__ 390 | [CONFSTR] = print_confstr, 391 | #endif 392 | [PATHCONF] = print_pathconf, 393 | [CONSTANT] = print_constant, 394 | [UCONSTANT] = print_uconstant, 395 | }; 396 | 397 | int main(int argc, char **argv) 398 | { 399 | const char *progname = argv[0]; 400 | const struct conf_variable *cp; 401 | const char *varname, *pathname; 402 | int ch, found = 0; 403 | 404 | (void)setlocale(LC_ALL, ""); 405 | while ((ch = getopt(argc, argv, "a")) != -1) { 406 | switch (ch) { 407 | case 'a': 408 | all = 1; 409 | break; 410 | case '?': 411 | default: 412 | usage(progname); 413 | } 414 | } 415 | argc -= optind; 416 | argv += optind; 417 | 418 | if (!all) { 419 | if (argc == 0) 420 | usage(progname); 421 | varname = argv[0]; 422 | argc--; 423 | argv++; 424 | } else 425 | varname = NULL; 426 | 427 | if (argc > 1) 428 | usage(progname); 429 | pathname = argv[0]; /* may be NULL */ 430 | 431 | for (cp = conf_table; cp->name != NULL; cp++) { 432 | if (!all && strcmp(varname, cp->name) != 0) continue; 433 | if ((cp->type == PATHCONF) == (pathname != NULL)) { 434 | if (type_handlers[cp->type](cp, pathname) < 0) 435 | print_string(cp->name, "undefined"); 436 | found = 1; 437 | } else if (!all) 438 | errx(EXIT_FAILURE, "%s: invalid variable type", cp->name); 439 | } 440 | if (!all && !found) errx(EXIT_FAILURE, "%s: unknown variable", varname); 441 | (void)fflush(stdout); 442 | return ferror(stdout) ? EXIT_FAILURE : EXIT_SUCCESS; 443 | } 444 | --------------------------------------------------------------------------------