3 #include <string.h> /* for memset() */
6 /* Function prototypes */
7 void diep(const char *s
);
8 void dieu(const char *pname
);
10 #define BUFSIZE 20 /* Must be dividable by 2 */
12 int main(int argc
, char *argv
[])
14 unsigned char buf
[BUFSIZE
+ 1]; /* +1 for the '\0' */
16 int cnt
, i
, j
, len
, skip
;
18 int readbytes
, totalbytes
= 0;
26 while ((opt
= getopt(argc
, argv
, "Cn:s:f:")) != -1) {
45 /* optind shows to the argv[] index of the first non-option element */
47 fprintf(stderr
, "non-option argv[]-elements: ");
49 fprintf(stderr
, "%s ", argv
[optind
++]);
50 fprintf(stderr
, "\n");
58 if ((fp
= fopen(fpath
, "r")) == NULL
)
61 /* Skip ``skip'' bytes */
62 fseek(fp
, skip
, SEEK_SET
);
66 while(!feof(fp
) && (totalbytes
< len
|| len
== -1)) {
67 /* Initialize buffer */
68 memset(buf
, 0, BUFSIZE
);
70 /* Read ``BUFSIZE'' elements of 1 byte long from file */
71 readbytes
= fread(buf
, 1, BUFSIZE
, fp
);
74 printf(caps
== 1 ? "%08lX " : "%08lx ", (unsigned long)cnt
* BUFSIZE
+ skip
);
75 for (i
= 0; i
< readbytes
&& (totalbytes
< len
|| len
== -1); i
++, totalbytes
++) {
76 printf(caps
== 1 ? "%02X " : "%02x ", buf
[i
]);
77 if (i
== (BUFSIZE
/ 2) - 1)
81 /* Fill in the blanks */
82 for (j
= 0; j
< (BUFSIZE
- i
); j
++) {
84 if (j
== (BUFSIZE
/2) - 1)
89 * Print the output characters in the default character set.
90 * Nonprinting characters are displayed as a single ``.''
93 for (j
= 0; j
< i
; j
++) {
94 if (buf
[j
] >= 32 && buf
[j
] <= 127)
107 /* Close device file */
113 void diep(const char *s
)
119 void dieu(const char *pname
)
121 fprintf(stderr
, "Usage: %s [-C] [-n length] [-s skip] -f file\n", pname
);