1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1987, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1987, 1993, 1994\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif 40 41 #ifndef lint 42 static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94"; 43 #endif 44 45 #include <sys/param.h> 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <inttypes.h> 54 #include <libutil.h> 55 #include <limits.h> 56 #include <locale.h> 57 #include <stdbool.h> 58 #include <stdint.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 #include <regex.h> 64 #include <sysexits.h> 65 66 #define DEFLINE 1000 /* Default num lines per file. */ 67 68 static off_t bytecnt; /* Byte count to split on. */ 69 static off_t chunks = 0; /* Chunks count to split into. */ 70 static bool clobber = true; /* Whether to overwrite existing output files. */ 71 static long numlines; /* Line count to split on. */ 72 static int file_open; /* If a file open. */ 73 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 74 static char fname[MAXPATHLEN]; /* File name prefix. */ 75 static regex_t rgx; 76 static int pflag; 77 static bool dflag; 78 static long sufflen = 2; /* File name suffix length. */ 79 static int autosfx = 1; /* Whether to auto-extend the suffix length. */ 80 81 static void newfile(void); 82 static void split1(void); 83 static void split2(void); 84 static void split3(void); 85 static void usage(void) __dead2; 86 87 int 88 main(int argc, char **argv) 89 { 90 int ch; 91 int error; 92 char *ep, *p; 93 94 setlocale(LC_ALL, ""); 95 96 dflag = false; 97 while ((ch = getopt(argc, argv, "0123456789a:b:cdl:n:p:")) != -1) 98 switch (ch) { 99 case '0': case '1': case '2': case '3': case '4': 100 case '5': case '6': case '7': case '8': case '9': 101 /* 102 * Undocumented kludge: split was originally designed 103 * to take a number after a dash. 104 */ 105 if (numlines == 0) { 106 p = argv[optind - 1]; 107 if (p[0] == '-' && p[1] == ch && !p[2]) 108 numlines = strtol(++p, &ep, 10); 109 else 110 numlines = 111 strtol(argv[optind] + 1, &ep, 10); 112 if (numlines <= 0 || *ep) 113 errx(EX_USAGE, 114 "%s: illegal line count", optarg); 115 } 116 break; 117 case 'a': /* Suffix length */ 118 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep) 119 errx(EX_USAGE, 120 "%s: illegal suffix length", optarg); 121 autosfx = 0; 122 break; 123 case 'b': /* Byte count. */ 124 errno = 0; 125 error = expand_number(optarg, &bytecnt); 126 if (error == -1) 127 errx(EX_USAGE, "%s: offset too large", optarg); 128 break; 129 case 'c': /* Continue, don't overwrite output files. */ 130 clobber = false; 131 break; 132 case 'd': /* Decimal suffix */ 133 dflag = true; 134 break; 135 case 'l': /* Line count. */ 136 if (numlines != 0) 137 usage(); 138 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep) 139 errx(EX_USAGE, 140 "%s: illegal line count", optarg); 141 break; 142 case 'n': /* Chunks. */ 143 if (!isdigit((unsigned char)optarg[0]) || 144 (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 || 145 *ep != '\0') { 146 errx(EX_USAGE, "%s: illegal number of chunks", 147 optarg); 148 } 149 break; 150 151 case 'p': /* pattern matching. */ 152 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0) 153 errx(EX_USAGE, "%s: illegal regexp", optarg); 154 pflag = 1; 155 break; 156 default: 157 usage(); 158 } 159 argv += optind; 160 argc -= optind; 161 162 if (*argv != NULL) { /* Input file. */ 163 if (strcmp(*argv, "-") == 0) 164 ifd = STDIN_FILENO; 165 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0) 166 err(EX_NOINPUT, "%s", *argv); 167 ++argv; 168 } 169 if (*argv != NULL) /* File name prefix. */ 170 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname)) 171 errx(EX_USAGE, "file name prefix is too long"); 172 if (*argv != NULL) 173 usage(); 174 175 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname)) 176 errx(EX_USAGE, "suffix is too long"); 177 if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0)) 178 usage(); 179 180 if (numlines == 0) 181 numlines = DEFLINE; 182 else if (bytecnt != 0 || chunks != 0) 183 usage(); 184 185 if (bytecnt && chunks) 186 usage(); 187 188 if (ifd == -1) /* Stdin by default. */ 189 ifd = 0; 190 191 if (bytecnt) { 192 split1(); 193 exit (0); 194 } else if (chunks) { 195 split3(); 196 exit (0); 197 } 198 split2(); 199 if (pflag) 200 regfree(&rgx); 201 exit(0); 202 } 203 204 /* 205 * split1 -- 206 * Split the input by bytes. 207 */ 208 static void 209 split1(void) 210 { 211 static char bfr[MAXBSIZE]; 212 off_t bcnt; 213 char *C; 214 ssize_t dist, len; 215 int nfiles; 216 217 nfiles = 0; 218 219 for (bcnt = 0;;) 220 switch ((len = read(ifd, bfr, sizeof(bfr)))) { 221 case 0: 222 exit(0); 223 case -1: 224 err(EX_IOERR, "read"); 225 /* NOTREACHED */ 226 default: 227 if (!file_open) { 228 if (!chunks || (nfiles < chunks)) { 229 newfile(); 230 nfiles++; 231 } 232 } 233 if (bcnt + len >= bytecnt) { 234 dist = bytecnt - bcnt; 235 if (write(ofd, bfr, dist) != dist) 236 err(EX_IOERR, "write"); 237 len -= dist; 238 for (C = bfr + dist; len >= bytecnt; 239 len -= bytecnt, C += bytecnt) { 240 if (!chunks || (nfiles < chunks)) { 241 newfile(); 242 nfiles++; 243 } 244 if (write(ofd, 245 C, bytecnt) != bytecnt) 246 err(EX_IOERR, "write"); 247 } 248 if (len != 0) { 249 if (!chunks || (nfiles < chunks)) { 250 newfile(); 251 nfiles++; 252 } 253 if (write(ofd, C, len) != len) 254 err(EX_IOERR, "write"); 255 } else 256 file_open = 0; 257 bcnt = len; 258 } else { 259 bcnt += len; 260 if (write(ofd, bfr, len) != len) 261 err(EX_IOERR, "write"); 262 } 263 } 264 } 265 266 /* 267 * split2 -- 268 * Split the input by lines. 269 */ 270 static void 271 split2(void) 272 { 273 char *buf; 274 size_t bufsize; 275 ssize_t len; 276 long lcnt = 0; 277 FILE *infp; 278 279 buf = NULL; 280 bufsize = 0; 281 282 /* Stick a stream on top of input file descriptor */ 283 if ((infp = fdopen(ifd, "r")) == NULL) 284 err(EX_NOINPUT, "fdopen"); 285 286 /* Process input one line at a time */ 287 while ((errno = 0, len = getline(&buf, &bufsize, infp)) > 0) { 288 /* Check if we need to start a new file */ 289 if (pflag) { 290 regmatch_t pmatch; 291 292 pmatch.rm_so = 0; 293 pmatch.rm_eo = len - 1; 294 if (regexec(&rgx, buf, 0, &pmatch, REG_STARTEND) == 0) 295 newfile(); 296 } else if (lcnt++ == numlines) { 297 newfile(); 298 lcnt = 1; 299 } 300 301 /* Open output file if needed */ 302 if (!file_open) 303 newfile(); 304 305 /* Write out line */ 306 if (write(ofd, buf, len) != len) 307 err(EX_IOERR, "write"); 308 } 309 310 /* EOF or error? */ 311 if ((len == -1 && errno != 0) || ferror(infp)) 312 err(EX_IOERR, "read"); 313 else 314 exit(0); 315 } 316 317 /* 318 * split3 -- 319 * Split the input into specified number of chunks 320 */ 321 static void 322 split3(void) 323 { 324 struct stat sb; 325 326 if (fstat(ifd, &sb) == -1) { 327 err(1, "stat"); 328 /* NOTREACHED */ 329 } 330 331 if (chunks > sb.st_size) { 332 errx(1, "can't split into more than %d files", 333 (int)sb.st_size); 334 /* NOTREACHED */ 335 } 336 337 bytecnt = sb.st_size / chunks; 338 split1(); 339 } 340 341 342 /* 343 * newfile -- 344 * Open a new output file. 345 */ 346 static void 347 newfile(void) 348 { 349 long i, maxfiles, tfnum; 350 static long fnum; 351 static char *fpnt; 352 char beg, end; 353 int pattlen; 354 int flags = O_WRONLY | O_CREAT | O_TRUNC; 355 356 if (!clobber) 357 flags |= O_EXCL; 358 359 if (ofd == -1) { 360 if (fname[0] == '\0') { 361 fname[0] = 'x'; 362 fpnt = fname + 1; 363 } else { 364 fpnt = fname + strlen(fname); 365 } 366 } else if (close(ofd) != 0) 367 err(1, "%s", fname); 368 369 again: 370 if (dflag) { 371 beg = '0'; 372 end = '9'; 373 } 374 else { 375 beg = 'a'; 376 end = 'z'; 377 } 378 pattlen = end - beg + 1; 379 380 /* 381 * If '-a' is not specified, then we automatically expand the 382 * suffix length to accomodate splitting all input. We do this 383 * by moving the suffix pointer (fpnt) forward and incrementing 384 * sufflen by one, thereby yielding an additional two characters 385 * and allowing all output files to sort such that 'cat *' yields 386 * the input in order. I.e., the order is '... xyy xyz xzaaa 387 * xzaab ... xzyzy, xzyzz, xzzaaaa, xzzaaab' and so on. 388 */ 389 if (!dflag && autosfx && (fpnt[0] == 'y') && 390 strspn(fpnt+1, "z") == strlen(fpnt+1)) { 391 fpnt = fname + strlen(fname) - sufflen; 392 fpnt[sufflen + 2] = '\0'; 393 fpnt[0] = end; 394 fpnt[1] = beg; 395 396 /* Basename | Suffix 397 * before: 398 * x | yz 399 * after: 400 * xz | a.. */ 401 fpnt++; 402 sufflen++; 403 404 /* Reset so we start back at all 'a's in our extended suffix. */ 405 tfnum = 0; 406 fnum = 0; 407 } 408 409 /* maxfiles = pattlen^sufflen, but don't use libm. */ 410 for (maxfiles = 1, i = 0; i < sufflen; i++) 411 if (LONG_MAX / pattlen < maxfiles) 412 errx(EX_USAGE, "suffix is too long (max %ld)", i); 413 else 414 maxfiles *= pattlen; 415 416 if (fnum == maxfiles) 417 errx(EX_DATAERR, "too many files"); 418 419 /* Generate suffix of sufflen letters */ 420 tfnum = fnum; 421 i = sufflen - 1; 422 do { 423 fpnt[i] = tfnum % pattlen + beg; 424 tfnum /= pattlen; 425 } while (i-- > 0); 426 fpnt[sufflen] = '\0'; 427 428 ++fnum; 429 if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) { 430 if (!clobber && errno == EEXIST) 431 goto again; 432 err(EX_IOERR, "%s", fname); 433 } 434 file_open = 1; 435 } 436 437 static void 438 usage(void) 439 { 440 (void)fprintf(stderr, 441 "usage: split [-cd] [-l line_count] [-a suffix_length] [file [prefix]]\n" 442 " split [-cd] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" 443 " split [-cd] -n chunk_count [-a suffix_length] [file [prefix]]\n" 444 " split [-cd] -p pattern [-a suffix_length] [file [prefix]]\n"); 445 exit(EX_USAGE); 446 } 447