1*26ba8048Schristos /* $NetBSD: compat_putenv.c,v 1.3 2015/01/20 18:31:24 christos Exp $ */
20bcf0d6fSchristos
30bcf0d6fSchristos /*-
40bcf0d6fSchristos * Copyright (c) 2012 The NetBSD Foundation, Inc.
50bcf0d6fSchristos * All rights reserved.
60bcf0d6fSchristos *
70bcf0d6fSchristos * This code is derived from software contributed to The NetBSD Foundation
80bcf0d6fSchristos * by Christos Zoulas.
90bcf0d6fSchristos *
100bcf0d6fSchristos * Redistribution and use in source and binary forms, with or without
110bcf0d6fSchristos * modification, are permitted provided that the following conditions
120bcf0d6fSchristos * are met:
130bcf0d6fSchristos * 1. Redistributions of source code must retain the above copyright
140bcf0d6fSchristos * notice, this list of conditions and the following disclaimer.
150bcf0d6fSchristos * 2. Redistributions in binary form must reproduce the above copyright
160bcf0d6fSchristos * notice, this list of conditions and the following disclaimer in the
170bcf0d6fSchristos * documentation and/or other materials provided with the distribution.
180bcf0d6fSchristos *
190bcf0d6fSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200bcf0d6fSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210bcf0d6fSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220bcf0d6fSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230bcf0d6fSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240bcf0d6fSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250bcf0d6fSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260bcf0d6fSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270bcf0d6fSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280bcf0d6fSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290bcf0d6fSchristos * POSSIBILITY OF SUCH DAMAGE.
300bcf0d6fSchristos */
310bcf0d6fSchristos #include <sys/cdefs.h>
320bcf0d6fSchristos #if defined(LIBC_SCCS) && !defined(lint)
33*26ba8048Schristos __RCSID("$NetBSD: compat_putenv.c,v 1.3 2015/01/20 18:31:24 christos Exp $");
340bcf0d6fSchristos #endif /* LIBC_SCCS and not lint */
350bcf0d6fSchristos
360bcf0d6fSchristos #define __LIBC12_SOURCE__
370bcf0d6fSchristos #include "namespace.h"
380bcf0d6fSchristos
390bcf0d6fSchristos #include <assert.h>
4037a2b6f0Schristos #include <errno.h>
410bcf0d6fSchristos #include <stdlib.h>
4237a2b6f0Schristos #include <string.h>
430bcf0d6fSchristos #include <compat/include/stdlib.h>
440bcf0d6fSchristos
4537a2b6f0Schristos #include "env.h"
460bcf0d6fSchristos #include "local.h"
470bcf0d6fSchristos
480bcf0d6fSchristos #ifdef __weak_alias
__weak_alias(putenv,_putenv)490bcf0d6fSchristos __weak_alias(putenv,_putenv)
500bcf0d6fSchristos #endif
510bcf0d6fSchristos
5237a2b6f0Schristos __warn_references(putenv,
530bcf0d6fSchristos "warning: reference to compatibility putenv();"
540bcf0d6fSchristos " include <stdlib.h> for correct reference")
550bcf0d6fSchristos
560bcf0d6fSchristos /*
570bcf0d6fSchristos * putenv(name) --
5837a2b6f0Schristos * This version implicitly copies the string for compatibility.
590bcf0d6fSchristos */
600bcf0d6fSchristos int
610bcf0d6fSchristos putenv(char *name)
620bcf0d6fSchristos {
6337a2b6f0Schristos size_t l_name;
640bcf0d6fSchristos char *copy;
6537a2b6f0Schristos int rv;
660bcf0d6fSchristos
670bcf0d6fSchristos _DIAGASSERT(name != NULL);
680bcf0d6fSchristos
6937a2b6f0Schristos l_name = __envvarnamelen(name, true);
7037a2b6f0Schristos if (l_name == 0) {
7137a2b6f0Schristos errno = EINVAL;
7237a2b6f0Schristos return -1;
7337a2b6f0Schristos }
7437a2b6f0Schristos
750bcf0d6fSchristos if ((copy = strdup(name)) == NULL)
760bcf0d6fSchristos return -1;
7737a2b6f0Schristos copy[l_name++] = '\0';
780bcf0d6fSchristos
7937a2b6f0Schristos rv = setenv(copy, copy + l_name, 1);
8037a2b6f0Schristos
8137a2b6f0Schristos free(copy);
8237a2b6f0Schristos return rv;
830bcf0d6fSchristos }
84