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