Add 'ls' command to nsh
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@63 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
parent
fecf0431f3
commit
d70708a4b0
|
@ -35,3 +35,5 @@
|
|||
0.1.2 2007-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
|
||||
* Add dirent.h, opendir(), readdir(), closedir(), etc.
|
||||
* Added 'ls' command to nsh
|
||||
|
||||
|
|
|
@ -33,16 +33,13 @@
|
|||
*
|
||||
************************************************************/
|
||||
|
||||
/************************************************************
|
||||
* Compilation Switches
|
||||
************************************************************/
|
||||
|
||||
/************************************************************
|
||||
* Included Files
|
||||
************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <sched.h>
|
||||
|
||||
|
@ -51,12 +48,13 @@
|
|||
************************************************************/
|
||||
|
||||
#define CONFIG_NSH_LINE_SIZE 80
|
||||
#undef CONFIG_FULL_PATH
|
||||
|
||||
/************************************************************
|
||||
* Private Types
|
||||
************************************************************/
|
||||
|
||||
typedef void (*cmd_t)(const char *cmd, const char *arg);
|
||||
typedef void (*cmd_t)(const char *cmd, char *arg);
|
||||
typedef void (*exec_t)(void);
|
||||
|
||||
struct cmdmap_s
|
||||
|
@ -70,11 +68,11 @@ struct cmdmap_s
|
|||
* Private Function Prototypes
|
||||
************************************************************/
|
||||
|
||||
static void cmd_echo(const char *cmd, const char *arg);
|
||||
static void cmd_exec(const char *cmd, const char *arg);
|
||||
static void cmd_help(const char *cmd, const char *arg);
|
||||
static void cmd_ls(const char *cmd, const char *arg);
|
||||
static void cmd_ps(const char *cmd, const char *arg);
|
||||
static void cmd_echo(const char *cmd, char *arg);
|
||||
static void cmd_exec(const char *cmd, char *arg);
|
||||
static void cmd_help(const char *cmd, char *arg);
|
||||
static void cmd_ls(const char *cmd, char *arg);
|
||||
static void cmd_ps(const char *cmd, char *arg);
|
||||
|
||||
/************************************************************
|
||||
* Private Data
|
||||
|
@ -114,11 +112,16 @@ static const char g_fmtargrequired[] = "nsh: %s: argument required\n";
|
|||
static const char g_fmtarginvalid[] = "nsh: %s: argument invalid\n";
|
||||
static const char g_fmtcmdnotfound[] = "nsh: %s: command not found\n";
|
||||
static const char g_fmtcmdnotimpl[] = "nsh: %s: command not implemented\n";
|
||||
static const char g_fmtnosuch[] = "nsh: %s: no such %s: %s\n";
|
||||
|
||||
/************************************************************
|
||||
* Private Functions
|
||||
************************************************************/
|
||||
|
||||
/************************************************************
|
||||
* Name: trim_arg
|
||||
************************************************************/
|
||||
|
||||
static char *trim_arg(char *arg)
|
||||
{
|
||||
if (arg)
|
||||
|
@ -129,7 +132,7 @@ static char *trim_arg(char *arg)
|
|||
|
||||
while (strchr(" \t", *arg) != NULL) arg++;
|
||||
|
||||
/* Skip any leading white space */
|
||||
/* Skip any trailing white space */
|
||||
|
||||
len = strlen(arg);
|
||||
if (len > 0)
|
||||
|
@ -148,11 +151,29 @@ static char *trim_arg(char *arg)
|
|||
return arg;
|
||||
}
|
||||
|
||||
/************************************************************
|
||||
* Name: trim_dir
|
||||
************************************************************/
|
||||
|
||||
#ifdef CONFIG_FULL_PATH
|
||||
void trim_dir(char *arg)
|
||||
{
|
||||
/* Skip any '/' characters white space */
|
||||
|
||||
int len = strlen(arg) - 1;
|
||||
while (len > 0 && arg[len] == '/')
|
||||
{
|
||||
arg[len] = '\0';
|
||||
len--;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/************************************************************
|
||||
* Name: cmd_echo
|
||||
************************************************************/
|
||||
|
||||
static void cmd_echo(const char *cmd, const char *arg)
|
||||
static void cmd_echo(const char *cmd, char *arg)
|
||||
{
|
||||
/* Echo the rest of the line */
|
||||
|
||||
|
@ -163,7 +184,7 @@ static void cmd_echo(const char *cmd, const char *arg)
|
|||
* Name: cmd_exec
|
||||
************************************************************/
|
||||
|
||||
static void cmd_exec(const char *cmd, const char *arg)
|
||||
static void cmd_exec(const char *cmd, char *arg)
|
||||
{
|
||||
char *endptr;
|
||||
long addr;
|
||||
|
@ -189,7 +210,7 @@ static void cmd_exec(const char *cmd, const char *arg)
|
|||
* Name: cmd_help
|
||||
************************************************************/
|
||||
|
||||
static void cmd_help(const char *cmd, const char *arg)
|
||||
static void cmd_help(const char *cmd, char *arg)
|
||||
{
|
||||
const struct cmdmap_s *ptr;
|
||||
|
||||
|
@ -211,9 +232,47 @@ static void cmd_help(const char *cmd, const char *arg)
|
|||
* Name: cmd_ls
|
||||
************************************************************/
|
||||
|
||||
static void cmd_ls(const char *cmd, const char *arg)
|
||||
static void cmd_ls(const char *cmd, char *arg)
|
||||
{
|
||||
printf(g_fmtcmdnotimpl, cmd);
|
||||
DIR *dirp;
|
||||
|
||||
#ifdef CONFIG_FULL_PATH
|
||||
trim_dir(arg);
|
||||
#endif
|
||||
dirp = opendir(arg);
|
||||
|
||||
if (!dirp)
|
||||
{
|
||||
printf(g_fmtnosuch, cmd, "directory", arg);
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
struct dirent *entryp = readdir(dirp);
|
||||
if (!entryp)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (DIRENT_ISFILE(entryp->d_type))
|
||||
{
|
||||
#ifdef CONFIG_FULL_PATH
|
||||
printf(" %s/%s\n", arg, entryp->d_name);
|
||||
#else
|
||||
printf(" %s\n", entryp->d_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (DIRENT_ISDIRECTORY(entryp->d_type))
|
||||
{
|
||||
#ifdef CONFIG_FULL_PATH
|
||||
printf(" %s/%s/\n", arg, entryp->d_name);
|
||||
#else
|
||||
printf(" %s/\n", entryp->d_name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
closedir(dirp);
|
||||
}
|
||||
|
||||
/************************************************************
|
||||
|
@ -251,7 +310,7 @@ static void ps_task(FAR _TCB *tcb, FAR void *arg)
|
|||
* Name: cmd_ps
|
||||
************************************************************/
|
||||
|
||||
static void cmd_ps(const char *cmd, const char *arg)
|
||||
static void cmd_ps(const char *cmd, char *arg)
|
||||
{
|
||||
printf("PID PRI SCHD TYPE NP STATE NAME\n");
|
||||
sched_foreach(ps_task, NULL);
|
||||
|
@ -261,7 +320,7 @@ static void cmd_ps(const char *cmd, const char *arg)
|
|||
* Name: cmd_unrecognized
|
||||
************************************************************/
|
||||
|
||||
static void cmd_unrecognized(const char *cmd, const char *arg)
|
||||
static void cmd_unrecognized(const char *cmd, char *arg)
|
||||
{
|
||||
printf(g_fmtcmdnotfound, cmd);
|
||||
}
|
||||
|
|
|
@ -79,7 +79,18 @@ FAR struct inode *inode_finddir(const char *path)
|
|||
FAR struct inode *node;
|
||||
FAR struct inode *child = NULL;
|
||||
|
||||
if (!*path || path[0] != '/')
|
||||
/* If we are given 'nothing' then we will interpret this as
|
||||
* request for the root inode.
|
||||
*/
|
||||
|
||||
if (!path || *path == 0 || strcmp(path, "/") == 0)
|
||||
{
|
||||
return root_inode;
|
||||
}
|
||||
|
||||
/* We don't know what to do with relative pathes */
|
||||
|
||||
if (*path != '/')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
@ -87,6 +98,9 @@ FAR struct inode *inode_finddir(const char *path)
|
|||
/* Find the node matching the path. */
|
||||
|
||||
inode_semtake();
|
||||
|
||||
/* Handle some special cases */
|
||||
|
||||
node = inode_search(&path, (FAR void*)NULL, (FAR void*)NULL);
|
||||
if (node)
|
||||
{
|
||||
|
|
|
@ -105,7 +105,7 @@ FAR DIR *opendir(const char *path)
|
|||
* container.
|
||||
*/
|
||||
|
||||
dir = (FAR struct internal_dir_s *)zmalloc(sizeof(struct internal_dir_s *));
|
||||
dir = (FAR struct internal_dir_s *)zalloc(sizeof(struct internal_dir_s));
|
||||
if (!dir)
|
||||
{
|
||||
/* Insufficient memory to complete the operation.*/
|
||||
|
|
|
@ -115,7 +115,7 @@ FAR struct dirent *readdir(DIR *dirp)
|
|||
|
||||
if (idir->next->i_child || !idir->next->i_ops)
|
||||
{
|
||||
idir->dir.d_type |= DTYPE_FILE;
|
||||
idir->dir.d_type |= DTYPE_DIRECTORY;
|
||||
}
|
||||
|
||||
/* Now get the inode to vist next time that readdir() is called */
|
||||
|
|
|
@ -56,8 +56,8 @@
|
|||
#define DTYPE_FILE 0x01
|
||||
#define DTYPE_DIRECTORY 0x02
|
||||
|
||||
#define DIRENT_ISFILE(dtype) (((dtype) & DTYPE_FILE) != )
|
||||
#define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != )
|
||||
#define DIRENT_ISFILE(dtype) (((dtype) & DTYPE_FILE) != 0 )
|
||||
#define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != 0 )
|
||||
|
||||
/************************************************************
|
||||
* Public Type Definitions
|
||||
|
|
Loading…
Reference in New Issue