2005-01-10 08:39:26 +00:00
|
|
|
/*-
|
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);
|
2013-05-15 18:38:28 +00:00
|
|
|
static int kgetenv(const char *);
|
|
|
|
static int ksetenv(const char *, char *);
|
|
|
|
static int kunsetenv(const char *);
|
2002-04-17 13:08:14 +00:00
|
|
|
|
|
|
|
static int hflag = 0;
|
2012-05-11 23:05:14 +00:00
|
|
|
static int Nflag = 0;
|
2005-09-13 19:01:53 +00:00
|
|
|
static int qflag = 0;
|
2002-04-17 13:08:14 +00:00
|
|
|
static int uflag = 0;
|
2012-05-11 23:05:14 +00:00
|
|
|
static int vflag = 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",
|
2012-05-11 23:05:14 +00:00
|
|
|
"usage: kenv [-hNq]",
|
|
|
|
" kenv [-qv] variable[=value]",
|
2005-09-13 19:01:53 +00:00
|
|
|
" kenv [-q] -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
|
|
|
val = NULL;
|
2000-07-08 08:33:40 +00:00
|
|
|
env = NULL;
|
2012-05-11 23:05:14 +00:00
|
|
|
while ((ch = getopt(argc, argv, "hNquv")) != -1) {
|
2000-07-08 08:33:40 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
hflag++;
|
|
|
|
break;
|
2012-05-11 23:05:14 +00:00
|
|
|
case 'N':
|
|
|
|
Nflag++;
|
|
|
|
break;
|
2005-09-13 19:01:53 +00:00
|
|
|
case 'q':
|
|
|
|
qflag++;
|
|
|
|
break;
|
2002-04-17 13:08:14 +00:00
|
|
|
case 'u':
|
|
|
|
uflag++;
|
|
|
|
break;
|
2012-05-11 23:05:14 +00:00
|
|
|
case 'v':
|
|
|
|
vflag++;
|
|
|
|
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--;
|
|
|
|
}
|
2012-05-11 23:05:14 +00:00
|
|
|
if ((hflag || Nflag) && env != NULL)
|
2000-07-08 08:33:40 +00:00
|
|
|
usage();
|
2012-05-11 23:05:14 +00:00
|
|
|
if (argc > 0 || ((uflag || vflag) && env == NULL))
|
2002-04-17 13:08:14 +00:00
|
|
|
usage();
|
2004-04-28 01:27:36 +00:00
|
|
|
if (env == NULL) {
|
|
|
|
error = kdumpenv();
|
2005-09-13 19:01:53 +00:00
|
|
|
if (error && !qflag)
|
2004-04-28 01:27:36 +00:00
|
|
|
warn("kdumpenv");
|
|
|
|
} else if (val == NULL) {
|
2002-04-17 13:08:14 +00:00
|
|
|
if (uflag) {
|
|
|
|
error = kunsetenv(env);
|
2005-09-13 19:01:53 +00:00
|
|
|
if (error && !qflag)
|
2002-04-17 13:08:14 +00:00
|
|
|
warnx("unable to unset %s", env);
|
|
|
|
} else {
|
|
|
|
error = kgetenv(env);
|
2005-09-13 19:01:53 +00:00
|
|
|
if (error && !qflag)
|
2002-04-17 13:08:14 +00:00
|
|
|
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);
|
2005-09-13 19:01:53 +00:00
|
|
|
if (error && !qflag)
|
2002-04-17 13:08:14 +00:00
|
|
|
warnx("unable to set %s to %s", env, val);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-12-29 08:45:07 +00:00
|
|
|
kdumpenv(void)
|
2002-04-17 13:08:14 +00:00
|
|
|
{
|
2016-11-28 07:19:45 +00:00
|
|
|
char *buf, *bp, *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;
|
2016-11-28 07:19:45 +00:00
|
|
|
buf = calloc(1, buflen + 1);
|
2004-04-28 01:27:36 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return (-1);
|
|
|
|
envlen = kenv(KENV_DUMP, NULL, buf, buflen);
|
|
|
|
if (envlen < 0) {
|
|
|
|
free(buf);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (envlen > buflen)
|
|
|
|
free(buf);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-28 07:19:45 +00:00
|
|
|
for (bp = buf; *bp != '\0'; bp += strlen(bp) + 1) {
|
2000-07-08 08:33:40 +00:00
|
|
|
if (hflag) {
|
2016-11-28 07:19:45 +00:00
|
|
|
if (strncmp(bp, "hint.", 5) != 0)
|
2000-07-08 08:33:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-28 07:19:45 +00:00
|
|
|
cp = strchr(bp, '=');
|
2002-04-17 13:08:14 +00:00
|
|
|
if (cp == NULL)
|
|
|
|
continue;
|
|
|
|
*cp++ = '\0';
|
2012-05-11 23:05:14 +00:00
|
|
|
if (Nflag)
|
2016-11-28 07:19:45 +00:00
|
|
|
printf("%s\n", bp);
|
2012-05-11 23:05:14 +00:00
|
|
|
else
|
2016-11-28 07:19:45 +00:00
|
|
|
printf("%s=\"%s\"\n", bp, cp);
|
|
|
|
bp = cp;
|
2000-07-08 08:33:40 +00:00
|
|
|
}
|
2016-11-28 07:19:45 +00:00
|
|
|
|
|
|
|
free(buf);
|
2002-04-17 13:08:14 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-05-15 18:38:28 +00:00
|
|
|
kgetenv(const char *env)
|
2002-04-17 13:08:14 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = kenv(KENV_GET, env, buf, sizeof(buf));
|
|
|
|
if (ret == -1)
|
|
|
|
return (ret);
|
2012-05-11 23:05:14 +00:00
|
|
|
if (vflag)
|
|
|
|
printf("%s=\"%s\"\n", env, buf);
|
|
|
|
else
|
|
|
|
printf("%s\n", buf);
|
2002-04-17 13:08:14 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-05-15 18:38:28 +00:00
|
|
|
ksetenv(const char *env, char *val)
|
2002-04-17 13:08:14 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-11-28 07:21:09 +00:00
|
|
|
ret = kenv(KENV_SET, env, val, strlen(val) + 1);
|
2002-04-17 13:08:14 +00:00
|
|
|
if (ret == 0)
|
|
|
|
printf("%s=\"%s\"\n", env, val);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-05-15 18:38:28 +00:00
|
|
|
kunsetenv(const char *env)
|
2002-04-17 13:08:14 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2016-11-28 07:21:09 +00:00
|
|
|
|
2002-04-17 13:08:14 +00:00
|
|
|
ret = kenv(KENV_UNSET, env, NULL, 0);
|
|
|
|
return (ret);
|
2000-07-08 08:33:40 +00:00
|
|
|
}
|