summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRob Purchase <shotofadds@rockbox.org>2009-07-19 21:53:11 +0000
committerRob Purchase <shotofadds@rockbox.org>2009-07-19 21:53:11 +0000
commit3bc86fd5c145527da2e1ab30d4616b19869ad20d (patch)
tree4cb3357fc5efe969e015d7a8626d1a3c4e899d7e
parent9153e8d025053fbdda5ec701dd5f4898fca0e6a4 (diff)
downloadrockbox-3bc86fd5c145527da2e1ab30d4616b19869ad20d.tar.gz
rockbox-3bc86fd5c145527da2e1ab30d4616b19869ad20d.zip
Tidy up error handling in mktccboot
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21972 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--tools/mktccboot.c40
1 files changed, 29 insertions, 11 deletions
diff --git a/tools/mktccboot.c b/tools/mktccboot.c
index ad7247419a..8c2e73d5f2 100644
--- a/tools/mktccboot.c
+++ b/tools/mktccboot.c
@@ -96,12 +96,13 @@ off_t filesize(int fd) {
int main(int argc, char *argv[])
{
char *infile, *bootfile, *outfile;
- int fdin, fdboot,fdout;
+ int fdin = -1, fdboot = -1, fdout = -1;
int n;
int inlength,bootlength;
uint32_t ldr;
unsigned char* image;
int origoffset;
+ int ret = 0;
if(argc < 3) {
usage();
@@ -115,37 +116,44 @@ int main(int argc, char *argv[])
if (fdin < 0)
{
perror(infile);
+ ret = 1;
+ goto error_exit;
}
fdboot = open(bootfile, O_RDONLY|O_BINARY);
if (fdboot < 0)
{
perror(bootfile);
+ ret = 2;
+ goto error_exit;
}
inlength = filesize(fdin);
bootlength = filesize(fdboot);
-
+
image = malloc(inlength + bootlength);
if (image==NULL)
{
printf("[ERR] Could not allocate memory, aborting\n");
- return 1;
+ ret = 3;
+ goto error_exit;
}
n = read(fdin, image, inlength);
if (n != inlength)
{
printf("[ERR] Could not read from %s\n",infile);
- return 2;
+ ret = 4;
+ goto error_exit;
}
n = read(fdboot, image + inlength, bootlength);
if (n != bootlength)
{
printf("[ERR] Could not read from %s\n",bootfile);
- return 3;
+ ret = 5;
+ goto error_exit;
}
ldr = get_uint32le(image);
@@ -170,18 +178,28 @@ int main(int argc, char *argv[])
if (fdout < 0)
{
perror(bootfile);
+ ret = 6;
+ goto error_exit;
}
n = write(fdout, image, inlength + bootlength);
if (n != inlength + bootlength)
{
printf("[ERR] Could not write output file %s\n",outfile);
- return 3;
+ ret = 7;
+ goto error_exit;
}
- close(fdin);
- close(fdboot);
- close(fdout);
-
- return 0;
+error_exit:
+
+ if (fdin >= 0)
+ close(fdin);
+
+ if (fdboot >= 0)
+ close(fdboot);
+
+ if (fdout >= 0)
+ close(fdout);
+
+ return ret;
}