retention.c revision 171195
1171195Sscf/*- 2171195Sscf * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org> 3171195Sscf * All rights reserved. 4171195Sscf * 5171195Sscf * Redistribution and use in source and binary forms, with or without 6171195Sscf * modification, are permitted provided that the following conditions 7171195Sscf * are met: 8171195Sscf * 1. Redistributions of source code must retain the above copyright 9171195Sscf * notice, this list of conditions and the following disclaimer, 10171195Sscf * without modification, immediately at the beginning of the file. 11171195Sscf * 2. Redistributions in binary form must reproduce the above copyright 12171195Sscf * notice, this list of conditions and the following disclaimer in the 13171195Sscf * documentation and/or other materials provided with the distribution. 14171195Sscf * 15171195Sscf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16171195Sscf * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17171195Sscf * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18171195Sscf * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19171195Sscf * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20171195Sscf * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21171195Sscf * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22171195Sscf * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23171195Sscf * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24171195Sscf * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25171195Sscf */ 26171195Sscf#include <stdio.h> 27171195Sscf#include <stdlib.h> 28171195Sscf#include <string.h> 29171195Sscf 30171195Sscf 31171195Sscf#include <sys/cdefs.h> 32171195Sscf__FBSDID("$FreeBSD: head/tools/regression/environ/retention.c 171195 2007-07-04 00:00:41Z scf $"); 33171195Sscf 34171195Sscf 35171195Sscfextern char **environ; 36171195Sscfconst char *envName = "FOOBAR"; 37171195Sscfconst char *envValSmall = "Hi"; 38171195Sscfconst char *envValLarge = "Hi, again"; 39171195Sscfconst char *envValAny = "Any value"; 40171195Sscf 41171195Sscf 42171195Sscfint 43171195Sscfmain(int argc, char **argv) 44171195Sscf{ 45171195Sscf const char *env1 = NULL; 46171195Sscf const char *env2 = NULL; 47171195Sscf const char *env3 = NULL; 48171195Sscf const char *env4 = NULL; 49171195Sscf const char *env5 = NULL; 50171195Sscf int testNdx; 51171195Sscf 52171195Sscf /* Clean slate. */ 53171195Sscf environ = NULL; 54171195Sscf testNdx = 0; 55171195Sscf 56171195Sscf /* Initial value of variable. */ 57171195Sscf if (getenv(envName) != NULL) 58171195Sscf printf("not "); 59171195Sscf printf("ok %d - getenv(\"%s\")\n", ++testNdx, envName); 60171195Sscf 61171195Sscf /* Set value of variable to smaller value and get value. */ 62171195Sscf if ((setenv(envName, envValSmall, 1) != 0) || 63171195Sscf ((env1 = getenv(envName)) == NULL) || 64171195Sscf (strcmp(env1, envValSmall) != 0)) 65171195Sscf printf("not "); 66171195Sscf printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName, 67171195Sscf envValSmall); 68171195Sscf 69171195Sscf /* Unset variable. */ 70171195Sscf if ((unsetenv(envName) == -1) || ((env2 = getenv(envName)) != NULL)) 71171195Sscf printf("not "); 72171195Sscf printf("ok %d - unsetenv(\"%s\")\n", ++testNdx, envName); 73171195Sscf 74171195Sscf /* Set variable to bigger value and get value. */ 75171195Sscf if ((setenv(envName, envValLarge, 1) != 0) || 76171195Sscf ((env3 = getenv(envName)) == NULL) || 77171195Sscf (strcmp(env3, envValLarge) != 0)) 78171195Sscf printf("not "); 79171195Sscf printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName, 80171195Sscf envValLarge); 81171195Sscf 82171195Sscf /* Set variable to smaller value and get value. */ 83171195Sscf if ((setenv(envName, envValSmall, 1) != 0) || 84171195Sscf ((env4 = getenv(envName)) == NULL) || 85171195Sscf (strcmp(env4, envValSmall) != 0)) 86171195Sscf printf("not "); 87171195Sscf printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName, 88171195Sscf envValSmall); 89171195Sscf 90171195Sscf /* Set variable to any value without overwrite and get value. */ 91171195Sscf if ((setenv(envName, envValAny, 0) != 0) || 92171195Sscf ((env5 = getenv(envName)) == NULL) || 93171195Sscf (strcmp(env5, envValAny) == 0)) 94171195Sscf printf("not "); 95171195Sscf printf("ok %d - setenv(\"%s\", \"%s\", 0)\n", ++testNdx, envName, 96171195Sscf envValAny); 97171195Sscf 98171195Sscf /* 99171195Sscf * Verify FreeBSD-ism about allowing a program to keep old pointers without 100171195Sscf * risk of segfaulting. 101171195Sscf */ 102171195Sscf if ((strcmp(env1, envValSmall) != 0) || 103171195Sscf (strcmp(env3, envValSmall) != 0) || 104171195Sscf (strcmp(env4, envValSmall) != 0)) 105171195Sscf printf("not "); 106171195Sscf printf("ok %d - old variables point to valid memory\n", ++testNdx); 107171195Sscf 108171195Sscf exit(EXIT_SUCCESS); 109171195Sscf} 110