diff options
-rw-r--r-- | nuttx/ChangeLog | 2 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh_main.c | 95 | ||||
-rw-r--r-- | nuttx/fs/fs_inodefinddir.c | 16 | ||||
-rw-r--r-- | nuttx/fs/fs_opendir.c | 2 | ||||
-rw-r--r-- | nuttx/fs/fs_readdir.c | 2 | ||||
-rw-r--r-- | nuttx/include/dirent.h | 4 |
6 files changed, 98 insertions, 23 deletions
diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 402a824b3..31a4ecc3f 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -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 + diff --git a/nuttx/examples/nsh/nsh_main.c b/nuttx/examples/nsh/nsh_main.c index 4135b2828..e9c8466c3 100644 --- a/nuttx/examples/nsh/nsh_main.c +++ b/nuttx/examples/nsh/nsh_main.c @@ -34,15 +34,12 @@ ************************************************************/ /************************************************************ - * 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) @@ -149,10 +152,28 @@ static char *trim_arg(char *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); } diff --git a/nuttx/fs/fs_inodefinddir.c b/nuttx/fs/fs_inodefinddir.c index 597d81096..bb27dd7b4 100644 --- a/nuttx/fs/fs_inodefinddir.c +++ b/nuttx/fs/fs_inodefinddir.c @@ -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) { diff --git a/nuttx/fs/fs_opendir.c b/nuttx/fs/fs_opendir.c index 810b365fe..b02d97bf7 100644 --- a/nuttx/fs/fs_opendir.c +++ b/nuttx/fs/fs_opendir.c @@ -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.*/ diff --git a/nuttx/fs/fs_readdir.c b/nuttx/fs/fs_readdir.c index 40f4f1747..034770c80 100644 --- a/nuttx/fs/fs_readdir.c +++ b/nuttx/fs/fs_readdir.c @@ -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 */ diff --git a/nuttx/include/dirent.h b/nuttx/include/dirent.h index 160dd04ea..e0d9fca68 100644 --- a/nuttx/include/dirent.h +++ b/nuttx/include/dirent.h @@ -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 |