2012-12-16 22:15:27 +01:00
|
|
|
/****************************************************************************
|
2018-11-08 14:27:14 +01:00
|
|
|
* libs/libc/misc/lib_envpath.c
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02: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
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
2021-04-02 09:22:52 +02: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.
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-12-19 23:18:30 +01:00
|
|
|
#include <stdio.h>
|
2012-12-16 22:15:27 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
#include <nuttx/envpath.h>
|
2012-12-16 22:15:27 +01:00
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
#include "libc.h"
|
|
|
|
|
2021-08-01 09:27:08 +02:00
|
|
|
#if defined(CONFIG_LIBC_ENVPATH)
|
2012-12-16 22:15:27 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
struct envpath_s
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
|
|
|
FAR char *next; /* Pointer to the next (unterminated) value in the PATH variable */
|
|
|
|
char path[1];
|
|
|
|
};
|
2018-11-08 14:27:14 +01:00
|
|
|
|
|
|
|
#define SIZEOF_ENVPATH_S(n) (sizeof(struct envpath_s) + (n) - 1)
|
2012-12-16 22:15:27 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-11-08 14:27:14 +01:00
|
|
|
* Name: envpath_init
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize for the traversal of each value in the PATH variable. The
|
|
|
|
* usage is sequence is as follows:
|
|
|
|
*
|
2018-11-08 14:27:14 +01:00
|
|
|
* 1) Call envpath_init() to initialize for the traversal. envpath_init()
|
2012-12-16 22:15:27 +01:00
|
|
|
* will return an opaque handle that can then be provided to
|
2018-11-08 14:27:14 +01:00
|
|
|
* envpath_next() and envpath_release().
|
|
|
|
* 2) Call envpath_next() repeatedly to examine every file that lies
|
2012-12-16 22:15:27 +01:00
|
|
|
* in the directories of the PATH variable
|
2018-11-08 14:27:14 +01:00
|
|
|
* 3) Call envpath_release() to free resources set aside by envpath_init().
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-11-08 14:27:14 +01:00
|
|
|
* On success, envpath_init() return a non-NULL, opaque handle that may
|
|
|
|
* subsequently be used in calls to envpath_next() and envpath_release().
|
2012-12-16 22:15:27 +01:00
|
|
|
* On error, a NULL handle value will be returned. The most likely cause
|
2017-05-11 21:35:56 +02:00
|
|
|
* of an error would be that there is no value associated with the PATH
|
|
|
|
* variable.
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
ENVPATH_HANDLE envpath_init(FAR const char *name)
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
2018-11-08 14:27:14 +01:00
|
|
|
FAR struct envpath_s *envpath;
|
2012-12-16 22:15:27 +01:00
|
|
|
FAR char *path;
|
2023-02-08 04:29:09 +01:00
|
|
|
size_t size;
|
2012-12-16 22:15:27 +01:00
|
|
|
|
|
|
|
/* Get the value of the PATH variable */
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
path = getenv(name);
|
2012-12-16 22:15:27 +01:00
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
/* getenv() will return a NULL value if the PATH variable does not
|
|
|
|
* exist in the environment.
|
|
|
|
*/
|
|
|
|
|
2022-10-15 19:48:35 +02:00
|
|
|
return NULL;
|
2012-12-16 22:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a container for the PATH variable contents */
|
|
|
|
|
2023-02-08 04:29:09 +01:00
|
|
|
size = strlen(path) + 1;
|
2018-11-08 14:27:14 +01:00
|
|
|
envpath = (FAR struct envpath_s *)
|
2023-02-08 04:29:09 +01:00
|
|
|
lib_malloc(SIZEOF_ENVPATH_S(size));
|
2018-11-08 14:27:14 +01:00
|
|
|
|
|
|
|
if (!envpath)
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
|
|
|
/* Ooops.. we are out of memory */
|
|
|
|
|
2022-10-15 19:48:35 +02:00
|
|
|
return NULL;
|
2012-12-16 22:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate the container */
|
|
|
|
|
2023-02-08 04:29:09 +01:00
|
|
|
strlcpy(envpath->path, path, size);
|
2018-11-08 14:27:14 +01:00
|
|
|
envpath->next = envpath->path;
|
2012-12-16 22:15:27 +01:00
|
|
|
|
|
|
|
/* And return the containing cast to an opaque handle */
|
|
|
|
|
2022-10-15 19:48:35 +02:00
|
|
|
return envpath;
|
2012-12-16 22:15:27 +01:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/****************************************************************************
|
2018-11-08 14:27:14 +01:00
|
|
|
* Name: envpath_next
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse all possible values in the PATH variable in attempt to find
|
2018-11-08 14:27:14 +01:00
|
|
|
* the full path to an envcutable file when only a relative path is
|
2012-12-16 22:15:27 +01:00
|
|
|
* provided.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-11-08 14:27:14 +01:00
|
|
|
* handle - The handle value returned by envpath_init
|
2014-04-14 00:22:22 +02:00
|
|
|
* relpath - The relative path to the file to be found.
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, a non-NULL pointer to a null-terminated string is provided.
|
|
|
|
* This is the full path to a file that exists in the file system. This
|
|
|
|
* function will verify that the file exists (but will not verify that it
|
2018-11-08 14:27:14 +01:00
|
|
|
* is marked envcutable).
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* NOTE: The string pointer return in the success case points to allocated
|
2018-11-08 14:27:14 +01:00
|
|
|
* memory. This memory must be freed by the called by calling lib_free().
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* NULL is returned if no path is found to any file with the provided
|
2012-12-17 15:43:31 +01:00
|
|
|
* 'relpath' from any absolute path in the PATH variable. In this case,
|
2018-11-08 14:27:14 +01:00
|
|
|
* there is no point in calling envpath_next() further; envpath_release()
|
2012-12-16 22:15:27 +01:00
|
|
|
* must be called to release resources set aside by expath_init().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
FAR char *envpath_next(ENVPATH_HANDLE handle, FAR const char *relpath)
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
2018-11-08 14:27:14 +01:00
|
|
|
FAR struct envpath_s *envpath = (FAR struct envpath_s *)handle;
|
2012-12-16 22:15:27 +01:00
|
|
|
struct stat buf;
|
|
|
|
FAR char *endptr;
|
|
|
|
FAR char *path;
|
|
|
|
FAR char *fullpath;
|
|
|
|
int pathlen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that a value handle and relative path were provided */
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
DEBUGASSERT(envpath && relpath);
|
2012-12-16 22:15:27 +01:00
|
|
|
DEBUGASSERT(relpath[0] != '\0' && relpath[0] != '/');
|
|
|
|
|
|
|
|
/* Loop until (1) we find a file with this relative path from one of the
|
|
|
|
* absolute paths in the PATH variable, or (2) all of the absolute paths
|
|
|
|
* in the PATH variable have been considered.
|
|
|
|
*/
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
for (; ; )
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
2018-11-08 14:27:14 +01:00
|
|
|
/* Make sure that envpath->next points to the beginning of a string */
|
2012-12-16 22:15:27 +01:00
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
path = envpath->next;
|
2012-12-16 22:15:27 +01:00
|
|
|
if (*path == '\0')
|
2021-04-02 07:44:54 +02:00
|
|
|
{
|
|
|
|
/* If it points to a NULL it means that either (1) the PATH
|
|
|
|
* varialbe is empty, or (2) we have already examined all of the
|
|
|
|
* paths in the path variable.
|
2012-12-16 22:15:27 +01:00
|
|
|
*/
|
|
|
|
|
2022-10-15 19:48:35 +02:00
|
|
|
return NULL;
|
2021-04-02 07:44:54 +02:00
|
|
|
}
|
2012-12-16 22:15:27 +01:00
|
|
|
|
2021-04-02 07:44:54 +02:00
|
|
|
/* Okay... 'path' points to the beginning of the string. The string
|
|
|
|
* may be terminated either with (1) ':' which separates the path from
|
|
|
|
* the next path in the list, or (2) NUL which marks the end of the
|
|
|
|
* list.
|
2012-12-16 22:15:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
endptr = strchr(path, ':');
|
2018-11-08 14:27:14 +01:00
|
|
|
if (endptr == NULL)
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
|
|
|
/* If strchr returns NUL it means that ':' does not appear in the
|
|
|
|
* string. Therefore, this must be the final path in the PATH
|
|
|
|
* variable content.
|
|
|
|
*/
|
|
|
|
|
|
|
|
endptr = &path[strlen(path)];
|
2018-11-08 14:27:14 +01:00
|
|
|
envpath->next = endptr;
|
2012-12-16 22:15:27 +01:00
|
|
|
DEBUGASSERT(*endptr == '\0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT(*endptr == ':');
|
2018-11-08 14:27:14 +01:00
|
|
|
envpath->next = endptr + 1;
|
2012-12-16 22:15:27 +01:00
|
|
|
*endptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
pathlen = strlen(path) + strlen(relpath) + 2;
|
2018-11-08 14:27:14 +01:00
|
|
|
fullpath = (FAR char *)lib_malloc(pathlen);
|
2018-11-09 16:35:25 +01:00
|
|
|
if (fullpath == NULL)
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
|
|
|
/* Failed to allocate memory */
|
|
|
|
|
2022-10-15 19:48:35 +02:00
|
|
|
return NULL;
|
2012-12-16 22:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Construct the full path */
|
|
|
|
|
2023-03-05 15:34:33 +01:00
|
|
|
snprintf(fullpath, pathlen, "%s/%s", path, relpath);
|
2012-12-16 22:15:27 +01:00
|
|
|
|
|
|
|
/* Verify that a regular file exists at this path */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
ret = stat(fullpath, &buf);
|
2012-12-16 22:15:27 +01:00
|
|
|
if (ret == OK && S_ISREG(buf.st_mode))
|
|
|
|
{
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Failed to stat the file. Just free the allocated memory and
|
|
|
|
* continue to try the next path.
|
|
|
|
*/
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
lib_free(fullpath);
|
2012-12-16 22:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We will not get here */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-11-08 14:27:14 +01:00
|
|
|
* Name: envpath_release
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-11-08 14:27:14 +01:00
|
|
|
* Release all resources set aside by envpath_init() when the handle value
|
2012-12-16 22:15:27 +01:00
|
|
|
* was created. The handle value is invalid on return from this function.
|
2018-11-08 14:27:14 +01:00
|
|
|
* Attempts to all envpath_next() or envpath_release() with such a 'stale'
|
2012-12-16 22:15:27 +01:00
|
|
|
* handle will result in undefined (i.e., not good) behavior.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-11-08 14:27:14 +01:00
|
|
|
* handle - The handle value returned by envpath_init
|
2012-12-16 22:15:27 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
void envpath_release(ENVPATH_HANDLE handle)
|
2012-12-16 22:15:27 +01:00
|
|
|
{
|
2018-11-08 14:27:14 +01:00
|
|
|
lib_free(handle);
|
2012-12-16 22:15:27 +01:00
|
|
|
}
|
|
|
|
|
2018-11-08 14:27:14 +01:00
|
|
|
#endif /* CONFIG_LIBC_ENVPATH */
|