コード例 #1
0
ファイル: setenv.c プロジェクト: busterb/libc-openbsd
/*
 * setenv --
 *	Set the value of the environmental variable "name" to be
 *	"value".  If rewrite is set, replace any current value.
 */
int
setenv(const char *name, const char *value, int rewrite)
{
	char *C, **P;
	const char *np;
	int l_value, offset = 0;

	if (!name || !*name) {
		errno = EINVAL;
		return (-1);
	}
	for (np = name; *np && *np != '='; ++np)
		;
	if (*np) {
		errno = EINVAL;
		return (-1);			/* has `=' in name */
	}

	l_value = strlen(value);
	if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
		int tmpoff = offset + 1;
		if (!rewrite)
			return (0);
#if 0 /* XXX - existing entry may not be writable */
		if (strlen(C) >= l_value) {	/* old larger; copy over */
			while ((*C++ = *value++))
				;
			return (0);
		}
#endif
		/* could be set multiple times */
		while (__findenv(name, (int)(np - name), &tmpoff)) {
			for (P = &environ[tmpoff];; ++P)
				if (!(*P = *(P + 1)))
					break;
		}
	} else {					/* create new slot */
		size_t cnt;

		for (P = environ; *P != NULL; P++)
			;
		cnt = P - environ;
		P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
		if (!P)
			return (-1);
		if (lastenv != environ)
			memcpy(P, environ, cnt * sizeof(char *));
		lastenv = environ = P;
		offset = cnt;
		environ[cnt + 1] = NULL;
	}
	if (!(environ[offset] =			/* name + `=' + value */
	    malloc((size_t)((int)(np - name) + l_value + 2))))
		return (-1);
	for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
		;
	for (*C++ = '='; (*C++ = *value++); )
		;
	return (0);
}
コード例 #2
0
ファイル: getenv.c プロジェクト: knz/slcore
/*
 * Unset variable with the same name by flagging it as inactive.  No variable is
 * ever freed.
 */
int
unsetenv(const char *name)
{
    int envNdx;
    size_t nameLen;

    /* Check for malformed name. */
    if (name == NULL || (nameLen = __strleneq(name)) == 0) {
        errno = EINVAL;
        return (-1);
    }

    /* Initialize environment. */
    if (__merge_environ() == -1 || (envVars == NULL && __build_env() == -1))
        return (-1);

    /* Deactivate specified variable. */
    envNdx = envVarsTotal - 1;
    if (__findenv(name, nameLen, &envNdx, true) != NULL) {
        envVars[envNdx].active = false;
        if (envVars[envNdx].putenv)
            __remove_putenv(envNdx);
        __rebuild_environ(envActive - 1);
    }

    return (0);
}
コード例 #3
0
ファイル: getenv.c プロジェクト: knz/slcore
/*
 * Returns the value of a variable or NULL if none are found.
 */
char *
getenv(const char *name)
{
    int envNdx;
    size_t nameLen;

    /* Check for malformed name. */
    if (name == NULL || (nameLen = __strleneq(name)) == 0) {
        errno = EINVAL;
        return (NULL);
    }

    /*
     * An empty environment (environ or its first value) regardless if
     * environ has been copied before will return a NULL.
     *
     * If the environment is not empty, find an environment variable via
     * environ if environ has not been copied via an *env() call or been
     * replaced by a running program, otherwise, use the rebuilt
     * environment.
     */
    if (environ == NULL || environ[0] == NULL)
        return (NULL);
    else if (envVars == NULL || environ != intEnviron)
        return (__findenv_environ(name, nameLen));
    else {
        envNdx = envVarsTotal - 1;
        return (__findenv(name, nameLen, &envNdx, true));
    }
}
コード例 #4
0
ファイル: setenv.c プロジェクト: mikekmv/aeriebsd-src
/*
 * putenv --
 *	Add a name=value string directly to the environmental, replacing
 *	any current value.
 */
int
putenv(char *str)
{
	char **P, *cp;
	size_t cnt;
	int offset;

	for (cp = str; *cp && *cp != '='; ++cp)
		;
	if (*cp != '=') {
		errno = EINVAL;
		return (-1);			/* missing `=' in string */
	}

	if (__findenv(str, (int)(cp - str), &offset) != NULL) {
		environ[offset] = str;
		return (0);
	}

	/* create new slot for string */
	for (P = environ; *P != NULL; P++)
		;
	cnt = P - environ;
	P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
	if (!P)
		return (-1);
	if (lastenv != environ)
		memcpy(P, environ, cnt * sizeof(char *));
	lastenv = environ = P;
	environ[cnt] = str;
	environ[cnt + 1] = NULL;
	return (0);
}
コード例 #5
0
ファイル: setenv.c プロジェクト: busterb/libc-openbsd
/*
 * unsetenv(name) --
 *	Delete environmental variable "name".
 */
int
unsetenv(const char *name)
{
	char **P;
	const char *np;
	int offset = 0;

	if (!name || !*name) {
		errno = EINVAL;
		return (-1);
	}
	for (np = name; *np && *np != '='; ++np)
		;
	if (*np) {
		errno = EINVAL;
		return (-1);			/* has `=' in name */
	}

	/* could be set multiple times */
	while (__findenv(name, (int)(np - name), &offset)) {
		for (P = &environ[offset];; ++P)
			if (!(*P = *(P + 1)))
				break;
	}
	return (0);
}
コード例 #6
0
ファイル: getenv.c プロジェクト: Bluerise/bitrig-xenocara
/*
 * getenv --
 *	Returns ptr to value associated with name, if any, else NULL.
 */
char *
getenv(const char *name)
{
	int offset = 0;
	const char *np;

	for (np = name; *np && *np != '='; ++np)
		;
	return (__findenv(name, (int)(np - name), &offset));
}
コード例 #7
0
ファイル: setenv.c プロジェクト: hzsunzixiang/programming
/*
 * unsetenv(name) --
 *	Delete environmental variable "name".
 */
void
unsetenv(const char *name)
{
	char **P;
	int offset;

	while (__findenv(name, &offset))	/* if set multiple times */
		for (P = &environ[offset];; ++P)
			if (!(*P = *(P + 1)))
				break;
}
コード例 #8
0
ファイル: setenv.c プロジェクト: Nicholas-S/xaric
/*
 * unsetenv(name) --
 *      Delete environmental variable "name".
 */
void unsetenv(const char *name)
{
    extern char **environ;
    register char **p;
    int offset;

    while (__findenv(name, &offset))	/* if set multiple times */
	for (p = &environ[offset];; ++p)
	    if (!(*p = *(p + 1)))
		break;
}
コード例 #9
0
ファイル: setenv.c プロジェクト: busterb/libc-openbsd
/*
 * putenv --
 *	Add a name=value string directly to the environmental, replacing
 *	any current value.
 */
int
putenv(char *str)
{
	char **P, *cp;
	size_t cnt;
	int offset = 0;

	for (cp = str; *cp && *cp != '='; ++cp)
		;
	if (*cp != '=') {
		errno = EINVAL;
		return (-1);			/* missing `=' in string */
	}

	if (__findenv(str, (int)(cp - str), &offset) != NULL) {
		environ[offset++] = str;
		/* could be set multiple times */
		while (__findenv(str, (int)(cp - str), &offset)) {
			for (P = &environ[offset];; ++P)
				if (!(*P = *(P + 1)))
					break;
		}
		return (0);
	}

	/* create new slot for string */
	for (P = environ; *P != NULL; P++)
		;
	cnt = P - environ;
	P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
	if (!P)
		return (-1);
	if (lastenv != environ)
		memcpy(P, environ, cnt * sizeof(char *));
	lastenv = environ = P;
	environ[cnt] = str;
	environ[cnt + 1] = NULL;
	return (0);
}
コード例 #10
0
/*
 * getenv --
 *	Returns ptr to value associated with name, if any, else NULL.
 *	XXX: we cannot use getenv_r to implement this, because getenv()
 *	cannot use a shared buffer, because if it did, subsequent calls
 *	to getenv would trash previous results.
 */
char *
getenv(const char *name)
{
    int offset;
    char *result;

    _DIAGASSERT(name != NULL);

    rwlock_rdlock(&__environ_lock);
    result = __findenv(name, &offset);
    rwlock_unlock(&__environ_lock);
    return result;
}
コード例 #11
0
ファイル: getenv.c プロジェクト: knz/slcore
/*
 * Insert a "name=value" string into the environment.  Special settings must be
 * made to keep setenv() from reusing this memory block and unsetenv() from
 * allowing it to be tracked.
 */
int
putenv(char *string)
{
    char *equals;
    int envNdx;
    int newEnvActive;
    size_t nameLen;

    /* Check for malformed argument. */
    if (string == NULL || (equals = strchr(string, '=')) == NULL ||
            (nameLen = equals - string) == 0) {
        errno = EINVAL;
        return (-1);
    }

    /* Initialize environment. */
    if (__merge_environ() == -1 || (envVars == NULL && __build_env() == -1))
        return (-1);

    /* Deactivate previous environment variable. */
    envNdx = envVarsTotal - 1;
    newEnvActive = envActive;
    if (__findenv(string, nameLen, &envNdx, true) != NULL) {
        /* Reuse previous putenv slot. */
        if (envVars[envNdx].putenv) {
            envVars[envNdx].name = string;
            return (__rebuild_environ(envActive));
        } else {
            newEnvActive--;
            envVars[envNdx].active = false;
        }
    }

    /* Enlarge environment. */
    envNdx = envVarsTotal;
    if (!__enlarge_env())
        return (-1);

    /* Create environment entry. */
    envVars[envNdx].name = string;
    envVars[envNdx].nameLen = -1;
    envVars[envNdx].value = NULL;
    envVars[envNdx].valueSize = -1;
    envVars[envNdx].putenv = true;
    envVars[envNdx].active = true;
    newEnvActive++;

    return (__rebuild_environ(newEnvActive));
}
コード例 #12
0
ファイル: setenv.c プロジェクト: Nicholas-S/xaric
/*
 * setenv --
 *      Set the value of the environmental variable "name" to be
 *      "value".  If rewrite is set, replace any current value.
 */
int setenv(const char *name, const char *value, int rewrite)
{
    extern char **environ;
    static int alloced;		/* if allocated space before */
    register char *c;
    int l_value, offset;

    if (*value == '=')		/* no `=' in value */
	++value;
    l_value = strlen(value);
    if ((c = __findenv(name, &offset))) {	/* find if already exists */
	if (!rewrite)
	    return (0);
	if (strlen(c) >= l_value) {	/* old larger; copy over */
	    while ((*c++ = *value++)) ;
	    return (0);
	}
    } else {			/* create new slot */
	register int cnt;
	register char **p;

	for (p = environ, cnt = 0; *p; ++p, ++cnt) ;
	if (alloced) {		/* just increase size */
	    environ = (char **) realloc((char *) environ, (size_t) (sizeof(char *) * (cnt + 2)));
	    if (!environ)
		return (-1);
	} else {		/* get new space */
	    alloced = 1;	/* copy old entries into it */
	    p = malloc((size_t) (sizeof(char *) * (cnt + 2)));
	    if (!p)
		return (-1);
	    memcpy(p, environ, cnt * sizeof(char *));
	    environ = p;
	}
	environ[cnt + 1] = NULL;
	offset = cnt;
    }
    for (c = (char *) name; *c && *c != '='; ++c) ;	/* no `=' in name */
    if (!(environ[offset] =	/* name + `=' + value */
	  malloc((size_t) ((int) (c - name) + l_value + 2))))
	return (-1);
    for (c = environ[offset]; (*c = *name++) && *c != '='; ++c) ;
    for (*c++ = '='; (*c++ = *value++);) ;
    return (0);
}
コード例 #13
0
ファイル: setenv.c プロジェクト: JinSeok/GIT_Test
/*
 * setenv --
 *	Set the value of the environmental variable "name" to be
 *	"value".  If rewrite is set, replace any current value.
 */
int
setenv(const char *name, const char *value, int rewrite)
{
	static char **lastenv;			/* last value of environ */
	char *C;
	int l_value, offset;

	if (*value == '=')			/* no `=' in value */
		++value;
	l_value = strlen(value);
	if ((C = __findenv(name, &offset))) {	/* find if already exists */
		if (!rewrite)
			return (0);
		if ((int)strlen(C) >= l_value) {	/* old larger; copy over */
			while ((*C++ = *value++))
				;
			return (0);
		}
	} else {					/* create new slot */
		size_t cnt;
		char **P;

		for (P = environ; *P != NULL; P++)
			;
		cnt = P - environ;
        P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
		if (!P)
			return (-1);
		if (lastenv != environ)
			memcpy(P, environ, cnt * sizeof(char *));
		lastenv = environ = P;
		offset = cnt;
		environ[cnt + 1] = NULL;
	}
	for (C = (char *)name; *C && *C != '='; ++C)
		;				/* no `=' in name */
	if (!(environ[offset] =			/* name + `=' + value */
	    malloc((size_t)((int)(C - name) + l_value + 2))))
		return (-1);
	for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
		;
	for (*C++ = '='; (*C++ = *value++); )
		;
	return (0);
}
コード例 #14
0
int
getenv_r(const char *name, char *buf, size_t len)
{
    int offset;
    char *result;
    int rv = -1;

    _DIAGASSERT(name != NULL);

    rwlock_rdlock(&__environ_lock);
    result = __findenv(name, &offset);
    if (result == NULL) {
        errno = ENOENT;
        goto out;
    }
    if (strlcpy(buf, result, len) >= len) {
        errno = ERANGE;
        goto out;
    }
    rv = 0;
out:
    rwlock_unlock(&__environ_lock);
    return rv;
}
コード例 #15
0
ファイル: getenv.c プロジェクト: knz/slcore
/*
 * Using environ, build an environment for use by standard C library calls.
 */
static int
__build_env(void)
{
    char **env;
    int activeNdx;
    int envNdx;
    int savedErrno;
    size_t nameLen;

    /* Check for non-existant environment. */
    if (environ == NULL || environ[0] == NULL)
        return (0);

    /* Count environment variables. */
    for (env = environ, envVarsTotal = 0; *env != NULL; env++)
        envVarsTotal++;
    envVarsSize = envVarsTotal * 2;

    /* Create new environment. */
    envVars = calloc(1, sizeof (*envVars) * envVarsSize);
    if (envVars == NULL)
        goto Failure;

    /* Copy environ values and keep track of them. */
    for (envNdx = envVarsTotal - 1; envNdx >= 0; envNdx--) {
        envVars[envNdx].putenv = false;
        envVars[envNdx].name =
            strdup(environ[envVarsTotal - envNdx - 1]);
        if (envVars[envNdx].name == NULL)
            goto Failure;
        envVars[envNdx].value = strchr(envVars[envNdx].name, '=');
        if (envVars[envNdx].value != NULL) {
            envVars[envNdx].value++;
            envVars[envNdx].valueSize =
                strlen(envVars[envNdx].value);
        } else {
            __env_warnx(CorruptEnvValueMsg, envVars[envNdx].name,
                        strlen(envVars[envNdx].name));
            errno = EFAULT;
            goto Failure;
        }

        /*
         * Find most current version of variable to make active.  This
         * will prevent multiple active variables from being created
         * during this initialization phase.
         */
        nameLen = envVars[envNdx].value - envVars[envNdx].name - 1;
        envVars[envNdx].nameLen = nameLen;
        activeNdx = envVarsTotal - 1;
        if (__findenv(envVars[envNdx].name, nameLen, &activeNdx,
                      false) == NULL) {
            __env_warnx(CorruptEnvFindMsg, envVars[envNdx].name,
                        nameLen);
            errno = EFAULT;
            goto Failure;
        }
        envVars[activeNdx].active = true;
    }

    /* Create a new environ. */
    origEnviron = environ;
    environ = NULL;
    if (__rebuild_environ(envVarsTotal) == 0)
        return (0);

Failure:
    savedErrno = errno;
    __clean_env(true);
    errno = savedErrno;

    return (-1);
}
コード例 #16
0
ファイル: setenv.c プロジェクト: Nicholas-S/xaric
/*
 * getenv --
 *      Returns ptr to value associated with name, if any, else NULL.
 */
char *getenv(const char *name)
{
    int offset;

    return (__findenv(name, &offset));
}
コード例 #17
0
ファイル: getenv.c プロジェクト: knz/slcore
/*
 * Set the value of a variable.  Older settings are labeled as inactive.  If an
 * older setting has enough room to store the new value, it will be reused.  No
 * previous variables are ever freed here to avoid causing a segmentation fault
 * in a user's code.
 *
 * The variables nameLen and valueLen are passed into here to allow the caller
 * to calculate the length by means besides just strlen().
 */
static int
__setenv(const char *name, size_t nameLen, const char *value, int overwrite)
{
    bool reuse;
    char *env;
    int envNdx;
    int newEnvActive;
    size_t valueLen;

    /* Find existing environment variable large enough to use. */
    envNdx = envVarsTotal - 1;
    newEnvActive = envActive;
    valueLen = strlen(value);
    reuse = false;
    if (__findenv(name, nameLen, &envNdx, false) != NULL) {
        /* Deactivate entry if overwrite is allowed. */
        if (envVars[envNdx].active) {
            if (overwrite == 0)
                return (0);
            envVars[envNdx].active = false;
            newEnvActive--;
        }

        /* putenv() created variable cannot be reused. */
        if (envVars[envNdx].putenv)
            __remove_putenv(envNdx);

        /* Entry is large enough to reuse. */
        else if (envVars[envNdx].valueSize >= valueLen)
            reuse = true;
    }

    /* Create new variable if none was found of sufficient size. */
    if (! reuse) {
        /* Enlarge environment. */
        envNdx = envVarsTotal;
        if (!__enlarge_env())
            return (-1);

        /* Create environment entry. */
        envVars[envNdx].name = malloc(nameLen + sizeof ("=") +
                                      valueLen);
        if (envVars[envNdx].name == NULL) {
            envVarsTotal--;
            return (-1);
        }
        envVars[envNdx].nameLen = nameLen;
        envVars[envNdx].valueSize = valueLen;

        /* Save name of name/value pair. */
        env = stpcpy(envVars[envNdx].name, name);
        if ((envVars[envNdx].name)[nameLen] != '=')
            env = stpcpy(env, "=");
    }
    else
        env = envVars[envNdx].value;

    /* Save value of name/value pair. */
    strcpy(env, value);
    envVars[envNdx].value = env;
    envVars[envNdx].active = true;
    newEnvActive++;

    /* No need to rebuild environ if an active variable was reused. */
    if (reuse && newEnvActive == envActive)
        return (0);
    else
        return (__rebuild_environ(newEnvActive));
}