kenv.c revision 94937
1/*
2 * Copyright (c) 2000  Peter Wemm <peter@freebsd.org>
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 * $FreeBSD: head/bin/kenv/kenv.c 94937 2002-04-17 13:08:14Z mux $
26 */
27#include <sys/types.h>
28#include <sys/sysctl.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <errno.h>
33#include <err.h>
34#include <kenv.h>
35#include <unistd.h>
36
37static void	usage(void);
38static int	kdumpenv(void);
39static int	kgetenv(char *);
40static int	ksetenv(char *, char *);
41static int	kunsetenv(char *);
42
43static int hflag = 0;
44static int uflag = 0;
45
46static void
47usage(void)
48{
49	(void)fprintf(stderr, "%s\n%s\n%s\n",
50	    "usage: kenv [-h]",
51	    "       kenv variable[=value]",
52	    "       kenv -u variable");
53	exit(1);
54}
55
56int
57main(int argc, char **argv)
58{
59	char *env, *eq, *val;
60	int ch, error;
61
62	error = 0;
63	val = NULL;
64	env = NULL;
65	while ((ch = getopt(argc, argv, "hu")) != -1) {
66		switch (ch) {
67		case 'h':
68			hflag++;
69			break;
70		case 'u':
71			uflag++;
72			break;
73		default:
74			usage();
75		}
76	}
77	argc -= optind;
78	argv += optind;
79	if (argc > 0) {
80		env = argv[0];
81		eq = strchr(env, '=');
82		if (eq != NULL) {
83			*eq++ = '\0';
84			val = eq;
85		}
86		argv++;
87		argc--;
88	}
89	if (hflag && (env != NULL))
90		usage();
91	if ((argc > 0) || (uflag && (env == NULL)))
92		usage();
93	if (env == NULL)
94		kdumpenv();
95	else if (val == NULL) {
96		if (uflag) {
97			error = kunsetenv(env);
98			if (error)
99				warnx("unable to unset %s", env);
100		} else {
101			error = kgetenv(env);
102			if (error)
103				warnx("unable to get %s", env);
104		}
105	} else {
106		error = ksetenv(env, val);
107		if (error)
108			warnx("unable to set %s to %s", env, val);
109	}
110	return (error);
111}
112
113static int
114kdumpenv()
115{
116	char *buf, *cp;
117	int len;
118
119	len = kenv(KENV_DUMP, NULL, NULL, 0);
120	len = len * 120 / 100;
121	buf = malloc(len);
122	if (buf == NULL)
123		return (-1);
124	/* Be defensive */
125	memset(buf, 0, len);
126	kenv(KENV_DUMP, NULL, buf, len);
127	for (; *buf != '\0'; buf += strlen(buf) + 1) {
128		if (hflag) {
129			if (strncmp(buf, "hint.", 5) != 0)
130				continue;
131		}
132		cp = strchr(buf, '=');
133		if (cp == NULL)
134			continue;
135		*cp++ = '\0';
136		printf("%s=\"%s\"\n", buf, cp);
137		buf = cp;
138	}
139	return (0);
140}
141
142static int
143kgetenv(char *env)
144{
145	char buf[1024];
146	int ret;
147
148	ret = kenv(KENV_GET, env, buf, sizeof(buf));
149	if (ret == -1)
150		return (ret);
151	printf("%s\n", buf);
152	return (0);
153}
154
155static int
156ksetenv(char *env, char *val)
157{
158	int ret;
159
160	ret = kenv(KENV_SET, env, val, strlen(val)+1);
161	if (ret == 0)
162		printf("%s=\"%s\"\n", env, val);
163	return (ret);
164}
165
166static int
167kunsetenv(char *env)
168{
169	int ret;
170
171	ret = kenv(KENV_UNSET, env, NULL, 0);
172	return (ret);
173}
174