2 * Copyright (c) 2007-2008 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
;
43 * Print entire environ array.
50 for (environPtr
= environ
; *environPtr
!= NULL
; environPtr
++)
51 printf("%s\n", *environPtr
);
61 usage(const char *program
)
63 fprintf(stderr
, "Usage: %s [-DGUchrt] [-c 1|2|3|4] [-gu name] "
65 "\t[(-S|-s name) value overwrite]\n\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 " -c 1|2|3|4\t\t\tClear environ variable using method:\n"
72 "\t\t\t\t1 - set environ to NULL pointer\n"
73 "\t\t\t\t2 - set environ[0] to NULL pointer\n"
74 "\t\t\t\t3 - set environ to calloc()'d NULL-terminated array\n"
75 "\t\t\t\t4 - set environ to static NULL-terminated array\n"
76 " -g name\t\t\tgetenv(name)\n"
78 " -p name=value\t\t\tputenv(name=value)\n"
79 " -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n"
80 " -s name value overwrite\tsetenv(name, value, overwrite)\n"
81 " -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
82 " -u name\t\t\tunsetenv(name)\n",
90 * Print the return value of a call along with errno upon error else zero.
91 * Also, use the eol string based upon whether running in test mode or not.
94 print_rtrn_errno(int rtrnVal
, const char *eol
)
96 printf("%d %d%s", rtrnVal
, rtrnVal
!= 0 ? errno
: 0, eol
);
103 main(int argc
, char **argv
)
106 const char *eol
= "\n";
108 static char *emptyEnv
[] = { NULL
};
109 static char *staticEnv
[] = { "FOO=bar", NULL
};
116 /* The entire program is basically executed from this loop. */
117 while ((arg
= getopt(argc
, argv
, "DGS:Uc:g:hp:rs:tu:")) != -1) {
120 switch (atoi(optarg
)) {
130 environ
= calloc(1, sizeof(*environ
));
145 value
= getenv(arg
== 'g' ? optarg
: NULL
);
146 printf("%s%s", value
== NULL
? "*NULL*" : value
, eol
);
150 print_rtrn_errno(putenv(optarg
), eol
);
158 print_rtrn_errno(setenv(NULL
, optarg
,
159 atoi(argv
[optind
])), eol
);
164 print_rtrn_errno(setenv(optarg
, argv
[optind
],
165 atoi(argv
[optind
+ 1])), eol
);
175 print_rtrn_errno(unsetenv(arg
== 'u' ? optarg
: NULL
),
186 /* Output a closing newline in test mode. */
190 return (EXIT_SUCCESS
);