Deleted Added
full compact
getconf.c (304949) getconf.c (324124)
1/*
2 * Copyright 2000 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all

--- 14 unchanged lines hidden (view full) ---

23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/*
2 * Copyright 2000 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all

--- 14 unchanged lines hidden (view full) ---

23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/11/usr.bin/getconf/getconf.c 304949 2016-08-28 07:16:11Z ngie $");
31__FBSDID("$FreeBSD: stable/11/usr.bin/getconf/getconf.c 324124 2017-09-30 17:30:22Z jhb $");
32
33#include <sys/types.h>
34
35#include <err.h>
36#include <errno.h>
32
33#include <sys/types.h>
34
35#include <err.h>
36#include <errno.h>
37#include <stdbool.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <sysexits.h>
40#include <unistd.h>
41
42#include "getconf.h"
43
38#include <stdio.h>
39#include <stdlib.h>
40#include <sysexits.h>
41#include <unistd.h>
42
43#include "getconf.h"
44
45static void do_allsys(void);
46static void do_allpath(const char *path);
44static void do_confstr(const char *name, int key);
45static void do_sysconf(const char *name, int key);
46static void do_pathconf(const char *name, int key, const char *path);
47
48static void
49usage(void)
50{
51 fprintf(stderr,
47static void do_confstr(const char *name, int key);
48static void do_sysconf(const char *name, int key);
49static void do_pathconf(const char *name, int key, const char *path);
50
51static void
52usage(void)
53{
54 fprintf(stderr,
52"usage: getconf [-v prog_env] system_var\n"
55"usage: getconf -a [pathname]\n"
56" getconf [-v prog_env] system_var\n"
53" getconf [-v prog_env] path_var pathname\n");
54 exit(EX_USAGE);
55}
56
57int
58main(int argc, char **argv)
59{
57" getconf [-v prog_env] path_var pathname\n");
58 exit(EX_USAGE);
59}
60
61int
62main(int argc, char **argv)
63{
64 bool aflag;
60 int c, key, valid;
61 const char *name, *vflag, *alt_path;
62 intmax_t limitval;
63
65 int c, key, valid;
66 const char *name, *vflag, *alt_path;
67 intmax_t limitval;
68
69 aflag = false;
64 vflag = NULL;
70 vflag = NULL;
65 while ((c = getopt(argc, argv, "v:")) != -1) {
71 while ((c = getopt(argc, argv, "av:")) != -1) {
66 switch (c) {
72 switch (c) {
73 case 'a':
74 aflag = true;
75 break;
67 case 'v':
68 vflag = optarg;
69 break;
70
71 default:
72 usage();
73 }
74 }
75
76 case 'v':
77 vflag = optarg;
78 break;
79
80 default:
81 usage();
82 }
83 }
84
85 if (aflag) {
86 if (vflag != NULL)
87 usage();
88 if (argv[optind] == NULL)
89 do_allsys();
90 else
91 do_allpath(argv[optind]);
92 return (0);
93 }
94
76 if ((name = argv[optind]) == NULL)
77 usage();
78
79 if (vflag != NULL) {
80 if ((valid = find_progenv(vflag, &alt_path)) == 0)
81 errx(EX_USAGE, "invalid programming environment %s",
82 vflag);
83 if (valid > 0 && alt_path != NULL) {

--- 47 unchanged lines hidden (view full) ---

131 errx(EX_USAGE,
132 "no such path configuration parameter `%s'",
133 name);
134 }
135 return 0;
136}
137
138static void
95 if ((name = argv[optind]) == NULL)
96 usage();
97
98 if (vflag != NULL) {
99 if ((valid = find_progenv(vflag, &alt_path)) == 0)
100 errx(EX_USAGE, "invalid programming environment %s",
101 vflag);
102 if (valid > 0 && alt_path != NULL) {

--- 47 unchanged lines hidden (view full) ---

150 errx(EX_USAGE,
151 "no such path configuration parameter `%s'",
152 name);
153 }
154 return 0;
155}
156
157static void
158do_onestr(const char *name, int key)
159{
160 size_t len;
161
162 errno = 0;
163 len = confstr(key, 0, 0);
164 if (len == 0 && errno != 0) {
165 warn("confstr: %s", name);
166 return;
167 }
168 printf("%s: ", name);
169 if (len == 0)
170 printf("undefined\n");
171 else {
172 char buf[len + 1];
173
174 confstr(key, buf, len);
175 printf("%s\n", buf);
176 }
177}
178
179static void
180do_onesys(const char *name, int key)
181{
182 long value;
183
184 errno = 0;
185 value = sysconf(key);
186 if (value == -1 && errno != 0) {
187 warn("sysconf: %s", name);
188 return;
189 }
190 printf("%s: ", name);
191 if (value == -1)
192 printf("undefined\n");
193 else
194 printf("%ld\n", value);
195}
196
197static void
198do_allsys(void)
199{
200
201 foreach_confstr(do_onestr);
202 foreach_sysconf(do_onesys);
203}
204
205static void
206do_onepath(const char *name, int key, const char *path)
207{
208 long value;
209
210 errno = 0;
211 value = pathconf(path, key);
212 if (value == -1 && errno != EINVAL && errno != 0)
213 warn("pathconf: %s", name);
214 printf("%s: ", name);
215 if (value == -1)
216 printf("undefined\n");
217 else
218 printf("%ld\n", value);
219}
220
221static void
222do_allpath(const char *path)
223{
224
225 foreach_pathconf(do_onepath, path);
226}
227
228static void
139do_confstr(const char *name, int key)
140{
141 size_t len;
142 int savederr;
143
144 savederr = errno;
145 errno = 0;
146 len = confstr(key, 0, 0);

--- 44 unchanged lines hidden ---
229do_confstr(const char *name, int key)
230{
231 size_t len;
232 int savederr;
233
234 savederr = errno;
235 errno = 0;
236 len = confstr(key, 0, 0);

--- 44 unchanged lines hidden ---