#include #include #include struct imghead_t { char *filename; int *els; }; void printel(struct imghead_t *img, char *el, char **elnames, int numels); int readhead(char *file, struct imghead_t *img, int *sizes, int numels); void printhead(struct imghead_t *img, char **elnames, int numels); int main(int argc, char **argv) { struct imghead_t myheader; int rc, i; /* info about bmp image header */ int bmpnumels = 16; char *bmpelnames[16] = {"sig", "sizefile", "res1", "res2", "imgoffset", "sizehead", "width", "height", "numplanes", "bpp", "comptype", "sizedata", "hres", "vres", "numcols", "numicols"}; int bmpsizes[16] = {2,4,2,2,4,4,4,4,2,2,4,4,4,4,4,4}; if (argc < 2) { printf("USAGE: %s \n", argv[0]); exit(1); } rc = readhead(argv[1], &myheader, bmpsizes, bmpnumels); if (!rc) { exit(1); } printhead(&myheader, bmpelnames, bmpnumels); exit(0); } void printhead(struct imghead_t *img, char **imgelnames, int numels) { int i; printf("Header Info for %s\n------------------\n", img->filename); for (i=0; iels[i]); break; } } } int readhead(char *file, struct imghead_t *img, int *imgsizes, int numels) { int rc, i, fd; img->filename = (char *)strdup(file); fd = open(file, O_RDONLY); if (fd < 0) { printf("ERROR: cannot open file %s\n", file); return(0); } img->els = (int *)malloc(sizeof(int) * numels); for (i=0; iels[i] = 0; rc = read(fd, &img->els[i], imgsizes[i]); if (rc < imgsizes[i]) { printf("ERROR: corrupt img header\n"); return(0); } } close(fd); return(1); }