示例#1
0
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*)krealloc(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:
  errno = ret;
  return ERROR;
}
示例#2
0
int unsetenv(FAR const char *name)
{
  FAR struct tcb_s *rtcb = (FAR struct tcb_s*)g_readytorun.head;
  FAR struct task_group_s *group = rtcb->group;
  FAR char *pvar;
  FAR char *newenvp;
  int newsize;
  int ret = OK;

  DEBUGASSERT(name && group);

  /* Check if the variable exists */

  sched_lock();
  if (group && (pvar = env_findvar(group, name)) != NULL)
    {
      /* It does!  Remove the name=value pair from the environment. */

      (void)env_removevar(group, pvar);

      /* Reallocate the new environment buffer */

      newsize = group->tg_envsize;
      newenvp = (FAR char *)kurealloc(group->tg_envp, newsize);
      if (!newenvp)
        {
          set_errno(ENOMEM);
          ret = ERROR;
        }
      else
        {
          /* Save the new environment pointer (it might have changed due to
           * reallocation.
           */

          group->tg_envp = newenvp;
        }
    }

  sched_unlock();
  return ret;
}
示例#3
0
int setenv(FAR const char *name, FAR const char *value, int overwrite)
{
  FAR struct tcb_s *rtcb;
  FAR struct task_group_s *group;
  FAR char *pvar;
  FAR char *newenvp;
  int newsize;
  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 struct tcb_s*)g_readytorun.head;
  group = rtcb->group;
  DEBUGASSERT(group);

  /* Check if the variable already exists */

  if (group->tg_envp && (pvar = env_findvar(group, 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(group, 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 (group->tg_envp)
    {
      newsize = group->tg_envsize + varlen;
      newenvp = (FAR char *)kurealloc(group->tg_envp, newsize);
      if (!newenvp)
        {
          ret = ENOMEM;
          goto errout_with_lock;
        }

      pvar = &newenvp[group->tg_envsize];
    }
  else
    {
      newsize = varlen;
      newenvp = (FAR char *)kumalloc(varlen);
      if (!newenvp)
        {
          ret = ENOMEM;
          goto errout_with_lock;
        }

      pvar = newenvp;
    }

  /* Save the new buffer and size */

  group->tg_envp    = newenvp;
  group->tg_envsize = newsize;

  /* Now, put the new name=value string into the environment buffer */

  sprintf(pvar, "%s=%s", name, value);
  sched_unlock();
  return OK;

errout_with_lock:
  sched_unlock();
errout:
  errno = ret;
  return ERROR;
}
示例#4
0
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*)krealloc(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*)kmalloc(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:
  errno = ret;
  return ERROR;
}