Example #1
0
int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
  struct stat st;
  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;
    }

  /* See if it is a single file */

  if (stat(fullpath, &st) < 0)
    {
      nsh_output(vtbl, g_fmtcmdfailed, argv[0], "stat", NSH_ERRNO);
      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(vtbl, fullpath, NULL, (void*)lsflags);
    }
  else
    {
      /* 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;
}
Example #2
0
/****************************************************************************
 * 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;
}