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>
36__FBSDID("$FreeBSD$");
37
38
39extern char **environ;
40
41
42/*
43 * Print entire environ array.
44 */
45static void
46dump_environ(void)
47{
48	char **environPtr;
49
50	for (environPtr = environ; *environPtr != NULL; environPtr++)
51		printf("%s\n", *environPtr);
52
53	return;
54}
55
56
57/*
58 * Print usage.
59 */
60static void
61usage(const char *program)
62{
63	fprintf(stderr, "Usage:  %s [-DGUchrt] [-c 1|2|3|4] [-bgu name] "
64	    "[-p name=value]\n"
65	    "\t[(-S|-s name) value overwrite]\n\n"
66	    "Options:\n"
67	    "  -D\t\t\t\tDump environ\n"
68	    "  -G name\t\t\tgetenv(NULL)\n"
69	    "  -S value overwrite\t\tsetenv(NULL, value, overwrite)\n"
70	    "  -U\t\t\t\tunsetenv(NULL)\n"
71	    "  -b name\t\t\tblank the 'name=$name' entry, corrupting it\n"
72	    "  -c 1|2|3|4\t\t\tClear environ variable using method:\n"
73	    "\t\t\t\t1 - set environ to NULL pointer\n"
74	    "\t\t\t\t2 - set environ[0] to NULL pointer\n"
75	    "\t\t\t\t3 - set environ to calloc()'d NULL-terminated array\n"
76	    "\t\t\t\t4 - set environ to static NULL-terminated array\n"
77	    "  -g name\t\t\tgetenv(name)\n"
78	    "  -h\t\t\t\tHelp\n"
79	    "  -p name=value\t\t\tputenv(name=value)\n"
80	    "  -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n"
81	    "  -s name value overwrite\tsetenv(name, value, overwrite)\n"
82	    "  -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
83	    "  -u name\t\t\tunsetenv(name)\n",
84	    basename(program));
85
86	return;
87}
88
89
90/*
91 * Print the return value of a call along with errno upon error else zero.
92 * Also, use the eol string based upon whether running in test mode or not.
93 */
94static void
95print_rtrn_errno(int rtrnVal, const char *eol)
96{
97	printf("%d %d%s", rtrnVal, rtrnVal != 0 ? errno : 0, eol);
98
99	return;
100}
101
102static void
103blank_env(const char *var)
104{
105	char **newenviron;
106	int n, varlen;
107
108	if (environ == NULL)
109		return;
110
111	for (n = 0; environ[n] != NULL; n++)
112		;
113	newenviron = malloc(sizeof(char *) * (n + 1));
114	varlen = strlen(var);
115	for (; n >= 0; n--) {
116		newenviron[n] = environ[n];
117		if (newenviron[n] != NULL &&
118		    strncmp(newenviron[n], var, varlen) == 0 &&
119		    newenviron[n][varlen] == '=')
120			newenviron[n] += strlen(newenviron[n]);
121	}
122	environ = newenviron;
123}
124
125int
126main(int argc, char **argv)
127{
128	char arg;
129	const char *eol = "\n";
130	const char *value;
131	static char *emptyEnv[] = { NULL };
132	static char *staticEnv[] = { "FOO=bar", NULL };
133
134	if (argc == 1) {
135		usage(argv[0]);
136		exit(EXIT_FAILURE);
137	}
138
139	/* The entire program is basically executed from this loop. */
140	while ((arg = getopt(argc, argv, "DGS:Ub:c:g:hp:rs:tu:")) != -1) {
141		switch (arg) {
142		case 'b':
143			blank_env(optarg);
144			break;
145
146		case 'c':
147			switch (atoi(optarg)) {
148			case 1:
149				environ = NULL;
150				break;
151
152			case 2:
153				environ[0] = NULL;
154				break;
155
156			case 3:
157				environ = calloc(1, sizeof(*environ));
158				break;
159
160			case 4:
161				environ = emptyEnv;
162				break;
163			}
164			break;
165
166		case 'D':
167			dump_environ();
168			break;
169
170		case 'G':
171		case 'g':
172			value = getenv(arg == 'g' ? optarg : NULL);
173			printf("%s%s", value == NULL ? "*NULL*" : value, eol);
174			break;
175
176		case 'p':
177			print_rtrn_errno(putenv(optarg), eol);
178			break;
179
180		case 'r':
181			environ = staticEnv;
182			break;
183
184		case 'S':
185			print_rtrn_errno(setenv(NULL, optarg,
186			    atoi(argv[optind])), eol);
187			optind += 1;
188			break;
189
190		case 's':
191			print_rtrn_errno(setenv(optarg, argv[optind],
192			    atoi(argv[optind + 1])), eol);
193			optind += 2;
194			break;
195
196		case 't':
197			eol = " ";
198			break;
199
200		case 'U':
201		case 'u':
202			print_rtrn_errno(unsetenv(arg == 'u' ? optarg : NULL),
203			    eol);
204			break;
205
206		case 'h':
207		default:
208			usage(argv[0]);
209			exit(EXIT_FAILURE);
210		}
211	}
212
213	/* Output a closing newline in test mode. */
214	if (eol[0] == ' ')
215		printf("\n");
216
217	return (EXIT_SUCCESS);
218}
219