Add a -v and -N option to kenv(1), so it can be more easily used in
scripts the way sysctl(8) is. The -N option, like in sysctl(8), displays only the kenv names, not their values. The -v option prints an individual kenv variable name with its value as name="value". This is the inverse of sysctl(8)'s -n flag, since the default behaviour of kenv(1) is already like sysctl(8) -n. Submitted by: Garrett Cooper < yanegomi AT gmail DOT com > MFC after: 1 week
This commit is contained in:
parent
7f4211ddb9
commit
af68c71a5e
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=235297
@ -32,9 +32,9 @@
|
|||||||
.Nd dump or modify the kernel environment
|
.Nd dump or modify the kernel environment
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl hq
|
.Op Fl hNq
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl q
|
.Op Fl qv
|
||||||
.Ar variable Ns Op = Ns Ar value
|
.Ar variable Ns Op = Ns Ar value
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl q
|
.Op Fl q
|
||||||
@ -54,6 +54,11 @@ name is specified,
|
|||||||
.Nm
|
.Nm
|
||||||
will only report that value.
|
will only report that value.
|
||||||
If the
|
If the
|
||||||
|
.Fl N
|
||||||
|
option is specified,
|
||||||
|
.Nm
|
||||||
|
will only display variable names and not their values.
|
||||||
|
If the
|
||||||
.Fl u
|
.Fl u
|
||||||
option is specified,
|
option is specified,
|
||||||
.Nm
|
.Nm
|
||||||
@ -68,6 +73,13 @@ If the
|
|||||||
option is set, warnings normally printed as a result of being unable to
|
option is set, warnings normally printed as a result of being unable to
|
||||||
perform the requested operation will be suppressed.
|
perform the requested operation will be suppressed.
|
||||||
.Pp
|
.Pp
|
||||||
|
If the
|
||||||
|
.Fl v
|
||||||
|
option is set, the variable name will be printed out for the
|
||||||
|
environment variable in addition to the value when
|
||||||
|
.Nm
|
||||||
|
is executed with a variable name.
|
||||||
|
.Pp
|
||||||
Variables can be added to the kernel environment using the
|
Variables can be added to the kernel environment using the
|
||||||
.Pa /boot/loader.conf
|
.Pa /boot/loader.conf
|
||||||
file, or also statically compiled into the kernel using the statement
|
file, or also statically compiled into the kernel using the statement
|
||||||
|
@ -42,15 +42,17 @@ static int ksetenv(char *, char *);
|
|||||||
static int kunsetenv(char *);
|
static int kunsetenv(char *);
|
||||||
|
|
||||||
static int hflag = 0;
|
static int hflag = 0;
|
||||||
|
static int Nflag = 0;
|
||||||
static int qflag = 0;
|
static int qflag = 0;
|
||||||
static int uflag = 0;
|
static int uflag = 0;
|
||||||
|
static int vflag = 0;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
(void)fprintf(stderr, "%s\n%s\n%s\n",
|
(void)fprintf(stderr, "%s\n%s\n%s\n",
|
||||||
"usage: kenv [-hq]",
|
"usage: kenv [-hNq]",
|
||||||
" kenv [-q] variable[=value]",
|
" kenv [-qv] variable[=value]",
|
||||||
" kenv [-q] -u variable");
|
" kenv [-q] -u variable");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -64,17 +66,23 @@ main(int argc, char **argv)
|
|||||||
error = 0;
|
error = 0;
|
||||||
val = NULL;
|
val = NULL;
|
||||||
env = NULL;
|
env = NULL;
|
||||||
while ((ch = getopt(argc, argv, "hqu")) != -1) {
|
while ((ch = getopt(argc, argv, "hNquv")) != -1) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'h':
|
case 'h':
|
||||||
hflag++;
|
hflag++;
|
||||||
break;
|
break;
|
||||||
|
case 'N':
|
||||||
|
Nflag++;
|
||||||
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
qflag++;
|
qflag++;
|
||||||
break;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
uflag++;
|
uflag++;
|
||||||
break;
|
break;
|
||||||
|
case 'v':
|
||||||
|
vflag++;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
@ -91,9 +99,9 @@ main(int argc, char **argv)
|
|||||||
argv++;
|
argv++;
|
||||||
argc--;
|
argc--;
|
||||||
}
|
}
|
||||||
if (hflag && (env != NULL))
|
if ((hflag || Nflag) && env != NULL)
|
||||||
usage();
|
usage();
|
||||||
if ((argc > 0) || (uflag && (env == NULL)))
|
if (argc > 0 || ((uflag || vflag) && env == NULL))
|
||||||
usage();
|
usage();
|
||||||
if (env == NULL) {
|
if (env == NULL) {
|
||||||
error = kdumpenv();
|
error = kdumpenv();
|
||||||
@ -152,7 +160,10 @@ kdumpenv(void)
|
|||||||
if (cp == NULL)
|
if (cp == NULL)
|
||||||
continue;
|
continue;
|
||||||
*cp++ = '\0';
|
*cp++ = '\0';
|
||||||
printf("%s=\"%s\"\n", buf, cp);
|
if (Nflag)
|
||||||
|
printf("%s\n", buf);
|
||||||
|
else
|
||||||
|
printf("%s=\"%s\"\n", buf, cp);
|
||||||
buf = cp;
|
buf = cp;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
@ -167,7 +178,10 @@ kgetenv(char *env)
|
|||||||
ret = kenv(KENV_GET, env, buf, sizeof(buf));
|
ret = kenv(KENV_GET, env, buf, sizeof(buf));
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
return (ret);
|
return (ret);
|
||||||
printf("%s\n", buf);
|
if (vflag)
|
||||||
|
printf("%s=\"%s\"\n", env, buf);
|
||||||
|
else
|
||||||
|
printf("%s\n", buf);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user