}
-uint8_t flags = 0;
+static uint8_t flags;
PATH_T *from;
PATH_T *to;
-#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)
+#define R_FLAG (1<<0) // copy directories recursively
+#define I_FLAG (1<<1) // prompt before overwrite (overrides a previous -n option)
+#define N_FLAG (1<<2) // do not overwrite an existing file (overrides a previous -i option)
+#define F_FLAG (1<<3) // if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
+#define P_FLAG (1<<4) // preserve attributes and timestamps
+#define V_FLAG (1<<5) // explain what is being done
static void
setfile(FILINFO *fs, FIL *fd)
*/
if (!dne) {
if (flags & I_FLAG) {
- int checkch, ch;
-
printf_P(PSTR("overwrite %s? "), to->p_path);
- checkch = ch = getchar();
- while (ch != '\n' && ch != EOF)
- ch = getchar();
- if (checkch != 'y') {
+ if (!confirm_yes()) {
f_close(&from_fd);
return;
}
char *old_to;
+ uint8_t tflags = 0;
command_ret = CMD_RET_SUCCESS;
/* reset getopt() */
optind = 0;
int opt;
- while ((opt = getopt(argc, argv, PSTR("Rrfip"))) != -1) {
+ while ((opt = getopt(argc, argv, PSTR("rfipv"))) != -1) {
switch (opt) {
case 'f':
- flags &= I_FLAG;
+ tflags &= ~I_FLAG;
+ tflags |= F_FLAG;
break;
case 'i':
- flags |= I_FLAG;
- flags &= F_FLAG;
+ tflags &= ~F_FLAG;
+ tflags |= I_FLAG;
break;
case 'p':
- flags |= P_FLAG;
+ tflags |= P_FLAG;
break;
- case 'R':
case 'r':
- flags |= R_FLAG;
+ tflags |= R_FLAG;
break;
case 'v':
- flags |= V_FLAG;
+ tflags |= V_FLAG;
break;
default:
return CMD_RET_USAGE;
break;
}
}
+ flags = tflags;
argc -= optind;
argv += optind;
*/
fr = f_stat(to->p_path, &to_stat);
-debug("==== main, stat to: fr: %d, attr: %02x\n", fr, to_stat.fattrib);
+debug("==== main, stat to: fr: %d, attr: %02x, flags:%02x\n", fr, to_stat.fattrib, flags);
debug(" from:'%s' to:'%s'\n", from->p_path, to->p_path);
if (fr != FR_OK && fr != FR_NO_FILE && fr != FR_NO_PATH) {
CMD_TBL_ITEM(
cp, CONFIG_SYS_MAXARGS, CTBL_DBG, do_cp,
"copy files",
- "[-R] [-f | -i | -n] [-aprv] source_file target_file\n"
- " - \n"
+ "[-f | -i | -n] [-prv] source_file target_file\n"
+ "cp [-f | -i | -n] [-prv] source_file ... target_dir\n"
+ " -f overwrite existing file ignoring write protection\n"
+ " this option is ignored when the -n option is also used\n"
+ " -i prompt before overwrite (overrides a previous -n option)\n"
+ " -n do not overwrite an existing file (overrides a previous -i option)\n"
+ " -p preserve attributes and timestamps\n"
+ " -r copy directories recursively\n"
+ " -v explain what is being done\n"
),
CMD_TBL_ITEM(
/* Reads user's confirmation.
Returns 1 if user's input is "y", "Y", "yes" or "YES"
*/
+uint_fast8_t confirm_yes(void)
+{
+ uint_fast8_t checkch, ch;
+
+ checkch = ch = my_getchar(1);
+ putchar(ch);
+ while (ch != '\r') {
+ ch = my_getchar(1);
+ putchar(ch);
+ }
+ putchar('\n');
+
+ return (checkch == 'y');
+}
+
+#if 0
uint_fast8_t confirm_yesno(void)
{
unsigned int i;
char str_input[5];
/* Flush input */
- while (serial_getc())
+ while (serial_getc() < 0)
;
i = 0;
while (i < sizeof(str_input)) {
return 1;
return 0;
}
+#endif
/* pass 1 to disable ctrlc() checking, 0 to enable.
* returns previous state
int my_getchar(uint_fast8_t waitforchar);
-/* test if ctrl-c was pressed */
+/* Test if ctrl-c was pressed */
uint_fast8_t ctrlc(void);
-
-/* pass 1 to disable ctrlc() checking, 0 to enable.
- * returns previous state
- */
+/* Pass 1 to disable ctrlc() checking, 0 to enable. Returns previous state. */
uint_fast8_t disable_ctrlc(uint_fast8_t disable);
uint_fast8_t had_ctrlc (void);
void clear_ctrlc(void);
+/* Reads user's confirmation. Returns 1 if user's input is "y", "Y", "yes" or "YES" */
+uint_fast8_t confirm_yes(void);
+
#endif /* CON_UTILS_H */