123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #define _GNU_SOURCE
- #include <features.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #ifdef __UCLIBC_HAS_THREADS__
- #include <pthread.h>
- static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
- # define LOCK __pthread_mutex_lock(&mylock)
- # define UNLOCK __pthread_mutex_unlock(&mylock);
- #else
- # define LOCK
- # define UNLOCK
- #endif
- static char **last_environ;
- int __add_to_environ (const char *name, const char *value,
- const char *combined, int replace)
- {
- register char **ep;
- register size_t size;
- const size_t namelen = strlen (name);
- const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
- LOCK;
-
- ep = __environ;
- size = 0;
- if (ep != NULL) {
- for (; *ep != NULL; ++ep) {
- if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
- break;
- else
- ++size;
- }
- }
- if (ep == NULL || *ep == NULL) {
- char **new_environ;
-
- new_environ = (char **) realloc (last_environ,
- (size + 2) * sizeof (char *));
- if (new_environ == NULL) {
- UNLOCK;
- return -1;
- }
-
- if (combined != NULL) {
-
- new_environ[size] = (char *) combined;
- } else {
-
- new_environ[size] = (char *) malloc (namelen + 1 + vallen);
- if (new_environ[size] == NULL) {
- __set_errno (ENOMEM);
- UNLOCK;
- return -1;
- }
- memcpy (new_environ[size], name, namelen);
- new_environ[size][namelen] = '=';
- memcpy (&new_environ[size][namelen + 1], value, vallen);
- }
- if (__environ != last_environ) {
- memcpy ((char *) new_environ, (char *) __environ,
- size * sizeof (char *));
- }
- new_environ[size + 1] = NULL;
- last_environ = __environ = new_environ;
- } else if (replace) {
- char *np;
-
- if (combined != NULL) {
- np = (char *) combined;
- } else {
- np = malloc (namelen + 1 + vallen);
- if (np == NULL) {
- UNLOCK;
- return -1;
- }
- memcpy (np, name, namelen);
- np[namelen] = '=';
- memcpy (&np[namelen + 1], value, vallen);
- }
- *ep = np;
- }
- UNLOCK;
- return 0;
- }
- int setenv (const char *name, const char *value, int replace)
- {
- return __add_to_environ (name, value, NULL, replace);
- }
- 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 clearenv (void)
- {
- LOCK;
- if (__environ == last_environ && __environ != NULL) {
-
- free (__environ);
- last_environ = NULL;
- }
-
- __environ = NULL;
- UNLOCK;
- return 0;
- }
- int putenv (char *string)
- {
- int result;
- const char *const name_end = strchr (string, '=');
- if (name_end != NULL) {
- char *name = strndup(string, name_end - string);
- result = __add_to_environ (name, NULL, string, 1);
- free(name);
- return(result);
- }
- unsetenv (string);
- return 0;
- }
|