6 char digit[] = "0123456789";
12 Biobuf *output = &bout;
14 extern int nextfile(void);
15 extern int matchfile(Resub*);
16 extern void openf(void);
17 extern char *fold(char*,int);
18 extern void usage(void);
19 extern void badexp(void);
22 main(int argc, char *argv[])
37 n=atoi(EARGF(usage()));
40 pattern = strdup(EARGF(usage()));
43 stem = strdup(EARGF(usage()));
46 suffix = strdup(EARGF(usage()));
60 if(argc < 0 || argc > 1)
64 b = Bopen(argv[0], OREAD);
66 fprint(2, "split: can't open %s: %r\n", argv[0]);
75 if(!(exp = regcomp(iflag? fold(pattern, strlen(pattern)):
78 memset(match, 0, sizeof match);
80 while((line=Brdline(b,'\n')) != 0) {
81 memset(match, 0, sizeof match);
82 line[Blinelen(b)-1] = 0;
83 if(regexec(exp, iflag? fold(line, Blinelen(b)-1): line,
85 if(matchfile(match) && xflag)
87 } else if(output == 0)
88 nextfile(); /* at most once */
89 Bwrite(output, line, Blinelen(b)-1);
95 while((line=Brdline(b,'\n')) != 0) {
100 Bwrite(output, line, Blinelen(b));
104 * in case we didn't end with a newline, tack whatever's
105 * left onto the last file
107 while((n = Bread(b, buf, sizeof(buf))) > 0)
108 Bwrite(output, buf, n);
118 static int canopen = 1;
122 fprint(2, "split: file %szz not split\n",stem);
125 snprint(name, sizeof name, "%s%s", stem, suff);
127 suff[1] = 'a', ++suff[0];
134 matchfile(Resub *match)
137 int len = match[1].ep - match[1].sp;
139 strncpy(name, match[1].sp, len);
140 strcpy(name+len, suffix);
156 fd = create(name,OWRITE,0666);
158 fprint(2, "grep: can't create %s: %r\n", name);
161 Binit(output, fd, OWRITE);
168 static int linesize = 0;
172 fline = realloc(fline,n+1);
175 for(t=fline; *t++ = tolower(*s++); )
177 /* we assume the 'A'-'Z' only appear as themselves
186 fprint(2, "usage: split [-n num] [-e exp] [-f stem] [-s suff] [-x] [-i] [file]\n");
193 fprint(2, "split: bad regular expression\n");
194 exits("bad regular expression");