#include #include #include static void disp(char *buf, int length, int dir) { int i, j, mask; for (i = 0; i < length; i++) { mask = dir ? 0x001 : 0x080; for (j = 0; j < 8; j++) { printf("%s", (*buf & mask) ? "**" : " "); if (dir) { mask <<= 1; } else { mask >>= 1; } } buf++; } return; } int main(int argc, char *argv[]) { int i, result, size, asize, width, step, dir, rem; FILE *fp; char *buf; /* usage */ if (argc < 3) { printf("usage: %s [width(byte)] [filename]\n", argv[0]); result = EXIT_FAILURE; goto fin0; } /* get width */ width = atoi(argv[1]); if (!width) { printf("bad width!\n"); goto fin0; } /* file open */ fp = fopen(argv[2], "rb"); if (fp == NULL) { printf("file open error\n"); goto fin0; } /* get file size */ fseek(fp, 0, SEEK_END); size = asize = ftell(fp); fseek(fp, 0, SEEK_SET); /* round-up */ rem = asize % width; if (rem) asize += width - rem; /* allocate memory */ buf = malloc(asize); if (buf == NULL) { printf("malloc() failed\n"); goto fin0; } /* clear */ memset(buf, 0, asize); /* read file */ if (fread(buf, 1, size, fp) < size) { printf("fread() failed\n"); goto fin1; } /* display */ if (width < 0) { step = -width; dir = 1; } else { step = width; dir = 0; } for (i = 0; i < size; i += step) { printf("%08x: ", i); disp(buf + i, step, dir); printf("\n"); } result = EXIT_SUCCESS; fin1: free(buf); fin0: return result; }