| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <fcntl.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
int main (int argc, char* argv[]) |
| 14 |
{ |
| 15 |
unsigned char buffer[16]; |
| 16 |
size_t offset = 0; |
| 17 |
size_t bytes_read; |
| 18 |
int i; |
| 19 |
|
| 20 |
/* Open the file for reading. */ |
| 21 |
int fd = open (argv[1], O_RDONLY); |
| 22 |
|
| 23 |
/* Read from the file, one chunk at a time. Continue until read |
| 24 |
"comes up short", i.e. reads less than we asked for. This |
| 25 |
indicates that we've hit the end of the file. */ |
| 26 |
do { |
| 27 |
/* Read the next lines's worth of bytes. */ |
| 28 |
bytes_read = read (fd, buffer, sizeof (buffer)); |
| 29 |
/* Print the offset in the file, followed by the bytes themselves. */ |
| 30 |
printf ("0x%06x : ", offset); |
| 31 |
for (i = 0; i < bytes_read; ++i) |
| 32 |
printf ("%02x ", buffer[i]); |
| 33 |
printf ("\n"); |
| 34 |
/* Keep count of our position in the file. */ |
| 35 |
offset += bytes_read; |
| 36 |
} |
| 37 |
while (bytes_read == sizeof (buffer)); |
| 38 |
|
| 39 |
/* All done. */ |
| 40 |
close (fd); |
| 41 |
return 0; |
| 42 |
} |