2015-09-30 18:59:40 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct var {
|
|
|
|
const char *k;
|
|
|
|
long v;
|
|
|
|
};
|
|
|
|
|
2016-10-03 18:55:22 -04:00
|
|
|
#include "getconf.h"
|
2015-12-14 15:22:43 -05:00
|
|
|
|
2015-09-30 18:59:40 -04:00
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-12-15 03:43:55 -05:00
|
|
|
eprintf("usage: %s [-v spec] var [path]\n", argv0);
|
2015-09-30 18:59:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
long res;
|
|
|
|
int i;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'v':
|
|
|
|
/* ignore */
|
2015-10-04 11:48:05 -04:00
|
|
|
EARGF(usage());
|
2015-09-30 18:59:40 -04:00
|
|
|
break;
|
2017-01-26 18:41:50 -05:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2015-09-30 18:59:40 -04:00
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
/* sysconf */
|
|
|
|
for (i = 0; i < LEN(sysconf_l); i++) {
|
|
|
|
if (strcmp(argv[0], sysconf_l[i].k))
|
|
|
|
continue;
|
|
|
|
errno = 0;
|
|
|
|
if ((res = sysconf(sysconf_l[i].v)) < 0) {
|
|
|
|
if (errno)
|
|
|
|
eprintf("sysconf %ld:", sysconf_l[i].v);
|
|
|
|
puts("undefined");
|
|
|
|
} else {
|
|
|
|
printf("%ld\n", res);
|
|
|
|
}
|
2017-09-03 20:26:04 -04:00
|
|
|
return fshut(stdout, "<stdout>");
|
2015-09-30 18:59:40 -04:00
|
|
|
}
|
|
|
|
/* confstr */
|
|
|
|
for (i = 0; i < LEN(confstr_l); i++) {
|
|
|
|
if (strcmp(argv[0], confstr_l[i].k))
|
|
|
|
continue;
|
|
|
|
errno = 0;
|
|
|
|
if (!(len = confstr(confstr_l[i].v, NULL, 0))) {
|
|
|
|
if (errno)
|
|
|
|
eprintf("confstr %ld:", confstr_l[i].v);
|
|
|
|
puts("undefined");
|
|
|
|
} else {
|
|
|
|
str = emalloc(len);
|
|
|
|
errno = 0;
|
|
|
|
if (!confstr(confstr_l[i].v, str, len)) {
|
|
|
|
if (errno)
|
|
|
|
eprintf("confstr %ld:", confstr_l[i].v);
|
|
|
|
puts("undefined");
|
|
|
|
} else {
|
|
|
|
puts(str);
|
|
|
|
}
|
|
|
|
free(str);
|
|
|
|
}
|
2017-09-03 20:26:04 -04:00
|
|
|
return fshut(stdout, "<stdout>");
|
2015-09-30 18:59:40 -04:00
|
|
|
}
|
|
|
|
/* limits */
|
|
|
|
for (i = 0; i < LEN(limits_l); i++) {
|
|
|
|
if (strcmp(argv[0], limits_l[i].k))
|
|
|
|
continue;
|
|
|
|
printf("%ld\n", limits_l[i].v);
|
2017-09-03 20:26:04 -04:00
|
|
|
return fshut(stdout, "<stdout>");
|
2015-09-30 18:59:40 -04:00
|
|
|
}
|
|
|
|
} else if (argc == 2) {
|
|
|
|
/* pathconf */
|
|
|
|
for (i = 0; i < LEN(pathconf_l); i++) {
|
|
|
|
if (strcmp(argv[0], pathconf_l[i].k))
|
|
|
|
continue;
|
|
|
|
errno = 0;
|
|
|
|
if ((res = pathconf(argv[1], pathconf_l[i].v)) < 0) {
|
|
|
|
if (errno)
|
|
|
|
eprintf("pathconf %ld:", pathconf_l[i].v);
|
|
|
|
puts("undefined");
|
|
|
|
} else {
|
|
|
|
printf("%ld\n", res);
|
|
|
|
}
|
2017-09-03 20:26:04 -04:00
|
|
|
return fshut(stdout, "<stdout>");
|
2015-09-30 18:59:40 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2017-09-03 18:39:47 -04:00
|
|
|
eprintf("invalid variable: %s\n", argv[0]);
|
2015-09-30 18:59:40 -04:00
|
|
|
}
|