123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- #include <config.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <errno.h>
- #ifndef __set_errno
- # define __set_errno(ev) ((errno) = (ev))
- #endif
- #include <string.h>
- #include <unistd.h>
- #if _LIBC
- # if HAVE_GNU_LD
- # define environ __environ
- # else
- extern char **environ;
- # endif
- #endif
- #if _LIBC
- # include <bits/libc-lock.h>
- __libc_lock_define_initialized (static, envlock)
- # define LOCK __libc_lock_lock (envlock)
- # define UNLOCK __libc_lock_unlock (envlock)
- #else
- # define LOCK
- # define UNLOCK
- #endif
- static int
- _unsetenv (const char *name)
- {
- size_t len;
- char **ep;
- if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
- {
- __set_errno (EINVAL);
- return -1;
- }
- len = strlen (name);
- LOCK;
- ep = environ;
- while (*ep != NULL)
- if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
- {
-
- char **dp = ep;
- do
- dp[0] = dp[1];
- while (*dp++);
-
- }
- else
- ++ep;
- UNLOCK;
- return 0;
- }
- int
- putenv (char *string)
- {
- const char *const name_end = strchr (string, '=');
- register size_t size;
- register char **ep;
- if (name_end == NULL)
- {
-
- return _unsetenv (string);
- }
- size = 0;
- for (ep = environ; *ep != NULL; ++ep)
- if (!strncmp (*ep, string, name_end - string) &&
- (*ep)[name_end - string] == '=')
- break;
- else
- ++size;
- if (*ep == NULL)
- {
- static char **last_environ = NULL;
- char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
- if (new_environ == NULL)
- return -1;
- (void) memcpy ((void *) new_environ, (void *) environ,
- size * sizeof (char *));
- new_environ[size] = (char *) string;
- new_environ[size + 1] = NULL;
- free (last_environ);
- last_environ = new_environ;
- environ = new_environ;
- }
- else
- *ep = string;
- return 0;
- }
|