aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/bindump.c
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2010-01-07 11:25:50 -0900
committerDavid M. Syzdek <david@syzdek.net>2010-01-07 11:25:50 -0900
commit327699f2d3d6eb766ae1628f1dbd6a769e890b1b (patch)
treeaeac8b6790b6b375b0c06ffe26ba106cbd248085 /src/bindump.c
parentc671fc87c8f2fa718c2cc2eeaa36440b11a44ac2 (diff)
downloaddmstools-327699f2d3d6eb766ae1628f1dbd6a769e890b1b.tar.gz
dmstools-327699f2d3d6eb766ae1628f1dbd6a769e890b1b.tar.bz2
dmstools-327699f2d3d6eb766ae1628f1dbd6a769e890b1b.tar.xz
dmstools-327699f2d3d6eb766ae1628f1dbd6a769e890b1b.zip
Moving file state information to struct in bindump
Diffstat (limited to 'src/bindump.c')
-rw-r--r--src/bindump.c107
1 files changed, 65 insertions, 42 deletions
diff --git a/src/bindump.c b/src/bindump.c
index 5340d29..f91e9c4 100644
--- a/src/bindump.c
+++ b/src/bindump.c
@@ -106,6 +106,32 @@
106#define PARAMS(protos) protos 106#define PARAMS(protos) protos
107#endif 107#endif
108 108
109/////////////////
110// //
111// Datatypes //
112// //
113/////////////////
114
115/// contains the state information for a file.
116typedef struct bindump_file BinDumpFile;
117
118
119/////////////////
120// //
121// Structs //
122// //
123/////////////////
124
125/// contains the state information for a file.
126struct bindump_file
127{
128 int fd; ///< file handle of open file
129 size_t pos; ///< current position within the file
130 ssize_t code; ///< return code of last file operation
131 const char * filename; ///< name of the file
132 char data[8]; ///< buffer for storing data for processing
133};
134
109 135
110////////////////// 136//////////////////
111// // 137// //
@@ -139,18 +165,14 @@ int main(int argc, char * argv[])
139{ 165{
140 int c; 166 int c;
141 int opt_index; 167 int opt_index;
142 int fd1;
143 char buff1[9]; 168 char buff1[9];
144 size_t offset; 169 size_t offset;
145 size_t offset_mod; 170 size_t offset_mod;
146 size_t offset_div; 171 size_t offset_div;
147 size_t s; 172 size_t s;
148 size_t pos1;
149 size_t len; 173 size_t len;
150 ssize_t code1;
151 uint8_t data1[8];
152 uint32_t verbose; 174 uint32_t verbose;
153 const char * file1; 175 BinDumpFile file1;
154 176
155 // getopt options 177 // getopt options
156 static char short_opt[] = "hl:o:vV"; 178 static char short_opt[] = "hl:o:vV";
@@ -167,6 +189,7 @@ int main(int argc, char * argv[])
167 offset_mod = 0; 189 offset_mod = 0;
168 offset_div = 0; 190 offset_div = 0;
169 verbose = 0; 191 verbose = 0;
192 memset(&file1, 0, sizeof(BinDumpFile));
170 193
171 while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) 194 while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
172 { 195 {
@@ -205,12 +228,12 @@ int main(int argc, char * argv[])
205 }; 228 };
206 229
207 // determines file to process (or STDIN) 230 // determines file to process (or STDIN)
208 file1 = NULL; 231 file1.filename = NULL;
209 switch(argc - optind) 232 switch(argc - optind)
210 { 233 {
211 case 1: 234 case 1:
212 if ((strcmp(argv[optind+0], "-"))) 235 if ((strcmp(argv[optind+0], "-")))
213 file1 = argv[optind+0]; 236 file1.filename = argv[optind+0];
214 break; 237 break;
215 case 0: 238 case 0:
216 break; 239 break;
@@ -221,7 +244,7 @@ int main(int argc, char * argv[])
221 ); 244 );
222 return(1); 245 return(1);
223 }; 246 };
224 if ( (!(file1)) && (offset) ) 247 if ( (!(file1.filename)) && (offset) )
225 { 248 {
226 fprintf(stderr, PROGRAM_NAME ": unable to specify an offset with STDIN\n"); 249 fprintf(stderr, PROGRAM_NAME ": unable to specify an offset with STDIN\n");
227 return(1); 250 return(1);
@@ -232,15 +255,15 @@ int main(int argc, char * argv[])
232 printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION); 255 printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION);
233 256
234 // open file for reading or set to STDIN 257 // open file for reading or set to STDIN
235 fd1 = STDIN_FILENO; 258 file1.fd = STDIN_FILENO;
236 if (!(file1)) 259 if (!(file1.filename))
237 if (verbose > 2) 260 if (verbose > 2)
238 printf("using stdin...\n"); 261 printf("using stdin...\n");
239 if (file1) 262 if (file1.filename)
240 { 263 {
241 if (verbose > 2) 264 if (verbose > 2)
242 printf("opening %s...\n", file1); 265 printf("opening %s...\n", file1.filename);
243 if ((fd1 = open(file1, O_RDONLY)) == -1) 266 if ((file1.fd = open(file1.filename, O_RDONLY)) == -1)
244 { 267 {
245 perror(PROGRAM_NAME ": open()"); 268 perror(PROGRAM_NAME ": open()");
246 return(1); 269 return(1);
@@ -248,85 +271,85 @@ int main(int argc, char * argv[])
248 }; 271 };
249 272
250 // mark the starting position 273 // mark the starting position
251 pos1 = 0; 274 file1.pos = 0;
252 275
253 // move to the specified offset 276 // move to the specified offset
254 if (offset) 277 if (offset)
255 { 278 {
256 if (verbose > 2) 279 if (verbose > 2)
257 printf("offsetting by %zi bytes...\n", offset); 280 printf("offsetting by %zi bytes...\n", offset);
258 if ((lseek(fd1, (off_t)offset, SEEK_SET) == -1)) 281 if ((lseek(file1.fd, (off_t)offset, SEEK_SET) == -1))
259 { 282 {
260 perror(PROGRAM_NAME ": lseek()"); 283 perror(PROGRAM_NAME ": lseek()");
261 if (verbose) 284 if (verbose)
262 printf("closing file...\n"); 285 printf("closing file...\n");
263 close(fd1); 286 close(file1.fd);
264 return(1); 287 return(1);
265 }; 288 };
266 pos1 += offset; 289 file1.pos += offset;
267 }; 290 };
268 291
269 // fill in white space so the offset markings align with the position in the file 292 // fill in white space so the offset markings align with the position in the file
270 if (verbose > 2) 293 if (verbose > 2)
271 printf("reading data...\n"); 294 printf("reading data...\n");
272 printf("offset 00 01 02 03 04 05 06 07 \n"); 295 printf("offset 00 01 02 03 04 05 06 07 \n");
273 if (pos1 % 8) 296 if (file1.pos % 8)
274 { 297 {
275 printf("0%04zo0:", (pos1/8)); 298 printf("0%04zo0:", (file1.pos/8));
276 for(s = 0; s < (pos1 % 8); s++) 299 for(s = 0; s < (file1.pos % 8); s++)
277 printf(" "); 300 printf(" ");
278 if ((code1 = read(fd1, data1, 8)) == -1) 301 if ((file1.code = read(file1.fd, file1.data, 8)) == -1)
279 { 302 {
280 perror(PROGRAM_NAME ": read()"); 303 perror(PROGRAM_NAME ": read()");
281 if (verbose > 2) 304 if (verbose > 2)
282 printf("closing file...\n"); 305 printf("closing file...\n");
283 if (verbose > 0) 306 if (verbose > 0)
284 printf("read %zu bytes\n", (pos1-offset)); 307 printf("read %zu bytes\n", (file1.pos-offset));
285 close(fd1); 308 close(file1.fd);
286 return(1); 309 return(1);
287 }; 310 };
288 for(s = offset_mod; ((s < 8) && ((s-offset_mod) < ((size_t)code1))); s++) 311 for(s = offset_mod; ((s < 8) && ((s-offset_mod) < ((size_t)file1.code))); s++)
289 printf(" %s", my_byte2str(data1[s], buff1)); 312 printf(" %s", my_byte2str(file1.data[s], buff1));
290 pos1 += code1; 313 file1.pos += file1.code;
291 }; 314 };
292 315
293 // read data from file handle 316 // read data from file handle
294 while((code1 = read(fd1, data1, 8)) > 0) 317 while((file1.code = read(file1.fd, file1.data, 8)) > 0)
295 { 318 {
296 if ((pos1 % 8) == 0) 319 if ((file1.pos % 8) == 0)
297 printf("0%04zo0:", (pos1/8)); 320 printf("0%04zo0:", (file1.pos/8));
298 for(s = 0; ((s < 8) && (s < ((size_t)code1))); s++) 321 for(s = 0; ((s < 8) && (s < ((size_t)file1.code))); s++)
299 printf(" %s", my_byte2str(data1[s], buff1)); 322 printf(" %s", my_byte2str(file1.data[s], buff1));
300 pos1 += code1; 323 file1.pos += file1.code;
301 if (!(pos1 % 8)) 324 if (!(file1.pos % 8))
302 printf("\n"); 325 printf("\n");
303 if ( ((pos1-offset) >= len) && (len)) 326 if ( ((file1.pos-offset) >= len) && (len))
304 break; 327 break;
305 if ( (!(pos1 % 8)) && (!(pos1 % (8 * 23))) ) 328 if ( (!(file1.pos % 8)) && (!(file1.pos % (8 * 23))) )
306 printf("offset 00 01 02 03 04 05 06 07 \n"); 329 printf("offset 00 01 02 03 04 05 06 07 \n");
307 }; 330 };
308 if (pos1 % 8) 331 if (file1.pos % 8)
309 printf("\n"); 332 printf("\n");
310 if (code1 == -1) 333 if (file1.code == -1)
311 { 334 {
312 perror(PROGRAM_NAME ": read()"); 335 perror(PROGRAM_NAME ": read()");
313 if (verbose > 2) 336 if (verbose > 2)
314 printf("closing file...\n"); 337 printf("closing file...\n");
315 if (verbose > 0) 338 if (verbose > 0)
316 printf("read %zu bytes\n", (pos1-offset)); 339 printf("read %zu bytes\n", (file1.pos-offset));
317 close(fd1); 340 close(file1.fd);
318 return(1); 341 return(1);
319 }; 342 };
320 343
321 // close file and finish up 344 // close file and finish up
322 if (fd1 != STDIN_FILENO) 345 if (file1.fd != STDIN_FILENO)
323 { 346 {
324 if (verbose > 2) 347 if (verbose > 2)
325 printf("closing file...\n"); 348 printf("closing file...\n");
326 close(fd1); 349 close(file1.fd);
327 }; 350 };
328 if (verbose > 0) 351 if (verbose > 0)
329 printf("read %zu bytes\n", (pos1-offset)); 352 printf("read %zu bytes\n", (file1.pos-offset));
330 printf("\n"); 353 printf("\n");
331 354
332 return(0); 355 return(0);