+ putchar('\b');
+ cnt = (cnt+1) & 3;
+ putchar(swirlchar[cnt]);
+ }
+}
+
+typedef struct {
+ char *p_end; /* pointer to NULL at end of path */
+ char p_path[PATH_MAX + 1]; /* pointer to the start of a path */
+} PATH_T;
+
+
+static char *path_skip_heading(char *p)
+{
+ if (isdigit(p[0]) && p[1] == ':') {
+ p += 2;
+ } else {
+ char *q = p;
+ if (*q++ == '.') {
+ if (*q == '.')
+ ++q;
+ if (*q == '\0' || *q == '/')
+ p = q;
+ }
+ return p;
+ }
+ if (*p == '/')
+ ++p;
+
+ return p;
+}
+
+static void strip_trailing_slash(PATH_T *p)
+{
+ char *beg = path_skip_heading(p->p_path);
+ char *end = p->p_end;
+
+ while (end > beg && end[-1] == '/')
+ *--end = '\0';
+
+ p->p_end =end;
+}
+
+/*
+ * Move specified string into path. Convert "" to "." to handle BSD
+ * semantics for a null path. Strip trailing slashes.
+ */
+static PATH_T *path_setup(char *string)
+{
+ if (strlen(string) > PATH_MAX) {
+ cmd_error(0, 0, PSTR("'%s': Name too long"), string);
+ return NULL;
+ }
+
+ PATH_T *p = (PATH_T *) malloc(sizeof *p);
+ if (p == NULL) {
+ cmd_error(0, ENOMEM, NULL);
+ return NULL;
+ }
+
+ strcpy(p->p_path, string);
+ size_t len = strlen(string);
+ if (len > 1 && p->p_path[1] == ':' && p->p_path[2] != '/') {
+ if (len < PATH_MAX) {
+ memmove(p->p_path+3, p->p_path+2, len-1);
+ p->p_path[2] = '/';
+ len += 1;
+ } else {
+ cmd_error(0, ENOMEM, NULL);
+ return NULL;
+ }
+ }
+
+ p->p_end = p->p_path + len;
+ if (p->p_path == p->p_end) {
+ *p->p_end++ = '.';
+ *p->p_end = '\0';
+ }
+
+ strip_trailing_slash(p);
+ return p;
+}
+
+/*
+ * 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;