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 $
26 * $DragonFly: src/usr.bin/kenv/kenv.c,v 1.3 2005/02/25 14:55:51 joerg Exp $
28 #include <sys/types.h>
29 #include <sys/sysctl.h>
37 static char sbuf[1024];
42 (void)fprintf(stderr, "usage: kenv [-h] [variable]\n");
47 main(int argc, char **argv)
49 int real_oid[CTL_MAXNAME];
51 int ch, error, hflag, i, slen;
52 char *env, *eq, *var, *val;
56 while ((ch = getopt(argc, argv, "h")) != -1) {
74 oidlen = __arysize(real_oid);
75 error = sysctlnametomib("kern.environment", real_oid, &oidlen);
77 err(1, "cannot find kern.environment base sysctl OID");
78 if (oidlen + 1 >= __arysize(real_oid))
79 errx(1, "kern.environment base OID too large");
82 real_oid[oidlen + 1] = i;
83 slen = sizeof(sbuf) - 1;
84 error = sysctl(real_oid, oidlen + 2, sbuf, &slen, NULL, 0);
87 err(1, "sysctl kern.environment.%d\n", i);
90 sbuf[sizeof(sbuf) - 1] = '\0';
91 eq = strchr(sbuf, '=');
93 err(1, "malformed environment string: %s\n", sbuf);
98 if (strcmp(var, env) != 0)
104 if (strncmp(var, "hint.", 5) != 0)
108 printf("%s=\"", var);