kenv.c revision 86404
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 86404 2001-11-15 15:46:45Z dd $
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 <unistd.h>
35
36static char sbuf[1024];
37
38static void
39usage(void)
40{
41	(void)fprintf(stderr, "usage: kenv [-h] [variable]\n");
42	exit(1);
43}
44
45int
46main(int argc, char **argv)
47{
48	int name2oid_oid[2];
49	int real_oid[CTL_MAXNAME+4];
50	size_t oidlen;
51	int ch, error, hflag, i, slen;
52	char *env, *eq, *name, *var, *val;
53
54	hflag = 0;
55	env = NULL;
56	while ((ch = getopt(argc, argv, "h")) != -1) {
57		switch (ch) {
58		case 'h':
59			hflag++;
60			break;
61		default:
62			usage();
63		}
64	}
65	argc -= optind;
66	argv += optind;
67	if (argc > 0) {
68		env = argv[0];
69		argv++;
70		argc--;
71	}
72	if (argc > 0)
73		usage();
74	name2oid_oid[0] = 0;	/* This is magic & undocumented! */
75	name2oid_oid[1] = 3;
76	oidlen = sizeof(real_oid);
77	name = "kern.environment";
78	error = sysctl(name2oid_oid, 2, real_oid, &oidlen, name, strlen(name));
79	if (error < 0)
80		err(1, "cannot find kern.environment base sysctl OID");
81	oidlen /= sizeof (int);
82	if (oidlen >= CTL_MAXNAME)
83		errx(1, "kern.environment OID is too large!");
84	real_oid[oidlen] = 0;
85	for (i = 0; ; i++) {
86		real_oid[oidlen + 1] = i;
87		slen = sizeof(sbuf) - 1;
88		error = sysctl(real_oid, oidlen + 2, sbuf, &slen, NULL, 0);
89		if (error < 0) {
90			if (errno != ENOENT)
91				err(1, "sysctl kern.environment.%d\n", i);
92			break;
93		}
94		sbuf[sizeof(sbuf) - 1] = '\0';
95		eq = strchr(sbuf, '=');
96		if (eq == NULL)
97			err(1, "malformed environment string: %s\n", sbuf);
98		var = sbuf;
99		*eq = '\0';
100		val = eq + 1;
101		if (env) {
102			if (strcmp(var, env) != 0)
103				continue;
104			printf("%s\n", val);
105			break;
106		}
107		if (hflag) {
108			if (strncmp(var, "hint.", 5) != 0)
109				continue;
110			/* FALLTHROUGH */
111		}
112		printf("%s=\"", var);
113		while (*val) {
114			switch (*val) {
115			case '"':
116				putchar('\\');
117				putchar('"');
118				break;
119			case '\\':
120				putchar('\\');
121				putchar('\\');
122				break;
123			default:
124				putchar(*val);
125				break;
126			}
127			val++;
128		}
129		printf("\"\n");
130	}
131	exit(0);
132}
133