Add environment variable function
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@294 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
parent
0466636a01
commit
475e731751
@ -188,5 +188,7 @@
|
|||||||
* Added basic OS support to manage environment variables: environment
|
* Added basic OS support to manage environment variables: environment
|
||||||
storage, cloning on task creation, sharing on pthread creation, destruction
|
storage, cloning on task creation, sharing on pthread creation, destruction
|
||||||
on thread/task exit.
|
on thread/task exit.
|
||||||
|
* Add environment variables APIs: environ, getenv, putenv, clearenv, setenv,
|
||||||
|
unsetenv
|
||||||
* Started m68322
|
* Started m68322
|
||||||
|
|
||||||
|
@ -623,6 +623,10 @@ Other memory:
|
|||||||
* Added basic OS support to manage environment variables: environment
|
* Added basic OS support to manage environment variables: environment
|
||||||
storage, cloning on task creation, sharing on pthread creation, destruction
|
storage, cloning on task creation, sharing on pthread creation, destruction
|
||||||
on thread/task exit.
|
on thread/task exit.
|
||||||
|
* Add environment variables APIs: environ, getenv, putenv, clearenv, setenv,
|
||||||
|
unsetenv
|
||||||
|
* Add environment variables APIs: environ, getenv, putenv, clearenv, setenv,
|
||||||
|
unsetenv
|
||||||
* Started m68322
|
* Started m68322
|
||||||
</pre></ul>
|
</pre></ul>
|
||||||
|
|
||||||
|
@ -154,7 +154,7 @@ struct environ_s
|
|||||||
unsigned int ev_crefs; /* Reference count used when environment
|
unsigned int ev_crefs; /* Reference count used when environment
|
||||||
* is shared by threads */
|
* is shared by threads */
|
||||||
size_t ev_alloc; /* Number of bytes allocated in environment */
|
size_t ev_alloc; /* Number of bytes allocated in environment */
|
||||||
ubyte ev_env[1]; /* Environment strings */
|
char ev_env[1]; /* Environment strings */
|
||||||
};
|
};
|
||||||
typedef struct environ_s environ_t;
|
typedef struct environ_s environ_t;
|
||||||
# define SIZEOF_ENVIRON_T(alloc) (sizeof(environ_t) + alloc - 1)
|
# define SIZEOF_ENVIRON_T(alloc) (sizeof(environ_t) + alloc - 1)
|
||||||
|
@ -56,7 +56,8 @@ SCHED_SRCS = sched_setparam.c sched_getparam.c \
|
|||||||
sched_getprioritymax.c sched_getprioritymin.c \
|
sched_getprioritymax.c sched_getprioritymin.c \
|
||||||
sched_lock.c sched_unlock.c sched_lockcount.c
|
sched_lock.c sched_unlock.c sched_lockcount.c
|
||||||
ENV_SRCS = env_getenvironptr.c env_dup.c env_share.c env_release.c \
|
ENV_SRCS = env_getenvironptr.c env_dup.c env_share.c env_release.c \
|
||||||
env_clearenv.c
|
env_findvar.c env_removevar.c \
|
||||||
|
env_clearenv.c env_getenv.c env_putenv.c env_setenv.c env_unsetenv.c
|
||||||
WDOG_SRCS = wd_initialize.c wd_create.c wd_start.c wd_cancel.c wd_delete.c \
|
WDOG_SRCS = wd_initialize.c wd_create.c wd_start.c wd_cancel.c wd_delete.c \
|
||||||
wd_gettime.c
|
wd_gettime.c
|
||||||
TIME_SRCS = sched_processtimer.c
|
TIME_SRCS = sched_processtimer.c
|
||||||
|
@ -60,7 +60,7 @@
|
|||||||
* Description:
|
* Description:
|
||||||
* The clearenv() function clears the environment of all name-value pairs
|
* The clearenv() function clears the environment of all name-value pairs
|
||||||
* and sets the value of the external variable environ to NULL.
|
* and sets the value of the external variable environ to NULL.
|
||||||
|
*
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* None
|
* None
|
||||||
*
|
*
|
||||||
|
126
sched/env_findvar.c
Normal file
126
sched/env_findvar.c
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* env_findvar.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <sched.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: env_cmpname
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static boolean env_cmpname(const char *pszname, const char *peqname)
|
||||||
|
{
|
||||||
|
/* Search until we find anything different in the two names */
|
||||||
|
|
||||||
|
for (; *pszname == *peqname; pszname++, peqname++);
|
||||||
|
|
||||||
|
/* On sucess, pszname will end with '\0' and peqname with '=' */
|
||||||
|
|
||||||
|
if ( *pszname == '\0' && *peqname == '=' )
|
||||||
|
{
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: env_findvar
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Search the provided environment structure for the variable of the
|
||||||
|
* specified name.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* envp The environment structre to be searched.
|
||||||
|
* pname The variable name to find
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* A pointer to the name=value string in the environment
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* - Not called from an interrupt handler
|
||||||
|
* - Pre-emptions is disabled by caller
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR char *env_findvar(environ_t *envp, const char *pname)
|
||||||
|
{
|
||||||
|
char *ret = NULL;
|
||||||
|
|
||||||
|
/* Verify input parameters */
|
||||||
|
|
||||||
|
if (envp && pname)
|
||||||
|
{
|
||||||
|
char *ptr;
|
||||||
|
char *end = &envp->ev_env[envp->ev_alloc];
|
||||||
|
|
||||||
|
/* Search for a name=value string with matching name */
|
||||||
|
|
||||||
|
for (ptr = envp->ev_env; ptr < end && !env_cmpname( pname, ptr); ptr += (strlen(ptr) + 1));
|
||||||
|
|
||||||
|
/* Check for success */
|
||||||
|
|
||||||
|
if (ptr < end)
|
||||||
|
{
|
||||||
|
ret = ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
|
|
||||||
|
|
136
sched/env_getenv.c
Normal file
136
sched/env_getenv.c
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* env_getenv.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
|
||||||
|
#include <sched.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "os_internal.h"
|
||||||
|
#include "env_internal.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: getenv
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* The getenv() function searches the environment list for a string that
|
||||||
|
* matches the string pointed to by name.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* name - The name of the variable to find.
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* The value of the valiable (read-only) or NULL on failure
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* Not called from an interrupt handler
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR char *getenv(const char *name)
|
||||||
|
{
|
||||||
|
FAR _TCB *rtcb;
|
||||||
|
FAR environ_t *envp;
|
||||||
|
FAR char *pvar;
|
||||||
|
FAR char *pvalue = NULL;
|
||||||
|
int ret = OK;
|
||||||
|
|
||||||
|
/* Verify that a string was passed */
|
||||||
|
|
||||||
|
if (!name)
|
||||||
|
{
|
||||||
|
ret = EINVAL;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Get a reference to the thread-private environ in the TCB.*/
|
||||||
|
|
||||||
|
sched_lock();
|
||||||
|
rtcb = (FAR _TCB*)g_readytorun.head;
|
||||||
|
envp = rtcb->envp;
|
||||||
|
|
||||||
|
/* Check if the variable exists */
|
||||||
|
|
||||||
|
if ( envp && (pvar = env_findvar(envp, name)) != NULL)
|
||||||
|
{
|
||||||
|
ret = ENOENT;
|
||||||
|
goto errout_with_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* It does! Get the value sub-string from the name=value string */
|
||||||
|
|
||||||
|
pvalue = strchr(pvar, '=');
|
||||||
|
if (!pvalue)
|
||||||
|
{
|
||||||
|
/* The name=value string has no '=' This is a bug! */
|
||||||
|
|
||||||
|
ret = EINVAL;
|
||||||
|
goto errout_with_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Adjust the pointer so that it points to the value right after the '=' */
|
||||||
|
|
||||||
|
pvalue++;
|
||||||
|
sched_unlock();
|
||||||
|
return pvalue;
|
||||||
|
|
||||||
|
errout_with_lock:
|
||||||
|
sched_unlock();
|
||||||
|
errout:
|
||||||
|
*get_errno_ptr() = ret;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -74,9 +74,16 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
/* functions used by the task/pthread creation and destruction logic */
|
||||||
|
|
||||||
EXTERN int env_dup(FAR _TCB *ptcb);
|
EXTERN int env_dup(FAR _TCB *ptcb);
|
||||||
EXTERN int env_share(FAR _TCB *ptcb);
|
EXTERN int env_share(FAR _TCB *ptcb);
|
||||||
EXTERN int env_release(FAR _TCB *ptcb);
|
EXTERN int env_release(FAR _TCB *ptcb);
|
||||||
|
|
||||||
|
/* functions used internally the environment handling logic */
|
||||||
|
|
||||||
|
EXTERN FAR char *env_findvar(environ_t *envp, const char *pname);
|
||||||
|
EXTERN int env_removevar(environ_t *envp, char *pvar);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#undef EXTERN
|
#undef EXTERN
|
||||||
|
120
sched/env_putenv.c
Normal file
120
sched/env_putenv.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* env_putenv.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
|
||||||
|
#include <sched.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: putenv
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* The putenv() function adds or changes the value of environment variables.
|
||||||
|
* The argument string is of the form name=value. If name does not already
|
||||||
|
* exist in the environment, then string is added to the environment. If
|
||||||
|
* name does exist, then the value of name in the environment is changed to
|
||||||
|
* value.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* name=value string describing the environment setting to add/modify
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* Zero on sucess
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* Not called from an interrupt handler
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int putenv(char *string)
|
||||||
|
{
|
||||||
|
char *pname;
|
||||||
|
char *pequal;
|
||||||
|
int ret = OK;
|
||||||
|
|
||||||
|
/* Verify that a string was passed */
|
||||||
|
|
||||||
|
if (!string)
|
||||||
|
{
|
||||||
|
ret = EINVAL;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse the name=value string */
|
||||||
|
|
||||||
|
pname = strdup(string);
|
||||||
|
if (!pname)
|
||||||
|
{
|
||||||
|
ret = ENOMEM;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
pequal = strchr( pname, '=');
|
||||||
|
if (pequal)
|
||||||
|
{
|
||||||
|
/* Then let setenv do all of the work */
|
||||||
|
|
||||||
|
*pequal = '\0';
|
||||||
|
ret = setenv(pname, pequal+1, TRUE);
|
||||||
|
}
|
||||||
|
free(pname);
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
errout:
|
||||||
|
*get_errno_ptr() = ret;
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
|
|
||||||
|
|
118
sched/env_removevar.c
Normal file
118
sched/env_removevar.c
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* env_removevar.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <sched.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: env_removevar
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Remove the referenced name=value pair from the environment
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* envp The environment containing the name=value pair
|
||||||
|
* pvar A pointer to the name=value pair in the restroom
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* Zero on success
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* - Not called from an interrupt handler
|
||||||
|
* - Caller has pre-emptions disabled
|
||||||
|
* - Caller will reallocate the environment structure to the correct size
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int env_removevar(environ_t *envp, char *pvar)
|
||||||
|
{
|
||||||
|
int ret = ERROR;
|
||||||
|
if (envp && pvar)
|
||||||
|
{
|
||||||
|
/* Verify that the pointer lies within the environment region */
|
||||||
|
|
||||||
|
int alloc = envp->ev_alloc; /* Size of the allocated environment */
|
||||||
|
char *end = &envp->ev_env[alloc]; /* Pointer to the end+1 of the environment */
|
||||||
|
|
||||||
|
if (pvar >= envp->ev_env && pvar < end)
|
||||||
|
{
|
||||||
|
/* Set up for the removal */
|
||||||
|
|
||||||
|
int len = strlen(pvar) + 1; /* Length of name=value string to remove */
|
||||||
|
char *src = &pvar[len]; /* Address of name=value string after */
|
||||||
|
char *dest = pvar; /* Location to move the next string */
|
||||||
|
int count = end - src; /* Number of bytes to move (might be zero) */
|
||||||
|
|
||||||
|
/* Move all of the environment strings after the removed one 'down.'
|
||||||
|
* this is inefficient, but robably not high duty.
|
||||||
|
*/
|
||||||
|
|
||||||
|
while (count-- > 0)
|
||||||
|
{
|
||||||
|
*dest++ = *src++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then set to the new allocation size. The caller is expected to
|
||||||
|
* call realloc at some point but we don't do that here because the
|
||||||
|
* caller may add more stuff to the environment.
|
||||||
|
*/
|
||||||
|
|
||||||
|
envp->ev_alloc -= len;
|
||||||
|
ret = OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
|
|
||||||
|
|
206
sched/env_setenv.c
Normal file
206
sched/env_setenv.c
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* env_setenv.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "os_internal.h"
|
||||||
|
#include "env_internal.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: setenv
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* The setenv() function adds the variable name to the environment with the
|
||||||
|
* specified 'value' if the varialbe 'name" does not exist. If the 'name'
|
||||||
|
* does exist in the environment, then its value is changed to 'value' if
|
||||||
|
* 'overwrite' is non-zero; if 'overwrite' is zero, then the value of name
|
||||||
|
* unaltered.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* name - The name of the variable to change
|
||||||
|
* value - The new value of the variable
|
||||||
|
* overwrite - Replace any existing value if non-zero.
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* Zero on success
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* Not called from an interrupt handler
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int setenv(const char *name, const char *value, int overwrite)
|
||||||
|
{
|
||||||
|
FAR _TCB *rtcb;
|
||||||
|
FAR environ_t *envp;
|
||||||
|
FAR char *pvar;
|
||||||
|
int varlen;
|
||||||
|
int ret = OK;
|
||||||
|
|
||||||
|
/* Verify input parameter */
|
||||||
|
|
||||||
|
if (!name)
|
||||||
|
{
|
||||||
|
ret = EINVAL;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* if no value is provided, then this is the same as unsetenv (unless
|
||||||
|
* overwrite is false)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!value || *value == '\0')
|
||||||
|
{
|
||||||
|
/* If overwite is set then this is the same as unsetenv */
|
||||||
|
|
||||||
|
if (overwrite)
|
||||||
|
{
|
||||||
|
return unsetenv(name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Otherwise, it is a request to remove a variable without altering it? */
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get a reference to the thread-private environ in the TCB.*/
|
||||||
|
|
||||||
|
sched_lock();
|
||||||
|
rtcb = (FAR _TCB*)g_readytorun.head;
|
||||||
|
envp = rtcb->envp;
|
||||||
|
|
||||||
|
/* Check if the variable alreay exists */
|
||||||
|
|
||||||
|
if ( envp && (pvar = env_findvar(envp, name)) != NULL)
|
||||||
|
{
|
||||||
|
/* It does! Do we have permission to overwrite the existing value? */
|
||||||
|
|
||||||
|
if (!overwrite)
|
||||||
|
{
|
||||||
|
/* No.. then just return success */
|
||||||
|
|
||||||
|
sched_unlock();
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Yes.. just remove the name=value pair from the environment. It will
|
||||||
|
* be added again below. Note that we are responsible for reallocating
|
||||||
|
* the environment buffer; this will happen below.
|
||||||
|
*/
|
||||||
|
|
||||||
|
(void)env_removevar(envp, pvar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the size of the new name=value string. The +2 is for the '=' and for
|
||||||
|
* null terminator
|
||||||
|
*/
|
||||||
|
|
||||||
|
varlen = strlen(name) + strlen(value) + 2;
|
||||||
|
|
||||||
|
/* Then allocate or reallocate the environment buffer */
|
||||||
|
|
||||||
|
if (envp)
|
||||||
|
{
|
||||||
|
int alloc = envp->ev_alloc;
|
||||||
|
environ_t *tmp = (environ_t*)realloc(envp, SIZEOF_ENVIRON_T(alloc + varlen));
|
||||||
|
if (!tmp)
|
||||||
|
{
|
||||||
|
ret = ENOMEM;
|
||||||
|
goto errout_with_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
envp = tmp;
|
||||||
|
envp->ev_alloc = alloc + varlen;
|
||||||
|
pvar = &envp->ev_env[alloc];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
envp = (environ_t*)malloc(SIZEOF_ENVIRON_T(varlen));
|
||||||
|
if (!envp)
|
||||||
|
{
|
||||||
|
ret = ENOMEM;
|
||||||
|
goto errout_with_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
envp->ev_crefs = 1;
|
||||||
|
envp->ev_alloc = varlen;
|
||||||
|
pvar = envp->ev_env;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now, put the new name=value string into the environment buffer */
|
||||||
|
|
||||||
|
sprintf(pvar, "%s=%s", name, value);
|
||||||
|
|
||||||
|
/* Save the new environment pointer (it might have changed due to allocation or
|
||||||
|
* reallocation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
rtcb->envp = envp;
|
||||||
|
sched_unlock();
|
||||||
|
return OK;
|
||||||
|
|
||||||
|
errout_with_lock:
|
||||||
|
sched_unlock();
|
||||||
|
errout:
|
||||||
|
*get_errno_ptr() = ret;
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
|
|
||||||
|
|
137
sched/env_unsetenv.c
Normal file
137
sched/env_unsetenv.c
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* env_unsetenv.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name Gregory Nutt nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
|
|
||||||
|
#include <sched.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "os_internal.h"
|
||||||
|
#include "env_internal.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: unsetenv
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* The unsetenv() function deletes the variable name from the environment.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* name - The name of the variable to delete
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* Zero on success
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* Not called from an interrupt handler
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int unsetenv(const char *name)
|
||||||
|
{
|
||||||
|
FAR _TCB *rtcb;
|
||||||
|
FAR environ_t *envp;
|
||||||
|
FAR char *pvar;
|
||||||
|
int ret = OK;
|
||||||
|
|
||||||
|
/* Verify input parameter */
|
||||||
|
|
||||||
|
if (!name)
|
||||||
|
{
|
||||||
|
ret = EINVAL;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get a reference to the thread-private environ in the TCB.*/
|
||||||
|
|
||||||
|
sched_lock();
|
||||||
|
rtcb = (FAR _TCB*)g_readytorun.head;
|
||||||
|
envp = rtcb->envp;
|
||||||
|
|
||||||
|
/* Check if the variable exists */
|
||||||
|
|
||||||
|
if ( envp && (pvar = env_findvar(envp, name)) != NULL)
|
||||||
|
{
|
||||||
|
int alloc;
|
||||||
|
environ_t *tmp;
|
||||||
|
|
||||||
|
/* It does! Remove the name=value pair from the environment. */
|
||||||
|
|
||||||
|
(void)env_removevar(envp, pvar);
|
||||||
|
|
||||||
|
/* Reallocate the new environment buffer */
|
||||||
|
|
||||||
|
alloc = envp->ev_alloc;
|
||||||
|
tmp = (environ_t*)realloc(envp, SIZEOF_ENVIRON_T(alloc));
|
||||||
|
if (!tmp)
|
||||||
|
{
|
||||||
|
ret = ENOMEM;
|
||||||
|
goto errout_with_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Save the new environment pointer (it might have changed due to reallocation. */
|
||||||
|
|
||||||
|
rtcb->envp = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
sched_unlock();
|
||||||
|
return OK;
|
||||||
|
|
||||||
|
errout_with_lock:
|
||||||
|
sched_unlock();
|
||||||
|
errout:
|
||||||
|
*get_errno_ptr() = ret;
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user