Show reason when cannot open a file, and some stuffs #3

Open
K4zoku wants to merge 4 commits from K4zoku/master into master
Showing only changes of commit 817e2a7f29 - Show all commits

View file

@ -19,16 +19,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#define RBUF_SIZE 640 #define RBUF_SIZE 640
static unsigned int asc_to_int(char a) static unsigned int asc_to_int(char a)
{ {
if (a >= 'A') return a >= 'A' ? (a - 'A') + 10 : a - '0';
return (a - 'A') + 10;
else
return a - '0';
} }
static unsigned int hex_to_int(const char *h) static unsigned int hex_to_int(const char *h)
@ -60,25 +58,43 @@ static int check_hex_line(const char *str, int len)
return 1; return 1;
} }
static FILE *open_file(const char *fileName, const char *mode)
{
FILE *f = fopen(fileName, mode);
if (f == NULL) {
printf("The file '%s' could not be opened with the following reason\n", fileName);
printf("%s\n", strerror(errno));
exit(-EIO);
}
return f;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned int addr = 0; unsigned int addr = 0;
char *ifn, *ofn;
FILE *ifp, *ofp; FILE *ifp, *ofp;
char *rbuf; char *rbuf;
ssize_t len, i; ssize_t len, i;
size_t buflen; size_t buflen;
if (argc != 3) { switch (argc) {
printf("Usage: %s <input hex file> <output file>\n", argv[0]); case 2:
return 0; ifn = ofn = argv[1];
break;
case 3:
ifn = argv[1];
ofn = argv[2];
break;
default:
printf("Usage: %s <input hex file> [<output file>]\n", argv[0]);
return 0;
} }
ifp = fopen(argv[1], "r");
ofp = fopen(argv[2], "w"); ifp = open_file(ifn, "r");
if ((ifp == NULL) || (ofp == NULL)) { ofp = open_file(ofn, "w");
puts("failed to open file.");
return -EIO;
}
rbuf = NULL; rbuf = NULL;
while ((len = getline(&rbuf, &buflen, ifp)) > 0) { while ((len = getline(&rbuf, &buflen, ifp)) > 0) {
@ -94,7 +110,7 @@ int main(int argc, char *argv[])
switch (type) { switch (type) {
case 4: case 4:
addr = lhex_to_int(rbuf + 9) * 0x10000; addr = lhex_to_int(rbuf + 9) * 0x10000;
printf("bump addr to 0x%08X\n", addr); printf("Bump addr to 0x%08X\n", addr);
break; break;
case 0: case 0:
dest_addr = addr + lhex_to_int(rbuf + 3); dest_addr = addr + lhex_to_int(rbuf + 3);
@ -109,27 +125,24 @@ int main(int argc, char *argv[])
goto output_err; goto output_err;
for (i = 0; i < hex_to_int(rbuf + 1); i++) { for (i = 0; i < hex_to_int(rbuf + 1); i++) {
obuf[0] = hex_to_int(rbuf + 9 + i * 2); obuf[0] = hex_to_int(rbuf + 9 + i * 2);
if (fwrite(obuf, 1, 1, ofp) != 1) if (fwrite(obuf, 1, 1, ofp) != 1) goto output_err;
goto output_err;
} }
break; break;
case 1: case 1:
if (fwrite("\x4e\xfc\x04\xff\xff\xff\xff", 7, 1, ofp) != 1) if (fwrite("\x4e\xfc\x04\xff\xff\xff\xff", 7, 1, ofp) != 1) goto output_err;
goto output_err;
goto end; goto end;
default: default:
return -EINVAL; return -EINVAL;
} }
} }
puts("hex file formatting error"); puts("Hex file formatting error");
return -EINVAL; return -EINVAL;
output_err: output_err:
puts("error on writing output file"); puts("Error on writing output file");
return -EIO; return -EIO;
end: end:
return 0; return 0;
} }