2007-07-04 00:00:41 +00:00
|
|
|
/*-
|
2008-08-02 02:34:35 +00:00
|
|
|
* Copyright (c) 2007-2008 Sean C. Farley <scf@FreeBSD.org>
|
2007-07-04 00:00:41 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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,
|
|
|
|
* without modification, immediately at the beginning of the file.
|
|
|
|
* 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 AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
#include <errno.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
|
2008-03-01 00:02:12 +00:00
|
|
|
/*
|
|
|
|
* Print entire environ array.
|
|
|
|
*/
|
2007-07-04 00:00:41 +00:00
|
|
|
static void
|
|
|
|
dump_environ(void)
|
|
|
|
{
|
|
|
|
char **environPtr;
|
|
|
|
|
2008-03-01 00:02:12 +00:00
|
|
|
for (environPtr = environ; *environPtr != NULL; environPtr++)
|
2007-07-04 00:00:41 +00:00
|
|
|
printf("%s\n", *environPtr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-01 00:02:12 +00:00
|
|
|
/*
|
|
|
|
* Print usage.
|
|
|
|
*/
|
2007-07-04 00:00:41 +00:00
|
|
|
static void
|
|
|
|
usage(const char *program)
|
|
|
|
{
|
2009-12-01 05:04:31 +00:00
|
|
|
fprintf(stderr, "Usage: %s [-DGUchrt] [-c 1|2|3|4] [-bgu name] "
|
2008-08-02 02:34:35 +00:00
|
|
|
"[-p name=value]\n"
|
|
|
|
"\t[(-S|-s name) value overwrite]\n\n"
|
2007-07-04 00:00:41 +00:00
|
|
|
"Options:\n"
|
|
|
|
" -D\t\t\t\tDump environ\n"
|
|
|
|
" -G name\t\t\tgetenv(NULL)\n"
|
|
|
|
" -S value overwrite\t\tsetenv(NULL, value, overwrite)\n"
|
|
|
|
" -U\t\t\t\tunsetenv(NULL)\n"
|
2009-12-01 05:04:31 +00:00
|
|
|
" -b name\t\t\tblank the 'name=$name' entry, corrupting it\n"
|
2008-08-02 02:34:35 +00:00
|
|
|
" -c 1|2|3|4\t\t\tClear environ variable using method:\n"
|
|
|
|
"\t\t\t\t1 - set environ to NULL pointer\n"
|
|
|
|
"\t\t\t\t2 - set environ[0] to NULL pointer\n"
|
|
|
|
"\t\t\t\t3 - set environ to calloc()'d NULL-terminated array\n"
|
|
|
|
"\t\t\t\t4 - set environ to static NULL-terminated array\n"
|
2007-07-04 00:00:41 +00:00
|
|
|
" -g name\t\t\tgetenv(name)\n"
|
|
|
|
" -h\t\t\t\tHelp\n"
|
|
|
|
" -p name=value\t\t\tputenv(name=value)\n"
|
2007-07-20 23:30:13 +00:00
|
|
|
" -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n"
|
2007-07-04 00:00:41 +00:00
|
|
|
" -s name value overwrite\tsetenv(name, value, overwrite)\n"
|
|
|
|
" -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
|
|
|
|
" -u name\t\t\tunsetenv(name)\n",
|
|
|
|
basename(program));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-01 00:02:12 +00:00
|
|
|
/*
|
|
|
|
* Print the return value of a call along with errno upon error else zero.
|
|
|
|
* Also, use the eol string based upon whether running in test mode or not.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
print_rtrn_errno(int rtrnVal, const char *eol)
|
|
|
|
{
|
|
|
|
printf("%d %d%s", rtrnVal, rtrnVal != 0 ? errno : 0, eol);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-01 05:04:31 +00:00
|
|
|
static void
|
|
|
|
blank_env(const char *var)
|
|
|
|
{
|
|
|
|
char **newenviron;
|
|
|
|
int n, varlen;
|
|
|
|
|
|
|
|
if (environ == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (n = 0; environ[n] != NULL; n++)
|
|
|
|
;
|
|
|
|
newenviron = malloc(sizeof(char *) * (n + 1));
|
|
|
|
varlen = strlen(var);
|
|
|
|
for (; n >= 0; n--) {
|
|
|
|
newenviron[n] = environ[n];
|
|
|
|
if (newenviron[n] != NULL &&
|
|
|
|
strncmp(newenviron[n], var, varlen) == 0 &&
|
|
|
|
newenviron[n][varlen] == '=')
|
|
|
|
newenviron[n] += strlen(newenviron[n]);
|
|
|
|
}
|
|
|
|
environ = newenviron;
|
|
|
|
}
|
2008-03-01 00:02:12 +00:00
|
|
|
|
2007-07-04 00:00:41 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char arg;
|
|
|
|
const char *eol = "\n";
|
|
|
|
const char *value;
|
2008-08-02 02:34:35 +00:00
|
|
|
static char *emptyEnv[] = { NULL };
|
|
|
|
static char *staticEnv[] = { "FOO=bar", NULL };
|
2007-07-04 00:00:41 +00:00
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-03-01 00:02:12 +00:00
|
|
|
/* The entire program is basically executed from this loop. */
|
2009-12-01 05:04:31 +00:00
|
|
|
while ((arg = getopt(argc, argv, "DGS:Ub:c:g:hp:rs:tu:")) != -1) {
|
2007-07-04 00:00:41 +00:00
|
|
|
switch (arg) {
|
2009-12-01 05:04:31 +00:00
|
|
|
case 'b':
|
|
|
|
blank_env(optarg);
|
|
|
|
break;
|
|
|
|
|
2008-08-02 01:38:03 +00:00
|
|
|
case 'c':
|
2008-08-02 02:34:35 +00:00
|
|
|
switch (atoi(optarg)) {
|
|
|
|
case 1:
|
|
|
|
environ = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
environ[0] = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
environ = calloc(1, sizeof(*environ));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
environ = emptyEnv;
|
|
|
|
break;
|
|
|
|
}
|
2008-08-02 01:38:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
dump_environ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
|
|
|
case 'g':
|
|
|
|
value = getenv(arg == 'g' ? optarg : NULL);
|
|
|
|
printf("%s%s", value == NULL ? "*NULL*" : value, eol);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
print_rtrn_errno(putenv(optarg), eol);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
environ = staticEnv;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
print_rtrn_errno(setenv(NULL, optarg,
|
|
|
|
atoi(argv[optind])), eol);
|
|
|
|
optind += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
print_rtrn_errno(setenv(optarg, argv[optind],
|
|
|
|
atoi(argv[optind + 1])), eol);
|
|
|
|
optind += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
eol = " ";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
print_rtrn_errno(unsetenv(arg == 'u' ? optarg : NULL),
|
|
|
|
eol);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
2007-07-04 00:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-01 00:02:12 +00:00
|
|
|
/* Output a closing newline in test mode. */
|
2007-07-04 00:00:41 +00:00
|
|
|
if (eol[0] == ' ')
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|