+
+/* ========================================================================== */
+
+
+typedef struct {
+ char *p_end; /* pointer to NULL at end of path */
+ char p_path[MAX_PATHLEN + 1]; /* pointer to the start of a path */
+} PATH_T;
+
+static inline void strip_trailing_slash(PATH_T *p)
+{
+ while (p->p_end > p->p_path && p->p_end[-1] == '/')
+ *--p->p_end = 0;
+}
+
+/*
+ * Move specified string into path. Convert "" to "." to handle BSD
+ * semantics for a null path. Strip trailing slashes.
+ */
+int
+path_set(PATH_T *p, char *string)
+{
+ if (strlen(string) > MAX_PATHLEN) {
+ //err("%s: name too long", string);
+ return(0);
+ }
+
+ (void)strcpy(p->p_path, string);
+ p->p_end = p->p_path + strlen(p->p_path);
+
+ if (p->p_path == p->p_end) {
+ *p->p_end++ = '.';
+ *p->p_end = 0;
+ }
+
+ strip_trailing_slash(p);
+ return(1);
+}
+
+#define R_FLAG (1<<0)
+#define I_FLAG (1<<1)
+#define N_FLAG (1<<2)
+#define F_FLAG (1<<3)
+#define P_FLAG (1<<4)
+#define V_FLAG (1<<5)
+
+command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+
+ FRESULT fr; /* Return value */
+ DIR dj; /* Directory search object */
+ FILINFO fno; /* File information */
+
+ uint8_t flags = 0;
+
+
+ /* reset getopt() */
+ optind = 0;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("Rrfip"))) != -1) {
+ switch (opt) {
+ case 'f':
+ flags &= I_FLAG;
+ break;
+ case 'i':
+ flags |= I_FLAG;
+ flags &= F_FLAG;
+ break;
+ case 'p':
+ flags |= P_FLAG;
+ break;
+ case 'R':
+ case 'r':
+ flags |= R_FLAG;
+ break;
+ case 'v':
+ flags |= V_FLAG;
+ break;
+ default:
+ return CMD_RET_USAGE;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 2)
+ return CMD_RET_USAGE;
+
+#if 0
+ /* consume last argument first. */
+ if (!path_set(&to, argv[--argc]))
+ exit(1);
+
+
+ if (flags & V_FLAG)
+ printf_P((PSTR("%s %s -> %s\n", badcp ? : "ERR:" : " ",
+ curr->fts_path, to.p_path);
+#endif
+
+/* Search a directory for objects and display it */
+
+
+ fr = f_findfirst(&dj, &fno, argv[1], argv[2]); /* Start to search for files */
+
+ while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */
+ printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
+ (fno.fattrib & AM_DIR) ? 'D' : '-',
+ (fno.fattrib & AM_RDO) ? 'R' : '-',
+ (fno.fattrib & AM_HID) ? 'H' : '-',
+ (fno.fattrib & AM_SYS) ? 'S' : '-',
+ (fno.fattrib & AM_ARC) ? 'A' : '-',
+ (fno.fdate >> 9) + 1980, (fno.fdate >> 5) & 15, fno.fdate & 31,
+ (fno.ftime >> 11), (fno.ftime >> 5) & 63,
+ fno.fsize, fno.fname);
+ fr = f_findnext(&dj, &fno); /* Search for next item */
+ }
+
+ f_closedir(&dj);
+
+ return CMD_RET_SUCCESS;
+}
+
+
+cmd_tbl_t cmd_tbl_fat[] = {
+CMD_TBL_ITEM(
+ stat, 2, CTBL_REPEAT, do_stat,
+ "Show logical drive status",
+ "dev"
+),
+CMD_TBL_ITEM(
+ pwd, 2, CTBL_REPEAT, do_pwd,
+ "Print name of current/working directory",
+ ""
+),
+CMD_TBL_ITEM(
+ cd, 2, CTBL_REPEAT, do_cd,
+ "Change the current/working directory.",
+ "path"
+),
+CMD_TBL_ITEM(
+ ls, 2, CTBL_REPEAT, do_ls,
+ "Directory listing",
+ "path"
+),
+CMD_TBL_ITEM(
+ tst, 2, CTBL_REPEAT, do_tst,
+ "FatFS test function",
+ "path"
+),
+CMD_TBL_ITEM(
+ load, 5, 0, do_rw,
+ "load binary file from a dos filesystem",
+ "<d:/path/filename> <addr> [bytes [pos]]\n"
+ " - Load binary file 'path/filename' on logical drive 'd'\n"
+ " to address 'addr' from dos filesystem.\n"
+ " 'pos' gives the file position to start loading from.\n"
+ " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
+ " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
+ " the load stops on end of file."
+),
+CMD_TBL_ITEM(
+ write, 4, 0, do_rw,
+ "write file into a dos filesystem",
+ "<d:/path/filename> <addr> <bytes>\n"
+ " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
+ " starting at address 'addr'.\n"
+),
+
+CMD_TBL_ITEM(
+ cp, CONFIG_SYS_MAXARGS, 0, do_cp,
+ "copy files",
+ "cp [-R] [-f | -i | -n] [-aprv] source_file target_file\n"
+ " - \n"
+),
+
+CMD_TBL_ITEM(
+ help, CONFIG_SYS_MAXARGS, CTBL_REPEAT, do_help,
+ "Print sub command description/usage",
+ "\n"
+ " - print brief description of all sub commands\n"
+ "fat help command ...\n"
+ " - print detailed usage of sub cmd 'command'"
+),
+
+/* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
+ {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help,
+ FSTR("Alias for 'help'"),
+#ifdef CONFIG_SYS_LONGHELP
+ FSTR(""),
+#endif /* CONFIG_SYS_LONGHELP */
+ NULL,
+#ifdef CONFIG_AUTO_COMPLETE
+ NULL,
+#endif
+},
+/* Mark end of table */
+CMD_TBL_END(cmd_tbl_fat)
+};
+
+
+command_ret_t do_fat(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+{
+ puts_P(PSTR("Huch?"));
+
+ return CMD_RET_USAGE;
+}