+/*
+ * 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(PATH_MAX);
+ if (buf == NULL)
+ cmd_error(CMD_RET_FAILURE, ENOMEM, NULL);
+ *buf = '\0';
+ res = f_getcwd(buf, PATH_MAX); /* Get current directory path */
+
+ debug_fa("### f_getcwd(): buf: '%s', res: %d\n", buf, res);
+
+ if (res == FR_OK) {
+ puts(buf);
+ }
+ free(buf);
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ 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 = FR_OK;
+
+ if (argc < 2) {
+ arg = getenv_str(PSTR(ENV_HOME));
+ if (arg == NULL) {
+ cmd_error(CMD_RET_FAILURE, 0, PSTR("\"%S\" is not set"), PSTR(ENV_HOME));
+ }
+ } else
+ arg = argv[1];
+
+ PATH_T *path = path_setup(arg);
+ if (path == NULL)
+ return CMD_RET_FAILURE;
+
+ if (strlen(path->p_path) > 1 && path->p_path[1] == ':') {
+ res = f_chdrive(path->p_path);
+ debug_fa("### f_chdrive(): p_path: '%s', res: %d\n", path->p_path, res);
+ }
+ if (res == FR_OK) {
+ res = f_chdir(path->p_path);
+ debug_fa("### f_chdir(): p_path: '%s', res: %d\n", path->p_path, res);
+ }
+ free(path);
+ if (res != FR_OK)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ 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(PATH_MAX);
+ if (buf == NULL)
+ cmd_error(CMD_RET_FAILURE, ENOMEM, NULL);
+
+ if (argc < 2)
+ res = f_getcwd(buf, PATH_MAX); /* Get current directory path */
+ else
+ strncpy(buf, argv[1], PATH_MAX);
+
+ if (res == FR_OK)
+ res = f_opendir(&Dir, buf);
+ if (res != FR_OK) {
+ free(buf);
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+ }
+
+ 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);
+ }
+
+ free(buf);
+ if (res)
+ cmd_error(CMD_RET_FAILURE, res, NULL);
+
+ return CMD_RET_SUCCESS;
+}
+
+#if 0
+static
+FRESULT mkpath(TCHAR *path)
+{
+ /* TODO: */
+ (void) path;
+ FILINFO fd
+ TCHAR *p, *q;
+ FRESULT ret;
+
+ res = f_stat (path, &fd)
+
+ p = strchr(path, ':');
+ if (p == NULL || *++p == '\0' || *p++ != '/')
+ return FR_OK;
+
+ while ((q = strchr(p, '/')) != NULL) {
+ *q = '\0';
+ ret = f_mkdir(path);
+ *q = '/';
+ if (ret != FR_OK && ret != FR_EXIST)
+ return ret;
+ p = q + 1;
+ }
+
+ return FR_OK;
+}
+#endif
+