1/*-
2 * Copyright (c) 2007-2008 Sean C. Farley <scf@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#include <errno.h>
27#include <libgen.h>
28#include <stdbool.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34
35#include <sys/cdefs.h>
36extern char **environ;
37
38
39/*
40 * Print entire environ array.
41 */
42static void
43dump_environ(void)
44{
45	char **environPtr;
46
47	for (environPtr = environ; *environPtr != NULL; environPtr++)
48		printf("%s\n", *environPtr);
49
50	return;
51}
52
53
54/*
55 * Print usage.
56 */
57static void
58usage(const char *program)
59{
60	fprintf(stderr, "Usage:  %s [-DGUchrt] [-c 1|2|3|4] [-bgu name] "
61	    "[-p name=value]\n"
62	    "\t[(-S|-s name) value overwrite]\n\n"
63	    "Options:\n"
64	    "  -D\t\t\t\tDump environ\n"
65	    "  -G name\t\t\tgetenv(NULL)\n"
66	    "  -S value overwrite\t\tsetenv(NULL, value, overwrite)\n"
67	    "  -U\t\t\t\tunsetenv(NULL)\n"
68	    "  -b name\t\t\tblank the 'name=$name' entry, corrupting it\n"
69	    "  -c 1|2|3|4\t\t\tClear environ variable using method:\n"
70	    "\t\t\t\t1 - set environ to NULL pointer\n"
71	    "\t\t\t\t2 - set environ[0] to NULL pointer\n"
72	    "\t\t\t\t3 - set environ to calloc()'d NULL-terminated array\n"
73	    "\t\t\t\t4 - set environ to static NULL-terminated array\n"
74	    "  -g name\t\t\tgetenv(name)\n"
75	    "  -h\t\t\t\tHelp\n"
76	    "  -p name=value\t\t\tputenv(name=value)\n"
77	    "  -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n"
78	    "  -s name value overwrite\tsetenv(name, value, overwrite)\n"
79	    "  -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
80	    "  -u name\t\t\tunsetenv(name)\n",
81	    basename(program));
82
83	return;
84}
85
86
87/*
88 * Print the return value of a call along with errno upon error else zero.
89 * Also, use the eol string based upon whether running in test mode or not.
90 */
91static void
92print_rtrn_errno(int rtrnVal, const char *eol)
93{
94	printf("%d %d%s", rtrnVal, rtrnVal != 0 ? errno : 0, eol);
95
96	return;
97}
98
99static void
100blank_env(const char *var)
101{
102	char **newenviron;
103	int n, varlen;
104
105	if (environ == NULL)
106		return;
107
108	for (n = 0; environ[n] != NULL; n++)
109		;
110	newenviron = malloc(sizeof(char *) * (n + 1));
111	varlen = strlen(var);
112	for (; n >= 0; n--) {
113		newenviron[n] = environ[n];
114		if (newenviron[n] != NULL &&
115		    strncmp(newenviron[n], var, varlen) == 0 &&
116		    newenviron[n][varlen] == '=')
117			newenviron[n] += strlen(newenviron[n]);
118	}
119	environ = newenviron;
120}
121
122int
123main(int argc, char **argv)
124{
125	char arg;
126	const char *eol = "\n";
127	const char *value;
128	static char *emptyEnv[] = { NULL };
129	static char *staticEnv[] = { "FOO=bar", NULL };
130
131	if (argc == 1) {
132		usage(argv[0]);
133		exit(EXIT_FAILURE);
134	}
135
136	/* The entire program is basically executed from this loop. */
137	while ((arg = getopt(argc, argv, "DGS:Ub:c:g:hp:rs:tu:")) != -1) {
138		switch (arg) {
139		case 'b':
140			blank_env(optarg);
141			break;
142
143		case 'c':
144			switch (atoi(optarg)) {
145			case 1:
146				environ = NULL;
147				break;
148
149			case 2:
150				environ[0] = NULL;
151				break;
152
153			case 3:
154				environ = calloc(1, sizeof(*environ));
155				break;
156
157			case 4:
158				environ = emptyEnv;
159				break;
160			}
161			break;
162
163		case 'D':
164			dump_environ();
165			break;
166
167		case 'G':
168		case 'g':
169			value = getenv(arg == 'g' ? optarg : NULL);
170			printf("%s%s", value == NULL ? "*NULL*" : value, eol);
171			break;
172
173		case 'p':
174			print_rtrn_errno(putenv(optarg), eol);
175			break;
176
177		case 'r':
178			environ = staticEnv;
179			break;
180
181		case 'S':
182			print_rtrn_errno(setenv(NULL, optarg,
183			    atoi(argv[optind])), eol);
184			optind += 1;
185			break;
186
187		case 's':
188			print_rtrn_errno(setenv(optarg, argv[optind],
189			    atoi(argv[optind + 1])), eol);
190			optind += 2;
191			break;
192
193		case 't':
194			eol = " ";
195			break;
196
197		case 'U':
198		case 'u':
199			print_rtrn_errno(unsetenv(arg == 'u' ? optarg : NULL),
200			    eol);
201			break;
202
203		case 'h':
204		default:
205			usage(argv[0]);
206			exit(EXIT_FAILURE);
207		}
208	}
209
210	/* Output a closing newline in test mode. */
211	if (eol[0] == ' ')
212		printf("\n");
213
214	return (EXIT_SUCCESS);
215}
216