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
|
|
|
*
|
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
|
|
|
|
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
|
|
|
|
* pvar - A pointer 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:
|
2007-06-30 21:39:17 +02:00
|
|
|
* Zero on success
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
int env_removevar(FAR struct task_group_s *group, FAR char *pvar)
|
2007-06-30 21:39:17 +02:00
|
|
|
{
|
2013-01-26 00:21:27 +01:00
|
|
|
FAR char *end; /* Pointer to the end+1 of the environment */
|
|
|
|
int alloc; /* Size of the allocated environment */
|
2007-06-30 21:39:17 +02:00
|
|
|
int ret = ERROR;
|
|
|
|
|
2018-08-10 18:16:39 +02:00
|
|
|
DEBUGASSERT(group != NULL && pvar != NULL);
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
/* Verify that the pointer lies within the environment region */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2018-09-30 19:40:10 +02:00
|
|
|
alloc = group->tg_envsize; /* Size of the allocated environment */
|
|
|
|
end = &group->tg_envp[alloc]; /* Pointer to the end+1 of the environment */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
if (pvar >= group->tg_envp && pvar < end)
|
|
|
|
{
|
|
|
|
/* Set up for the removal */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2018-09-30 19:40:10 +02:00
|
|
|
int len = strlen(pvar) + 1; /* Length of name=value string to remove */
|
|
|
|
FAR char *src = &pvar[len]; /* Address of name=value string after */
|
|
|
|
FAR char *dest = pvar; /* Location to move the next string */
|
|
|
|
int count = end - src; /* Number of bytes to move (might be zero) */
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
/* Move all of the environment strings after the removed one 'down.'
|
|
|
|
* this is inefficient, but robably not high duty.
|
|
|
|
*/
|
2007-06-30 21:39:17 +02:00
|
|
|
|
2013-01-26 00:21:27 +01:00
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
2013-01-26 00:21:27 +01:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
group->tg_envsize -= len;
|
|
|
|
ret = OK;
|
2007-06-30 21:39:17 +02:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2007-06-30 21:39:17 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_ENVIRON */
|