From 932424af0aadcfcc9dcf3f66ff844fea6ac53986 Mon Sep 17 00:00:00 2001 From: Thomas Albers Date: Thu, 10 Aug 2023 11:12:23 +0200 Subject: Add progress option to write command --- cmd.c | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'cmd.c') diff --git a/cmd.c b/cmd.c index 42a184d..a460808 100644 --- a/cmd.c +++ b/cmd.c @@ -11,6 +11,8 @@ #include #endif // USE_READLINE +#include + struct command; enum args_type { @@ -168,7 +170,34 @@ cmd_write(int fd, const struct command *cmd, int argc, char **args) if (err < 0) return -1; - if (param.human_readable) { + if (pathname) { + off_t n = 0; + FILE *fp; + struct stat st; + + if (!(fp = fopen(pathname, "r")) || (fstat(fileno(fp), &st) < 0)) + goto file_error; + + printf("Uploading %s :\n[ 0%%]", pathname); + fflush(stdout); + + // FIXME: fread MAX_PACKET_SIZE insted of 8 + // Split file into packets + while ((length = fread(buf, sizeof(uint8_t), 8, fp))) { + if ((err = z_write(fd, bank, address + n, length, buf))) + break; + + n += length; + + if (param.show_progress) { + printf("\r[% 3d%%]", 100 * n / st.st_size); + fflush(stdout); + } + } + + puts("\nDone!"); + fclose(fp); + } else if (param.human_readable) { do { // TODO: Handle fgets error fgets(line, LEN(line), stdin); @@ -193,26 +222,20 @@ cmd_write(int fd, const struct command *cmd, int argc, char **args) return err; } } else { - FILE *fp = pathname ? fopen(pathname, "r") : stdin; - - if (!fp) { - perror("File could not be opened"); - return -1; - } - // FIXME: fread MAX_PACKET_SIZE insted of 8 // Split file into packets - while ((length = fread(buf, sizeof(uint8_t), 8, fp))) { + while ((length = fread(buf, sizeof(uint8_t), 8, stdin))) { if ((err = z_write(fd, bank, address, length, buf))) break; address += length; } - - if (fp != stdin) - fclose(fp); } return err; + +file_error: + perror("File could not be opened"); + return -1; } static int -- cgit v1.2.3