Annotation of src/usr.bin/split/split.c, Revision 1.20
1.20 ! deraadt 1: /* $OpenBSD: split.c,v 1.19 2015/10/07 14:37:11 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.19 deraadt 70:
1.20 ! deraadt 71: if (pledge("stdio rpath wpath cpath", NULL) == -1)
! 72: err(1, "pledge");
1.1 deraadt 73:
1.12 millert 74: while ((ch = getopt(argc, argv, "0123456789a:b:l:p:-")) != -1)
1.1 deraadt 75: switch (ch) {
76: case '0': case '1': case '2': case '3': case '4':
77: case '5': case '6': case '7': case '8': case '9':
78: /*
79: * Undocumented kludge: split was originally designed
80: * to take a number after a dash.
81: */
82: if (numlines == 0) {
83: p = argv[optind - 1];
84: if (p[0] == '-' && p[1] == ch && !p[2])
85: numlines = strtol(++p, &ep, 10);
86: else
87: numlines =
88: strtol(argv[optind] + 1, &ep, 10);
89: if (numlines <= 0 || *ep)
1.4 millert 90: errx(EX_USAGE,
91: "%s: illegal line count", optarg);
1.1 deraadt 92: }
93: break;
94: case '-': /* Undocumented: historic stdin flag. */
95: if (ifd != -1)
96: usage();
97: ifd = 0;
98: break;
1.12 millert 99: case 'a': /* suffix length. */
100: sufflen = strtonum(optarg, 1, NAME_MAX, &errstr);
101: if (errstr)
102: errx(EX_USAGE, "%s: %s", optarg, errstr);
103: break;
1.1 deraadt 104: case 'b': /* Byte count. */
105: if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 ||
1.4 millert 106: (*ep != '\0' && *ep != 'k' && *ep != 'm'))
107: errx(EX_USAGE,
108: "%s: illegal byte count", optarg);
1.1 deraadt 109: if (*ep == 'k')
1.13 millert 110: scale = 1024;
1.1 deraadt 111: else if (*ep == 'm')
1.13 millert 112: scale = 1048576;
113: else
114: scale = 1;
115: if (bytecnt > SSIZE_MAX / scale)
116: errx(EX_USAGE, "%s: byte count too large",
117: optarg);
118: bytecnt *= scale;
1.1 deraadt 119: break;
1.4 millert 120: case 'p' : /* pattern matching. */
121: if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
122: errx(EX_USAGE, "%s: illegal regexp", optarg);
123: pflag = 1;
124: break;
1.1 deraadt 125: case 'l': /* Line count. */
126: if (numlines != 0)
127: usage();
128: if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
1.4 millert 129: errx(EX_USAGE,
130: "%s: illegal line count", optarg);
1.1 deraadt 131: break;
132: default:
133: usage();
134: }
135: argv += optind;
136: argc -= optind;
137:
138: if (*argv != NULL)
139: if (ifd == -1) { /* Input file. */
140: if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
1.4 millert 141: err(EX_NOINPUT, "%s", *argv);
1.1 deraadt 142: ++argv;
143: }
144: if (*argv != NULL) /* File name prefix. */
1.5 deraadt 145: (void)strlcpy(fname, *argv++, sizeof(fname));
1.1 deraadt 146: if (*argv != NULL)
147: usage();
148:
1.12 millert 149: if (strlen(fname) + sufflen >= sizeof(fname))
150: errx(EX_USAGE, "suffix is too long");
1.4 millert 151: if (pflag && (numlines != 0 || bytecnt != 0))
152: usage();
153:
1.1 deraadt 154: if (numlines == 0)
155: numlines = DEFLINE;
1.4 millert 156: else if (bytecnt != 0)
1.1 deraadt 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();
1.4 millert 167: if (pflag)
168: regfree(&rgx);
1.1 deraadt 169: exit(0);
170: }
171:
172: /*
173: * split1 --
174: * Split the input by bytes.
175: */
176: void
1.9 deraadt 177: split1(void)
1.1 deraadt 178: {
1.13 millert 179: ssize_t bcnt, dist, len;
1.1 deraadt 180: char *C;
181:
182: for (bcnt = 0;;)
1.4 millert 183: switch ((len = read(ifd, bfr, MAXBSIZE))) {
1.1 deraadt 184: case 0:
185: exit(0);
186: case -1:
1.4 millert 187: err(EX_IOERR, "read");
1.1 deraadt 188: /* NOTREACHED */
189: default:
1.4 millert 190: if (!file_open)
1.1 deraadt 191: newfile();
192: if (bcnt + len >= bytecnt) {
193: dist = bytecnt - bcnt;
194: if (write(ofd, bfr, dist) != dist)
1.4 millert 195: err(EX_IOERR, "write");
1.1 deraadt 196: len -= dist;
197: for (C = bfr + dist; len >= bytecnt;
198: len -= bytecnt, C += bytecnt) {
199: newfile();
1.13 millert 200: if (write(ofd, C, bytecnt) != bytecnt)
1.4 millert 201: err(EX_IOERR, "write");
1.1 deraadt 202: }
1.4 millert 203: if (len != 0) {
1.1 deraadt 204: newfile();
205: if (write(ofd, C, len) != len)
1.4 millert 206: err(EX_IOERR, "write");
1.1 deraadt 207: } else
208: file_open = 0;
209: bcnt = len;
210: } else {
211: bcnt += len;
212: if (write(ofd, bfr, len) != len)
1.4 millert 213: err(EX_IOERR, "write");
1.1 deraadt 214: }
215: }
216: }
217:
218: /*
219: * split2 --
220: * Split the input by lines.
221: */
222: void
1.9 deraadt 223: split2(void)
1.1 deraadt 224: {
1.4 millert 225: long lcnt = 0;
226: FILE *infp;
1.1 deraadt 227:
1.4 millert 228: /* Stick a stream on top of input file descriptor */
229: if ((infp = fdopen(ifd, "r")) == NULL)
230: err(EX_NOINPUT, "fdopen");
231:
232: /* Process input one line at a time */
233: while (fgets(bfr, sizeof(bfr), infp) != NULL) {
234: const int len = strlen(bfr);
1.15 chl 235:
236: if (len == 0)
237: continue;
1.4 millert 238:
239: /* If line is too long to deal with, just write it out */
240: if (bfr[len - 1] != '\n')
241: goto writeit;
242:
243: /* Check if we need to start a new file */
244: if (pflag) {
245: regmatch_t pmatch;
246:
247: pmatch.rm_so = 0;
248: pmatch.rm_eo = len - 1;
249: if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
1.1 deraadt 250: newfile();
1.4 millert 251: } else if (lcnt++ == numlines) {
252: newfile();
253: lcnt = 1;
1.1 deraadt 254: }
1.4 millert 255:
256: writeit:
257: /* Open output file if needed */
258: if (!file_open)
259: newfile();
260:
261: /* Write out line */
262: if (write(ofd, bfr, len) != len)
263: err(EX_IOERR, "write");
264: }
265:
266: /* EOF or error? */
267: if (ferror(infp))
268: err(EX_IOERR, "read");
269: else
270: exit(0);
1.1 deraadt 271: }
272:
273: /*
274: * newfile --
275: * Open a new output file.
276: */
277: void
1.9 deraadt 278: newfile(void)
1.1 deraadt 279: {
1.12 millert 280: static char *suffix, *sufftail;
1.14 millert 281: char *sptr;
1.1 deraadt 282:
283: if (ofd == -1) {
1.14 millert 284: ofd = fileno(stdout);
285: if (*fname == '\0') {
286: *fname = 'x'; /* no name specified, use 'x' */
287: memset(fname + 1, 'a', sufflen);
288: suffix = fname;
289: sufflen++; /* treat 'x' as part of suffix */
1.1 deraadt 290: } else {
1.12 millert 291: suffix = fname + strlen(fname);
1.14 millert 292: memset(suffix, 'a', sufflen);
1.1 deraadt 293: }
1.12 millert 294: suffix[sufflen] = '\0';
295: sufftail = suffix + sufflen - 1;
1.14 millert 296: } else {
297: for (sptr = sufftail; sptr >= suffix; sptr--) {
298: if (*sptr != 'z') {
299: (*sptr)++;
1.12 millert 300: break;
1.14 millert 301: } else
302: *sptr = 'a';
1.12 millert 303: }
1.14 millert 304: if (sptr < suffix)
305: errx(EX_DATAERR, "too many files");
306: }
1.12 millert 307:
1.1 deraadt 308: if (!freopen(fname, "w", stdout))
1.4 millert 309: err(EX_IOERR, "%s", fname);
310: file_open = 1;
1.1 deraadt 311: }
312:
1.13 millert 313: __dead void
1.9 deraadt 314: usage(void)
1.1 deraadt 315: {
1.4 millert 316: extern char *__progname;
317:
1.16 sobrado 318: (void)fprintf(stderr, "usage: %s [-a suffix_length]\n"
319: " [-b byte_count[k|m] | -l line_count | -p pattern] "
320: "[file [name]]\n", __progname);
1.4 millert 321: exit(EX_USAGE);
1.1 deraadt 322: }