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