+ putchar('\b');
+ cnt = (cnt+1) & 3;
+ putchar(swirlchar[cnt]);
+ }
+}
+
+/*
+ * pwd - Print current directory of the current drive.
+ *
+ */
+command_ret_t do_pwd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
+{
+ FRESULT res;
+ TCHAR buf[PATH_MAX];
+
+ res = f_getcwd(buf, PATH_MAX); /* Get current directory path */
+
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ puts(buf);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+/*
+ * cd - Change the current/working directory.
+ *
+ */
+command_ret_t do_cd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ TCHAR *path;
+ FRESULT res = FR_OK;
+
+ if (argc < 2) {
+ path = getenv_str(PSTR(ENV_HOME));
+ if (path == NULL) {
+ cmd_error(CMD_RET_FAILURE, 0, PSTR("\"%S\" not set"), PSTR(ENV_HOME));
+ }
+ } else
+ path = argv[1];
+
+ if (strlen(path) > 1 && path[1] == ':')
+ res = f_chdrive(path);
+
+ if (res == FR_OK)
+ res = f_chdir(path);
+
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+static
+void print_dirent(FILINFO *f)
+{
+ printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
+ (f->fattrib & AM_DIR) ? 'D' : '-',
+ (f->fattrib & AM_RDO) ? 'R' : '-',
+ (f->fattrib & AM_HID) ? 'H' : '-',
+ (f->fattrib & AM_SYS) ? 'S' : '-',
+ (f->fattrib & AM_ARC) ? 'A' : '-',
+ (f->fdate >> 9) + 1980, (f->fdate >> 5) & 15, f->fdate & 31,
+ (f->ftime >> 11), (f->ftime >> 5) & 63,
+ f->fsize, f->fname);
+}
+
+char *path_split(char *p)
+{
+ if (isdigit(p[0]) && (p[1] == ':'))
+ p += 2;
+
+ char *ps = strrchr(p, '/');
+ if (ps) {
+ if (ps == p) {
+ ++ps;
+ memmove(ps+1, ps, strlen(ps)+1);
+ }
+ *ps++ = '\0';
+ }
+
+ return ps;
+}
+
+/*
+ * ls path - Directory listing
+ *
+ */
+command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ FATFS *fs;
+ DIR dir; /* Directory object */
+ FILINFO finfo;
+ unsigned long p1;
+ unsigned int s1, s2;
+ FRESULT res = FR_OK;
+ char buf[PATH_MAX];
+ char *path = buf;
+ char *pattern;
+
+
+ memset(buf, 0, PATH_MAX);
+ if (argc < 2) {
+ buf[0] = '.';
+ } else {
+ strncpy(path, argv[1], PATH_MAX-1);
+ }
+ pattern = path_split(path);
+ debug_ls("### path, pattern: '%s', '%s'\n", path[0] ? path : "<NULL>", pattern ? pattern : "<NULL>");
+
+ if (pattern == NULL) {
+ res = f_opendir(&dir, path);
+ if (res == FR_NO_PATH || res == FR_INVALID_NAME) {
+ pattern = path;
+ path = ".";
+ } else if (res != FR_OK) {
+ cmd_error(CMD_RET_FAILURE, res, PSTR("'%s'"), path);
+ }
+ }
+ debug_ls("### path, pattern: '%s', '%s'\n", path, pattern ? pattern : "<NULL>");
+
+ if (pattern == NULL || *pattern == '\0')
+ pattern = "*";
+
+ debug_ls("### path, pattern: '%s', '%s'\n", path, pattern ? pattern : "<NULL>");
+
+ res = f_findfirst(&dir, &finfo, path, pattern);
+
+ p1 = s1 = s2 = 0;
+ for(;;) {
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+ if (!finfo.fname[0])
+ break;
+ if (finfo.fattrib & AM_DIR) {
+ s2++;
+ } else {
+ s1++; p1 += finfo.fsize;
+ }
+ print_dirent(&finfo);
+ if (check_abort())
+ break;
+ res = f_findnext(&dir, &finfo);
+ }
+
+ if (res == FR_OK) {
+ printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
+ if (f_getfree(path, (DWORD*)&p1, &fs) == FR_OK)
+ printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
+ }
+
+ if (res)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+/*
+ * tst path - for debugging: test access with different functions
+ *
+ */
+command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+ DIR Dir; /* Directory object */
+ FILINFO finfo;
+ FRESULT res = FR_OK;
+ char *path = "";
+ char *pattern = "*";
+
+ printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO));
+
+ char * buf = (char *) malloc(BUFFER_SIZE);
+ if (buf == NULL) {
+ cmd_error(CMD_RET_FAILURE, ENOMEM, NULL);
+ }
+ res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */
+
+ if (!res) {
+ printf_P(PSTR("cwd: '%s'\n"), buf);
+ }
+ free(buf);
+ if (res)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ if (argc > 1)
+ path = argv[1];
+ if (argc > 2)
+ pattern = argv[2];
+
+ printf_P(PSTR("arg: '%s' '%s'\n"), path, pattern);
+ printf_P(PSTR("==== f_stat: "));
+ res = f_stat(path, &finfo);
+ cmd_error(0, res, NULL);
+ if (res == FR_OK) {
+ print_dirent(&finfo);