2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_fscmds.c
|
|
|
|
*
|
2017-02-03 02:40:59 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights reserved.
|
2012-02-01 20:07:57 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# if !defined(CONFIG_DISABLE_MOUNTPOINT)
|
|
|
|
# ifdef CONFIG_FS_READABLE /* Need at least one filesytem in configuration */
|
|
|
|
# include <sys/mount.h>
|
2016-07-20 22:02:41 +02:00
|
|
|
# include <nuttx/drivers/ramdisk.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
# endif
|
2015-11-26 00:55:16 +01:00
|
|
|
# ifdef CONFIG_DEV_LOOP
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <nuttx/fs/loop.h>
|
|
|
|
# endif
|
2011-03-18 20:46:25 +01:00
|
|
|
# ifdef CONFIG_FS_FAT
|
2012-03-21 19:01:07 +01:00
|
|
|
# include <nuttx/fs/mkfatfs.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
# endif
|
2013-05-01 02:03:04 +02:00
|
|
|
# ifdef CONFIG_FS_SMARTFS
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "fsutils/mksmartfs.h"
|
2013-05-01 02:03:04 +02:00
|
|
|
# endif
|
2015-11-28 16:05:05 +01:00
|
|
|
# ifdef CONFIG_SMART_DEV_LOOP
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <nuttx/fs/smart.h>
|
|
|
|
# endif
|
2012-04-23 21:55:32 +02:00
|
|
|
# ifdef CONFIG_NFS
|
2012-04-23 22:33:25 +02:00
|
|
|
# include <sys/socket.h>
|
2012-04-24 01:21:30 +02:00
|
|
|
# include <netinet/in.h>
|
2012-04-23 21:55:32 +02:00
|
|
|
# include <nuttx/fs/nfs.h>
|
|
|
|
# endif
|
2012-08-02 02:42:46 +02:00
|
|
|
# ifdef CONFIG_RAMLOG_SYSLOG
|
2014-04-10 17:29:30 +02:00
|
|
|
# include <nuttx/syslog/ramlog.h>
|
2012-08-02 02:42:46 +02:00
|
|
|
# endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include "nsh.h"
|
2012-02-02 17:04:09 +01:00
|
|
|
#include "nsh_console.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define LSFLAGS_SIZE 1
|
|
|
|
#define LSFLAGS_LONG 2
|
|
|
|
#define LSFLAGS_RECURSIVE 4
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_getdirpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-26 19:33:16 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && \
|
|
|
|
(!defined(CONFIG_NSH_DISABLE_LS) || !defined(CONFIG_NSH_DISABLE_CP))
|
2015-11-28 18:13:47 +01:00
|
|
|
static char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR const char *path, FAR const char *file)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
/* Handle the case where all that is left is '/' */
|
|
|
|
|
|
|
|
if (strcmp(path, "/") == 0)
|
|
|
|
{
|
2016-10-12 01:35:31 +02:00
|
|
|
snprintf(vtbl->iobuffer, IOBUFFERSIZE, "/%s", file);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-12 01:35:31 +02:00
|
|
|
snprintf(vtbl->iobuffer, IOBUFFERSIZE, "%s/%s", path, file);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 18:13:47 +01:00
|
|
|
vtbl->iobuffer[PATH_MAX] = '\0';
|
|
|
|
return strdup(vtbl->iobuffer);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2013-10-01 20:10:09 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ls_specialdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int ls_specialdir(const char *dir)
|
|
|
|
{
|
|
|
|
/* '.' and '..' directories are not listed like normal directories */
|
|
|
|
|
|
|
|
return (strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ls_handler
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-07-22 22:09:29 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_NSH_DISABLE_LS)
|
2013-09-13 19:41:00 +02:00
|
|
|
static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
|
|
|
|
FAR struct dirent *entryp, FAR void *pvarg)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-04-11 20:13:18 +02:00
|
|
|
unsigned int lsflags = (unsigned int)((uintptr_t)pvarg);
|
2011-03-18 20:46:25 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if any options will require that we stat the file */
|
|
|
|
|
2015-03-15 19:22:12 +01:00
|
|
|
if ((lsflags & (LSFLAGS_SIZE | LSFLAGS_LONG)) != 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
2013-09-13 19:41:00 +02:00
|
|
|
/* stat the file */
|
|
|
|
|
|
|
|
if (entryp != NULL)
|
|
|
|
{
|
2015-11-28 18:13:47 +01:00
|
|
|
FAR char *fullpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
|
2013-09-13 19:41:00 +02:00
|
|
|
ret = stat(fullpath, &buf);
|
|
|
|
free(fullpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A NULL entryp signifies that we are running ls on a single file */
|
|
|
|
|
|
|
|
ret = stat(dirpath, &buf);
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, "ls", "stat", NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lsflags & LSFLAGS_LONG) != 0)
|
|
|
|
{
|
|
|
|
char details[] = "----------";
|
|
|
|
if (S_ISDIR(buf.st_mode))
|
|
|
|
{
|
|
|
|
details[0]='d';
|
|
|
|
}
|
|
|
|
else if (S_ISCHR(buf.st_mode))
|
|
|
|
{
|
|
|
|
details[0]='c';
|
|
|
|
}
|
|
|
|
else if (S_ISBLK(buf.st_mode))
|
|
|
|
{
|
|
|
|
details[0]='b';
|
|
|
|
}
|
2014-09-30 22:32:23 +02:00
|
|
|
else if (!S_ISREG(buf.st_mode))
|
2014-09-29 01:30:42 +02:00
|
|
|
{
|
|
|
|
details[0]='?';
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
if ((buf.st_mode & S_IRUSR) != 0)
|
|
|
|
{
|
|
|
|
details[1]='r';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IWUSR) != 0)
|
|
|
|
{
|
|
|
|
details[2]='w';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IXUSR) != 0)
|
|
|
|
{
|
|
|
|
details[3]='x';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IRGRP) != 0)
|
|
|
|
{
|
|
|
|
details[4]='r';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IWGRP) != 0)
|
|
|
|
{
|
|
|
|
details[5]='w';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IXGRP) != 0)
|
|
|
|
{
|
|
|
|
details[6]='x';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IROTH) != 0)
|
|
|
|
{
|
|
|
|
details[7]='r';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IWOTH) != 0)
|
|
|
|
{
|
|
|
|
details[8]='w';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((buf.st_mode & S_IXOTH) != 0)
|
|
|
|
{
|
|
|
|
details[9]='x';
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_output(vtbl, " %s", details);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lsflags & LSFLAGS_SIZE) != 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "%8d", buf.st_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 19:41:00 +02:00
|
|
|
/* Then provide the filename that is common to normal and verbose output */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2013-09-13 19:41:00 +02:00
|
|
|
if (entryp != NULL)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, " %s", entryp->d_name);
|
2015-11-28 15:29:27 +01:00
|
|
|
|
2013-09-13 19:41:00 +02:00
|
|
|
if (DIRENT_ISDIRECTORY(entryp->d_type) &&
|
|
|
|
!ls_specialdir(entryp->d_name))
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "/\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 19:41:00 +02:00
|
|
|
/* A single file */
|
|
|
|
|
|
|
|
nsh_output(vtbl, " %s\n", dirpath);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2013-09-13 19:41:00 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ls_recursive
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-15 20:07:27 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_NSH_DISABLE_LS)
|
2011-03-18 20:46:25 +01:00
|
|
|
static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath,
|
|
|
|
struct dirent *entryp, void *pvarg)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Is this entry a directory (and not one of the special directories, . and ..)? */
|
|
|
|
|
|
|
|
if (DIRENT_ISDIRECTORY(entryp->d_type) && !ls_specialdir(entryp->d_name))
|
|
|
|
{
|
|
|
|
/* Yes.. */
|
|
|
|
|
2015-03-29 22:42:03 +02:00
|
|
|
FAR char *newpath;
|
2015-11-28 18:13:47 +01:00
|
|
|
newpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* List the directory contents */
|
|
|
|
|
|
|
|
nsh_output(vtbl, "%s:\n", newpath);
|
|
|
|
|
|
|
|
/* Traverse the directory */
|
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_handler, pvarg);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* Then recurse to list each directory within the directory */
|
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_recursive,
|
|
|
|
pvarg);
|
2011-03-18 20:46:25 +01:00
|
|
|
free(newpath);
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 22:42:03 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-06-15 20:07:27 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 && !CONFIG_NSH_DISABLE_LS */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2012-02-11 16:27:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-11-23 17:21:15 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_basename
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_BASENAME
|
|
|
|
int cmd_basename(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *filename;
|
|
|
|
|
|
|
|
/* Usage: basename <path> [<suffix>]
|
|
|
|
*
|
|
|
|
* NOTE: basename() may modify path.
|
|
|
|
*/
|
|
|
|
|
|
|
|
filename = basename(argv[1]);
|
|
|
|
if (argc > 2)
|
|
|
|
{
|
|
|
|
FAR char *suffix = argv[2];
|
|
|
|
int nndx;
|
|
|
|
int sndx;
|
|
|
|
|
|
|
|
/* Check for any trailing sub-string */
|
|
|
|
|
|
|
|
nndx = strlen(filename);
|
|
|
|
sndx = strlen(suffix);
|
|
|
|
nndx -= sndx;
|
|
|
|
|
|
|
|
if (nndx > 0 && strcmp(&filename[nndx], suffix) == 0)
|
|
|
|
{
|
|
|
|
filename[nndx] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And output the resulting basename */
|
|
|
|
|
|
|
|
nsh_output(vtbl, "%s\n", filename);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_dirname
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_DIRNAME
|
|
|
|
int cmd_dirname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *filename;
|
|
|
|
|
|
|
|
/* Usage: dirname <path>
|
|
|
|
*
|
|
|
|
* NOTE: basename() may modify path.
|
|
|
|
*/
|
|
|
|
|
|
|
|
filename = dirname(argv[1]);
|
|
|
|
nsh_output(vtbl, "%s\n", filename);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 16:27:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cat
|
|
|
|
****************************************************************************/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2012-02-11 16:27:44 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_CAT
|
|
|
|
int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *fullpath;
|
|
|
|
int i;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Loop for each file name on the command line */
|
|
|
|
|
|
|
|
for (i = 1; i < argc && ret == OK; i++)
|
|
|
|
{
|
|
|
|
/* Get the fullpath to the file */
|
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, argv[i]);
|
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Dump the file to the console */
|
|
|
|
|
2015-11-27 20:04:11 +01:00
|
|
|
ret = nsh_catfile(vtbl, argv[0], fullpath);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Free the allocated full path */
|
|
|
|
|
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
|
|
|
}
|
2012-02-11 16:27:44 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 16:27:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_dmesg
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-27 19:36:34 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_RAMLOG_SYSLOG) && \
|
|
|
|
!defined(CONFIG_NSH_DISABLE_DMESG)
|
2012-02-11 16:27:44 +01:00
|
|
|
int cmd_dmesg(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2015-11-27 20:04:11 +01:00
|
|
|
return nsh_catfile(vtbl, argv[0], CONFIG_SYSLOG_DEVPATH);
|
2012-02-11 16:27:44 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cp
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_CP
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
char *srcpath = NULL;
|
|
|
|
char *destpath = NULL;
|
|
|
|
char *allocpath = NULL;
|
2015-11-27 20:04:11 +01:00
|
|
|
int oflags = O_WRONLY | O_CREAT | O_TRUNC;
|
2011-03-18 20:46:25 +01:00
|
|
|
int rdfd;
|
|
|
|
int wrfd;
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
/* Get the full path to the source file */
|
|
|
|
|
|
|
|
srcpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
if (!srcpath)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the source file for reading */
|
|
|
|
|
|
|
|
rdfd = open(srcpath, O_RDONLY);
|
|
|
|
if (rdfd < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
goto errout_with_srcpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the full path to the destination file or directory */
|
|
|
|
|
|
|
|
destpath = nsh_getfullpath(vtbl, argv[2]);
|
|
|
|
if (!destpath)
|
|
|
|
{
|
|
|
|
goto errout_with_rdfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the destination is a directory */
|
|
|
|
|
|
|
|
ret = stat(destpath, &buf);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* Something exists here... is it a directory? */
|
|
|
|
|
|
|
|
if (S_ISDIR(buf.st_mode))
|
|
|
|
{
|
|
|
|
/* Yes, it is a directory. Remove any trailing '/' characters from the path */
|
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
nsh_trimdir(argv[2]);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Construct the full path to the new file */
|
|
|
|
|
2015-11-28 18:13:47 +01:00
|
|
|
allocpath = nsh_getdirpath(vtbl, argv[2], basename(argv[1]) );
|
2011-03-18 20:46:25 +01:00
|
|
|
if (!allocpath)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdoutofmemory, argv[0]);
|
|
|
|
goto errout_with_destpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open then dest for writing */
|
|
|
|
|
|
|
|
nsh_freefullpath(destpath);
|
|
|
|
destpath = allocpath;
|
|
|
|
}
|
|
|
|
else if (!S_ISREG(buf.st_mode))
|
|
|
|
{
|
|
|
|
/* Maybe it is a driver? */
|
|
|
|
|
|
|
|
oflags = O_WRONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now open the destination */
|
|
|
|
|
|
|
|
wrfd = open(destpath, oflags, 0666);
|
|
|
|
if (wrfd < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
goto errout_with_allocpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now copy the file */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int nbytesread;
|
|
|
|
int nbyteswritten;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-11-28 18:13:47 +01:00
|
|
|
nbytesread = read(rdfd, vtbl->iobuffer, IOBUFFERSIZE);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (nbytesread == 0)
|
|
|
|
{
|
|
|
|
/* End of file */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
goto errout_with_wrfd;
|
|
|
|
}
|
|
|
|
else if (nbytesread < 0)
|
|
|
|
{
|
|
|
|
/* EINTR is not an error (but will still stop the copy) */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtsignalrecvd, argv[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Read error */
|
|
|
|
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
goto errout_with_wrfd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread <= 0);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-11-28 18:13:47 +01:00
|
|
|
nbyteswritten = write(wrfd, vtbl->iobuffer, nbytesread);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (nbyteswritten >= 0)
|
|
|
|
{
|
|
|
|
nbytesread -= nbyteswritten;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* EINTR is not an error (but will still stop the copy) */
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtsignalrecvd, argv[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Read error */
|
|
|
|
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
goto errout_with_wrfd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_wrfd:
|
|
|
|
close(wrfd);
|
|
|
|
|
|
|
|
errout_with_allocpath:
|
|
|
|
if (allocpath)
|
|
|
|
{
|
|
|
|
free(allocpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_destpath:
|
|
|
|
if (destpath && !allocpath)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(destpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_rdfd:
|
|
|
|
close(rdfd);
|
|
|
|
|
|
|
|
errout_with_srcpath:
|
|
|
|
if (srcpath)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(srcpath);
|
|
|
|
}
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_losetup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_MOUNTPOINT)
|
2015-11-26 00:55:16 +01:00
|
|
|
# if defined(CONFIG_DEV_LOOP) && !defined(CONFIG_NSH_DISABLE_LOSETUP)
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2015-11-26 00:55:16 +01:00
|
|
|
FAR char *loopdev = NULL;
|
|
|
|
FAR char *filepath = NULL;
|
|
|
|
struct losetup_s setup;
|
|
|
|
bool teardown = false;
|
|
|
|
bool readonly = false;
|
|
|
|
off_t offset = 0;
|
|
|
|
bool badarg = false;
|
|
|
|
int ret = ERROR;
|
|
|
|
int option;
|
|
|
|
int fd;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Get the losetup options: Two forms are supported:
|
|
|
|
*
|
|
|
|
* losetup -d <loop-device>
|
|
|
|
* losetup [-o <offset>] [-r] <loop-device> <filename>
|
|
|
|
*
|
|
|
|
* NOTE that the -o and -r options are accepted with the -d option, but
|
|
|
|
* will be ignored.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, "d:o:r")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
loopdev = nsh_getfullpath(vtbl, optarg);
|
|
|
|
teardown = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
offset = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
readonly = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a bad argument was encountered, then return without processing the command */
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is not a tear down operation, then additional command line
|
|
|
|
* parameters are required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!teardown)
|
|
|
|
{
|
|
|
|
/* There must be two arguments on the command line after the options */
|
|
|
|
|
|
|
|
if (optind + 1 < argc)
|
|
|
|
{
|
|
|
|
loopdev = nsh_getfullpath(vtbl, argv[optind]);
|
|
|
|
optind++;
|
|
|
|
|
|
|
|
filepath = nsh_getfullpath(vtbl, argv[optind]);
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing else on the command line */
|
|
|
|
|
|
|
|
if (optind < argc)
|
2015-11-26 00:55:16 +01:00
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the loop device */
|
|
|
|
|
|
|
|
fd = open("/dev/loop", O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Perform the teardown operation */
|
|
|
|
|
|
|
|
if (teardown)
|
|
|
|
{
|
|
|
|
/* Tear down the loop device. */
|
|
|
|
|
2015-11-26 00:55:16 +01:00
|
|
|
ret = ioctl(fd, LOOPIOC_TEARDOWN, (unsigned long)((uintptr_t)loopdev));
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-11-26 00:55:16 +01:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
|
|
|
|
goto errout_with_fd;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set up the loop device */
|
|
|
|
|
2015-11-26 00:55:16 +01:00
|
|
|
setup.devname = loopdev; /* The loop block device to be created */
|
|
|
|
setup.filename = filepath; /* The file or character device to use */
|
|
|
|
setup.sectsize = 512; /* The sector size to use with the block device */
|
|
|
|
setup.offset = offset; /* An offset that may be applied to the device */
|
|
|
|
setup.readonly = readonly; /* True: Read access will be supported only */
|
|
|
|
|
|
|
|
ret = ioctl(fd, LOOPIOC_SETUP, (unsigned long)((uintptr_t)&setup));
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-11-26 00:55:16 +01:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
|
|
|
|
goto errout_with_fd;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:55:16 +01:00
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
/* Free resources */
|
|
|
|
|
|
|
|
errout_with_fd:
|
|
|
|
(void)close(fd);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
errout_with_paths:
|
|
|
|
if (loopdev)
|
|
|
|
{
|
|
|
|
free(loopdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filepath)
|
|
|
|
{
|
|
|
|
free(filepath);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 16:05:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_losmart
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_MOUNTPOINT)
|
|
|
|
# if defined(CONFIG_SMART_DEV_LOOP) && !defined(CONFIG_NSH_DISABLE_LOSMART)
|
|
|
|
int cmd_losmart(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *loopdev = NULL;
|
|
|
|
FAR char *filepath = NULL;
|
|
|
|
struct smart_losetup_s setup;
|
|
|
|
bool teardown = false;
|
|
|
|
bool readonly = false;
|
|
|
|
int minor = -1;
|
|
|
|
int erasesize = -1;
|
|
|
|
int sectsize = -1;
|
|
|
|
off_t offset = 0;
|
|
|
|
bool badarg = false;
|
|
|
|
int ret = ERROR;
|
|
|
|
int option;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Get the losetup options: Two forms are supported:
|
|
|
|
*
|
|
|
|
* losmart -d <loop-device>
|
|
|
|
* losmart [-m minor-number] [-o <offset>] [-e erasesize] [-s sectsize] [-r] <filename>
|
|
|
|
*
|
|
|
|
* NOTE that the -o and -r options are accepted with the -d option, but
|
|
|
|
* will be ignored.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, "d:e:m:o:rs:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
loopdev = nsh_getfullpath(vtbl, optarg);
|
|
|
|
teardown = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
erasesize = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
minor = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
offset = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
readonly = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
sectsize = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a bad argument was encountered, then return without processing the command */
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is not a tear down operation, then additional command line
|
|
|
|
* parameters are required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!teardown)
|
|
|
|
{
|
|
|
|
/* There must be two arguments on the command line after the options */
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
filepath = nsh_getfullpath(vtbl, argv[optind]);
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing else on the command line */
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the loop device */
|
|
|
|
|
|
|
|
fd = open("/dev/smart", O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the teardown operation */
|
|
|
|
|
|
|
|
if (teardown)
|
|
|
|
{
|
|
|
|
/* Tear down the loop device. */
|
|
|
|
|
|
|
|
ret = ioctl(fd, SMART_LOOPIOC_TEARDOWN, (unsigned long)((uintptr_t) loopdev));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set up the loop device */
|
|
|
|
|
|
|
|
setup.minor = minor; /* The loop block device to be created */
|
|
|
|
setup.filename = filepath; /* The file or character device to use */
|
|
|
|
setup.sectsize = sectsize; /* The sector size to use with the block device */
|
|
|
|
setup.erasesize = erasesize; /* The sector size to use with the block device */
|
|
|
|
setup.offset = offset; /* An offset that may be applied to the device */
|
|
|
|
setup.readonly = readonly; /* True: Read access will be supported only */
|
|
|
|
|
|
|
|
ret = ioctl(fd, SMART_LOOPIOC_SETUP, (unsigned long)((uintptr_t)&setup));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
/* Free resources */
|
|
|
|
|
|
|
|
errout_with_fd:
|
|
|
|
(void)close(fd);
|
|
|
|
|
|
|
|
errout_with_paths:
|
|
|
|
if (loopdev)
|
|
|
|
{
|
|
|
|
free(loopdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filepath)
|
|
|
|
{
|
|
|
|
free(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-02-03 02:40:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ln
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
# if !defined(CONFIG_NSH_DISABLE_LN) && defined(CONFIG_PSEUDOFS_SOFTLINKS)
|
|
|
|
int cmd_ln(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *fullpath;
|
|
|
|
FAR char *target;
|
|
|
|
int ndx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* ln [-s] <target> <link> */
|
|
|
|
|
|
|
|
if (argc == 4)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "-s") != 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ndx = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the fullpath to the directory */
|
|
|
|
|
|
|
|
target = argv[ndx];
|
|
|
|
fullpath = nsh_getfullpath(vtbl, argv[ndx + 1]);
|
|
|
|
|
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = link(target, fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "link", NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ls
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_LS
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2013-09-13 19:41:00 +02:00
|
|
|
struct stat st;
|
2017-02-03 02:40:59 +01:00
|
|
|
FAR const char *relpath;
|
2011-03-18 20:46:25 +01:00
|
|
|
unsigned int lsflags = 0;
|
2017-02-03 02:40:59 +01:00
|
|
|
FAR char *fullpath;
|
2011-03-18 20:46:25 +01:00
|
|
|
bool badarg = false;
|
2015-11-27 19:28:46 +01:00
|
|
|
int len;
|
2011-03-18 20:46:25 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the ls options */
|
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = getopt(argc, argv, "lRs")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'l':
|
|
|
|
lsflags |= (LSFLAGS_SIZE|LSFLAGS_LONG);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'R':
|
|
|
|
lsflags |= LSFLAGS_RECURSIVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
lsflags |= LSFLAGS_SIZE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a bad argument was encountered, then return without processing the command */
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There may be one argument after the options */
|
|
|
|
|
2012-05-14 16:25:57 +02:00
|
|
|
if (optind + 1 < argc)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2012-05-14 16:25:57 +02:00
|
|
|
else if (optind >= argc)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
relpath = nsh_getcwd();
|
|
|
|
#else
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relpath = argv[optind];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the fullpath to the directory */
|
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, relpath);
|
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-11-27 19:28:46 +01:00
|
|
|
/* Trim any trailing '/' characters */
|
|
|
|
|
|
|
|
len = strlen(fullpath) - 1;
|
|
|
|
while (len > 0 && fullpath[len] == '/')
|
|
|
|
{
|
|
|
|
fullpath[len] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2013-09-13 19:41:00 +02:00
|
|
|
/* See if it is a single file */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2013-11-13 22:59:14 +01:00
|
|
|
if (stat(fullpath, &st) < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "stat", NSH_ERRNO);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
else if (!S_ISDIR(st.st_mode))
|
2013-09-13 19:41:00 +02:00
|
|
|
{
|
|
|
|
/* Pass a null dirent to ls_handler to signify that this is a single
|
|
|
|
* file
|
|
|
|
*/
|
|
|
|
|
2015-04-11 20:13:18 +02:00
|
|
|
ret = ls_handler(vtbl, fullpath, NULL, (FAR void *)((uintptr_t)lsflags));
|
2013-09-13 19:41:00 +02:00
|
|
|
}
|
|
|
|
else
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2013-09-13 19:41:00 +02:00
|
|
|
/* List the directory contents */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2013-09-13 19:41:00 +02:00
|
|
|
nsh_output(vtbl, "%s:\n", fullpath);
|
2015-04-11 20:13:18 +02:00
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
ret = nsh_foreach_direntry(vtbl, "ls", fullpath, ls_handler,
|
|
|
|
(FAR void*)((uintptr_t)lsflags));
|
2013-09-13 19:41:00 +02:00
|
|
|
if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0)
|
|
|
|
{
|
|
|
|
/* Then recurse to list each directory within the directory */
|
|
|
|
|
2015-11-28 15:29:27 +01:00
|
|
|
ret = nsh_foreach_direntry(vtbl, "ls", fullpath, ls_recursive,
|
|
|
|
(FAR void *)((uintptr_t)lsflags));
|
2013-09-13 19:41:00 +02:00
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2013-09-13 19:41:00 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifdef NSH_HAVE_DIROPTS
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_MKDIR
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_mkdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
ret = mkdir(fullpath, 0777);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mkdir", NSH_ERRNO);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
2017-02-03 02:40:59 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkfatfs
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_FAT)
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_MKFATFS
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_mkfatfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct fat_format_s fmt = FAT_FORMAT_INITIALIZER;
|
2013-12-06 01:12:11 +01:00
|
|
|
FAR char *fullpath;
|
|
|
|
bool badarg;
|
|
|
|
int option;
|
2011-03-18 20:46:25 +01:00
|
|
|
int ret = ERROR;
|
|
|
|
|
2013-12-06 01:12:11 +01:00
|
|
|
/* mkfatfs [-F <fatsize>] <block-driver> */
|
|
|
|
|
|
|
|
badarg = false;
|
|
|
|
while ((option = getopt(argc, argv, ":F:")) != ERROR)
|
2013-05-01 02:03:04 +02:00
|
|
|
{
|
2013-12-06 01:12:11 +01:00
|
|
|
switch (option)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2013-12-06 01:12:11 +01:00
|
|
|
case 'F':
|
|
|
|
fmt.ff_fattype = atoi(optarg);
|
|
|
|
if (fmt.ff_fattype != 0 && fmt.ff_fattype != 12 &&
|
|
|
|
fmt.ff_fattype != 16 && fmt.ff_fattype != 32)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrange, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
|
|
|
/* If a bad argument was encountered, then return without processing the command */
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be exactly one parameter left on the command-line */
|
|
|
|
|
|
|
|
if (optind == argc-1)
|
|
|
|
{
|
|
|
|
fullpath = nsh_getfullpath(vtbl, argv[optind]);
|
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "nsh_getfullpath",
|
|
|
|
NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (optind >= argc)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now format the FAT file system */
|
|
|
|
|
|
|
|
ret = mkfatfs(fullpath, &fmt);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mkfatfs", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_freefullpath(fullpath);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkfifo
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2016-08-05 00:25:47 +02:00
|
|
|
# if defined(CONFIG_PIPES) && CONFIG_DEV_FIFO_SIZE > 0 && \
|
2016-08-04 20:57:02 +02:00
|
|
|
!defined(CONFIG_NSH_DISABLE_MKFIFO)
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_mkfifo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
ret = mkfifo(fullpath, 0777);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mkfifo", NSH_ERRNO);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2016-08-05 00:25:47 +02:00
|
|
|
#endif /* CONFIG_PIPES && CONFIG_DEV_FIFO_SIZE > 0 && !CONFIG_NSH_DISABLE_MKFIFO */
|
2016-08-04 21:32:19 +02:00
|
|
|
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkrd
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifdef NSH_HAVE_WRITABLE_MOUNTPOINT
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_MKRD
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *fmt;
|
|
|
|
uint8_t *buffer;
|
|
|
|
uint32_t nsectors;
|
|
|
|
bool badarg = false;
|
|
|
|
int sectsize = 512;
|
|
|
|
int minor = 0;
|
|
|
|
int ret;
|
|
|
|
|
2012-08-04 01:47:32 +02:00
|
|
|
/* Get the mkrd options */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = getopt(argc, argv, ":m:s:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'm':
|
|
|
|
minor = atoi(optarg);
|
|
|
|
if (minor < 0 || minor > 255)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrange, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
sectsize = atoi(optarg);
|
|
|
|
if (minor < 0 || minor > 16384)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrange, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a bad argument was encountered, then return without processing the command */
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:12:11 +01:00
|
|
|
/* There should be exactly one parameter left on the command-line */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
if (optind == argc-1)
|
|
|
|
{
|
|
|
|
nsectors = (uint32_t)atoi(argv[optind]);
|
|
|
|
}
|
|
|
|
else if (optind >= argc)
|
|
|
|
{
|
|
|
|
fmt = g_fmttoomanyargs;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the memory backing up the ramdisk */
|
|
|
|
|
|
|
|
buffer = (uint8_t*)malloc(sectsize * nsectors);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:50:38 +02:00
|
|
|
#ifdef CONFIG_DEBUG_INFO
|
2011-03-18 20:46:25 +01:00
|
|
|
memset(buffer, 0, sectsize * nsectors);
|
|
|
|
#endif
|
2016-06-23 16:04:33 +02:00
|
|
|
finfo("RAMDISK at %p\n", buffer);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Then register the ramdisk */
|
|
|
|
|
2015-02-01 14:24:16 +01:00
|
|
|
ret = ramdisk_register(minor, buffer, nsectors, sectsize,
|
|
|
|
RDFLAG_WRENABLED | RDFLAG_FUNLINK);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ramdisk_register", NSH_ERRNO_OF(-ret));
|
|
|
|
free(buffer);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout_with_fmt:
|
|
|
|
nsh_output(vtbl, fmt, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-05-01 02:03:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mksmartfs
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && \
|
2015-11-23 13:59:56 +01:00
|
|
|
defined(CONFIG_FS_SMARTFS) && defined(CONFIG_FSUTILS_MKSMARTFS)
|
2013-05-01 02:03:04 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_MKSMARTFS
|
|
|
|
int cmd_mksmartfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2015-11-23 13:59:56 +01:00
|
|
|
char *fullpath = NULL;
|
2013-05-01 02:03:04 +02:00
|
|
|
int ret = ERROR;
|
2015-11-23 13:59:56 +01:00
|
|
|
uint16_t sectorsize = 0;
|
|
|
|
int opt;
|
2013-05-01 02:03:04 +02:00
|
|
|
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
|
|
|
|
int nrootdirs = 1;
|
|
|
|
#endif
|
|
|
|
|
2015-11-23 13:59:56 +01:00
|
|
|
/* Process any options */
|
|
|
|
|
|
|
|
optind = 0;
|
|
|
|
while ((opt = getopt(argc, argv, "s:")) != -1)
|
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
sectorsize = atoi(optarg);
|
|
|
|
if (sectorsize < 256 || sectorsize > 16384)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "Sector size must be 256-16384\n");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if (sectorsize & (sectorsize-1))
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "Sector size must be power of 2\n");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
fullpath = nsh_getfullpath(vtbl, argv[optind]);
|
|
|
|
}
|
|
|
|
|
2013-05-01 02:03:04 +02:00
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
/* Test if number of root directories was supplied */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
|
2015-11-23 13:59:56 +01:00
|
|
|
if (optind + 1 < argc)
|
2013-05-01 02:03:04 +02:00
|
|
|
{
|
2015-11-23 13:59:56 +01:00
|
|
|
nrootdirs = atoi(argv[optind + 1]);
|
2013-05-01 02:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nrootdirs > 8 || nrootdirs < 1)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "Invalid number of root directories specified\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SMARTFS_MULTI_ROOT_DIRS
|
2015-11-23 13:59:56 +01:00
|
|
|
ret = mksmartfs(fullpath, sectorsize, nrootdirs);
|
2013-05-01 02:03:04 +02:00
|
|
|
#else
|
2015-11-23 13:59:56 +01:00
|
|
|
ret = mksmartfs(fullpath, sectorsize);
|
2013-05-01 02:03:04 +02:00
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mksmartfs", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-06-12 01:47:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mv
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifdef NSH_HAVE_DIROPTS
|
2012-06-12 01:47:31 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_MV
|
|
|
|
int cmd_mv(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *oldpath;
|
|
|
|
char *newpath;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the full path to the old and new file paths */
|
|
|
|
|
|
|
|
oldpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
if (!oldpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
newpath = nsh_getfullpath(vtbl, argv[2]);
|
|
|
|
if (!newpath)
|
|
|
|
{
|
2015-08-31 02:28:04 +02:00
|
|
|
ret = ERROR;
|
2015-08-31 02:57:32 +02:00
|
|
|
goto errout_with_oldpath;
|
2012-06-12 01:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the mount */
|
|
|
|
|
|
|
|
ret = rename(oldpath, newpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "rename", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the file paths */
|
2015-08-31 02:57:32 +02:00
|
|
|
|
2012-06-12 01:47:31 +02:00
|
|
|
nsh_freefullpath(newpath);
|
2015-08-31 02:57:32 +02:00
|
|
|
|
|
|
|
errout_with_oldpath:
|
|
|
|
nsh_freefullpath(oldpath);
|
2012-06-12 01:47:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_rm
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifdef NSH_HAVE_DIROPTS
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_RM
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
ret = unlink(fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "unlink", NSH_ERRNO);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_rmdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifdef NSH_HAVE_DIROPTS
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_RMDIR
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_rmdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
ret = rmdir(fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "rmdir", NSH_ERRNO);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
nsh_freefullpath(fullpath);
|
|
|
|
}
|
2013-12-06 01:12:11 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_sh
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT)
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_SH
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
return nsh_script(vtbl, argv[0], argv[1]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2013-07-18 16:24:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cmp
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_CMP
|
|
|
|
int cmd_cmp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *path1 = NULL;
|
|
|
|
FAR char *path2 = NULL;
|
|
|
|
off_t total_read = 0;
|
|
|
|
int fd1 = -1;
|
|
|
|
int fd2 = -1;
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
/* Get the full path to the two files */
|
|
|
|
|
|
|
|
path1 = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
if (!path1)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
path2 = nsh_getfullpath(vtbl, argv[2]);
|
|
|
|
if (!path2)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
goto errout_with_path1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the files for reading */
|
|
|
|
|
|
|
|
fd1 = open(path1, O_RDONLY);
|
|
|
|
if (fd1 < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
goto errout_with_path2;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd2 = open(path2, O_RDONLY);
|
|
|
|
if (fd2 < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
goto errout_with_fd1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The loop until we hit the end of file or find a difference in the two
|
|
|
|
* files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char buf1[128];
|
|
|
|
char buf2[128];
|
|
|
|
|
|
|
|
/* Read the file data */
|
|
|
|
|
|
|
|
ssize_t nbytesread1 = read(fd1, buf1, sizeof(buf1));
|
|
|
|
ssize_t nbytesread2 = read(fd2, buf2, sizeof(buf2));
|
|
|
|
|
|
|
|
if (nbytesread1 < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
|
|
|
|
goto errout_with_fd2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytesread2 < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
|
|
|
|
goto errout_with_fd2;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_read += nbytesread1 > nbytesread2 ? nbytesread2 : nbytesread1;
|
|
|
|
|
|
|
|
/* Compare the file data */
|
|
|
|
|
|
|
|
if (nbytesread1 != nbytesread2 ||
|
|
|
|
memcmp(buf1, buf2, nbytesread1) != 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "files differ: byte %u\n", total_read);
|
|
|
|
goto errout_with_fd2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A partial read indicates the end of file (usually) */
|
|
|
|
|
2014-01-14 20:30:22 +01:00
|
|
|
if (nbytesread1 < (size_t)sizeof(buf1))
|
2013-07-18 16:24:29 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The files are the same, i.e., the end of file was encountered
|
|
|
|
* without finding any differences.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_fd2:
|
|
|
|
close(fd2);
|
|
|
|
errout_with_fd1:
|
|
|
|
close(fd1);
|
|
|
|
errout_with_path2:
|
|
|
|
nsh_freefullpath(path2);
|
|
|
|
errout_with_path1:
|
|
|
|
nsh_freefullpath(path1);
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|