2 * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 extern char **environ;
47 for (environPtr = environ; *environPtr != NULL; *environPtr++)
48 printf("%s\n", *environPtr);
55 usage(const char *program)
57 fprintf(stderr, "Usage: %s [-CDGUchrt] [-gu name] [-p name=value] "
58 "[(-S|-s name) value overwrite]\n\n"
60 " -C\t\t\t\tClear environ variable with NULL pointer\n"
61 " -D\t\t\t\tDump environ\n"
62 " -G name\t\t\tgetenv(NULL)\n"
63 " -S value overwrite\t\tsetenv(NULL, value, overwrite)\n"
64 " -U\t\t\t\tunsetenv(NULL)\n"
65 " -c\t\t\t\tClear environ variable with calloc()'d memory\n"
66 " -g name\t\t\tgetenv(name)\n"
68 " -p name=value\t\t\tputenv(name=value)\n"
69 " -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n"
70 " -s name value overwrite\tsetenv(name, value, overwrite)\n"
71 " -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
72 " -u name\t\t\tunsetenv(name)\n",
80 main(int argc, char **argv)
82 char *staticEnv[] = { "FOO=bar", NULL };
84 const char *eol = "\n";
92 while ((arg = getopt(argc, argv, "CDGS:Ucg:hp:rs:tu:")) != -1) {
99 environ = calloc(1, sizeof(*environ));
108 value = getenv(NULL);
109 printf("%s%s", value == NULL ? "" : value, eol);
113 value = getenv(optarg);
114 printf("%s%s", value == NULL ? "" : value, eol);
119 printf("%d %d%s", putenv(optarg), errno, eol);
128 printf("%d %d%s", setenv(NULL, optarg,
129 atoi(argv[optind])), errno, eol);
135 printf("%d %d%s", setenv(optarg, argv[optind],
136 atoi(argv[optind + 1])), errno, eol);
145 printf("%d %d%s", unsetenv(NULL), errno, eol);
149 printf("%d %d%s", unsetenv(optarg), errno, eol);
159 // Output a closing newline in test mode.
163 return (EXIT_SUCCESS);