1/* See LICENSE file for copyright and license details. */
2
3#include <errno.h>
4#include <limits.h>
5#include <stdlib.h>
6#include <string.h>
7#include <unistd.h>
8
9#include "util.h"
10
11struct var {
12 const char *k;
13 long v;
14};
15
16#include "getconf.h"
17
18void
19usage(void)
20{
21 eprintf("usage: %s [-v spec] var [path]\n", argv0);
22}
23
24// ?man getconf: query configuration variables
25// ?man arguments: var [path]
26// ?man query system configuration variables
27int
28main(int argc, char *argv[])
29{
30 size_t len;
31 long res;
32 size_t i;
33 char *str;
34
35 ARGBEGIN
36 {
37 // ?man -v:str: verbose mode; show progress
38 case 'v':
39 /* ignore */
40 EARGF(usage());
41 break;
42 default:
43 usage();
44 break;
45 }
46 ARGEND
47
48 if (argc == 1) {
49 /* sysconf */
50 for (i = 0; i < LEN(sysconf_l); i++) {
51 if (strcmp(argv[0], sysconf_l[i].k))
52 continue;
53 errno = 0;
54 if ((res = sysconf(sysconf_l[i].v)) < 0) {
55 if (errno)
56 eprintf("sysconf %ld:", sysconf_l[i].v);
57 puts("undefined");
58 } else {
59 printf("%ld\n", res);
60 }
61 return fshut(stdout, "<stdout>");
62 }
63 /* confstr */
64 for (i = 0; i < LEN(confstr_l); i++) {
65 if (strcmp(argv[0], confstr_l[i].k))
66 continue;
67 errno = 0;
68 if (!(len = confstr(confstr_l[i].v, NULL, 0))) {
69 if (errno)
70 eprintf("confstr %ld:", confstr_l[i].v);
71 puts("undefined");
72 } else {
73 str = emalloc(len);
74 errno = 0;
75 if (!confstr(confstr_l[i].v, str, len)) {
76 if (errno)
77 eprintf("confstr %ld:", confstr_l[i].v);
78 puts("undefined");
79 } else {
80 puts(str);
81 }
82 free(str);
83 }
84 return fshut(stdout, "<stdout>");
85 }
86 /* limits */
87 for (i = 0; i < LEN(limits_l); i++) {
88 if (strcmp(argv[0], limits_l[i].k))
89 continue;
90 printf("%ld\n", limits_l[i].v);
91 return fshut(stdout, "<stdout>");
92 }
93 } else if (argc == 2) {
94 /* pathconf */
95 for (i = 0; i < LEN(pathconf_l); i++) {
96 if (strcmp(argv[0], pathconf_l[i].k))
97 continue;
98 errno = 0;
99 if ((res = pathconf(argv[1], pathconf_l[i].v)) < 0) {
100 if (errno)
101 eprintf("pathconf %ld:", pathconf_l[i].v);
102 puts("undefined");
103 } else {
104 printf("%ld\n", res);
105 }
106 return fshut(stdout, "<stdout>");
107 }
108 } else {
109 usage();
110 }
111
112 eprintf("invalid variable: %s\n", argv[0]);
113
114 return 0;
115}