7 char digit[] = "0123456789";
13 Biobuf *output = &bout;
15 extern int nextfile(void);
16 extern int matchfile(Resub*);
17 extern void openf(void);
18 extern char *fold(char*,int);
19 extern void usage(void);
20 extern void badexp(void);
23 main(int argc, char *argv[])
38 n=atoi(EARGF(usage()));
41 pattern = strdup(EARGF(usage()));
44 stem = strdup(EARGF(usage()));
47 suffix = strdup(EARGF(usage()));
61 if(argc < 0 || argc > 1)
65 b = Bopen(argv[0], OREAD);
67 fprint(2, "split: can't open %s: %r\n", argv[0]);
74 if(!(exp = regcomp(iflag? fold(pattern,strlen(pattern)): pattern)))
76 while((line=Brdline(b,'\n')) != 0) {
78 memset(match, 0, sizeof match);
79 line[Blinelen(b)-1] = 0;
80 if(regexec(exp,iflag?fold(line,Blinelen(b)-1):line,match,2)) {
81 if(matchfile(match) && xflag)
83 } else if(output == 0)
84 nextfile(); /* at most once */
85 Bwrite(output, line, Blinelen(b)-1);
91 while((line=Brdline(b,'\n')) != 0) {
96 Bwrite(output, line, Blinelen(b));
100 * in case we didn't end with a newline, tack whatever's
101 * left onto the last file
103 while((n = Bread(b, buf, sizeof(buf))) > 0)
104 Bwrite(output, buf, n);
114 static int canopen = 1;
117 fprint(2, "split: file %szz not split\n",stem);
123 suff[1] = 'a', ++suff[0];
130 matchfile(Resub *match)
133 int len = match[1].e.ep - match[1].s.sp;
134 strncpy(name, match[1].s.sp, len);
135 strcpy(name+len, suffix);
150 fd = create(name,OWRITE,0666);
152 fprint(2, "grep: can't create %s: %r\n", name);
155 Binit(output, fd, OWRITE);
162 static int linesize = 0;
166 fline = realloc(fline,n+1);
169 for(t=fline; *t++ = tolower((uchar)*s++); )
171 /* we assume the 'A'-'Z' only appear as themselves
180 fprint(2, "usage: split [-n num] [-e exp] [-f stem] [-s suff] [-x] [-i] [file]\n");
187 fprint(2, "split: bad regular expression\n");
188 exits("bad regular expression");