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