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