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