FreeBSD/sys/kern/kern_environment.c
/*
 * Set an environment variable by name.
 */
Set an environment variable by name.
Set an environment variable by name .
int
kern_setenv(const char *name, const char *value)
{
	char *buf, *cp, *oldenv;
	int namelen, vallen, i;
	if (dynamic_kenv == 0 && env_len > 0)
		return (setenv_static(name, value));
	KENV_CHECK;
	namelen = strlen(name) + 1;
	if (namelen > KENV_MNAMELEN + 1)
		return (-1);
	vallen = strlen(value) + 1;
	if (vallen > KENV_MVALLEN + 1)
		return (-1);
	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
	sprintf(buf, "%s=%s", name, value);
	mtx_lock(&kenv_lock);
	cp = _getenv_dynamic(name, &i);
	if (cp != NULL) {
		oldenv = kenvp[i];
		kenvp[i] = buf;
		mtx_unlock(&kenv_lock);
		free(oldenv, M_KENV);
	} else {
		for (i = 0; (cp = kenvp[i]) != NULL; i++)
			;
		if (i < 0 || i >= KENV_SIZE) {
			free(buf, M_KENV);
			mtx_unlock(&kenv_lock);
			return (-1);
		}
		kenvp[i] = buf;
		kenvp[i + 1] = NULL;
		mtx_unlock(&kenv_lock);
	}
	return (0);
}
int kern_setenv ( const char * name , const char * value ) { char * buf , * cp , * oldenv ; int namelen , vallen , i ; if ( dynamic_kenv == 0 && env_len > 0 ) return ( setenv_static ( name , value ) ) ; KENV_CHECK ; namelen = strlen ( name ) + 1 ; if ( namelen > KENV_MNAMELEN + 1 ) return ( -1 ) ; vallen = strlen ( value ) + 1 ; if ( vallen > KENV_MVALLEN + 1 ) return ( -1 ) ; buf = malloc ( namelen + vallen , M_KENV , M_WAITOK ) ; sprintf ( buf , " " , name , value ) ; mtx_lock ( & kenv_lock ) ; cp = _getenv_dynamic ( name , & i ) ; if ( cp != NULL ) { oldenv = kenvp [ i ] ; kenvp [ i ] = buf ; mtx_unlock ( & kenv_lock ) ; free ( oldenv , M_KENV ) ; } else { for ( i = 0 ; ( cp = kenvp [ i ] ) != NULL ; i ++ ) ; if ( i < 0 || i >= KENV_SIZE ) { free ( buf , M_KENV ) ; mtx_unlock ( & kenv_lock ) ; return ( -1 ) ; } kenvp [ i ] = buf ; kenvp [ i + 1 ] = NULL ; mtx_unlock ( & kenv_lock ) ; } return ( 0 ) ; }
Set an environment variable by name.
Set an environment variable by name .
/* We add the option if it wasn't found */ /* Bounds checking */