From ead843ec2aef6eaec96b9f6ea4309cbf9bff83e9 Mon Sep 17 00:00:00 2001 From: TitanMKD Date: Sun, 7 Apr 2013 18:57:30 +0200 Subject: [PATCH] hackrf_spiflash modified -l argument is not used anymore with -w argument, file len is automatically read. --- host/libhackrf/examples/hackrf_spiflash.c | 31 ++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/host/libhackrf/examples/hackrf_spiflash.c b/host/libhackrf/examples/hackrf_spiflash.c index 17d1dfd3..4e42d870 100644 --- a/host/libhackrf/examples/hackrf_spiflash.c +++ b/host/libhackrf/examples/hackrf_spiflash.c @@ -70,7 +70,7 @@ static void usage() { printf("Usage:\n"); printf("\t-a, --address : starting address (default: 0)\n"); - printf("\t-l, --length : number of bytes to read or write (default: 0)\n"); + printf("\t-l, --length : number of bytes to read (default: 0)\n"); printf("\t-r : Read data into file.\n"); printf("\t-w : Write data from file.\n"); } @@ -136,9 +136,28 @@ int main(int argc, char** argv) usage(); return EXIT_FAILURE; } + + if (path == NULL) { + fprintf(stderr, "Specify a path to a file.\n"); + usage(); + return EXIT_FAILURE; + } + + if( write ) + { + fd = fopen(path, "rb"); + /* Get size of the file */ + fseek(fd, 0, SEEK_END); /* Not really portable but work on major OS Linux/Win32 */ + length = ftell(fd); + /* Move to start */ + rewind(fd); + printf("File size %d bytes.\n", length); + } if (length == 0) { fprintf(stderr, "Requested transfer of zero bytes.\n"); + if(fd != NULL) + fclose(fd); usage(); return EXIT_FAILURE; } @@ -146,20 +165,14 @@ int main(int argc, char** argv) if ((length > MAX_LENGTH) || (address > MAX_LENGTH) || ((address + length) > MAX_LENGTH)) { fprintf(stderr, "Request exceeds size of flash memory.\n"); - usage(); - return EXIT_FAILURE; - } - - if (path == NULL) { - fprintf(stderr, "Specify a path to a file.\n"); + if(fd != NULL) + fclose(fd); usage(); return EXIT_FAILURE; } if (read) { fd = fopen(path, "wb"); - } else { - fd = fopen(path, "rb"); } if (fd == NULL) {