+ 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;
+ char *buf;
+
+ buf = (char *) malloc(BUFFER_SIZE);
+ if (buf == NULL) {
+ printf_P(PSTR("pwd: Out of Memory!\n"));
+ free(buf);
+ return CMD_RET_FAILURE;
+ }
+
+ res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */
+
+ if (!res) {
+ puts(buf);
+ }
+ free(buf);
+ if (res) {
+ put_rc(res);
+ return CMD_RET_FAILURE;
+ }
+ 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[])
+{
+ char *arg;
+ FRESULT res = 0;
+
+ if (argc < 2) {
+ arg = getenv_str(PSTR(ENV_HOME));
+ if (arg == NULL) {
+ printf_P(PSTR("%s: \"%S\" is not set\n"), argv[0], PSTR(ENV_HOME));
+ return CMD_RET_FAILURE;
+ }
+ } else
+ arg = argv[1];
+
+ if (arg[1] == ':')
+ res = f_chdrive(arg);
+debug_fa("### f_chdrive(): arg: '%s', res: %d\n", arg, res);
+ if (res == FR_OK) {
+ if (arg[2] == '\0') {
+ arg[0] = '/';
+ arg[1] = '\0';
+ }
+ res = f_chdir(arg);
+ }
+debug_fa("### f_chdir(): arg: '%s', res: %d\n", arg, res);
+ if (res != FR_OK) {
+ put_rc(res);
+ return CMD_RET_FAILURE;
+ }
+ return CMD_RET_SUCCESS;
+}
+
+
+/*
+ * 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;
+
+
+ buf = (char *) malloc(BUFFER_SIZE);
+ if (buf == NULL) {
+ printf_P(PSTR("pwd: Out of Memory!\n"));
+ free(buf);
+ return CMD_RET_FAILURE;
+ }
+
+ if (argc < 2)
+ res = f_getcwd(buf, BUFFER_SIZE); /* Get current directory path */
+ else
+ strncpy(buf, argv[1], BUFFER_SIZE);
+
+ if (res == FR_OK)
+ res = f_opendir(&Dir, buf);
+ if (res != FR_OK) {
+ free(buf);
+ put_rc(res);
+ return CMD_RET_FAILURE;
+ }
+
+ p1 = s1 = s2 = 0;
+ for(;;) {
+ res = f_readdir(&Dir, &Finfo);
+ if ((res != FR_OK) || !Finfo.fname[0])
+ break;
+ if (Finfo.fattrib & AM_DIR) {
+ s2++;
+ } else {
+ s1++; p1 += Finfo.fsize;
+ }
+ printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
+ (Finfo.fattrib & AM_DIR) ? 'D' : '-',
+ (Finfo.fattrib & AM_RDO) ? 'R' : '-',
+ (Finfo.fattrib & AM_HID) ? 'H' : '-',
+ (Finfo.fattrib & AM_SYS) ? 'S' : '-',
+ (Finfo.fattrib & AM_ARC) ? 'A' : '-',
+ (Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
+ (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63,
+ Finfo.fsize, Finfo.fname);
+ if (check_abort())
+ break;
+ }
+
+ if (res == FR_OK) {
+ printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
+ if (f_getfree(buf, (DWORD*)&p1, &fs) == FR_OK)
+ printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);