#ifndef lint static char sccsID[] = "@(#)textpieces.c 1.4 90/10/25 22:08:17"; #endif /* Break getfree output into continuous-text pieces and discard non-text data Copyright (c) 1988, 1990 Timothy R. Eliseo. All rights reserved. Permission is granted to use but not to sell this software provided that the above message is not removed and that any modification is noted. */ #include #include /* The size of a fragment is defined here to avoid cluttering this program with filesystem dependencies. It is shown by dumpfs as "fsize" */ #define FG_SIZE 1024 int isopen = 0; FILE *outf; int fragno = 0, firstfrag; char outfn[200], /* used to construct an output filename */ *endpos, /* points past the prefix */ newfn[200]; /* used to rename the file to a "partial" */ main(argc, argv) char *argv[]; { unsigned char buf[FG_SIZE], *bp; int nnuls, curnuls, ngrps, nbit7s, curfn = 0; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } strcpy(outfn, argv[1]); strcpy(newfn, argv[1]); endpos = outfn + strlen(outfn); while (fread(buf, FG_SIZE, 1, stdin)) { nnuls = ngrps = nbit7s = curnuls = 0; /* check each byte in the fragment */ for (bp = buf; bp < buf + FG_SIZE; bp++) { if (*bp & 0x80) nbit7s++; if (!*bp) { /* scan for the first non-NUL */ while (curnuls++, ++bp < buf + FG_SIZE) { if (*bp) { nnuls += curnuls; curnuls = 0; ngrps++; break; } } bp--; /* since the outer loop will increment bp */ } } if (nnuls || nbit7s) { /* any embedded NULs or high bits? */ close_partial(); printf( "Fragment %d rejected: %d NULs in %d groups (%d at end), %d high bits\n", fragno, nnuls, ngrps, curnuls, nbit7s); } else if (isopen || curnuls != FG_SIZE) { /* if the data is good and it's not just another NUL-filled fragment at the end of a block */ if (isopen) { fputs("/*~~~*/", outf); /* write a separator string */ } else { /* start a new file */ firstfrag = fragno; sprintf(endpos, ".%03d", ++curfn); if (!(outf = fopen(outfn, "w"))) { perror(outfn); exit(2); } isopen = 1; } /* write all but any trailing NULs */ if (fwrite(buf, FG_SIZE - curnuls, 1, outf) != 1) { perror(outfn); exit(2); } if (curnuls) { /* if there were trailing NULs, close the current file and report it */ fclose(outf); isopen = 0; printf("File %s: fragments %d-%d (%d NULs)\n", outfn, firstfrag, fragno, curnuls); } } /* if good data */ fragno++; } /* while read not eof */ close_partial(); return 0; } /* close a file of "partial" data, rename it to end in "p", and report */ close_partial() { if (isopen) { fclose(outf); isopen = 0; strcpy(newfn + (endpos - outfn), endpos); strcat(newfn, "p"); rename(outfn, newfn); printf("File %s: fragments %d-%d (partial)\n", newfn, firstfrag, fragno - 1); } }