Add 'ls' command to nsh

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@63 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2007-03-14 23:34:37 +00:00
parent 599f8b4682
commit bfee6e1ca9
6 changed files with 98 additions and 23 deletions
+2
View File
@@ -35,3 +35,5 @@
0.1.2 2007-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> 0.1.2 2007-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
* Add dirent.h, opendir(), readdir(), closedir(), etc. * Add dirent.h, opendir(), readdir(), closedir(), etc.
* Added 'ls' command to nsh
+77 -18
View File
@@ -33,16 +33,13 @@
* *
************************************************************/ ************************************************************/
/************************************************************
* Compilation Switches
************************************************************/
/************************************************************ /************************************************************
* Included Files * Included Files
************************************************************/ ************************************************************/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h>
#include <string.h> #include <string.h>
#include <sched.h> #include <sched.h>
@@ -51,12 +48,13 @@
************************************************************/ ************************************************************/
#define CONFIG_NSH_LINE_SIZE 80 #define CONFIG_NSH_LINE_SIZE 80
#undef CONFIG_FULL_PATH
/************************************************************ /************************************************************
* Private Types * 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); typedef void (*exec_t)(void);
struct cmdmap_s struct cmdmap_s
@@ -70,11 +68,11 @@ struct cmdmap_s
* Private Function Prototypes * Private Function Prototypes
************************************************************/ ************************************************************/
static void cmd_echo(const char *cmd, const char *arg); static void cmd_echo(const char *cmd, char *arg);
static void cmd_exec(const char *cmd, const char *arg); static void cmd_exec(const char *cmd, char *arg);
static void cmd_help(const char *cmd, const char *arg); static void cmd_help(const char *cmd, char *arg);
static void cmd_ls(const char *cmd, const char *arg); static void cmd_ls(const char *cmd, char *arg);
static void cmd_ps(const char *cmd, const char *arg); static void cmd_ps(const char *cmd, char *arg);
/************************************************************ /************************************************************
* Private Data * 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_fmtarginvalid[] = "nsh: %s: argument invalid\n";
static const char g_fmtcmdnotfound[] = "nsh: %s: command not found\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_fmtcmdnotimpl[] = "nsh: %s: command not implemented\n";
static const char g_fmtnosuch[] = "nsh: %s: no such %s: %s\n";
/************************************************************ /************************************************************
* Private Functions * Private Functions
************************************************************/ ************************************************************/
/************************************************************
* Name: trim_arg
************************************************************/
static char *trim_arg(char *arg) static char *trim_arg(char *arg)
{ {
if (arg) if (arg)
@@ -129,7 +132,7 @@ static char *trim_arg(char *arg)
while (strchr(" \t", *arg) != NULL) arg++; while (strchr(" \t", *arg) != NULL) arg++;
/* Skip any leading white space */ /* Skip any trailing white space */
len = strlen(arg); len = strlen(arg);
if (len > 0) if (len > 0)
@@ -148,11 +151,29 @@ static char *trim_arg(char *arg)
return 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 * 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 */ /* Echo the rest of the line */
@@ -163,7 +184,7 @@ static void cmd_echo(const char *cmd, const char *arg)
* Name: cmd_exec * Name: cmd_exec
************************************************************/ ************************************************************/
static void cmd_exec(const char *cmd, const char *arg) static void cmd_exec(const char *cmd, char *arg)
{ {
char *endptr; char *endptr;
long addr; long addr;
@@ -189,7 +210,7 @@ static void cmd_exec(const char *cmd, const char *arg)
* Name: cmd_help * 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; const struct cmdmap_s *ptr;
@@ -211,9 +232,47 @@ static void cmd_help(const char *cmd, const char *arg)
* Name: cmd_ls * 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 * 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"); printf("PID PRI SCHD TYPE NP STATE NAME\n");
sched_foreach(ps_task, NULL); sched_foreach(ps_task, NULL);
@@ -261,7 +320,7 @@ static void cmd_ps(const char *cmd, const char *arg)
* Name: cmd_unrecognized * 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); printf(g_fmtcmdnotfound, cmd);
} }
+15 -1
View File
@@ -79,7 +79,18 @@ FAR struct inode *inode_finddir(const char *path)
FAR struct inode *node; FAR struct inode *node;
FAR struct inode *child = NULL; 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; return NULL;
} }
@@ -87,6 +98,9 @@ FAR struct inode *inode_finddir(const char *path)
/* Find the node matching the path. */ /* Find the node matching the path. */
inode_semtake(); inode_semtake();
/* Handle some special cases */
node = inode_search(&path, (FAR void*)NULL, (FAR void*)NULL); node = inode_search(&path, (FAR void*)NULL, (FAR void*)NULL);
if (node) if (node)
{ {
+1 -1
View File
@@ -105,7 +105,7 @@ FAR DIR *opendir(const char *path)
* container. * 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) if (!dir)
{ {
/* Insufficient memory to complete the operation.*/ /* Insufficient memory to complete the operation.*/
+1 -1
View File
@@ -115,7 +115,7 @@ FAR struct dirent *readdir(DIR *dirp)
if (idir->next->i_child || !idir->next->i_ops) 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 */ /* Now get the inode to vist next time that readdir() is called */
+2 -2
View File
@@ -56,8 +56,8 @@
#define DTYPE_FILE 0x01 #define DTYPE_FILE 0x01
#define DTYPE_DIRECTORY 0x02 #define DTYPE_DIRECTORY 0x02
#define DIRENT_ISFILE(dtype) (((dtype) & DTYPE_FILE) != ) #define DIRENT_ISFILE(dtype) (((dtype) & DTYPE_FILE) != 0 )
#define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != ) #define DIRENT_ISDIRECTORY(dtype) (((dtype) & DTYPE_DIRECTORY) != 0 )
/************************************************************ /************************************************************
* Public Type Definitions * Public Type Definitions