Deleted Added
full compact
envctl.c (171195) envctl.c (171525)
1/*-
2 * Copyright (c) 2007 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>
1/*-
2 * Copyright (c) 2007 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: head/tools/regression/environ/envctl.c 171195 2007-07-04 00:00:41Z scf $");
36__FBSDID("$FreeBSD: head/tools/regression/environ/envctl.c 171525 2007-07-20 23:30:13Z scf $");
37
38
39extern char **environ;
40
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
54static void
55usage(const char *program)
56{
37
38
39extern char **environ;
40
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
54static void
55usage(const char *program)
56{
57 fprintf(stderr, "Usage: %s [-DGUcht] [-gu name] [-p name=value] "
57 fprintf(stderr, "Usage: %s [-CDGUchrt] [-gu name] [-p name=value] "
58 "[(-S|-s name) value overwrite]\n\n"
59 "Options:\n"
58 "[(-S|-s name) value overwrite]\n\n"
59 "Options:\n"
60 " -C\t\t\t\tClear environ variable with NULL pointer\n"
60 " -D\t\t\t\tDump environ\n"
61 " -G name\t\t\tgetenv(NULL)\n"
62 " -S value overwrite\t\tsetenv(NULL, value, overwrite)\n"
63 " -U\t\t\t\tunsetenv(NULL)\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"
64 " -c\t\t\t\tClear environ variable\n"
65 " -c\t\t\t\tClear environ variable with calloc()'d memory\n"
65 " -g name\t\t\tgetenv(name)\n"
66 " -h\t\t\t\tHelp\n"
67 " -p name=value\t\t\tputenv(name=value)\n"
66 " -g name\t\t\tgetenv(name)\n"
67 " -h\t\t\t\tHelp\n"
68 " -p name=value\t\t\tputenv(name=value)\n"
69 " -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n"
68 " -s name value overwrite\tsetenv(name, value, overwrite)\n"
69 " -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
70 " -u name\t\t\tunsetenv(name)\n",
71 basename(program));
72
73 return;
74}
75
76
77int
78main(int argc, char **argv)
79{
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",
73 basename(program));
74
75 return;
76}
77
78
79int
80main(int argc, char **argv)
81{
80 char *cleanEnv[] = { NULL };
82 char *staticEnv[] = { "FOO=bar", NULL };
81 char arg;
82 const char *eol = "\n";
83 const char *value;
84
85 if (argc == 1) {
86 usage(argv[0]);
87 exit(EXIT_FAILURE);
88 }
89
83 char arg;
84 const char *eol = "\n";
85 const char *value;
86
87 if (argc == 1) {
88 usage(argv[0]);
89 exit(EXIT_FAILURE);
90 }
91
90 while ((arg = getopt(argc, argv, "DGS:Ucg:hp:s:tu:")) != -1) {
92 while ((arg = getopt(argc, argv, "CDGS:Ucg:hp:rs:tu:")) != -1) {
91 switch (arg) {
93 switch (arg) {
92 case 'D':
93 errno = 0;
94 dump_environ();
94 case 'C':
95 environ = NULL;
95 break;
96
97 case 'c':
96 break;
97
98 case 'c':
98 environ = cleanEnv;
99 environ = calloc(1, sizeof(*environ));
99 break;
100
100 break;
101
102 case 'D':
103 errno = 0;
104 dump_environ();
105 break;
106
101 case 'G':
102 value = getenv(NULL);
103 printf("%s%s", value == NULL ? "" : value, eol);
104 break;
105
106 case 'g':
107 value = getenv(optarg);
108 printf("%s%s", value == NULL ? "" : value, eol);
109 break;
110
111 case 'p':
112 errno = 0;
113 printf("%d %d%s", putenv(optarg), errno, eol);
114 break;
115
107 case 'G':
108 value = getenv(NULL);
109 printf("%s%s", value == NULL ? "" : value, eol);
110 break;
111
112 case 'g':
113 value = getenv(optarg);
114 printf("%s%s", value == NULL ? "" : value, eol);
115 break;
116
117 case 'p':
118 errno = 0;
119 printf("%d %d%s", putenv(optarg), errno, eol);
120 break;
121
122 case 'r':
123 environ = staticEnv;
124 break;
125
116 case 'S':
117 errno = 0;
118 printf("%d %d%s", setenv(NULL, optarg,
119 atoi(argv[optind])), errno, eol);
120 optind += 1;
121 break;
122
123 case 's':
124 errno = 0;
125 printf("%d %d%s", setenv(optarg, argv[optind],
126 atoi(argv[optind + 1])), errno, eol);
127 optind += 2;
128 break;
129
130 case 't':
131 eol = " ";
132 break;
133
134 case 'U':
135 printf("%d %d%s", unsetenv(NULL), errno, eol);
136 break;
137
138 case 'u':
139 printf("%d %d%s", unsetenv(optarg), errno, eol);
140 break;
141
142 case 'h':
143 default:
144 usage(argv[0]);
145 exit(EXIT_FAILURE);
146 }
147 }
148
149 // Output a closing newline in test mode.
150 if (eol[0] == ' ')
151 printf("\n");
152
153 return (EXIT_SUCCESS);
154}
126 case 'S':
127 errno = 0;
128 printf("%d %d%s", setenv(NULL, optarg,
129 atoi(argv[optind])), errno, eol);
130 optind += 1;
131 break;
132
133 case 's':
134 errno = 0;
135 printf("%d %d%s", setenv(optarg, argv[optind],
136 atoi(argv[optind + 1])), errno, eol);
137 optind += 2;
138 break;
139
140 case 't':
141 eol = " ";
142 break;
143
144 case 'U':
145 printf("%d %d%s", unsetenv(NULL), errno, eol);
146 break;
147
148 case 'u':
149 printf("%d %d%s", unsetenv(optarg), errno, eol);
150 break;
151
152 case 'h':
153 default:
154 usage(argv[0]);
155 exit(EXIT_FAILURE);
156 }
157 }
158
159 // Output a closing newline in test mode.
160 if (eol[0] == ' ')
161 printf("\n");
162
163 return (EXIT_SUCCESS);
164}