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.. */ char *newpath; newpath = nsh_getdirpath(dirpath, entryp->d_name); /* List the directory contents */ nsh_output(vtbl, "%s:\n", newpath); /* Traverse the directory */ ret = foreach_direntry(vtbl, "ls", newpath, ls_handler, pvarg); if (ret == 0) { /* Then recurse to list each directory within the directory */ ret = foreach_direntry(vtbl, "ls", newpath, ls_recursive, pvarg); free(newpath); } } return ret; }
static int ls_recursive(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.. */ char *newpath; newpath = get_dirpath(dirpath, entryp->d_name); /* List the directory contents */ FSCMD_OUTPUT("%s:\n", newpath); /* Traverse the directory */ ret = foreach_direntry("ls", newpath, ls_handler, pvarg); if (ret == 0) { /* * Then recurse to list each directory * within the directory */ ret = foreach_direntry("ls", newpath, ls_recursive, pvarg); } fscmd_free(newpath); } return ret; }
int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { const char *relpath; unsigned int lsflags = 0; char *fullpath; bool badarg = false; 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 */ if (optind + 1 < argc) { nsh_output(vtbl, g_fmttoomanyargs, argv[0]); return ERROR; } else if (optind >= argc) { #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; } /* List the directory contents */ nsh_output(vtbl, "%s:\n", fullpath); ret = foreach_direntry(vtbl, "ls", fullpath, ls_handler, (void*)lsflags); if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0) { /* Then recurse to list each directory within the directory */ ret = foreach_direntry(vtbl, "ls", fullpath, ls_recursive, (void*)lsflags); } nsh_freefullpath(fullpath); return ret; }
/**************************************************************************** * Name: tash_ls * * Description: * Show contents of directory of specific directory. * -R : shows contents recursively. * -l : shows size & attributes of contents * -s : size of contents * * Usage: * ls [-lRs] <directory> ****************************************************************************/ static int tash_ls(int argc, char **args) { struct stat st; const char *relpath; unsigned int lsflags = 0; char *fullpath; int ret; int option; int badarg = false; optind = -1; while ((option = getopt(argc, args, "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: badarg = true; break; } } if (badarg) { FSCMD_OUTPUT(INVALID_ARGS " : [-lRs] <dir-path>\n", args[0]); return 0; } if (optind + 1 < argc) { FSCMD_OUTPUT(TOO_MANY_ARGS, args[0]); return ERROR; } else if (optind >= argc) { relpath = getwd(PWD); } else { relpath = args[optind]; } fullpath = get_fullpath(relpath); if (!fullpath) { return ERROR; } if (stat(fullpath, &st) < 0) { FSCMD_OUTPUT(CMD_FAILED, args[0], "stat"); ret = ERROR; } else if (!S_ISDIR(st.st_mode)) { /* * Pass a null dirent to ls_handler to signify * that this is a single file */ ret = ls_handler(fullpath, NULL, (void *)lsflags); } else { /* List the directory contents */ FSCMD_OUTPUT("%s:\n", fullpath); ret = foreach_direntry("ls", fullpath, ls_handler, (void *)lsflags); if (ret == OK && (lsflags & LSFLAGS_RECURSIVE) != 0) { /* * Then recurse to list each directory * within the directory */ ret = foreach_direntry("ls", fullpath, ls_recursive, (void *)lsflags); } } fscmd_free(fullpath); return ret; }