2000-07-08 08:33:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Peter Wemm <peter@freebsd.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2002-06-30 05:25:07 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-07-08 08:33:40 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <err.h>
|
2002-04-17 13:08:14 +00:00
|
|
|
#include <kenv.h>
|
2004-04-28 01:27:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-07-08 08:33:40 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2002-04-17 13:08:14 +00:00
|
|
|
static void usage(void);
|
|
|
|
static int kdumpenv(void);
|
|
|
|
static int kgetenv(char *);
|
|
|
|
static int ksetenv(char *, char *);
|
|
|
|
static int kunsetenv(char *);
|
|
|
|
|
|
|
|
static int hflag = 0;
|
|
|
|
static int uflag = 0;
|
2000-07-08 08:33:40 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2002-04-17 13:08:14 +00:00
|
|
|
(void)fprintf(stderr, "%s\n%s\n%s\n",
|
|
|
|
"usage: kenv [-h]",
|
|
|
|
" kenv variable[=value]",
|
|
|
|
" kenv -u variable");
|
2001-11-15 15:46:45 +00:00
|
|
|
exit(1);
|
2000-07-08 08:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2002-04-17 13:08:14 +00:00
|
|
|
char *env, *eq, *val;
|
|
|
|
int ch, error;
|
2000-07-08 08:33:40 +00:00
|
|
|
|
2002-04-17 13:08:14 +00:00
|
|
|
error = 0;
|
|
|
|
val = NULL;
|
2000-07-08 08:33:40 +00:00
|
|
|
env = NULL;
|
2002-04-17 13:08:14 +00:00
|
|
|
while ((ch = getopt(argc, argv, "hu")) != -1) {
|
2000-07-08 08:33:40 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
hflag++;
|
|
|
|
break;
|
2002-04-17 13:08:14 +00:00
|
|
|
case 'u':
|
|
|
|
uflag++;
|
|
|
|
break;
|
2000-07-08 08:33:40 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc > 0) {
|
|
|
|
env = argv[0];
|
2002-04-17 13:08:14 +00:00
|
|
|
eq = strchr(env, '=');
|
|
|
|
if (eq != NULL) {
|
|
|
|
*eq++ = '\0';
|
|
|
|
val = eq;
|
|
|
|
}
|
2000-07-08 08:33:40 +00:00
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
2002-04-17 13:08:14 +00:00
|
|
|
if (hflag && (env != NULL))
|
2000-07-08 08:33:40 +00:00
|
|
|
usage();
|
2002-04-17 13:08:14 +00:00
|
|
|
if ((argc > 0) || (uflag && (env == NULL)))
|
|
|
|
usage();
|
2004-04-28 01:27:36 +00:00
|
|
|
if (env == NULL) {
|
|
|
|
error = kdumpenv();
|
|
|
|
if (error)
|
|
|
|
warn("kdumpenv");
|
|
|
|
} else if (val == NULL) {
|
2002-04-17 13:08:14 +00:00
|
|
|
if (uflag) {
|
|
|
|
error = kunsetenv(env);
|
|
|
|
if (error)
|
|
|
|
warnx("unable to unset %s", env);
|
|
|
|
} else {
|
|
|
|
error = kgetenv(env);
|
|
|
|
if (error)
|
|
|
|
warnx("unable to get %s", env);
|
2000-07-08 08:33:40 +00:00
|
|
|
}
|
2002-04-17 13:08:14 +00:00
|
|
|
} else {
|
|
|
|
error = ksetenv(env, val);
|
|
|
|
if (error)
|
|
|
|
warnx("unable to set %s to %s", env, val);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kdumpenv()
|
|
|
|
{
|
|
|
|
char *buf, *cp;
|
2004-04-28 01:27:36 +00:00
|
|
|
int buflen, envlen;
|
2002-04-17 13:08:14 +00:00
|
|
|
|
2004-04-28 01:27:36 +00:00
|
|
|
envlen = kenv(KENV_DUMP, NULL, NULL, 0);
|
|
|
|
if (envlen < 0)
|
2002-04-17 13:08:14 +00:00
|
|
|
return (-1);
|
2004-04-28 01:27:36 +00:00
|
|
|
for (;;) {
|
|
|
|
buflen = envlen * 120 / 100;
|
|
|
|
buf = malloc(buflen + 1);
|
|
|
|
if (buf == NULL)
|
|
|
|
return (-1);
|
|
|
|
memset(buf, 0, buflen + 1); /* Be defensive */
|
|
|
|
envlen = kenv(KENV_DUMP, NULL, buf, buflen);
|
|
|
|
if (envlen < 0) {
|
|
|
|
free(buf);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (envlen > buflen)
|
|
|
|
free(buf);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-04-17 13:08:14 +00:00
|
|
|
for (; *buf != '\0'; buf += strlen(buf) + 1) {
|
2000-07-08 08:33:40 +00:00
|
|
|
if (hflag) {
|
2002-04-17 13:08:14 +00:00
|
|
|
if (strncmp(buf, "hint.", 5) != 0)
|
2000-07-08 08:33:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-04-17 13:08:14 +00:00
|
|
|
cp = strchr(buf, '=');
|
|
|
|
if (cp == NULL)
|
|
|
|
continue;
|
|
|
|
*cp++ = '\0';
|
|
|
|
printf("%s=\"%s\"\n", buf, cp);
|
|
|
|
buf = cp;
|
2000-07-08 08:33:40 +00:00
|
|
|
}
|
2002-04-17 13:08:14 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kgetenv(char *env)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = kenv(KENV_GET, env, buf, sizeof(buf));
|
|
|
|
if (ret == -1)
|
|
|
|
return (ret);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ksetenv(char *env, char *val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = kenv(KENV_SET, env, val, strlen(val)+1);
|
|
|
|
if (ret == 0)
|
|
|
|
printf("%s=\"%s\"\n", env, val);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kunsetenv(char *env)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = kenv(KENV_UNSET, env, NULL, 0);
|
|
|
|
return (ret);
|
2000-07-08 08:33:40 +00:00
|
|
|
}
|