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 * @(#) Copyright (c) 1987, 1993, 1994 The Regents of the University of California. All rights reserved. 34 * @(#)split.c 8.2 (Berkeley) 4/16/94 35 * $FreeBSD: src/usr.bin/split/split.c,v 1.6.2.2 2002/07/25 12:46:36 tjr Exp $ 36 */ 37 38 #include <sys/param.h> 39 40 #include <ctype.h> 41 #include <err.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <limits.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <regex.h> 50 #include <sysexits.h> 51 52 #define DEFLINE 1000 /* Default num lines per file. */ 53 54 off_t bytecnt; /* Byte count to split on. */ 55 long numlines; /* Line count to split on. */ 56 int file_open; /* If a file open. */ 57 int ifd = -1, ofd = -1; /* Input/output file descriptors. */ 58 char *bfr; /* I/O buffer. */ 59 char fname[MAXPATHLEN]; /* File name prefix. */ 60 regex_t rgx; 61 int pflag; 62 long sufflen = 2; /* File name suffix length. */ 63 64 void newfile(void); 65 void split1(void); 66 void split2(void); 67 static void usage(void); 68 69 int 70 main(int argc, char **argv) 71 { 72 long long bytecnti; 73 long scale; 74 int ch; 75 char *ep, *p; 76 77 while ((ch = getopt(argc, argv, "0123456789a:b:l:p:")) != -1) 78 switch (ch) { 79 case '0': case '1': case '2': case '3': case '4': 80 case '5': case '6': case '7': case '8': case '9': 81 /* 82 * Undocumented kludge: split was originally designed 83 * to take a number after a dash. 84 */ 85 if (numlines == 0) { 86 p = argv[optind - 1]; 87 if (p[0] == '-' && p[1] == ch && !p[2]) 88 numlines = strtol(++p, &ep, 10); 89 else 90 numlines = 91 strtol(argv[optind] + 1, &ep, 10); 92 if (numlines <= 0 || *ep) 93 errx(EX_USAGE, 94 "%s: illegal line count", optarg); 95 } 96 break; 97 case 'a': /* Suffix length */ 98 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep) 99 errx(EX_USAGE, 100 "%s: illegal suffix length", optarg); 101 break; 102 case 'b': /* Byte count. */ 103 errno = 0; 104 if ((bytecnti = strtoll(optarg, &ep, 10)) <= 0 || 105 (*ep != '\0' && *ep != 'k' && *ep != 'm') || 106 errno != 0) 107 errx(EX_USAGE, 108 "%s: illegal byte count", optarg); 109 if (*ep == 'k') 110 scale = 1024; 111 else if (*ep == 'm') 112 scale = 1024 * 1024; 113 else 114 scale = 1; 115 bytecnt = (off_t)(bytecnti * scale); 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 (strcmp(*argv, "-") == 0) 137 ifd = STDIN_FILENO; 138 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0) 139 err(EX_NOINPUT, "%s", *argv); 140 ++argv; 141 } 142 143 if (*argv != NULL) /* File name prefix. */ 144 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname)) 145 errx(EX_USAGE, "file name prefix is too long"); 146 if (*argv != NULL) 147 usage(); 148 149 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname)) 150 errx(EX_USAGE, "suffix is too long"); 151 if (pflag && (numlines != 0 || bytecnt != 0)) 152 usage(); 153 154 if (numlines == 0) 155 numlines = DEFLINE; 156 else if (bytecnt != 0) 157 usage(); 158 159 if (ifd == -1) /* Stdin by default. */ 160 ifd = 0; 161 162 if (bytecnt) { 163 split1(); 164 exit (0); 165 } 166 split2(); 167 if (pflag) 168 regfree(&rgx); 169 exit(0); 170 } 171 172 /* 173 * split1 -- 174 * Split the input by bytes. 175 */ 176 void 177 split1(void) 178 { 179 off_t bcnt; 180 char *C; 181 ssize_t dist, len; 182 183 if((bfr = (char *)malloc(bytecnt)) == NULL) 184 err(EX_OSERR, "malloc"); 185 186 for (bcnt = 0;;) 187 switch ((len = read(ifd, bfr, bytecnt))) { 188 case 0: 189 free(bfr); 190 exit(0); 191 case -1: 192 free(bfr); 193 err(EX_IOERR, "read"); 194 /* NOTREACHED */ 195 default: 196 if (!file_open) 197 newfile(); 198 if (bcnt + len >= bytecnt) { 199 dist = bytecnt - bcnt; 200 if (write(ofd, bfr, dist) != dist) 201 err(EX_IOERR, "write"); 202 len -= dist; 203 for (C = bfr + dist; len >= bytecnt; 204 len -= bytecnt, C += bytecnt) { 205 newfile(); 206 if (write(ofd, 207 C, (int)bytecnt) != bytecnt) { 208 free(bfr); 209 err(EX_IOERR, "write"); 210 } 211 } 212 if (len != 0) { 213 newfile(); 214 if (write(ofd, C, len) != len) { 215 free(bfr); 216 err(EX_IOERR, "write"); 217 } 218 } else 219 file_open = 0; 220 bcnt = len; 221 } else { 222 bcnt += len; 223 if (write(ofd, bfr, len) != len) { 224 free(bfr); 225 err(EX_IOERR, "write"); 226 } 227 } 228 } 229 free(bfr); 230 } 231 232 /* 233 * split2 -- 234 * Split the input by lines. 235 */ 236 void 237 split2(void) 238 { 239 int startofline = 1; 240 long lcnt = 0; 241 FILE *infp; 242 243 /* Stick a stream on top of input file descriptor */ 244 if ((infp = fdopen(ifd, "r")) == NULL) 245 err(EX_NOINPUT, "fdopen"); 246 247 if((bfr = (char *)malloc(MAXBSIZE)) == NULL) 248 err(EX_OSERR, "malloc"); 249 250 /* Process input one line at a time */ 251 while (fgets(bfr, MAXBSIZE, infp) != NULL) { 252 const int len = strlen(bfr); 253 254 /* Consider starting a new file only when at beginning of a line */ 255 if (startofline) { 256 /* Check if we need to start a new file */ 257 if (pflag) { 258 regmatch_t pmatch; 259 260 pmatch.rm_so = 0; 261 pmatch.rm_eo = len - 1; 262 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0) 263 newfile(); 264 } else if (lcnt++ == numlines) { 265 newfile(); 266 lcnt = 1; 267 } 268 } 269 270 if (bfr[len - 1] != '\n') 271 startofline = 0; 272 else 273 startofline = 1; 274 275 /* Open output file if needed */ 276 if (!file_open) 277 newfile(); 278 279 /* Write out line */ 280 if (write(ofd, bfr, len) != len) { 281 free(bfr); 282 err(EX_IOERR, "write"); 283 } 284 } 285 286 free(bfr); 287 288 /* EOF or error? */ 289 if (ferror(infp)) 290 err(EX_IOERR, "read"); 291 else 292 exit(0); 293 } 294 295 /* 296 * newfile -- 297 * Open a new output file. 298 */ 299 void 300 newfile(void) 301 { 302 long i, maxfiles, tfnum; 303 static long fnum; 304 static char *fpnt; 305 306 if (ofd == -1) { 307 if (fname[0] == '\0') { 308 fname[0] = 'x'; 309 fpnt = fname + 1; 310 } else { 311 fpnt = fname + strlen(fname); 312 } 313 ofd = fileno(stdout); 314 } 315 316 /* maxfiles = 26^sufflen, but don't use libm. */ 317 for (maxfiles = 1, i = 0; i < sufflen; i++) 318 if ((maxfiles *= 26) <= 0) 319 errx(EX_USAGE, "suffix is too long (max %ld)", i); 320 321 if (fnum == maxfiles) 322 errx(EX_DATAERR, "too many files"); 323 324 /* Generate suffix of sufflen letters */ 325 tfnum = fnum; 326 i = sufflen - 1; 327 do { 328 fpnt[i] = tfnum % 26 + 'a'; 329 tfnum /= 26; 330 } while (i-- > 0); 331 fpnt[sufflen] = '\0'; 332 333 ++fnum; 334 if (!freopen(fname, "w", stdout)) 335 err(EX_IOERR, "%s", fname); 336 file_open = 1; 337 } 338 339 static void 340 usage(void) 341 { 342 (void)fprintf(stderr, 343 "usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n"); 344 (void)fprintf(stderr, 345 " [file [prefix]]\n"); 346 exit(EX_USAGE); 347 } 348