added "more" command
[unix-history] / usr / src / usr.bin / split / split.c
CommitLineData
78d90aa3 1static char *sccsid = "@(#)split.c 4.2 (Berkeley) %G%";
f37d3151
BJ
2#include <stdio.h>
3
4unsigned count = 1000;
5int fnumber;
6char fname[100];
7char *ifil;
8char *ofil;
9FILE *is;
10FILE *os;
11
12main(argc, argv)
13char *argv[];
14{
15 register i, c, f;
16 int iflg = 0;
17
18 for(i=1; i<argc; i++)
19 if(argv[i][0] == '-')
20 switch(argv[i][1]) {
21
22 case '\0':
23 iflg = 1;
24 continue;
25
26 case '0':
27 case '1':
28 case '2':
29 case '3':
30 case '4':
31 case '5':
32 case '6':
33 case '7':
34 case '8':
35 case '9':
36 count = atoi(argv[i]+1);
37 continue;
38 }
39 else if(iflg)
40 ofil = argv[i];
41 else {
42 ifil = argv[i];
43 iflg = 2;
44 }
45 if(iflg != 2)
46 is = stdin;
47 else
48 if((is=fopen(ifil,"r")) == NULL) {
78d90aa3 49 perror(ifil);
f37d3151
BJ
50 exit(1);
51 }
52 if(ofil == 0)
53 ofil = "x";
54
55loop:
56 f = 1;
57 for(i=0; i<count; i++)
58 do {
59 c = getc(is);
60 if(c == EOF) {
61 if(f == 0)
62 fclose(os);
63 exit(0);
64 }
65 if(f) {
66 for(f=0; ofil[f]; f++)
67 fname[f] = ofil[f];
68 fname[f++] = fnumber/26 + 'a';
69 fname[f++] = fnumber%26 + 'a';
70 fname[f] = '\0';
71 fnumber++;
72 if((os=fopen(fname,"w")) == NULL) {
73 fprintf(stderr,"Cannot create output\n");
74 exit(1);
75 }
76 f = 0;
77 }
78 putc(c, os);
79 } while(c != '\n');
80 fclose(os);
81 goto loop;
82}