1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000 Peter Wemm <peter@freebsd.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29 #include <sys/sysctl.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <kenv.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 static void usage(void);
39 static int kdumpenv(int dump_type);
40 static int kgetenv(const char *);
41 static int ksetenv(const char *, char *);
42 static int kunsetenv(const char *);
43
44 static int hflag = 0;
45 static int lflag = 0;
46 static int Nflag = 0;
47 static int qflag = 0;
48 static int sflag = 0;
49 static int uflag = 0;
50 static int vflag = 0;
51
52 static void
usage(void)53 usage(void)
54 {
55 (void)fprintf(stderr, "%s\n%s\n%s\n",
56 "usage: kenv [-l|-s] [-hNq]",
57 " kenv [-qv] variable[=value]",
58 " kenv [-q] -u variable");
59 exit(1);
60 }
61
62 int
main(int argc,char ** argv)63 main(int argc, char **argv)
64 {
65 char *env, *eq, *val;
66 int ch, error;
67
68 val = NULL;
69 env = NULL;
70 while ((ch = getopt(argc, argv, "hlNqsuv")) != -1) {
71 switch (ch) {
72 case 'h':
73 hflag++;
74 break;
75 case 'l':
76 lflag++;
77 break;
78 case 'N':
79 Nflag++;
80 break;
81 case 'q':
82 qflag++;
83 break;
84 case 's':
85 sflag++;
86 break;
87 case 'u':
88 uflag++;
89 break;
90 case 'v':
91 vflag++;
92 break;
93 default:
94 usage();
95 }
96 }
97 argc -= optind;
98 argv += optind;
99 if (argc > 0) {
100 env = argv[0];
101 eq = strchr(env, '=');
102 if (eq != NULL) {
103 *eq++ = '\0';
104 val = eq;
105 }
106 argv++;
107 argc--;
108 }
109 if ((hflag || Nflag) && env != NULL)
110 usage();
111 if (lflag && sflag)
112 usage();
113 if (argc > 0 || ((uflag || vflag) && env == NULL))
114 usage();
115 if (env == NULL) {
116 if (lflag)
117 error = kdumpenv(KENV_DUMP_LOADER);
118 else if (sflag)
119 error = kdumpenv(KENV_DUMP_STATIC);
120 else
121 error = kdumpenv(KENV_DUMP);
122 if (error && !qflag) {
123 if (errno == ENOENT)
124 warnx("requested environment is unavailable");
125 else
126 warn("kdumpenv");
127 }
128 } else if (val == NULL) {
129 if (uflag) {
130 error = kunsetenv(env);
131 if (error && !qflag)
132 warnx("unable to unset %s", env);
133 } else {
134 error = kgetenv(env);
135 if (error && !qflag)
136 warnx("unable to get %s", env);
137 }
138 } else {
139 error = ksetenv(env, val);
140 if (error && !qflag)
141 warnx("unable to set %s to %s", env, val);
142 }
143 return (error);
144 }
145
146 static int
kdumpenv(int dump_type)147 kdumpenv(int dump_type)
148 {
149 char *buf, *bp, *cp;
150 int buflen, envlen;
151
152 envlen = kenv(dump_type, NULL, NULL, 0);
153 if (envlen < 0)
154 return (-1);
155 for (;;) {
156 buflen = envlen * 120 / 100;
157 buf = calloc(1, buflen + 1);
158 if (buf == NULL)
159 return (-1);
160 envlen = kenv(dump_type, NULL, buf, buflen);
161 if (envlen < 0) {
162 free(buf);
163 return (-1);
164 }
165 if (envlen > buflen)
166 free(buf);
167 else
168 break;
169 }
170
171 for (bp = buf; *bp != '\0'; bp += strlen(bp) + 1) {
172 if (hflag) {
173 if (strncmp(bp, "hint.", 5) != 0)
174 continue;
175 }
176 cp = strchr(bp, '=');
177 if (cp == NULL)
178 continue;
179 *cp++ = '\0';
180 if (Nflag)
181 printf("%s\n", bp);
182 else
183 printf("%s=\"%s\"\n", bp, cp);
184 bp = cp;
185 }
186
187 free(buf);
188 return (0);
189 }
190
191 static int
kgetenv(const char * env)192 kgetenv(const char *env)
193 {
194 char buf[1024];
195 int ret;
196
197 ret = kenv(KENV_GET, env, buf, sizeof(buf));
198 if (ret == -1)
199 return (ret);
200 if (vflag)
201 printf("%s=\"%s\"\n", env, buf);
202 else
203 printf("%s\n", buf);
204 return (0);
205 }
206
207 static int
ksetenv(const char * env,char * val)208 ksetenv(const char *env, char *val)
209 {
210 int ret;
211
212 ret = kenv(KENV_SET, env, val, strlen(val) + 1);
213 if (ret == 0)
214 printf("%s=\"%s\"\n", env, val);
215 return (ret);
216 }
217
218 static int
kunsetenv(const char * env)219 kunsetenv(const char *env)
220 {
221 int ret;
222
223 ret = kenv(KENV_UNSET, env, NULL, 0);
224 return (ret);
225 }
226