Annotation of src/usr.bin/split/split.c, Revision 1.18
1.18 ! deraadt 1: /* $OpenBSD: split.c,v 1.17 2009/10/27 23:59:43 deraadt Exp $ */
1.1 deraadt 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.
1.8 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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:
1.18 ! deraadt 33: #include <sys/param.h> /* MAXBSIZE */
1.4 millert 34: #include <sys/types.h>
1.1 deraadt 35:
36: #include <ctype.h>
37: #include <err.h>
38: #include <fcntl.h>
1.13 millert 39: #include <limits.h>
1.1 deraadt 40: #include <stdio.h>
41: #include <stdlib.h>
42: #include <string.h>
43: #include <unistd.h>
1.4 millert 44: #include <regex.h>
45: #include <sysexits.h>
1.1 deraadt 46:
47: #define DEFLINE 1000 /* Default num lines per file. */
48:
1.13 millert 49: ssize_t bytecnt; /* Byte count to split on. */
1.1 deraadt 50: long numlines; /* Line count to split on. */
51: int file_open; /* If a file open. */
52: int ifd = -1, ofd = -1; /* Input/output file descriptors. */
53: char bfr[MAXBSIZE]; /* I/O buffer. */
1.18 ! deraadt 54: char fname[PATH_MAX]; /* File name prefix. */
1.4 millert 55: regex_t rgx;
56: int pflag;
1.12 millert 57: int sufflen = 2; /* File name suffix length. */
1.1 deraadt 58:
1.6 millert 59: void newfile(void);
60: void split1(void);
61: void split2(void);
1.13 millert 62: __dead void usage(void);
1.1 deraadt 63:
64: int
1.9 deraadt 65: main(int argc, char *argv[])
1.1 deraadt 66: {
1.13 millert 67: int ch, scale;
1.1 deraadt 68: char *ep, *p;
1.12 millert 69: const char *errstr;
1.1 deraadt 70:
1.12 millert 71: while ((ch = getopt(argc, argv, "0123456789a:b:l:p:-")) != -1)
1.1 deraadt 72: switch (ch) {
73: case '0': case '1': case '2': case '3': case '4':
74: case '5': case '6': case '7': case '8': case '9':
75: /*
76: * Undocumented kludge: split was originally designed
77: * to take a number after a dash.
78: */
79: if (numlines == 0) {
80: p = argv[optind - 1];
81: if (p[0] == '-' && p[1] == ch && !p[2])
82: numlines = strtol(++p, &ep, 10);
83: else
84: numlines =
85: strtol(argv[optind] + 1, &ep, 10);
86: if (numlines <= 0 || *ep)
1.4 millert 87: errx(EX_USAGE,
88: "%s: illegal line count", optarg);
1.1 deraadt 89: }
90: break;
91: case '-': /* Undocumented: historic stdin flag. */
92: if (ifd != -1)
93: usage();
94: ifd = 0;
95: break;
1.12 millert 96: case 'a': /* suffix length. */
97: sufflen = strtonum(optarg, 1, NAME_MAX, &errstr);
98: if (errstr)
99: errx(EX_USAGE, "%s: %s", optarg, errstr);
100: break;
1.1 deraadt 101: case 'b': /* Byte count. */
102: if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 ||
1.4 millert 103: (*ep != '\0' && *ep != 'k' && *ep != 'm'))
104: errx(EX_USAGE,
105: "%s: illegal byte count", optarg);
1.1 deraadt 106: if (*ep == 'k')
1.13 millert 107: scale = 1024;
1.1 deraadt 108: else if (*ep == 'm')
1.13 millert 109: scale = 1048576;
110: else
111: scale = 1;
112: if (bytecnt > SSIZE_MAX / scale)
113: errx(EX_USAGE, "%s: byte count too large",
114: optarg);
115: bytecnt *= scale;
1.1 deraadt 116: break;
1.4 millert 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;
1.1 deraadt 122: case 'l': /* Line count. */
123: if (numlines != 0)
124: usage();
125: if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
1.4 millert 126: errx(EX_USAGE,
127: "%s: illegal line count", optarg);
1.1 deraadt 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)
1.4 millert 138: err(EX_NOINPUT, "%s", *argv);
1.1 deraadt 139: ++argv;
140: }
141: if (*argv != NULL) /* File name prefix. */
1.5 deraadt 142: (void)strlcpy(fname, *argv++, sizeof(fname));
1.1 deraadt 143: if (*argv != NULL)
144: usage();
145:
1.12 millert 146: if (strlen(fname) + sufflen >= sizeof(fname))
147: errx(EX_USAGE, "suffix is too long");
1.4 millert 148: if (pflag && (numlines != 0 || bytecnt != 0))
149: usage();
150:
1.1 deraadt 151: if (numlines == 0)
152: numlines = DEFLINE;
1.4 millert 153: else if (bytecnt != 0)
1.1 deraadt 154: usage();
155:
156: if (ifd == -1) /* Stdin by default. */
157: ifd = 0;
158:
159: if (bytecnt) {
160: split1();
161: exit (0);
162: }
163: split2();
1.4 millert 164: if (pflag)
165: regfree(&rgx);
1.1 deraadt 166: exit(0);
167: }
168:
169: /*
170: * split1 --
171: * Split the input by bytes.
172: */
173: void
1.9 deraadt 174: split1(void)
1.1 deraadt 175: {
1.13 millert 176: ssize_t bcnt, dist, len;
1.1 deraadt 177: char *C;
178:
179: for (bcnt = 0;;)
1.4 millert 180: switch ((len = read(ifd, bfr, MAXBSIZE))) {
1.1 deraadt 181: case 0:
182: exit(0);
183: case -1:
1.4 millert 184: err(EX_IOERR, "read");
1.1 deraadt 185: /* NOTREACHED */
186: default:
1.4 millert 187: if (!file_open)
1.1 deraadt 188: newfile();
189: if (bcnt + len >= bytecnt) {
190: dist = bytecnt - bcnt;
191: if (write(ofd, bfr, dist) != dist)
1.4 millert 192: err(EX_IOERR, "write");
1.1 deraadt 193: len -= dist;
194: for (C = bfr + dist; len >= bytecnt;
195: len -= bytecnt, C += bytecnt) {
196: newfile();
1.13 millert 197: if (write(ofd, C, bytecnt) != bytecnt)
1.4 millert 198: err(EX_IOERR, "write");
1.1 deraadt 199: }
1.4 millert 200: if (len != 0) {
1.1 deraadt 201: newfile();
202: if (write(ofd, C, len) != len)
1.4 millert 203: err(EX_IOERR, "write");
1.1 deraadt 204: } else
205: file_open = 0;
206: bcnt = len;
207: } else {
208: bcnt += len;
209: if (write(ofd, bfr, len) != len)
1.4 millert 210: err(EX_IOERR, "write");
1.1 deraadt 211: }
212: }
213: }
214:
215: /*
216: * split2 --
217: * Split the input by lines.
218: */
219: void
1.9 deraadt 220: split2(void)
1.1 deraadt 221: {
1.4 millert 222: long lcnt = 0;
223: FILE *infp;
1.1 deraadt 224:
1.4 millert 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);
1.15 chl 232:
233: if (len == 0)
234: continue;
1.4 millert 235:
236: /* If line is too long to deal with, just write it out */
237: if (bfr[len - 1] != '\n')
238: goto writeit;
239:
240: /* Check if we need to start a new file */
241: if (pflag) {
242: regmatch_t pmatch;
243:
244: pmatch.rm_so = 0;
245: pmatch.rm_eo = len - 1;
246: if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
1.1 deraadt 247: newfile();
1.4 millert 248: } else if (lcnt++ == numlines) {
249: newfile();
250: lcnt = 1;
1.1 deraadt 251: }
1.4 millert 252:
253: writeit:
254: /* Open output file if needed */
255: if (!file_open)
256: newfile();
257:
258: /* Write out line */
259: if (write(ofd, bfr, len) != len)
260: err(EX_IOERR, "write");
261: }
262:
263: /* EOF or error? */
264: if (ferror(infp))
265: err(EX_IOERR, "read");
266: else
267: exit(0);
1.1 deraadt 268: }
269:
270: /*
271: * newfile --
272: * Open a new output file.
273: */
274: void
1.9 deraadt 275: newfile(void)
1.1 deraadt 276: {
1.12 millert 277: static char *suffix, *sufftail;
1.14 millert 278: char *sptr;
1.1 deraadt 279:
280: if (ofd == -1) {
1.14 millert 281: ofd = fileno(stdout);
282: if (*fname == '\0') {
283: *fname = 'x'; /* no name specified, use 'x' */
284: memset(fname + 1, 'a', sufflen);
285: suffix = fname;
286: sufflen++; /* treat 'x' as part of suffix */
1.1 deraadt 287: } else {
1.12 millert 288: suffix = fname + strlen(fname);
1.14 millert 289: memset(suffix, 'a', sufflen);
1.1 deraadt 290: }
1.12 millert 291: suffix[sufflen] = '\0';
292: sufftail = suffix + sufflen - 1;
1.14 millert 293: } else {
294: for (sptr = sufftail; sptr >= suffix; sptr--) {
295: if (*sptr != 'z') {
296: (*sptr)++;
1.12 millert 297: break;
1.14 millert 298: } else
299: *sptr = 'a';
1.12 millert 300: }
1.14 millert 301: if (sptr < suffix)
302: errx(EX_DATAERR, "too many files");
303: }
1.12 millert 304:
1.1 deraadt 305: if (!freopen(fname, "w", stdout))
1.4 millert 306: err(EX_IOERR, "%s", fname);
307: file_open = 1;
1.1 deraadt 308: }
309:
1.13 millert 310: __dead void
1.9 deraadt 311: usage(void)
1.1 deraadt 312: {
1.4 millert 313: extern char *__progname;
314:
1.16 sobrado 315: (void)fprintf(stderr, "usage: %s [-a suffix_length]\n"
316: " [-b byte_count[k|m] | -l line_count | -p pattern] "
317: "[file [name]]\n", __progname);
1.4 millert 318: exit(EX_USAGE);
1.1 deraadt 319: }