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