2 * Copyright (c) 2000 Peter Wemm <peter@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
25 * $FreeBSD: src/usr.bin/kenv/kenv.c,v 1.1.2.2 2001/12/19 04:52:15 dd Exp $
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
36 static char sbuf[1024];
41 (void)fprintf(stderr, "usage: kenv [-h] [variable]\n");
46 main(int argc, char **argv)
49 int real_oid[CTL_MAXNAME+4];
51 int ch, error, hflag, i, slen;
52 char *env, *eq, *name, *var, *val;
56 while ((ch = getopt(argc, argv, "h")) != -1) {
74 name2oid_oid[0] = 0; /* This is magic & undocumented! */
76 oidlen = sizeof(real_oid);
77 name = "kern.environment";
78 error = sysctl(name2oid_oid, 2, real_oid, &oidlen, name, strlen(name));
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!");
86 real_oid[oidlen + 1] = i;
87 slen = sizeof(sbuf) - 1;
88 error = sysctl(real_oid, oidlen + 2, sbuf, &slen, NULL, 0);
91 err(1, "sysctl kern.environment.%d\n", i);
94 sbuf[sizeof(sbuf) - 1] = '\0';
95 eq = strchr(sbuf, '=');
97 err(1, "malformed environment string: %s\n", sbuf);
102 if (strcmp(var, env) != 0)
108 if (strncmp(var, "hint.", 5) != 0)
112 printf("%s=\"", var);