xref: /netbsd-src/lib/libc/stdlib/unsetenv.c (revision 26ba80487baf540a03edf0f75a16a427b5c4c9ab)
1*26ba8048Schristos /*	$NetBSD: unsetenv.c,v 1.11 2015/01/20 18:31:25 christos Exp $	*/
213dee93fSkleink 
313dee93fSkleink /*
45b84b398Schristos  * Copyright (c) 1987, 1993
55b84b398Schristos  *	The Regents of the University of California.  All rights reserved.
65b84b398Schristos  *
75b84b398Schristos  * Redistribution and use in source and binary forms, with or without
85b84b398Schristos  * modification, are permitted provided that the following conditions
95b84b398Schristos  * are met:
105b84b398Schristos  * 1. Redistributions of source code must retain the above copyright
115b84b398Schristos  *    notice, this list of conditions and the following disclaimer.
125b84b398Schristos  * 2. Redistributions in binary form must reproduce the above copyright
135b84b398Schristos  *    notice, this list of conditions and the following disclaimer in the
145b84b398Schristos  *    documentation and/or other materials provided with the distribution.
155b84b398Schristos  * 3. Neither the name of the University nor the names of its contributors
165b84b398Schristos  *    may be used to endorse or promote products derived from this software
175b84b398Schristos  *    without specific prior written permission.
185b84b398Schristos  *
195b84b398Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205b84b398Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215b84b398Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225b84b398Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235b84b398Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245b84b398Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255b84b398Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265b84b398Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275b84b398Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285b84b398Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295b84b398Schristos  * SUCH DAMAGE.
3013dee93fSkleink  */
3113dee93fSkleink 
325b84b398Schristos #include <sys/cdefs.h>
335b84b398Schristos #if defined(LIBC_SCCS) && !defined(lint)
345b84b398Schristos #if 0
355b84b398Schristos static char sccsid[] = "from: @(#)setenv.c	8.1 (Berkeley) 6/4/93";
365b84b398Schristos #else
37*26ba8048Schristos __RCSID("$NetBSD: unsetenv.c,v 1.11 2015/01/20 18:31:25 christos Exp $");
385b84b398Schristos #endif
395b84b398Schristos #endif /* LIBC_SCCS and not lint */
4013dee93fSkleink 
415b84b398Schristos #include "namespace.h"
425b84b398Schristos 
435b84b398Schristos #include <assert.h>
445b84b398Schristos #include <errno.h>
455b84b398Schristos #include <stdlib.h>
465b84b398Schristos #include <string.h>
477db74b9aSchristos #include <bitstring.h>
48fbf4aa16Stron 
49fbf4aa16Stron #include "env.h"
507db74b9aSchristos #include "local.h"
515b84b398Schristos 
525b84b398Schristos /*
535b84b398Schristos  * unsetenv(name) --
545b84b398Schristos  *	Delete environmental variable "name".
555b84b398Schristos  */
565b84b398Schristos int
unsetenv(const char * name)575cdca2e6Stron unsetenv(const char *name)
585b84b398Schristos {
59fbf4aa16Stron 	size_t l_name;
60fbf4aa16Stron 	ssize_t r_offset, w_offset;
615b84b398Schristos 
625b84b398Schristos 	_DIAGASSERT(name != NULL);
635b84b398Schristos 
64fbf4aa16Stron 	l_name = __envvarnamelen(name, false);
65fbf4aa16Stron 	if (l_name == 0) {
665b84b398Schristos 		errno = EINVAL;
675b84b398Schristos 		return -1;
685b84b398Schristos 	}
695b84b398Schristos 
70fbf4aa16Stron 	if (!__writelockenv())
715cdca2e6Stron 		return -1;
72da3a4052Schristos 
73fbf4aa16Stron 	/* Search for the given name in the environment. */
74fbf4aa16Stron 	r_offset = __getenvslot(name, l_name, false);
75fbf4aa16Stron 	if (r_offset == -1) {
76fbf4aa16Stron 		/* Not found. */
77fbf4aa16Stron 		(void)__unlockenv();
78fbf4aa16Stron 		return 0;
79fbf4aa16Stron 	}
80fbf4aa16Stron 	__freeenvvar(environ[r_offset]);
81fbf4aa16Stron 
82fbf4aa16Stron 	/*
83fbf4aa16Stron 	 * Remove all matches from the environment and free the associated
84fbf4aa16Stron 	 * memory if possible.
85fbf4aa16Stron 	 */
86fbf4aa16Stron 	w_offset = r_offset;
87fbf4aa16Stron 	while (environ[++r_offset] != NULL) {
88fbf4aa16Stron 		if (strncmp(environ[r_offset], name, l_name) != 0 ||
89fbf4aa16Stron 		    environ[r_offset][l_name] != '=') {
90fbf4aa16Stron 			/* Not a match, keep this entry. */
91fbf4aa16Stron 			environ[w_offset++] = environ[r_offset];
92fbf4aa16Stron 		} else {
93fbf4aa16Stron 			/* We found another match. */
94fbf4aa16Stron 			__freeenvvar(environ[r_offset]);
95fbf4aa16Stron 		}
961f5de17dStron 	}
977db74b9aSchristos 
98fbf4aa16Stron 	/* Clear out remaining stale entries in the environment vector. */
99fbf4aa16Stron 	do {
100fbf4aa16Stron 		environ[w_offset++] = NULL;
101fbf4aa16Stron 	} while (w_offset < r_offset);
1025cdca2e6Stron 
103fbf4aa16Stron 	(void)__unlockenv();
1045b84b398Schristos 	return 0;
1055b84b398Schristos }
106