]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - tools/regression/environ/envctl.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / tools / regression / environ / envctl.c
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
39 extern char **environ;
40
41
42 /*
43  * Print entire environ array.
44  */
45 static void
46 dump_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  */
60 static void
61 usage(const char *program)
62 {
63         fprintf(stderr, "Usage:  %s [-DGUchrt] [-c 1|2|3|4] [-gu 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             "  -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"
77             "  -h\t\t\t\tHelp\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",
83             basename(program));
84
85         return;
86 }
87
88
89 /*
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.
92  */
93 static void
94 print_rtrn_errno(int rtrnVal, const char *eol)
95 {
96         printf("%d %d%s", rtrnVal, rtrnVal != 0 ? errno : 0, eol);
97
98         return;
99 }
100
101
102 int
103 main(int argc, char **argv)
104 {
105         char arg;
106         const char *eol = "\n";
107         const char *value;
108         static char *emptyEnv[] = { NULL };
109         static char *staticEnv[] = { "FOO=bar", NULL };
110
111         if (argc == 1) {
112                 usage(argv[0]);
113                 exit(EXIT_FAILURE);
114         }
115
116         /* The entire program is basically executed from this loop. */
117         while ((arg = getopt(argc, argv, "DGS:Uc:g:hp:rs:tu:")) != -1) {
118                 switch (arg) {
119                 case 'c':
120                         switch (atoi(optarg)) {
121                         case 1:
122                                 environ = NULL;
123                                 break;
124
125                         case 2:
126                                 environ[0] = NULL;
127                                 break;
128
129                         case 3:
130                                 environ = calloc(1, sizeof(*environ));
131                                 break;
132
133                         case 4:
134                                 environ = emptyEnv;
135                                 break;
136                         }
137                         break;
138
139                 case 'D':
140                         dump_environ();
141                         break;
142
143                 case 'G':
144                 case 'g':
145                         value = getenv(arg == 'g' ? optarg : NULL);
146                         printf("%s%s", value == NULL ? "*NULL*" : value, eol);
147                         break;
148
149                 case 'p':
150                         print_rtrn_errno(putenv(optarg), eol);
151                         break;
152
153                 case 'r':
154                         environ = staticEnv;
155                         break;
156
157                 case 'S':
158                         print_rtrn_errno(setenv(NULL, optarg,
159                             atoi(argv[optind])), eol);
160                         optind += 1;
161                         break;
162
163                 case 's':
164                         print_rtrn_errno(setenv(optarg, argv[optind],
165                             atoi(argv[optind + 1])), eol);
166                         optind += 2;
167                         break;
168
169                 case 't':
170                         eol = " ";
171                         break;
172
173                 case 'U':
174                 case 'u':
175                         print_rtrn_errno(unsetenv(arg == 'u' ? optarg : NULL),
176                             eol);
177                         break;
178
179                 case 'h':
180                 default:
181                         usage(argv[0]);
182                         exit(EXIT_FAILURE);
183                 }
184         }
185
186         /* Output a closing newline in test mode. */
187         if (eol[0] == ' ')
188                 printf("\n");
189
190         return (EXIT_SUCCESS);
191 }