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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30
31 #include <sys/cdefs.h>
32 extern char **environ;
33 const char *envName = "FOOBAR";
34 const char *envValSmall = "Hi";
35 const char *envValLarge = "Hi, again";
36 const char *envValAny = "Any value";
37
38
39 int
main(int argc,char ** argv)40 main(int argc, char **argv)
41 {
42 const char *env1 = NULL;
43 const char *env2 = NULL;
44 const char *env3 = NULL;
45 const char *env4 = NULL;
46 const char *env5 = NULL;
47 int testNdx;
48
49 /* Clean slate. */
50 environ = NULL;
51 testNdx = 0;
52
53 /* Initial value of variable. */
54 if (getenv(envName) != NULL)
55 printf("not ");
56 printf("ok %d - getenv(\"%s\")\n", ++testNdx, envName);
57
58 /* Set value of variable to smaller value and get value. */
59 if ((setenv(envName, envValSmall, 1) != 0) ||
60 ((env1 = getenv(envName)) == NULL) ||
61 (strcmp(env1, envValSmall) != 0))
62 printf("not ");
63 printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName,
64 envValSmall);
65
66 /* Unset variable. */
67 if ((unsetenv(envName) == -1) || ((env2 = getenv(envName)) != NULL))
68 printf("not ");
69 printf("ok %d - unsetenv(\"%s\")\n", ++testNdx, envName);
70
71 /* Set variable to bigger value and get value. */
72 if ((setenv(envName, envValLarge, 1) != 0) ||
73 ((env3 = getenv(envName)) == NULL) ||
74 (strcmp(env3, envValLarge) != 0))
75 printf("not ");
76 printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName,
77 envValLarge);
78
79 /* Set variable to smaller value and get value. */
80 if ((setenv(envName, envValSmall, 1) != 0) ||
81 ((env4 = getenv(envName)) == NULL) ||
82 (strcmp(env4, envValSmall) != 0))
83 printf("not ");
84 printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName,
85 envValSmall);
86
87 /* Set variable to any value without overwrite and get value. */
88 if ((setenv(envName, envValAny, 0) != 0) ||
89 ((env5 = getenv(envName)) == NULL) ||
90 (strcmp(env5, envValAny) == 0))
91 printf("not ");
92 printf("ok %d - setenv(\"%s\", \"%s\", 0)\n", ++testNdx, envName,
93 envValAny);
94
95 /*
96 * Verify FreeBSD-ism about allowing a program to keep old pointers without
97 * risk of segfaulting.
98 */
99 if ((strcmp(env1, envValSmall) != 0) ||
100 (strcmp(env3, envValSmall) != 0) ||
101 (strcmp(env4, envValSmall) != 0))
102 printf("not ");
103 printf("ok %d - old variables point to valid memory\n", ++testNdx);
104
105 exit(EXIT_SUCCESS);
106 }
107