2015-11-27 20:05:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_fsutils.c
|
|
|
|
*
|
2021-06-10 16:34:12 +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
|
2015-11-27 20:05:02 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-27 20:05:02 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +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.
|
2015-11-27 20:05:02 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2023-02-21 23:08:13 +01:00
|
|
|
#include <stdio.h>
|
2015-11-27 20:05:02 +01:00
|
|
|
#include <stdlib.h>
|
2015-11-28 15:29:27 +01:00
|
|
|
#include <string.h>
|
2015-11-28 22:01:00 +01:00
|
|
|
#include <ctype.h>
|
2015-11-27 20:05:02 +01:00
|
|
|
#include <fcntl.h>
|
2015-11-28 15:29:27 +01:00
|
|
|
#include <dirent.h>
|
2021-05-20 05:32:57 +02:00
|
|
|
#include <assert.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2015-11-27 20:05:02 +01:00
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_catfile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump the contents of a file to the current NSH terminal.
|
|
|
|
*
|
|
|
|
* Input Paratemets:
|
|
|
|
* vtbl - session vtbl
|
|
|
|
* cmd - NSH command name to use in error reporting
|
|
|
|
* filepath - The full path to the file to be dumped
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; -1 (ERROR) on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-28 14:23:08 +01:00
|
|
|
#ifdef NSH_HAVE_CATFILE
|
2015-11-27 20:05:02 +01:00
|
|
|
int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
|
|
|
FAR const char *filepath)
|
|
|
|
{
|
|
|
|
FAR char *buffer;
|
|
|
|
int fd;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Open the file for reading */
|
|
|
|
|
|
|
|
fd = open(filepath, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2020-10-28 12:30:16 +01:00
|
|
|
#if defined(CONFIG_NSH_PROC_MOUNTPOINT)
|
2020-10-28 11:04:16 +01:00
|
|
|
if (strncmp(filepath, CONFIG_NSH_PROC_MOUNTPOINT,
|
|
|
|
strlen(CONFIG_NSH_PROC_MOUNTPOINT)) == 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl,
|
2022-07-17 08:05:23 +02:00
|
|
|
"nsh: %s: Could not open %s (is procfs mounted?)\n",
|
|
|
|
cmd, filepath);
|
2020-10-28 11:04:16 +01:00
|
|
|
}
|
2020-10-28 12:30:16 +01:00
|
|
|
#endif
|
2022-07-21 12:04:23 +02:00
|
|
|
|
2022-07-17 08:05:23 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
|
2015-11-27 20:05:02 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (FAR char *)malloc(IOBUFFERSIZE);
|
2020-10-28 13:30:33 +01:00
|
|
|
if (buffer == NULL)
|
2015-11-27 20:05:02 +01:00
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "malloc", NSH_ERRNO);
|
2015-11-27 20:05:02 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And just dump it byte for byte into stdout */
|
|
|
|
|
2020-10-28 13:30:33 +01:00
|
|
|
for (; ; )
|
2015-11-27 20:05:02 +01:00
|
|
|
{
|
|
|
|
int nbytesread = read(fd, buffer, IOBUFFERSIZE);
|
|
|
|
|
|
|
|
/* Check for read errors */
|
|
|
|
|
|
|
|
if (nbytesread < 0)
|
|
|
|
{
|
|
|
|
int errval = errno;
|
|
|
|
|
|
|
|
/* EINTR is not an error (but will stop stop the cat) */
|
|
|
|
|
|
|
|
if (errval == EINTR)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtsignalrecvd, cmd);
|
2015-11-27 20:05:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-28 13:30:33 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "read",
|
|
|
|
NSH_ERRNO_OF(errval));
|
2015-11-27 20:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for data successfully read */
|
|
|
|
|
|
|
|
else if (nbytesread > 0)
|
|
|
|
{
|
|
|
|
int nbyteswritten = 0;
|
|
|
|
|
|
|
|
while (nbyteswritten < nbytesread)
|
|
|
|
{
|
2020-10-28 13:30:33 +01:00
|
|
|
ssize_t n = nsh_write(vtbl, buffer + nbyteswritten,
|
|
|
|
nbytesread - nbyteswritten);
|
2015-11-27 20:05:02 +01:00
|
|
|
if (n < 0)
|
|
|
|
{
|
2016-01-06 17:14:09 +01:00
|
|
|
int errcode = errno;
|
2015-11-27 20:05:02 +01:00
|
|
|
|
|
|
|
/* EINTR is not an error (but will stop stop the cat) */
|
|
|
|
|
2016-01-06 17:14:09 +01:00
|
|
|
if (errcode == EINTR)
|
2015-11-27 20:05:02 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtsignalrecvd, cmd);
|
2015-11-27 20:05:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "write",
|
2016-01-06 17:14:09 +01:00
|
|
|
NSH_ERRNO_OF(errcode));
|
2015-11-27 20:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbyteswritten += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, it is the end of file */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 13:30:33 +01:00
|
|
|
/* NOTE that the following NSH prompt may appear on the same line as file
|
|
|
|
* content. The IEEE Std requires that "The standard output shall
|
|
|
|
* contain the sequence of bytes read from the input files. Nothing else
|
|
|
|
* shall be written to the standard output." Reference:
|
|
|
|
* https://pubs.opengroup.org/onlinepubs/009695399/utilities/cat.html.
|
|
|
|
*/
|
2015-11-27 20:05:02 +01:00
|
|
|
|
2020-10-28 13:30:33 +01:00
|
|
|
/* Close the input file and return the result */
|
2015-11-27 20:05:02 +01:00
|
|
|
|
2020-10-28 13:30:33 +01:00
|
|
|
close(fd);
|
|
|
|
free(buffer);
|
|
|
|
return ret;
|
2015-11-27 20:05:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 14:23:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_readfile
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-28 18:57:37 +01:00
|
|
|
* Read a small file into a user-provided buffer. The data is assumed to
|
2022-07-21 12:04:23 +02:00
|
|
|
* be a string and is guaranteed to be NUL-terminated. An error occurs if
|
2015-11-28 18:57:37 +01:00
|
|
|
* the file content (+terminator) will not fit into the provided 'buffer'.
|
2015-11-28 14:23:08 +01:00
|
|
|
*
|
2020-02-22 19:52:12 +01:00
|
|
|
* Input Parameters:
|
2015-11-28 14:23:08 +01:00
|
|
|
* vtbl - The console vtable
|
|
|
|
* filepath - The full path to the file to be read
|
|
|
|
* buffer - The user-provided buffer into which the file is read.
|
|
|
|
* buflen - The size of the user provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; -1 (ERROR) is returned on any
|
|
|
|
* failure to read the fil into the buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NSH_HAVE_READFILE
|
2015-11-28 18:39:50 +01:00
|
|
|
int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
|
|
|
FAR const char *filepath, FAR char *buffer, size_t buflen)
|
2015-11-28 14:23:08 +01:00
|
|
|
{
|
|
|
|
FAR char *bufptr;
|
|
|
|
size_t remaining;
|
|
|
|
ssize_t nread;
|
|
|
|
ssize_t ntotal;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the file */
|
|
|
|
|
|
|
|
fd = open(filepath, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
|
2015-11-28 14:23:08 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read until we hit the end of the file, until we have exhausted the
|
|
|
|
* buffer space, or until some irrecoverable error occurs
|
|
|
|
*/
|
|
|
|
|
|
|
|
ntotal = 0; /* No bytes read yet */
|
|
|
|
*buffer = '\0'; /* NUL terminate the empty buffer */
|
|
|
|
bufptr = buffer; /* Working pointer */
|
|
|
|
remaining = buflen - 1; /* Reserve one byte for a NUL terminator */
|
|
|
|
ret = ERROR; /* Assume failure */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nread = read(fd, bufptr, remaining);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
/* Read error */
|
|
|
|
|
|
|
|
int errcode = errno;
|
|
|
|
DEBUGASSERT(errcode > 0);
|
|
|
|
|
|
|
|
/* EINTR is not a read error. It simply means that a signal was
|
|
|
|
* received while waiting for the read to complete.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (errcode != EINTR)
|
|
|
|
{
|
|
|
|
/* Fatal error */
|
|
|
|
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO);
|
2015-11-28 14:23:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nread == 0)
|
|
|
|
{
|
|
|
|
/* End of file */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Successful read. Make sure that the buffer is null terminated */
|
|
|
|
|
2022-08-29 17:58:36 +02:00
|
|
|
DEBUGASSERT(nread <= (ssize_t)remaining);
|
2015-11-28 14:23:08 +01:00
|
|
|
ntotal += nread;
|
|
|
|
buffer[ntotal] = '\0';
|
|
|
|
|
|
|
|
/* Bump up the read count and continuing reading to the end of
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bufptr += nread;
|
|
|
|
remaining -= nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (buflen > 0);
|
|
|
|
|
|
|
|
/* Close the file and return. */
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-28 15:29:27 +01:00
|
|
|
|
2022-01-20 14:12:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_writefile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump the contents of a file to the current NSH terminal.
|
|
|
|
*
|
|
|
|
* Input Paratemets:
|
|
|
|
* vtbl - session vtbl
|
|
|
|
* cmd - NSH command name to use in error reporting
|
2022-07-21 12:04:23 +02:00
|
|
|
* buffer - The pointer of writing buffer
|
|
|
|
* len - The length of writing buffer
|
2022-01-20 14:12:14 +01:00
|
|
|
* filepath - The full path to the file to be dumped
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; -1 (ERROR) on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NSH_HAVE_WRITEFILE
|
|
|
|
int nsh_writefile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
|
|
|
FAR const char *buffer, size_t len,
|
|
|
|
FAR const char *filepath)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the file for reading */
|
|
|
|
|
|
|
|
fd = open(filepath, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_NSH_PROC_MOUNTPOINT)
|
|
|
|
if (strncmp(filepath, CONFIG_NSH_PROC_MOUNTPOINT,
|
|
|
|
strlen(CONFIG_NSH_PROC_MOUNTPOINT)) == 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl,
|
2022-07-17 08:05:23 +02:00
|
|
|
"nsh: %s: Could not open %s (is procfs mounted?)\n",
|
|
|
|
cmd, filepath);
|
2022-01-20 14:12:14 +01:00
|
|
|
}
|
|
|
|
#endif
|
2022-07-21 12:04:23 +02:00
|
|
|
|
2022-07-17 08:05:23 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
|
2022-01-20 14:12:14 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, buffer, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "write", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return ret > 0 ? OK : ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_foreach_direntry
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Call the provided 'handler' for each entry found in the directory at
|
|
|
|
* 'dirpath'.
|
|
|
|
*
|
|
|
|
* Input Parameters
|
|
|
|
* vtbl - The console vtable
|
|
|
|
* cmd - NSH command name to use in error reporting
|
|
|
|
* dirpath - The full path to the directory to be traversed
|
|
|
|
* handler - The handler to be called for each entry of the directory
|
|
|
|
* pvarg - User provided argument to be passed to the 'handler'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) returned on success; -1 (ERROR) returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NSH_HAVE_FOREACH_DIRENTRY
|
|
|
|
int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
|
|
|
|
FAR const char *dirpath,
|
|
|
|
nsh_direntry_handler_t handler, void *pvarg)
|
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Open the directory */
|
|
|
|
|
|
|
|
dirp = opendir(dirpath);
|
2015-11-28 18:57:37 +01:00
|
|
|
if (dirp == NULL)
|
2015-11-28 15:29:27 +01:00
|
|
|
{
|
|
|
|
/* Failed to open the directory */
|
|
|
|
|
2020-10-28 12:30:16 +01:00
|
|
|
#if defined(CONFIG_NSH_PROC_MOUNTPOINT)
|
2020-10-28 11:04:16 +01:00
|
|
|
if (strncmp(dirpath, CONFIG_NSH_PROC_MOUNTPOINT,
|
|
|
|
strlen(CONFIG_NSH_PROC_MOUNTPOINT)) == 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl,
|
2022-07-17 08:05:23 +02:00
|
|
|
"nsh: %s: Could not open %s (is procfs mounted?)\n",
|
|
|
|
cmd, dirpath);
|
2020-10-28 11:04:16 +01:00
|
|
|
}
|
2022-07-21 12:04:23 +02:00
|
|
|
|
2020-10-28 12:30:16 +01:00
|
|
|
#endif
|
2022-07-17 08:05:23 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "opendir", NSH_ERRNO);
|
2015-11-28 15:29:27 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read each directory entry */
|
|
|
|
|
2015-11-28 18:57:37 +01:00
|
|
|
for (; ; )
|
2015-11-28 15:29:27 +01:00
|
|
|
{
|
|
|
|
FAR struct dirent *entryp = readdir(dirp);
|
2015-11-28 18:57:37 +01:00
|
|
|
if (entryp == NULL)
|
2015-11-28 15:29:27 +01:00
|
|
|
{
|
|
|
|
/* Finished with this directory */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the handler with this directory entry */
|
|
|
|
|
|
|
|
if (handler(vtbl, dirpath, entryp, pvarg) < 0)
|
|
|
|
{
|
|
|
|
/* The handler reported a problem */
|
|
|
|
|
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_trimdir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Skip any trailing '/' characters (unless it is also the leading '/')
|
|
|
|
*
|
2020-02-23 05:51:44 +01:00
|
|
|
* Input Parameters:
|
2015-11-28 15:29:27 +01:00
|
|
|
* dirpath - The directory path to be trimmed. May be modified!
|
|
|
|
*
|
2015-11-28 22:01:00 +01:00
|
|
|
* Returned value:
|
|
|
|
* None
|
2015-11-28 15:29:27 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NSH_HAVE_TRIMDIR
|
|
|
|
void nsh_trimdir(FAR char *dirpath)
|
|
|
|
{
|
|
|
|
/* Skip any trailing '/' characters (unless it is also the leading '/') */
|
|
|
|
|
|
|
|
int len = strlen(dirpath) - 1;
|
|
|
|
while (len > 0 && dirpath[len] == '/')
|
|
|
|
{
|
|
|
|
dirpath[len] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 22:01:00 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_trimspaces
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Trim any leading or trailing spaces from a string.
|
|
|
|
*
|
2020-02-23 05:51:44 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* str - The string to be trimmed. May be modified!
|
2015-11-28 22:01:00 +01:00
|
|
|
*
|
|
|
|
* Returned value:
|
|
|
|
* The new string pointer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NSH_HAVE_TRIMSPACES
|
|
|
|
FAR char *nsh_trimspaces(FAR char *str)
|
|
|
|
{
|
|
|
|
FAR char *trimmed;
|
|
|
|
int ndx;
|
|
|
|
|
|
|
|
/* Strip leading whitespace from the value */
|
|
|
|
|
|
|
|
for (trimmed = str;
|
|
|
|
*trimmed != '\0' && isspace(*trimmed);
|
|
|
|
trimmed++);
|
|
|
|
|
|
|
|
/* Strip trailing whitespace from the value */
|
|
|
|
|
|
|
|
for (ndx = strlen(trimmed) - 1;
|
|
|
|
ndx >= 0 && isspace(trimmed[ndx]);
|
|
|
|
ndx--)
|
|
|
|
{
|
|
|
|
trimmed[ndx] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return trimmed;
|
|
|
|
}
|
|
|
|
#endif
|
2022-02-10 08:36:42 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getdirpath
|
|
|
|
*
|
|
|
|
* Description:
|
2022-07-21 12:04:23 +02:00
|
|
|
* Combine dirpath with a file/path, this will generated a new string,
|
2022-02-10 08:36:42 +01:00
|
|
|
* which need free outside.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dirpath - the dirpath
|
|
|
|
* path - the file/path
|
|
|
|
*
|
|
|
|
* Returned value:
|
|
|
|
* The new string pointer, need free in caller.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NSH_HAVE_IOBUFFER
|
|
|
|
FAR char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR const char *dirpath, FAR const char *path)
|
|
|
|
{
|
|
|
|
/* Handle the case where all that is left is '/' */
|
|
|
|
|
|
|
|
if (strcmp(dirpath, "/") == 0)
|
|
|
|
{
|
|
|
|
snprintf(vtbl->iobuffer, IOBUFFERSIZE, "/%s", path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(vtbl->iobuffer, IOBUFFERSIZE, "%s/%s", dirpath, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(vtbl->iobuffer);
|
|
|
|
}
|
|
|
|
#endif
|