xref: /minix3/tools/compat/setenv.c (revision c8a0e2f4c688362b79ef1474cea604c8ebd6045d)
1*c8a0e2f4SThomas Veerman /*	$NetBSD: setenv.c,v 1.4 2008/04/28 20:24:12 martin Exp $	*/
2*c8a0e2f4SThomas Veerman 
3*c8a0e2f4SThomas Veerman /*-
4*c8a0e2f4SThomas Veerman  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5*c8a0e2f4SThomas Veerman  * All rights reserved.
6*c8a0e2f4SThomas Veerman  *
7*c8a0e2f4SThomas Veerman  * This code is derived from software contributed to The NetBSD Foundation
8*c8a0e2f4SThomas Veerman  * by Todd Vierling.
9*c8a0e2f4SThomas Veerman  *
10*c8a0e2f4SThomas Veerman  * Redistribution and use in source and binary forms, with or without
11*c8a0e2f4SThomas Veerman  * modification, are permitted provided that the following conditions
12*c8a0e2f4SThomas Veerman  * are met:
13*c8a0e2f4SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
14*c8a0e2f4SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
15*c8a0e2f4SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
16*c8a0e2f4SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
17*c8a0e2f4SThomas Veerman  *    documentation and/or other materials provided with the distribution.
18*c8a0e2f4SThomas Veerman  *
19*c8a0e2f4SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*c8a0e2f4SThomas Veerman  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*c8a0e2f4SThomas Veerman  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*c8a0e2f4SThomas Veerman  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*c8a0e2f4SThomas Veerman  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*c8a0e2f4SThomas Veerman  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*c8a0e2f4SThomas Veerman  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*c8a0e2f4SThomas Veerman  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*c8a0e2f4SThomas Veerman  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*c8a0e2f4SThomas Veerman  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*c8a0e2f4SThomas Veerman  * POSSIBILITY OF SUCH DAMAGE.
30*c8a0e2f4SThomas Veerman  */
31*c8a0e2f4SThomas Veerman 
32*c8a0e2f4SThomas Veerman /* Emulate setenv() with getenv()/malloc()/putenv(). */
33*c8a0e2f4SThomas Veerman 
34*c8a0e2f4SThomas Veerman #include "nbtool_config.h"
35*c8a0e2f4SThomas Veerman 
36*c8a0e2f4SThomas Veerman #if !HAVE_SETENV
setenv(const char * name,const char * value,int overwrite)37*c8a0e2f4SThomas Veerman int setenv(const char *name, const char *value, int overwrite) {
38*c8a0e2f4SThomas Veerman 	char *buf;
39*c8a0e2f4SThomas Veerman 
40*c8a0e2f4SThomas Veerman 	if (!overwrite && getenv(name))
41*c8a0e2f4SThomas Veerman 		return 0;
42*c8a0e2f4SThomas Veerman 
43*c8a0e2f4SThomas Veerman 	/* Include sizes plus '=' and trailing null. */
44*c8a0e2f4SThomas Veerman 	if (!(buf = malloc(strlen(name) + strlen(value) + 2)))
45*c8a0e2f4SThomas Veerman 		return -1;
46*c8a0e2f4SThomas Veerman 
47*c8a0e2f4SThomas Veerman 	sprintf(buf, "%s=%s", name, value);
48*c8a0e2f4SThomas Veerman 	putenv(buf);
49*c8a0e2f4SThomas Veerman }
50*c8a0e2f4SThomas Veerman #endif
51