From c3617cca3dcd4246d5466517b78cd2442b93bbcb Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 17 Oct 2015 10:56:26 -0600 Subject: iceprog: Added support for reading/writing data to pipes --- iceprog/iceprog.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 25b8cf2..6dd34a5 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -289,6 +289,10 @@ void help(const char *progname) "\n" "Usage: %s [options] \n" "\n" + " \n" + " the path to the file to read or write from, or - to read\n" + " from stdin and write to stdout\n" + "\n" " -d \n" " use the specified USB device:\n" "\n" @@ -489,7 +493,8 @@ int main(int argc, char **argv) // Program // --------------------------------------------------------- - FILE *f = fopen(filename, "r"); + FILE *f = (strcmp(filename, "-") == 0) ? stdin : + fopen(filename, "r"); if (f == NULL) { fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); error(); @@ -506,7 +511,8 @@ int main(int argc, char **argv) send_spi(buffer, rc); } - fclose(f); + if (f != stdin) + fclose(f); // add 48 dummy bits send_byte(0x8f); @@ -543,7 +549,8 @@ int main(int argc, char **argv) if (!read_mode && !check_mode) { - FILE *f = fopen(filename, "r"); + FILE *f = (strcmp(filename, "-") == 0) ? stdin : + fopen(filename, "r"); if (f == NULL) { fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); error(); @@ -584,7 +591,8 @@ int main(int argc, char **argv) flash_wait(); } - fclose(f); + if (f != stdin) + fclose(f); } @@ -594,7 +602,8 @@ int main(int argc, char **argv) if (read_mode) { - FILE *f = fopen(filename, "w"); + FILE *f = (strcmp(filename, "-") == 0) ? stdout : + fopen(filename, "w"); if (f == NULL) { fprintf(stderr, "Error: Can't open '%s' for writing: %s\n", filename, strerror(errno)); error(); @@ -607,11 +616,13 @@ int main(int argc, char **argv) fwrite(buffer, 256, 1, f); } - fclose(f); + if (f != stdout) + fclose(f); } else { - FILE *f = fopen(filename, "r"); + FILE *f = (strcmp(filename, "-") == 0) ? stdin : + fopen(filename, "r"); if (f == NULL) { fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); error(); @@ -631,7 +642,8 @@ int main(int argc, char **argv) fprintf(stderr, "VERIFY OK\n"); - fclose(f); + if (f != stdin) + fclose(f); } -- cgit v1.2.3