From 475e73175171e12ae24705189877181fa34f2228 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 30 Jun 2007 19:39:17 +0000 Subject: [PATCH] Add environment variable function git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@294 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 2 + Documentation/NuttX.html | 4 + include/nuttx/sched.h | 2 +- sched/Makefile | 3 +- sched/env_clearenv.c | 2 +- sched/env_findvar.c | 126 ++++++++++++++++++++++++ sched/env_getenv.c | 136 ++++++++++++++++++++++++++ sched/env_internal.h | 7 ++ sched/env_putenv.c | 120 +++++++++++++++++++++++ sched/env_removevar.c | 118 ++++++++++++++++++++++ sched/env_setenv.c | 206 +++++++++++++++++++++++++++++++++++++++ sched/env_unsetenv.c | 137 ++++++++++++++++++++++++++ 12 files changed, 860 insertions(+), 3 deletions(-) create mode 100644 sched/env_findvar.c create mode 100644 sched/env_getenv.c create mode 100644 sched/env_putenv.c create mode 100644 sched/env_removevar.c create mode 100644 sched/env_setenv.c create mode 100644 sched/env_unsetenv.c diff --git a/ChangeLog b/ChangeLog index 5737ece75d..7a6e935f08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -188,5 +188,7 @@ * Added basic OS support to manage environment variables: environment storage, cloning on task creation, sharing on pthread creation, destruction on thread/task exit. + * Add environment variables APIs: environ, getenv, putenv, clearenv, setenv, + unsetenv * Started m68322 diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html index 15b34a54d6..0aa964dc90 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -623,6 +623,10 @@ Other memory: * Added basic OS support to manage environment variables: environment storage, cloning on task creation, sharing on pthread creation, destruction 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 diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 83f59e64da..84a8026b21 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -154,7 +154,7 @@ struct environ_s unsigned int ev_crefs; /* Reference count used when environment * is shared by threads */ 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; # define SIZEOF_ENVIRON_T(alloc) (sizeof(environ_t) + alloc - 1) diff --git a/sched/Makefile b/sched/Makefile index 97c59201e7..56bcb3a905 100644 --- a/sched/Makefile +++ b/sched/Makefile @@ -56,7 +56,8 @@ SCHED_SRCS = sched_setparam.c sched_getparam.c \ sched_getprioritymax.c sched_getprioritymin.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_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 \ wd_gettime.c TIME_SRCS = sched_processtimer.c diff --git a/sched/env_clearenv.c b/sched/env_clearenv.c index 1181596ea1..7fe01b5a2e 100644 --- a/sched/env_clearenv.c +++ b/sched/env_clearenv.c @@ -60,7 +60,7 @@ * Description: * The clearenv() function clears the environment of all name-value pairs * and sets the value of the external variable environ to NULL. - + * * Parameters: * None * diff --git a/sched/env_findvar.c b/sched/env_findvar.c new file mode 100644 index 0000000000..c1717d0513 --- /dev/null +++ b/sched/env_findvar.c @@ -0,0 +1,126 @@ +/**************************************************************************** + * env_findvar.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifndef CONFIG_DISABLE_ENVIRON + +#include +#include + +/**************************************************************************** + * 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 */ + + + diff --git a/sched/env_getenv.c b/sched/env_getenv.c new file mode 100644 index 0000000000..a1de739a97 --- /dev/null +++ b/sched/env_getenv.c @@ -0,0 +1,136 @@ +/**************************************************************************** + * env_getenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifndef CONFIG_DISABLE_ENVIRON + +#include +#include +#include +#include + +#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 */ + + + diff --git a/sched/env_internal.h b/sched/env_internal.h index 5961362df3..12395e256a 100644 --- a/sched/env_internal.h +++ b/sched/env_internal.h @@ -74,9 +74,16 @@ extern "C" { #endif #ifndef CONFIG_DISABLE_ENVIRON +/* functions used by the task/pthread creation and destruction logic */ + EXTERN int env_dup(FAR _TCB *ptcb); EXTERN int env_share(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 #undef EXTERN diff --git a/sched/env_putenv.c b/sched/env_putenv.c new file mode 100644 index 0000000000..7ea8ef96b6 --- /dev/null +++ b/sched/env_putenv.c @@ -0,0 +1,120 @@ +/**************************************************************************** + * env_putenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifndef CONFIG_DISABLE_ENVIRON + +#include +#include +#include +#include + +/**************************************************************************** + * 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 */ + + + diff --git a/sched/env_removevar.c b/sched/env_removevar.c new file mode 100644 index 0000000000..990e9659a5 --- /dev/null +++ b/sched/env_removevar.c @@ -0,0 +1,118 @@ +/**************************************************************************** + * env_removevar.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifndef CONFIG_DISABLE_ENVIRON + +#include +#include + +/**************************************************************************** + * 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 */ + + + diff --git a/sched/env_setenv.c b/sched/env_setenv.c new file mode 100644 index 0000000000..adac2c5c1a --- /dev/null +++ b/sched/env_setenv.c @@ -0,0 +1,206 @@ +/**************************************************************************** + * env_setenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifndef CONFIG_DISABLE_ENVIRON + +#include +#include +#include +#include +#include + +#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 */ + + + diff --git a/sched/env_unsetenv.c b/sched/env_unsetenv.c new file mode 100644 index 0000000000..8002cfdbb5 --- /dev/null +++ b/sched/env_unsetenv.c @@ -0,0 +1,137 @@ +/**************************************************************************** + * env_unsetenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#ifndef CONFIG_DISABLE_ENVIRON + +#include +#include +#include +#include + +#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 */ + + +