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.2 2003/06/17 04:29:27 dillon 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)
50 int real_oid[CTL_MAXNAME+4];
52 int ch, error, hflag, i, slen;
53 char *env, *eq, *name, *var, *val;
57 while ((ch = getopt(argc, argv, "h")) != -1) {
75 name2oid_oid[0] = 0; /* This is magic & undocumented! */
77 oidlen = sizeof(real_oid);
78 name = "kern.environment";
79 error = sysctl(name2oid_oid, 2, real_oid, &oidlen, name, strlen(name));
81 err(1, "cannot find kern.environment base sysctl OID");
82 oidlen /= sizeof (int);
83 if (oidlen >= CTL_MAXNAME)
84 errx(1, "kern.environment OID is too large!");
87 real_oid[oidlen + 1] = i;
88 slen = sizeof(sbuf) - 1;
89 error = sysctl(real_oid, oidlen + 2, sbuf, &slen, NULL, 0);
92 err(1, "sysctl kern.environment.%d\n", i);
95 sbuf[sizeof(sbuf) - 1] = '\0';
96 eq = strchr(sbuf, '=');
98 err(1, "malformed environment string: %s\n", sbuf);
103 if (strcmp(var, env) != 0)
109 if (strncmp(var, "hint.", 5) != 0)
113 printf("%s=\"", var);