/* expand_mask.c - convert bitmas to a byte mask */ #include #include unsigned char maskset[8] = { 128, 64, 32, 16, 8, 4, 2, 1}; main(argc, argv) int argc; char **argv; { FILE *fpi, *fpo; int i, ntot, nmask; unsigned char *a, *b; /* read the command line */ if (argc != 4) { fprintf(stderr,"usage: expand_mask ntot\n"); if (argc > 1) fprintf(stderr,"expand_mask: ERROR. Cannot open r %s\n", argv[1]); exit(-1); } if ( (fpi = fopen(argv[1], "r")) == NULL) { fprintf(stderr,"expand_mask: ERROR. Cannot open r %s\n", argv[1]); exit(-1); } if ( (fpo = fopen(argv[2], "w")) == NULL) { fprintf(stderr,"expand_mask: ERROR. Cannot open w %s\n", argv[2]); exit(-1); } ntot=atoi(argv[3]); nmask=ntot/8; /* allocate memory */ if(( a = (unsigned char *) malloc(nmask)) == NULL){ printf("sorry, couldn't allocate memory for mask\n"); exit(-1); } if(( b = (unsigned char *) malloc(ntot)) == NULL){ printf("sorry, couldn't allocate memory for bytemask\n"); exit(-1); } /* read the bit mask */ if ((fread(a, 1, nmask, fpi)) != nmask) { fprintf(stderr,"expand_mask: ERROR during read of %s\n", argv[1]); exit(-1); } fclose(fpi); /* convert bits to bytes */ for (i = 0; i < ntot; i++) { b[i] = 0; if((a[i/8] & maskset[i%8]) > 0) b[i] = 1; } /* write the byte mask */ if ((fwrite(b, 1, ntot, fpo)) != ntot) { fprintf(stderr,"expand_mask: ERROR during write of %s\n", argv[2]); exit(-1); } fclose(fpo); exit(0); }