summaryrefslogtreecommitdiff
path: root/cmd.c
diff options
context:
space:
mode:
authorThomas Albers <thomas@thomaslabs.org>2023-08-10 11:12:23 +0200
committerThomas Albers <thomas@thomaslabs.org>2023-08-10 11:12:23 +0200
commit932424af0aadcfcc9dcf3f66ff844fea6ac53986 (patch)
tree9f8ae78e06e3457ffdc0a0214da0b39638e65811 /cmd.c
parent312dea1d673365656cf98d282fb2bce64289a11d (diff)
Add progress option to write command
Diffstat (limited to 'cmd.c')
-rw-r--r--cmd.c47
1 files changed, 35 insertions, 12 deletions
diff --git a/cmd.c b/cmd.c
index 42a184d..a460808 100644
--- a/cmd.c
+++ b/cmd.c
@@ -11,6 +11,8 @@
#include <readline/history.h>
#endif // USE_READLINE
+#include <sys/stat.h>
+
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