From: Leo C Date: Wed, 11 Apr 2018 21:35:46 +0000 (+0200) Subject: fat cp: flags X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/commitdiff_plain/1cdd02d8298159e76a045896abc11a8670504b2a fat cp: flags --- diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c index b0c889c..29d96de 100644 --- a/avr/cmd_fat.c +++ b/avr/cmd_fat.c @@ -522,16 +522,16 @@ char *path_basename(PATH_T *p) } -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) @@ -576,13 +576,8 @@ debug(" from:'%s' to:'%s'\n", from->p_path, to->p_path); */ 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; } @@ -784,36 +779,38 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, 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; @@ -852,7 +849,7 @@ command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, */ 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) { @@ -1206,8 +1203,15 @@ CMD_TBL_ITEM( 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( diff --git a/avr/con-utils.c b/avr/con-utils.c index 4a96771..4108221 100644 --- a/avr/con-utils.c +++ b/avr/con-utils.c @@ -80,13 +80,29 @@ uint_fast8_t ctrlc(void) /* 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)) { @@ -104,6 +120,7 @@ uint_fast8_t confirm_yesno(void) return 1; return 0; } +#endif /* pass 1 to disable ctrlc() checking, 0 to enable. * returns previous state diff --git a/include/con-utils.h b/include/con-utils.h index 86c0df0..a71fdf9 100644 --- a/include/con-utils.h +++ b/include/con-utils.h @@ -15,16 +15,16 @@ uint_fast8_t tstc(void); 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 */