/* * SPLIT.C written by H.Moran * * Intended for use in conjunction with: MPRINT.C * * Last Update: * 2/28/83 hrm * 4 /1/83 -t option (hrm) * 10/ 9/83 get from STDIN instead of named file (hrm) * * Split STDIN into multiple files by 'n' line packets. * * Usage: * * split [] [ ... ] * * The title option is used to include a copy of the contents of the * title file in the first packet of the first output file and an equivalent * number of "\n" in the first packet of each of the other output files. * This is included to allow a title on the first page of output without * having to account for its length in the number of lines per page when * using MPRINT.C. */ #include #include #define REV_DATE "10/9/83" /* * Device definitions */ #define STDIN 0 /* This is the only one currently used */ #define STDOUT 1 #define STDERR 4 #define LST_DEV 2 #define RDR_DEV 3 #define PUN_DEV 3 /* * Documentation and Convience definitions */ #define PROC int /* PROCedure i.e. function returning no value */ #define NIL 0 /* pointer to nothing */ #define FOREVER for(;;) /* Infinite Loop */ #define END_TEXT 0x1a #define MAXLINE 200 #define MAXFILES 8 /* limited by BDS C to # of open files - 1 */ /* * main() * * process arguments * open files * form a circular list of output files * while (more in input file) * read an 'n' line packet from the input file * write the packet to the next output file on the list * close files * print line count statistics */ PROC main(ac, av) int ac; char *av[]; { int incount, outcount[MAXFILES]; int numfiles, lines_in_packet; FILE in_file, out_file[MAXFILES]; char lbuf[MAXLINE]; struct { /* all the junk associated with the -t option */ FILE fyle; char *name; int fudge; } title; int i, j; dioinit(&ac, av); if ( strcmp(av[1], "-t") == 0 || strcmp(av[1], "-T") == 0 ) { title.name = av[2]; if ( fopen(title.name, &title.fyle) == ERROR ) { printf("Can't open %s as a Title file\n", title.name); exit(1); } for ( i = 1; i < (ac-2); ++i ) av[i] = av[i+2]; ac -= 2; } else title.name = NIL; if ( (lines_in_packet = atoi(av[1])) < 1 ) usage("Illegal number of lines in packet"); for ( i = 2, j = 0; i < ac; ++i, ++j ) if ( fcreat(av[i], &out_file[j]) == ERROR ) { printf("Can't create: %s\n", av[i]); exit(1); } numfiles = j; incount = 0; for ( i = 0; i < numfiles; ++i ) outcount[i] = 0; if ( title.name ) { for ( title.fudge = 0; fgets(lbuf, &title.fyle); title.fudge++ ) for ( j = 0; j < numfiles; ++j ) { fputs( (j) ? "\n" : lbuf, &out_file[j]); outcount[j]++; } fclose(&title.fyle); } else title.fudge = 0; FOREVER { for ( j = 0; j < numfiles; ++j ) for ( i = title.fudge; i < lines_in_packet; ++i ) if ( fgets(lbuf, STDIN) ) { fputs(lbuf, &out_file[j]); incount++; outcount[j]++; } else goto end_input; title.fudge = 0; /* disable title fudge after 1'st packet */ } end_input: printf("%5d lines read\n", incount); for ( j = 0; j < numfiles; ++j ) { putc(END_TEXT, &out_file[j]); fflush(&out_file[j]); if ( fclose(&out_file[j]) == ERROR ) printf("Can't close: %s\n", av[j+2]); printf("%5d lines written to %s\n", outcount[j], av[j+2]); } dioflush(); } /* * usage() * * abort with error message */ PROC usage(s) char *s; { printf("\n%s\n\n", s); printf("Usage:\n\n"); printf("split [-t ] "); printf("<n> <outfile> [... <outfile>]\n\n"); printf("\t<n> indicates how many lines in a packet\n"); printf("\tpackets of STDIN will be copied to the <outfile>s in "); printf("rotation.\n\tThis program intended for use with mprint\n"); printf("The -t option allows the inclusion of the contents of "); printf("<title file> as part of the\n"); printf("first packet of the first output file, along with an\n"); printf("equivalent number of \"\\n\" as part of the first packet\n"); printf("of each of the other output files\n"); printf("\nLast Revision Date: %s\n", REV_DATE); exit(1); }