1*0a6a1f1dSLionel Sambuc /* $NetBSD: compat_putenv.c,v 1.3 2015/01/20 18:31:24 christos Exp $ */
2f14fb602SLionel Sambuc
3f14fb602SLionel Sambuc /*-
4f14fb602SLionel Sambuc * Copyright (c) 2012 The NetBSD Foundation, Inc.
5f14fb602SLionel Sambuc * All rights reserved.
6f14fb602SLionel Sambuc *
7f14fb602SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8f14fb602SLionel Sambuc * by Christos Zoulas.
9f14fb602SLionel Sambuc *
10f14fb602SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11f14fb602SLionel Sambuc * modification, are permitted provided that the following conditions
12f14fb602SLionel Sambuc * are met:
13f14fb602SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14f14fb602SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15f14fb602SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16f14fb602SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17f14fb602SLionel Sambuc * documentation and/or other materials provided with the distribution.
18f14fb602SLionel Sambuc *
19f14fb602SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f14fb602SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f14fb602SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f14fb602SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f14fb602SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f14fb602SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f14fb602SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f14fb602SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f14fb602SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f14fb602SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f14fb602SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30f14fb602SLionel Sambuc */
31f14fb602SLionel Sambuc #include <sys/cdefs.h>
32f14fb602SLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: compat_putenv.c,v 1.3 2015/01/20 18:31:24 christos Exp $");
34f14fb602SLionel Sambuc #endif /* LIBC_SCCS and not lint */
35f14fb602SLionel Sambuc
36f14fb602SLionel Sambuc #define __LIBC12_SOURCE__
37f14fb602SLionel Sambuc #include "namespace.h"
38f14fb602SLionel Sambuc
39f14fb602SLionel Sambuc #include <assert.h>
40f14fb602SLionel Sambuc #include <errno.h>
41f14fb602SLionel Sambuc #include <stdlib.h>
42f14fb602SLionel Sambuc #include <string.h>
43f14fb602SLionel Sambuc #include <compat/include/stdlib.h>
44f14fb602SLionel Sambuc
45f14fb602SLionel Sambuc #include "env.h"
46f14fb602SLionel Sambuc #include "local.h"
47f14fb602SLionel Sambuc
48f14fb602SLionel Sambuc #ifdef __weak_alias
__weak_alias(putenv,_putenv)49f14fb602SLionel Sambuc __weak_alias(putenv,_putenv)
50f14fb602SLionel Sambuc #endif
51f14fb602SLionel Sambuc
52f14fb602SLionel Sambuc __warn_references(putenv,
53f14fb602SLionel Sambuc "warning: reference to compatibility putenv();"
54f14fb602SLionel Sambuc " include <stdlib.h> for correct reference")
55f14fb602SLionel Sambuc
56f14fb602SLionel Sambuc /*
57f14fb602SLionel Sambuc * putenv(name) --
58f14fb602SLionel Sambuc * This version implicitly copies the string for compatibility.
59f14fb602SLionel Sambuc */
60f14fb602SLionel Sambuc int
61f14fb602SLionel Sambuc putenv(char *name)
62f14fb602SLionel Sambuc {
63f14fb602SLionel Sambuc size_t l_name;
64f14fb602SLionel Sambuc char *copy;
65f14fb602SLionel Sambuc int rv;
66f14fb602SLionel Sambuc
67f14fb602SLionel Sambuc _DIAGASSERT(name != NULL);
68f14fb602SLionel Sambuc
69f14fb602SLionel Sambuc l_name = __envvarnamelen(name, true);
70f14fb602SLionel Sambuc if (l_name == 0) {
71f14fb602SLionel Sambuc errno = EINVAL;
72f14fb602SLionel Sambuc return -1;
73f14fb602SLionel Sambuc }
74f14fb602SLionel Sambuc
75f14fb602SLionel Sambuc if ((copy = strdup(name)) == NULL)
76f14fb602SLionel Sambuc return -1;
77f14fb602SLionel Sambuc copy[l_name++] = '\0';
78f14fb602SLionel Sambuc
79f14fb602SLionel Sambuc rv = setenv(copy, copy + l_name, 1);
80f14fb602SLionel Sambuc
81f14fb602SLionel Sambuc free(copy);
82f14fb602SLionel Sambuc return rv;
83f14fb602SLionel Sambuc }
84