1 /* $OpenBSD: split.c,v 1.9 2003/06/10 22:20:51 deraadt Exp $ */ 2 /* $NetBSD: split.c,v 1.5 1995/08/31 22:22:05 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1987, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1987, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)split.c 8.3 (Berkeley) 4/25/94"; 42 #else 43 static char rcsid[] = "$OpenBSD: split.c,v 1.9 2003/06/10 22:20:51 deraadt Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/types.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <fcntl.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 #include <regex.h> 58 #include <sysexits.h> 59 60 #define DEFLINE 1000 /* Default num lines per file. */ 61 62 long bytecnt; /* Byte count to split on. */ 63 long numlines; /* Line count to split on. */ 64 int file_open; /* If a file open. */ 65 int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 66 char bfr[MAXBSIZE]; /* I/O buffer. */ 67 char fname[MAXPATHLEN]; /* File name prefix. */ 68 regex_t rgx; 69 int pflag; 70 71 void newfile(void); 72 void split1(void); 73 void split2(void); 74 void usage(void); 75 76 int 77 main(int argc, char *argv[]) 78 { 79 int ch; 80 char *ep, *p; 81 82 while ((ch = getopt(argc, argv, "0123456789b:l:p:-")) != -1) 83 switch (ch) { 84 case '0': case '1': case '2': case '3': case '4': 85 case '5': case '6': case '7': case '8': case '9': 86 /* 87 * Undocumented kludge: split was originally designed 88 * to take a number after a dash. 89 */ 90 if (numlines == 0) { 91 p = argv[optind - 1]; 92 if (p[0] == '-' && p[1] == ch && !p[2]) 93 numlines = strtol(++p, &ep, 10); 94 else 95 numlines = 96 strtol(argv[optind] + 1, &ep, 10); 97 if (numlines <= 0 || *ep) 98 errx(EX_USAGE, 99 "%s: illegal line count", optarg); 100 } 101 break; 102 case '-': /* Undocumented: historic stdin flag. */ 103 if (ifd != -1) 104 usage(); 105 ifd = 0; 106 break; 107 case 'b': /* Byte count. */ 108 if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 || 109 (*ep != '\0' && *ep != 'k' && *ep != 'm')) 110 errx(EX_USAGE, 111 "%s: illegal byte count", optarg); 112 if (*ep == 'k') 113 bytecnt *= 1024; 114 else if (*ep == 'm') 115 bytecnt *= 1048576; 116 break; 117 case 'p' : /* pattern matching. */ 118 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0) 119 errx(EX_USAGE, "%s: illegal regexp", optarg); 120 pflag = 1; 121 break; 122 case 'l': /* Line count. */ 123 if (numlines != 0) 124 usage(); 125 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep) 126 errx(EX_USAGE, 127 "%s: illegal line count", optarg); 128 break; 129 default: 130 usage(); 131 } 132 argv += optind; 133 argc -= optind; 134 135 if (*argv != NULL) 136 if (ifd == -1) { /* Input file. */ 137 if ((ifd = open(*argv, O_RDONLY, 0)) < 0) 138 err(EX_NOINPUT, "%s", *argv); 139 ++argv; 140 } 141 if (*argv != NULL) /* File name prefix. */ 142 (void)strlcpy(fname, *argv++, sizeof(fname)); 143 if (*argv != NULL) 144 usage(); 145 146 if (pflag && (numlines != 0 || bytecnt != 0)) 147 usage(); 148 149 if (numlines == 0) 150 numlines = DEFLINE; 151 else if (bytecnt != 0) 152 usage(); 153 154 if (ifd == -1) /* Stdin by default. */ 155 ifd = 0; 156 157 if (bytecnt) { 158 split1(); 159 exit (0); 160 } 161 split2(); 162 if (pflag) 163 regfree(&rgx); 164 exit(0); 165 } 166 167 /* 168 * split1 -- 169 * Split the input by bytes. 170 */ 171 void 172 split1(void) 173 { 174 long bcnt; 175 int dist, len; 176 char *C; 177 178 for (bcnt = 0;;) 179 switch ((len = read(ifd, bfr, MAXBSIZE))) { 180 case 0: 181 exit(0); 182 case -1: 183 err(EX_IOERR, "read"); 184 /* NOTREACHED */ 185 default: 186 if (!file_open) 187 newfile(); 188 if (bcnt + len >= bytecnt) { 189 dist = bytecnt - bcnt; 190 if (write(ofd, bfr, dist) != dist) 191 err(EX_IOERR, "write"); 192 len -= dist; 193 for (C = bfr + dist; len >= bytecnt; 194 len -= bytecnt, C += bytecnt) { 195 newfile(); 196 if (write(ofd, 197 C, (int)bytecnt) != bytecnt) 198 err(EX_IOERR, "write"); 199 } 200 if (len != 0) { 201 newfile(); 202 if (write(ofd, C, len) != len) 203 err(EX_IOERR, "write"); 204 } else 205 file_open = 0; 206 bcnt = len; 207 } else { 208 bcnt += len; 209 if (write(ofd, bfr, len) != len) 210 err(EX_IOERR, "write"); 211 } 212 } 213 } 214 215 /* 216 * split2 -- 217 * Split the input by lines. 218 */ 219 void 220 split2(void) 221 { 222 long lcnt = 0; 223 FILE *infp; 224 225 /* Stick a stream on top of input file descriptor */ 226 if ((infp = fdopen(ifd, "r")) == NULL) 227 err(EX_NOINPUT, "fdopen"); 228 229 /* Process input one line at a time */ 230 while (fgets(bfr, sizeof(bfr), infp) != NULL) { 231 const int len = strlen(bfr); 232 233 /* If line is too long to deal with, just write it out */ 234 if (bfr[len - 1] != '\n') 235 goto writeit; 236 237 /* Check if we need to start a new file */ 238 if (pflag) { 239 regmatch_t pmatch; 240 241 pmatch.rm_so = 0; 242 pmatch.rm_eo = len - 1; 243 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0) 244 newfile(); 245 } else if (lcnt++ == numlines) { 246 newfile(); 247 lcnt = 1; 248 } 249 250 writeit: 251 /* Open output file if needed */ 252 if (!file_open) 253 newfile(); 254 255 /* Write out line */ 256 if (write(ofd, bfr, len) != len) 257 err(EX_IOERR, "write"); 258 } 259 260 /* EOF or error? */ 261 if (ferror(infp)) 262 err(EX_IOERR, "read"); 263 else 264 exit(0); 265 } 266 267 /* 268 * newfile -- 269 * Open a new output file. 270 */ 271 void 272 newfile(void) 273 { 274 static long fnum; 275 static int defname; 276 static char *fpnt; 277 278 if (ofd == -1) { 279 if (fname[0] == '\0') { 280 fname[0] = 'x'; 281 fpnt = fname + 1; 282 defname = 1; 283 } else { 284 fpnt = fname + strlen(fname); 285 defname = 0; 286 } 287 ofd = fileno(stdout); 288 } 289 /* 290 * Hack to increase max files; original code wandered through 291 * magic characters. Maximum files is 3 * 26 * 26 == 2028 292 */ 293 #define MAXFILES 676 294 if (fnum == MAXFILES) { 295 if (!defname || fname[0] == 'z') 296 errx(EX_DATAERR, "too many files"); 297 ++fname[0]; 298 fnum = 0; 299 } 300 fpnt[0] = fnum / 26 + 'a'; 301 fpnt[1] = fnum % 26 + 'a'; 302 ++fnum; 303 if (!freopen(fname, "w", stdout)) 304 err(EX_IOERR, "%s", fname); 305 file_open = 1; 306 } 307 308 void 309 usage(void) 310 { 311 extern char *__progname; 312 313 (void)fprintf(stderr, 314 "usage: %s [-b byte_count] [-l line_count] [-p pattern] [file [prefix]]\n", 315 __progname); 316 exit(EX_USAGE); 317 } 318