2007-07-01 03:39:17 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 03:53:29 +08:00
|
|
|
* sched/environ/env_removevar.c
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
2021-05-18 14:59:14 +08:00
|
|
|
#include <assert.h>
|
2007-07-01 03:39:17 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-08-09 03:53:29 +08:00
|
|
|
#include "environ/environ.h"
|
2014-01-15 03:30:22 +08:00
|
|
|
|
2007-07-01 03:39:17 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-15 03:30:31 +08:00
|
|
|
* Name: env_removevar
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove the referenced name=value pair from the environment
|
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2018-08-11 00:16:39 +08:00
|
|
|
* group - The task group with the environment containing the name=value
|
|
|
|
* pair
|
2022-04-16 19:38:17 +08:00
|
|
|
* index - A index to the name=value pair in the restroom
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2022-04-16 19:38:17 +08:00
|
|
|
* None
|
2007-07-01 03:39:17 +08:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - Not called from an interrupt handler
|
2018-08-11 00:16:39 +08:00
|
|
|
* - Caller has pre-emption disabled
|
2007-07-01 03:39:17 +08:00
|
|
|
* - Caller will reallocate the environment structure to the correct size
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
void env_removevar(FAR struct task_group_s *group, int index)
|
2007-07-01 03:39:17 +08:00
|
|
|
{
|
2022-04-16 19:38:17 +08:00
|
|
|
DEBUGASSERT(group != NULL && index >= 0);
|
2007-07-01 03:39:17 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
/* Free the allocate environment string */
|
2007-07-01 03:39:17 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
group_free(group, group->tg_envp[index]);
|
2007-07-01 03:39:17 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
/* Move all of the environment strings after the removed one 'down.'
|
|
|
|
* this is inefficient, but robably not high duty.
|
|
|
|
*/
|
2007-07-01 03:39:17 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
do
|
2013-01-26 07:21:27 +08:00
|
|
|
{
|
2022-04-16 19:38:17 +08:00
|
|
|
group->tg_envp[index] = group->tg_envp[index + 1];
|
|
|
|
}
|
|
|
|
while (group->tg_envp[++index] != NULL);
|
2013-01-26 07:21:27 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
/* Free the old environment (if there was one) */
|
2013-01-26 07:21:27 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
if (index == 1)
|
|
|
|
{
|
|
|
|
group_free(group, group->tg_envp);
|
|
|
|
group->tg_envp = NULL;
|
2007-07-01 03:39:17 +08:00
|
|
|
}
|
2022-04-16 19:38:17 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reallocate the environment to reclaim a little memory */
|
2012-07-15 03:30:31 +08:00
|
|
|
|
2022-04-16 19:38:17 +08:00
|
|
|
group->tg_envp = group_realloc(group, group->tg_envp,
|
|
|
|
sizeof(*group->tg_envp) * (index + 1));
|
|
|
|
DEBUGASSERT(group->tg_envp != NULL);
|
|
|
|
}
|
2007-07-01 03:39:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_ENVIRON */
|