#include #include #include #include #include typedef int bool_t; int main(int argc, char *argv[]); FILE *openfile(char *name, char *mode); char *txtcat(char *a, char *b); void error(char *msg); #ifdef MSDOS #include #define RMODE "rb" #define WMODE "wb" #else #define RMODE "r" #define WMODE "w" int fclose(FILE *); int fflush(FILE *); int _flsbuf(unsigned char, FILE*); int _filbuf(FILE *); #endif int main(int argc, char *argv[]) { long unsigned chunksize; char *outname; char *tail; int chunk; long unsigned nchars; FILE *infile, *outfile; int c; if ((argc != 5) | (strcmp(argv[1], "-c") != 0)) { error("usage: charsplit -c nnnnnn infile outname"); } chunksize = atoi(argv[2]); infile = openfile(argv[3], RMODE); c = getc(infile); outname = txtcat(argv[4], ".saa"); tail = outname + strlen(argv[4])+2; for (chunk = 0; (c != EOF) && (chunk < 26*26); chunk++) { tail[0] = 'a' + (chunk / 26); tail[1] = 'a' + (chunk % 26); assert(tail[2] == '\0'); outfile = openfile(outname, WMODE); nchars = 0; while((c != EOF) && (nchars < chunksize)) { putc(c, outfile); nchars++; c = getc(infile); } fprintf(stderr, "%8ld %s\n", nchars, outname); fclose(outfile); } if(c != EOF) error("too many chunks!"); if(ferror(infile)) perror("read error on input file"); fclose(infile); return(0); } FILE *openfile(char *name, char *mode) { FILE *f; fprintf(stderr, "opening %s mode = %s\n", name, mode); f = fopen(name, mode); if(f == NULL) error(txtcat("unable to open ", name)); return(f); } char *txtcat(char *a, char *b) { char *r = malloc(strlen(a) + strlen(b) + 1); if(r == NULL) error("memory exhausted"); strcpy(r, a); strcat(r, b); return(r); } void error(char *msg) { fprintf(stderr, "** %s\n", msg); exit(1); }