2007-06-30 21:39:17 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 21:53:29 +02:00
|
|
|
* sched/environ/env_removevar.c
|
2007-06-30 21:39:17 +02:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 16:33:58 +01: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-06-30 21:39:17 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-06-30 21:39:17 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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-06-30 21:39:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-08-08 21:53:29 +02:00
|
|
|
#include "environ/environ.h"
|
2014-01-14 20:30:22 +01:00
|
|
|
|
2007-06-30 21:39:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: env_removevar
|
2007-06-30 21:39:17 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove the referenced name=value pair from the environment
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-08-10 18:16:39 +02:00
|
|
|
* group - The task group with the environment containing the name=value
|
|
|
|
* pair
|
2022-04-16 13:38:17 +02:00
|
|
|
* index - A index to the name=value pair in the restroom
|
2007-06-30 21:39:17 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2022-04-16 13:38:17 +02:00
|
|
|
* None
|
2007-06-30 21:39:17 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - Not called from an interrupt handler
|
2018-08-10 18:16:39 +02:00
|
|
|
* - Caller has pre-emption disabled
|
2007-06-30 21:39:17 +02:00
|
|
|
* - Caller will reallocate the environment structure to the correct size
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
void env_removevar(FAR struct task_group_s *group, ssize_t index)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
2023-02-09 07:59:03 +01:00
|
|
|
DEBUGASSERT(group != NULL && index >= 0 && index < group->tg_envc);
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
/* Free the allocate environment string */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
group_free(group, group->tg_envp[index]);
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
/* Exchange the last env and the index env */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
group->tg_envc--;
|
|
|
|
if (index == group->tg_envc)
|
2013-01-26 00:21:27 +01:00
|
|
|
{
|
2023-02-09 07:59:03 +01:00
|
|
|
group->tg_envp[index] = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
group->tg_envp[index] = group->tg_envp[group->tg_envc];
|
|
|
|
group->tg_envp[group->tg_envc] = NULL;
|
2022-04-16 13:38:17 +02:00
|
|
|
}
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
/* Free the old environment (if there was one) */
|
2013-01-26 00:21:27 +01:00
|
|
|
|
2023-02-09 07:59:03 +01:00
|
|
|
if (group->tg_envc == 0)
|
2022-04-16 13:38:17 +02:00
|
|
|
{
|
|
|
|
group_free(group, group->tg_envp);
|
|
|
|
group->tg_envp = NULL;
|
2024-03-27 03:41:15 +01:00
|
|
|
group->tg_envpc = 0;
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
2024-03-27 03:41:15 +01:00
|
|
|
else if (group->tg_envc <=
|
|
|
|
(group->tg_envpc - SCHED_ENVIRON_RESERVED * 2))
|
2022-04-16 13:38:17 +02:00
|
|
|
{
|
|
|
|
/* Reallocate the environment to reclaim a little memory */
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2024-03-27 03:41:15 +01:00
|
|
|
group->tg_envpc = group->tg_envc + SCHED_ENVIRON_RESERVED + 1;
|
|
|
|
|
2022-04-16 13:38:17 +02:00
|
|
|
group->tg_envp = group_realloc(group, group->tg_envp,
|
2024-03-27 03:41:15 +01:00
|
|
|
sizeof(*group->tg_envp) * group->tg_envpc);
|
2022-04-16 13:38:17 +02:00
|
|
|
DEBUGASSERT(group->tg_envp != NULL);
|
|
|
|
}
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_ENVIRON */
|