diff options
author | Rob Purchase <shotofadds@rockbox.org> | 2009-07-19 21:53:11 +0000 |
---|---|---|
committer | Rob Purchase <shotofadds@rockbox.org> | 2009-07-19 21:53:11 +0000 |
commit | 3bc86fd5c145527da2e1ab30d4616b19869ad20d (patch) | |
tree | 4cb3357fc5efe969e015d7a8626d1a3c4e899d7e | |
parent | 9153e8d025053fbdda5ec701dd5f4898fca0e6a4 (diff) | |
download | rockbox-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.c | 40 |
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) { | |||
96 | int main(int argc, char *argv[]) | 96 | int main(int argc, char *argv[]) |
97 | { | 97 | { |
98 | char *infile, *bootfile, *outfile; | 98 | char *infile, *bootfile, *outfile; |
99 | int fdin, fdboot,fdout; | 99 | int fdin = -1, fdboot = -1, fdout = -1; |
100 | int n; | 100 | int n; |
101 | int inlength,bootlength; | 101 | int inlength,bootlength; |
102 | uint32_t ldr; | 102 | uint32_t ldr; |
103 | unsigned char* image; | 103 | unsigned char* image; |
104 | int origoffset; | 104 | int origoffset; |
105 | int ret = 0; | ||
105 | 106 | ||
106 | if(argc < 3) { | 107 | if(argc < 3) { |
107 | usage(); | 108 | usage(); |
@@ -115,37 +116,44 @@ int main(int argc, char *argv[]) | |||
115 | if (fdin < 0) | 116 | if (fdin < 0) |
116 | { | 117 | { |
117 | perror(infile); | 118 | perror(infile); |
119 | ret = 1; | ||
120 | goto error_exit; | ||
118 | } | 121 | } |
119 | 122 | ||
120 | fdboot = open(bootfile, O_RDONLY|O_BINARY); | 123 | fdboot = open(bootfile, O_RDONLY|O_BINARY); |
121 | if (fdboot < 0) | 124 | if (fdboot < 0) |
122 | { | 125 | { |
123 | perror(bootfile); | 126 | perror(bootfile); |
127 | ret = 2; | ||
128 | goto error_exit; | ||
124 | } | 129 | } |
125 | 130 | ||
126 | inlength = filesize(fdin); | 131 | inlength = filesize(fdin); |
127 | bootlength = filesize(fdboot); | 132 | bootlength = filesize(fdboot); |
128 | 133 | ||
129 | image = malloc(inlength + bootlength); | 134 | image = malloc(inlength + bootlength); |
130 | 135 | ||
131 | if (image==NULL) | 136 | if (image==NULL) |
132 | { | 137 | { |
133 | printf("[ERR] Could not allocate memory, aborting\n"); | 138 | printf("[ERR] Could not allocate memory, aborting\n"); |
134 | return 1; | 139 | ret = 3; |
140 | goto error_exit; | ||
135 | } | 141 | } |
136 | 142 | ||
137 | n = read(fdin, image, inlength); | 143 | n = read(fdin, image, inlength); |
138 | if (n != inlength) | 144 | if (n != inlength) |
139 | { | 145 | { |
140 | printf("[ERR] Could not read from %s\n",infile); | 146 | printf("[ERR] Could not read from %s\n",infile); |
141 | return 2; | 147 | ret = 4; |
148 | goto error_exit; | ||
142 | } | 149 | } |
143 | 150 | ||
144 | n = read(fdboot, image + inlength, bootlength); | 151 | n = read(fdboot, image + inlength, bootlength); |
145 | if (n != bootlength) | 152 | if (n != bootlength) |
146 | { | 153 | { |
147 | printf("[ERR] Could not read from %s\n",bootfile); | 154 | printf("[ERR] Could not read from %s\n",bootfile); |
148 | return 3; | 155 | ret = 5; |
156 | goto error_exit; | ||
149 | } | 157 | } |
150 | 158 | ||
151 | ldr = get_uint32le(image); | 159 | ldr = get_uint32le(image); |
@@ -170,18 +178,28 @@ int main(int argc, char *argv[]) | |||
170 | if (fdout < 0) | 178 | if (fdout < 0) |
171 | { | 179 | { |
172 | perror(bootfile); | 180 | perror(bootfile); |
181 | ret = 6; | ||
182 | goto error_exit; | ||
173 | } | 183 | } |
174 | 184 | ||
175 | n = write(fdout, image, inlength + bootlength); | 185 | n = write(fdout, image, inlength + bootlength); |
176 | if (n != inlength + bootlength) | 186 | if (n != inlength + bootlength) |
177 | { | 187 | { |
178 | printf("[ERR] Could not write output file %s\n",outfile); | 188 | printf("[ERR] Could not write output file %s\n",outfile); |
179 | return 3; | 189 | ret = 7; |
190 | goto error_exit; | ||
180 | } | 191 | } |
181 | 192 | ||
182 | close(fdin); | 193 | error_exit: |
183 | close(fdboot); | 194 | |
184 | close(fdout); | 195 | if (fdin >= 0) |
185 | 196 | close(fdin); | |
186 | return 0; | 197 | |
198 | if (fdboot >= 0) | ||
199 | close(fdboot); | ||
200 | |||
201 | if (fdout >= 0) | ||
202 | close(fdout); | ||
203 | |||
204 | return ret; | ||
187 | } | 205 | } |