2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
2008-01-01 00:52:53 +01:00
|
|
|
* examples/nsh/nsh_fscmds.c
|
2007-07-01 17:35:32 +02:00
|
|
|
*
|
2008-01-01 00:52:53 +01:00
|
|
|
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
|
2007-07-01 17:35:32 +02:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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.
|
2008-01-01 00:52:53 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-07-01 17:35:32 +02:00
|
|
|
* 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>
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <fcntl.h>
|
2008-08-10 20:42:55 +02:00
|
|
|
# if !defined(CONFIG_DISABLE_MOUNTPOINT)
|
2008-09-10 21:29:24 +02:00
|
|
|
# ifdef CONFIG_FS_READABLE /* Need at least one filesytem in configuration */
|
2008-08-10 20:42:55 +02:00
|
|
|
# include <sys/mount.h>
|
2008-09-10 21:29:24 +02:00
|
|
|
# include <nuttx/ramdisk.h>
|
|
|
|
# endif
|
|
|
|
# ifdef CONFIG_FS_FAT
|
2008-08-10 20:42:55 +02:00
|
|
|
# include <nuttx/mkfatfs.h>
|
|
|
|
# endif
|
2007-07-01 17:35:32 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
2007-07-01 20:23:03 +02:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
2008-09-07 15:42:55 +02:00
|
|
|
#include <debug.h>
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define LSFLAGS_SIZE 1
|
|
|
|
#define LSFLAGS_LONG 2
|
|
|
|
#define LSFLAGS_RECURSIVE 4
|
|
|
|
|
2007-07-01 20:23:03 +02:00
|
|
|
/* The size of the I/O buffer may be specified in the
|
|
|
|
* configs/<board-name>defconfig file -- provided that it is at least as
|
|
|
|
* large as PATH_MAX.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-08-12 01:07:26 +02:00
|
|
|
# ifdef CONFIG_EXAMPLES_NSH_FILEIOSIZE
|
|
|
|
# if CONFIG_EXAMPLES_NSH_FILEIOSIZE > (PATH_MAX + 1)
|
|
|
|
# define IOBUFFERSIZE CONFIG_EXAMPLES_NSH_FILEIOSIZE
|
2007-07-01 20:23:03 +02:00
|
|
|
# else
|
|
|
|
# define IOBUFFERSIZE (PATH_MAX + 1)
|
|
|
|
# endif
|
|
|
|
# else
|
2008-08-12 01:07:26 +02:00
|
|
|
# define IOBUFFERSIZE 1024
|
2007-07-01 20:23:03 +02:00
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# define IOBUFFERSIZE (PATH_MAX + 1)
|
|
|
|
#endif
|
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
typedef int (*direntry_handler_t)(FAR struct nsh_vtbl_s *, const char *, struct dirent *, void *);
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-12 01:07:26 +02:00
|
|
|
/* Common buffer for file I/O. Note the use of this common buffer precludes
|
|
|
|
* multiple copies of NSH running concurrently. It should be allocated per
|
2008-08-16 20:39:46 +02:00
|
|
|
* NSH instance and retained in the "vtbl" as is done for the telnet
|
2008-08-12 01:07:26 +02:00
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
|
2007-07-01 20:23:03 +02:00
|
|
|
static char g_iobuffer[IOBUFFERSIZE];
|
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: trim_dir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void trim_dir(char *arg)
|
|
|
|
{
|
2007-07-01 20:23:03 +02:00
|
|
|
/* Skip any trailing '/' characters (unless it is also the leading '/') */
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
int len = strlen(arg) - 1;
|
|
|
|
while (len > 0 && arg[len] == '/')
|
|
|
|
{
|
|
|
|
arg[len] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-23 17:16:10 +02:00
|
|
|
* Name: nsh_getdirpath
|
2007-07-01 17:35:32 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
static char *nsh_getdirpath(const char *path, const char *file)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2007-07-01 20:23:03 +02:00
|
|
|
/* Handle the case where all that is left is '/' */
|
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
if (strcmp(path, "/") == 0)
|
|
|
|
{
|
2007-07-01 20:23:03 +02:00
|
|
|
sprintf(g_iobuffer, "/%s", file);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-01 20:23:03 +02:00
|
|
|
sprintf(g_iobuffer, "%s/%s", path, file);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
2007-07-01 20:23:03 +02:00
|
|
|
g_iobuffer[PATH_MAX] = '\0';
|
|
|
|
return strdup(g_iobuffer);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: foreach_direntry
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-12-02 19:18:59 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-08-16 20:39:46 +02:00
|
|
|
static int foreach_direntry(FAR struct nsh_vtbl_s *vtbl, const char *cmd, const char *dirpath,
|
2007-07-01 17:35:32 +02:00
|
|
|
direntry_handler_t handler, void *pvarg)
|
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Trim trailing '/' from directory names */
|
|
|
|
|
2008-08-12 01:07:26 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_NSH_FULLPATH
|
2007-07-01 17:35:32 +02:00
|
|
|
trim_dir(arg);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Open the directory */
|
|
|
|
|
|
|
|
dirp = opendir(dirpath);
|
|
|
|
|
|
|
|
if (!dirp)
|
|
|
|
{
|
|
|
|
/* Failed to open the directory */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtnosuch, cmd, "directory", dirpath);
|
2007-07-01 17:35:32 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read each directory entry */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
struct dirent *entryp = readdir(dirp);
|
|
|
|
if (!entryp)
|
|
|
|
{
|
|
|
|
/* Finished with this directory */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the handler with this directory entry */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
if (handler(vtbl, dirpath, entryp, pvarg) < 0)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
|
|
|
/* The handler reported a problem */
|
|
|
|
|
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-12-02 19:18:59 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-09-11 23:54:23 +02: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);
|
|
|
|
}
|
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ls_handler
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-08-16 20:39:46 +02:00
|
|
|
static int ls_handler(FAR struct nsh_vtbl_s *vtbl, const char *dirpath, struct dirent *entryp, void *pvarg)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
|
|
|
unsigned int lsflags = (unsigned int)pvarg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if any options will require that we stat the file */
|
|
|
|
|
|
|
|
if ((lsflags & (LSFLAGS_SIZE|LSFLAGS_LONG)) != 0)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getdirpath(dirpath, entryp->d_name);
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/* Yes, stat the file */
|
|
|
|
|
|
|
|
ret = stat(fullpath, &buf);
|
|
|
|
free(fullpath);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, "ls", "stat", NSH_ERRNO);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, " %s", details);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((lsflags & LSFLAGS_SIZE) != 0)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "%8d", buf.st_size);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then provide the filename that is common to normal and verbose output */
|
|
|
|
|
2008-08-12 01:07:26 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_NSH_FULLPATH
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, " %s/%s", arg, entryp->d_name);
|
2007-07-01 17:35:32 +02:00
|
|
|
#else
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, " %s", entryp->d_name);
|
2007-07-01 17:35:32 +02:00
|
|
|
#endif
|
|
|
|
|
2008-09-11 23:54:23 +02:00
|
|
|
if (DIRENT_ISDIRECTORY(entryp->d_type) && !ls_specialdir(entryp->d_name))
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "/\n");
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "\n");
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ls_recursive
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-09-11 23:54:23 +02:00
|
|
|
static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath,
|
|
|
|
struct dirent *entryp, void *pvarg)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-17 20:59:50 +02:00
|
|
|
int ret = OK;
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-09-11 23:54:23 +02:00
|
|
|
/* Is this entry a directory (and not one of the special directories, . and ..)? */
|
|
|
|
|
|
|
|
if (DIRENT_ISDIRECTORY(entryp->d_type) && !ls_specialdir(entryp->d_name))
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
|
|
|
/* Yes.. */
|
|
|
|
|
|
|
|
char *newpath;
|
2008-08-23 17:16:10 +02:00
|
|
|
newpath = nsh_getdirpath(dirpath, entryp->d_name);
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/* List the directory contents */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "%s:\n", newpath);
|
2008-09-11 23:54:23 +02:00
|
|
|
|
|
|
|
/* Traverse the directory */
|
|
|
|
|
2008-08-17 20:59:50 +02:00
|
|
|
ret = foreach_direntry(vtbl, "ls", newpath, ls_handler, pvarg);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
/* Then recurse to list each directory within the directory */
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-17 20:59:50 +02:00
|
|
|
ret = foreach_direntry(vtbl, "ls", newpath, ls_recursive, pvarg);
|
|
|
|
free(newpath);
|
|
|
|
}
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_CAT
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-12 01:07:26 +02:00
|
|
|
char buffer[IOBUFFERSIZE];
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath;
|
2008-08-22 02:07:57 +02:00
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
int ret = OK;
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-22 02:07:57 +02:00
|
|
|
/* Loop for each file name on the command line */
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-22 02:07:57 +02:00
|
|
|
for (i = 1; i < argc && ret == OK; i++)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Get the fullpath to the file */
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
fullpath = nsh_getfullpath(vtbl, argv[i]);
|
|
|
|
if (fullpath)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Open the file for reading */
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
fd = open(fullpath, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* And just dump it byte for byte into stdout */
|
2008-08-22 02:07:57 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
for (;;)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
int nbytesread = read(fd, buffer, IOBUFFERSIZE);
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Check for read errors */
|
|
|
|
|
|
|
|
if (nbytesread < 0)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
/* EINTR is not an error */
|
2008-08-22 02:07:57 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
|
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 02:07:57 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Check for data successfully read */
|
2008-08-22 02:07:57 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
else if (nbytesread > 0)
|
2008-08-22 02:07:57 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
int nbyteswritten = 0;
|
2008-08-22 02:07:57 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
while (nbyteswritten < nbytesread)
|
|
|
|
{
|
|
|
|
int n = write(1, buffer, nbytesread);
|
|
|
|
if (n < 0)
|
2008-08-22 02:07:57 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
/* EINTR is not an error */
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO);
|
|
|
|
ret = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbyteswritten += n;
|
2008-08-22 02:07:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Otherwise, it is the end of file */
|
2008-08-22 02:07:57 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
(void)close(fd);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Free the allocated full path */
|
|
|
|
|
|
|
|
nsh_freefullpath(fullpath);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2007-07-01 20:23:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_cp
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_CP
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 20:23:03 +02:00
|
|
|
{
|
|
|
|
struct stat buf;
|
2008-08-23 17:16:10 +02:00
|
|
|
char *srcpath = NULL;
|
|
|
|
char *destpath = NULL;
|
|
|
|
char *allocpath = NULL;
|
2007-07-01 20:23:03 +02:00
|
|
|
int oflags = O_WRONLY|O_CREAT|O_TRUNC;
|
|
|
|
int rdfd;
|
|
|
|
int wrfd;
|
2008-08-17 20:59:50 +02:00
|
|
|
int ret = ERROR;
|
2007-07-01 20:23:03 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Get the full path to the source file */
|
|
|
|
|
|
|
|
srcpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
if (!srcpath)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2007-07-01 20:23:03 +02:00
|
|
|
/* Open the source file for reading */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
rdfd = open(srcpath, O_RDONLY);
|
2007-07-01 20:23:03 +02:00
|
|
|
if (rdfd < 0)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
2008-08-23 17:16:10 +02:00
|
|
|
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;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the destination is a directory */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
ret = stat(destpath, &buf);
|
2007-07-01 20:23:03 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
trim_dir(argv[2]);
|
|
|
|
|
|
|
|
/* Construct the full path to the new file */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
allocpath = nsh_getdirpath(argv[2], basename(argv[1]) );
|
|
|
|
if (!allocpath)
|
2007-07-01 20:23:03 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdoutofmemory, argv[0]);
|
2008-08-23 17:16:10 +02:00
|
|
|
goto errout_with_destpath;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Open then dest for writing */
|
|
|
|
|
|
|
|
nsh_freefullpath(destpath);
|
|
|
|
destpath = allocpath;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
else if (!S_ISREG(buf.st_mode))
|
|
|
|
{
|
|
|
|
/* Maybe it is a driver? */
|
|
|
|
|
|
|
|
oflags = O_WRONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now open the destination */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
wrfd = open(destpath, oflags, 0666);
|
2007-07-01 20:23:03 +02:00
|
|
|
if (wrfd < 0)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
2008-08-23 17:16:10 +02:00
|
|
|
goto errout_with_allocpath;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now copy the file */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int nbytesread;
|
|
|
|
int nbyteswritten;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nbytesread = read(rdfd, g_iobuffer, IOBUFFERSIZE);
|
|
|
|
if (nbytesread == 0)
|
|
|
|
{
|
|
|
|
/* End of file */
|
|
|
|
|
2008-08-17 20:59:50 +02:00
|
|
|
ret = OK;
|
2008-08-23 17:16:10 +02:00
|
|
|
goto errout_with_wrfd;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
else if (nbytesread < 0 && errno != EINTR)
|
|
|
|
{
|
|
|
|
/* Read error */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "read", NSH_ERRNO);
|
2008-08-23 17:16:10 +02:00
|
|
|
goto errout_with_wrfd;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread <= 0);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nbyteswritten = write(wrfd, g_iobuffer, nbytesread);
|
|
|
|
if (nbyteswritten >= 0)
|
|
|
|
{
|
|
|
|
nbytesread -= nbyteswritten;
|
|
|
|
}
|
|
|
|
else if (errno != EINTR)
|
|
|
|
{
|
|
|
|
/* Read error */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "write", NSH_ERRNO);
|
2008-08-23 17:16:10 +02:00
|
|
|
goto errout_with_wrfd;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread > 0);
|
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
errout_with_wrfd:
|
2007-07-01 20:23:03 +02:00
|
|
|
close(wrfd);
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
errout_with_allocpath:
|
|
|
|
if (allocpath)
|
2007-07-01 20:23:03 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
free(allocpath);
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
errout_with_destpath:
|
|
|
|
if (destpath && !allocpath)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(destpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_rdfd:
|
2007-07-01 20:23:03 +02:00
|
|
|
close(rdfd);
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
errout_with_srcpath:
|
|
|
|
if (srcpath)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(srcpath);
|
|
|
|
}
|
|
|
|
errout:
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 20:23:03 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 20:23:03 +02:00
|
|
|
|
2008-11-15 18:42:49 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_losetup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_MOUNTPOINT)
|
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_LOSETUP
|
|
|
|
int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *loopdev = NULL;
|
|
|
|
char *filepath = NULL;
|
|
|
|
boolean teardown = FALSE;
|
|
|
|
boolean readonly = FALSE;
|
|
|
|
off_t offset = 0;
|
|
|
|
int ret = ERROR;
|
|
|
|
int option;
|
|
|
|
|
|
|
|
/* 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]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the teardown operation */
|
|
|
|
|
|
|
|
if (teardown)
|
|
|
|
{
|
|
|
|
/* Tear down the loop device. */
|
|
|
|
|
|
|
|
ret = loteardown(loopdev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "loteardown", NSH_ERRNO_OF(-ret));
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set up the loop device */
|
|
|
|
|
|
|
|
ret = losetup(loopdev, filepath, 512, offset, readonly);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "losetup", NSH_ERRNO_OF(-ret));
|
|
|
|
goto errout_with_paths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free memory */
|
|
|
|
|
|
|
|
errout_with_paths:
|
|
|
|
if (loopdev)
|
|
|
|
{
|
|
|
|
free(loopdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filepath)
|
|
|
|
{
|
|
|
|
free(filepath);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ls
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_LS
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
const char *relpath;
|
2007-07-01 17:35:32 +02:00
|
|
|
unsigned int lsflags = 0;
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath;
|
2007-07-01 17:35:32 +02: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:
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* There may be one argument after the options */
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
if (optind + 1 < argc)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-23 17:16:10 +02:00
|
|
|
else if (optind >= argc)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
|
|
|
relpath = nsh_getcwd();
|
|
|
|
#else
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2008-08-23 17:16:10 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relpath = argv[optind];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the fullpath to the directory */
|
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, relpath);
|
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List the directory contents */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
nsh_output(vtbl, "%s:\n", fullpath);
|
|
|
|
ret = foreach_direntry(vtbl, "ls", fullpath, ls_handler, (void*)lsflags);
|
2007-07-01 17:35:32 +02:00
|
|
|
if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0)
|
|
|
|
{
|
|
|
|
/* Then recurse to list each directory within the directory */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
ret = foreach_direntry(vtbl, "ls", fullpath, ls_recursive, (void*)lsflags);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-23 17:16:10 +02:00
|
|
|
nsh_freefullpath(fullpath);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_WRITABLE)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_MKDIR
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_mkdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
ret = mkdir(fullpath, 0777);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mkdir", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
nsh_freefullpath(fullpath);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2008-08-10 20:42:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkfatfs
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_FAT)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_MKFATFS
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_mkfatfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2008-08-10 20:42:55 +02:00
|
|
|
{
|
|
|
|
struct fat_format_s fmt = FAT_FORMAT_INITIALIZER;
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
ret = mkfatfs(fullpath, &fmt);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mkfatfs", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
nsh_freefullpath(fullpath);
|
2008-08-10 20:42:55 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2008-08-10 20:42:55 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-08-10 20:42:55 +02:00
|
|
|
|
2008-08-10 21:16:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkfifo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-06 15:29:14 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_MKFIFO
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_mkfifo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2008-08-10 21:16:39 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
|
|
|
int ret = ERROR;
|
|
|
|
|
|
|
|
if (fullpath)
|
2008-08-10 21:16:39 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
ret = mkfifo(fullpath, 0777);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mkfifo", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
nsh_freefullpath(fullpath);
|
2008-08-10 21:16:39 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2008-08-10 21:16:39 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-08-10 21:16:39 +02:00
|
|
|
|
2008-09-06 15:29:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mkrd
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_WRITABLE)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_MKRD
|
2008-09-06 15:29:14 +02:00
|
|
|
int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *fmt;
|
|
|
|
ubyte *buffer;
|
|
|
|
uint32 nsectors;
|
|
|
|
int sectsize = 512;
|
|
|
|
int minor = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the mount options */
|
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = getopt(argc, argv, ":m:s:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'm':
|
|
|
|
minor = atoi(optarg);
|
|
|
|
if (minor < 0 || minor > 255)
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrange;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
sectsize = atoi(optarg);
|
|
|
|
if (minor < 0 || minor > 16384)
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrange;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
fmt = g_fmtarginvalid;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be exactly on parameter left on the command-line */
|
|
|
|
|
|
|
|
if (optind == argc-1)
|
|
|
|
{
|
|
|
|
nsectors = (uint32)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 = (ubyte*)malloc(sectsize * nsectors);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout_with_fmt;
|
|
|
|
}
|
2008-09-07 22:18:36 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_VERBOSE
|
|
|
|
memset(buffer, 0, sectsize * nsectors);
|
|
|
|
#endif
|
2008-09-07 15:42:55 +02:00
|
|
|
dbg("RAMDISK at %p\n", buffer);
|
2008-09-06 15:29:14 +02:00
|
|
|
|
|
|
|
/* Then register the ramdisk */
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
ret = ramdisk_register(minor, buffer, nsectors, sectsize, TRUE);
|
2008-09-06 15:29:14 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-09-10 21:29:24 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ramdisk_register", NSH_ERRNO_OF(-ret));
|
2008-09-06 15:29:14 +02:00
|
|
|
free(buffer);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout_with_fmt:
|
|
|
|
nsh_output(vtbl, fmt, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-09-06 15:29:14 +02:00
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_mount
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_READABLE)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_MOUNT
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_mount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *source;
|
|
|
|
char *target;
|
2007-07-01 17:35:32 +02:00
|
|
|
char *filesystem = 0;
|
2008-08-23 17:16:10 +02:00
|
|
|
int ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/* Get the mount options */
|
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = getopt(argc, argv, ":t:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 't':
|
|
|
|
filesystem = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There are two required arguments after the options */
|
|
|
|
|
|
|
|
if (optind + 2 < argc)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
else if (optind + 2 > argc)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* The source and target pathes might be relative to the current
|
|
|
|
* working directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
source = nsh_getfullpath(vtbl, argv[optind]);
|
|
|
|
if (!source)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = nsh_getfullpath(vtbl, argv[optind+1]);
|
|
|
|
if (!source)
|
|
|
|
{
|
|
|
|
nsh_freefullpath(source);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/* Perform the mount */
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
ret = mount(source, target, filesystem, 0, NULL);
|
|
|
|
if (ret < 0)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "mount", NSH_ERRNO);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
nsh_freefullpath(source);
|
|
|
|
nsh_freefullpath(target);
|
|
|
|
return ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
2007-07-01 21:22:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_rm
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_WRITABLE)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_RM
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 21:22:54 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
2008-08-29 17:36:02 +02:00
|
|
|
int ret = ERROR;
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
if (fullpath)
|
2007-07-01 21:22:54 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
ret = unlink(fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "unlink", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
nsh_freefullpath(fullpath);
|
2007-07-01 21:22:54 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 21:22:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 21:22:54 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-13 01:59:32 +02:00
|
|
|
* Name: cmd_rmdir
|
2007-07-01 21:22:54 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_WRITABLE)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_RMDIR
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_rmdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 21:22:54 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
2008-08-29 17:36:02 +02:00
|
|
|
int ret = ERROR;
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
if (fullpath)
|
2007-07-01 21:22:54 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
ret = rmdir(fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "rmdir", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
nsh_freefullpath(fullpath);
|
2007-07-01 21:22:54 +02:00
|
|
|
}
|
2008-08-17 20:59:50 +02:00
|
|
|
return ret;
|
2007-07-01 21:22:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 21:22:54 +02:00
|
|
|
|
2008-08-13 01:59:32 +02:00
|
|
|
/****************************************************************************
|
2008-09-11 23:54:23 +02:00
|
|
|
* Name: nsh_script
|
2008-08-13 01:59:32 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-02 02:37:18 +02:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_EXAMPLES_NSH_DISABLESCRIPT)
|
2008-09-11 23:54:23 +02:00
|
|
|
int nsh_script(FAR struct nsh_vtbl_s *vtbl, const char *cmd, const char *path)
|
2008-08-13 01:59:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath;
|
2008-08-13 01:59:32 +02:00
|
|
|
FILE *stream;
|
|
|
|
char *buffer;
|
|
|
|
char *pret;
|
2008-08-19 01:15:59 +02:00
|
|
|
int ret = ERROR;
|
2008-08-13 01:59:32 +02:00
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
/* The path to the script may be relative to the current working directory */
|
|
|
|
|
2008-09-11 23:54:23 +02:00
|
|
|
fullpath = nsh_getfullpath(vtbl, path);
|
2008-08-23 17:16:10 +02:00
|
|
|
if (!fullpath)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2008-08-13 01:59:32 +02:00
|
|
|
/* Get a reference to the common input buffer */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
buffer = nsh_linebuffer(vtbl);
|
2008-08-13 01:59:32 +02:00
|
|
|
if (buffer)
|
|
|
|
{
|
2008-08-19 01:15:59 +02:00
|
|
|
/* Open the file containing the script */
|
|
|
|
|
2008-08-23 17:16:10 +02:00
|
|
|
stream = fopen(fullpath, "r");
|
2008-08-13 01:59:32 +02:00
|
|
|
if (!stream)
|
|
|
|
{
|
2008-09-11 23:54:23 +02:00
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, cmd, "fopen", NSH_ERRNO);
|
2008-08-23 17:16:10 +02:00
|
|
|
nsh_freefullpath(fullpath);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2008-08-13 01:59:32 +02:00
|
|
|
}
|
|
|
|
|
2008-08-19 01:15:59 +02:00
|
|
|
/* Loop, processing each command line in the script file (or
|
|
|
|
* until an error occurs)
|
|
|
|
*/
|
|
|
|
|
2008-08-13 01:59:32 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Get the next line of input from the file*/
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
pret = fgets(buffer, CONFIG_EXAMPLES_NSH_LINELEN, stream);
|
|
|
|
if (pret)
|
|
|
|
{
|
|
|
|
/* Parse process the command. NOTE: this is recursive...
|
|
|
|
* we got to cmd_sh via a call to nsh_parse. So some
|
|
|
|
* considerable amount of stack may be used.
|
|
|
|
*/
|
|
|
|
|
2008-08-19 01:15:59 +02:00
|
|
|
ret = nsh_parse(vtbl, buffer);
|
2008-08-13 01:59:32 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-19 01:15:59 +02:00
|
|
|
while (pret && ret == OK);
|
2008-08-13 01:59:32 +02:00
|
|
|
fclose(stream);
|
|
|
|
}
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
nsh_freefullpath(fullpath);
|
2008-08-19 01:15:59 +02:00
|
|
|
return ret;
|
2008-08-13 01:59:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-11 23:54:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_sh
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_EXAMPLES_NSH_DISABLESCRIPT)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_SH
|
2008-09-11 23:54:23 +02:00
|
|
|
int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
return nsh_script(vtbl, argv[0], argv[1]);
|
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-09-11 23:54:23 +02:00
|
|
|
|
2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_umount
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-09-10 21:29:24 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_FS_READABLE)
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_UMOUNT
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_umount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
char *fullpath = nsh_getfullpath(vtbl, argv[1]);
|
2008-08-29 17:36:02 +02:00
|
|
|
int ret = ERROR;
|
2008-08-23 17:16:10 +02:00
|
|
|
|
|
|
|
if (fullpath)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-23 17:16:10 +02:00
|
|
|
/* Perform the umount */
|
|
|
|
|
|
|
|
ret = umount(fullpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "umount", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
nsh_freefullpath(fullpath);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-08-23 17:16:10 +02:00
|
|
|
return ret;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|