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