OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [stdlib/] [setenv.c] - Blame information for rev 1325

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1992,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
2
   This file is part of the GNU C Library.
3
 
4
   The GNU C Library is free software; you can redistribute it and/or
5
   modify it under the terms of the GNU Lesser General Public
6
   License as published by the Free Software Foundation; either
7
   version 2.1 of the License, or (at your option) any later version.
8
 
9
   The GNU C Library is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Lesser General Public License for more details.
13
 
14
   You should have received a copy of the GNU Lesser General Public
15
   License along with the GNU C Library; if not, write to the Free
16
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17
   02111-1307 USA.
18
 
19
   modified for uClibc by Erik Andersen <andersen@codepoet.org>
20
   */
21
 
22
#define _GNU_SOURCE
23
#include <features.h>
24
#include <errno.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <unistd.h>
28
 
29
#ifdef __UCLIBC_HAS_THREADS__
30
#include <pthread.h>
31
static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
32
# define LOCK   __pthread_mutex_lock(&mylock)
33
# define UNLOCK __pthread_mutex_unlock(&mylock);
34
#else
35
# define LOCK
36
# define UNLOCK
37
#endif
38
 
39
 
40
/* If this variable is not a null pointer we allocated the current
41
   environment.  */
42
static char **last_environ;
43
 
44
 
45
/* This function is used by `setenv' and `putenv'.  The difference between
46
   the two functions is that for the former must create a new string which
47
   is then placed in the environment, while the argument of `putenv'
48
   must be used directly.  This is all complicated by the fact that we try
49
   to reuse values once generated for a `setenv' call since we can never
50
   free the strings.  */
51
int __add_to_environ (const char *name, const char *value,
52
        const char *combined, int replace)
53
{
54
    register char **ep;
55
    register size_t size;
56
    const size_t namelen = strlen (name);
57
    const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
58
 
59
    LOCK;
60
 
61
    /* We have to get the pointer now that we have the lock and not earlier
62
       since another thread might have created a new environment.  */
63
    ep = __environ;
64
 
65
    size = 0;
66
    if (ep != NULL) {
67
        for (; *ep != NULL; ++ep) {
68
            if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
69
                break;
70
            else
71
                ++size;
72
        }
73
    }
74
 
75
    if (ep == NULL || *ep == NULL) {
76
        char **new_environ;
77
 
78
        /* We allocated this space; we can extend it.  */
79
        new_environ = (char **) realloc (last_environ,
80
                (size + 2) * sizeof (char *));
81
        if (new_environ == NULL) {
82
            UNLOCK;
83
            return -1;
84
        }
85
 
86
        /* If the whole entry is given add it.  */
87
        if (combined != NULL) {
88
            /* We must not add the string to the search tree since it belongs
89
               to the user.  */
90
            new_environ[size] = (char *) combined;
91
        } else {
92
            /* See whether the value is already known.  */
93
            new_environ[size] = (char *) malloc (namelen + 1 + vallen);
94
            if (new_environ[size] == NULL) {
95
                __set_errno (ENOMEM);
96
                UNLOCK;
97
                return -1;
98
            }
99
 
100
            memcpy (new_environ[size], name, namelen);
101
            new_environ[size][namelen] = '=';
102
            memcpy (&new_environ[size][namelen + 1], value, vallen);
103
        }
104
 
105
        if (__environ != last_environ) {
106
            memcpy ((char *) new_environ, (char *) __environ,
107
                    size * sizeof (char *));
108
        }
109
 
110
        new_environ[size + 1] = NULL;
111
        last_environ = __environ = new_environ;
112
    } else if (replace) {
113
        char *np;
114
 
115
        /* Use the user string if given.  */
116
        if (combined != NULL) {
117
            np = (char *) combined;
118
        } else {
119
            np = malloc (namelen + 1 + vallen);
120
            if (np == NULL) {
121
                UNLOCK;
122
                return -1;
123
            }
124
            memcpy (np, name, namelen);
125
            np[namelen] = '=';
126
            memcpy (&np[namelen + 1], value, vallen);
127
        }
128
        *ep = np;
129
    }
130
 
131
    UNLOCK;
132
    return 0;
133
}
134
 
135
int setenv (const char *name, const char *value, int replace)
136
{
137
    return __add_to_environ (name, value, NULL, replace);
138
}
139
 
140
int unsetenv (const char *name)
141
{
142
    size_t len;
143
    char **ep;
144
 
145
    if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) {
146
        __set_errno (EINVAL);
147
        return -1;
148
    }
149
 
150
    len = strlen (name);
151
    LOCK;
152
    ep = __environ;
153
    while (*ep != NULL) {
154
        if (!strncmp (*ep, name, len) && (*ep)[len] == '=') {
155
            /* Found it.  Remove this pointer by moving later ones back.  */
156
            char **dp = ep;
157
            do {
158
                dp[0] = dp[1];
159
            } while (*dp++);
160
            /* Continue the loop in case NAME appears again.  */
161
        } else {
162
            ++ep;
163
        }
164
    }
165
    UNLOCK;
166
    return 0;
167
}
168
 
169
/* The `clearenv' was planned to be added to POSIX.1 but probably
170
   never made it.  Nevertheless the POSIX.9 standard (POSIX bindings
171
   for Fortran 77) requires this function.  */
172
int clearenv (void)
173
{
174
    LOCK;
175
    if (__environ == last_environ && __environ != NULL) {
176
        /* We allocated this environment so we can free it.  */
177
        free (__environ);
178
        last_environ = NULL;
179
    }
180
    /* Clear the environment pointer removes the whole environment.  */
181
    __environ = NULL;
182
    UNLOCK;
183
    return 0;
184
}
185
 
186
/* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
187
int putenv (char *string)
188
{
189
    int result;
190
    const char *const name_end = strchr (string, '=');
191
 
192
    if (name_end != NULL) {
193
        char *name = strndup(string, name_end - string);
194
        result = __add_to_environ (name, NULL, string, 1);
195
        free(name);
196
        return(result);
197
    }
198
    unsetenv (string);
199
    return 0;
200
}
201
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.