]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/split/split.c
split: Fix linecount parsing.
[FreeBSD/FreeBSD.git] / usr.bin / split / split.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1987, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1987, 1993, 1994\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif
38
39 #ifndef lint
40 static const char sccsid[] = "@(#)split.c       8.2 (Berkeley) 4/16/94";
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/stat.h>
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <inttypes.h>
51 #include <libutil.h>
52 #include <limits.h>
53 #include <locale.h>
54 #include <stdbool.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <regex.h>
61 #include <sysexits.h>
62
63 #define DEFLINE 1000                    /* Default num lines per file. */
64
65 static off_t     bytecnt;               /* Byte count to split on. */
66 static off_t     chunks = 0;            /* Chunks count to split into. */
67 static bool      clobber = true;        /* Whether to overwrite existing output files. */
68 static long      numlines;              /* Line count to split on. */
69 static int       file_open;             /* If a file open. */
70 static int       ifd = -1, ofd = -1;    /* Input/output file descriptors. */
71 static char      fname[MAXPATHLEN];     /* File name prefix. */
72 static regex_t   rgx;
73 static int       pflag;
74 static bool      dflag;
75 static long      sufflen = 2;           /* File name suffix length. */
76 static int       autosfx = 1;           /* Whether to auto-extend the suffix length. */
77
78 static void newfile(void);
79 static void split1(void);
80 static void split2(void);
81 static void split3(void);
82 static void usage(void) __dead2;
83
84 int
85 main(int argc, char **argv)
86 {
87         const char *p;
88         char *ep;
89         int ch, error;
90
91         setlocale(LC_ALL, "");
92
93         dflag = false;
94         while ((ch = getopt(argc, argv, "0::1::2::3::4::5::6::7::8::9::a:b:cdl:n:p:")) != -1)
95                 switch (ch) {
96                 case '0': case '1': case '2': case '3': case '4':
97                 case '5': case '6': case '7': case '8': case '9':
98                         /*
99                          * Undocumented kludge: split was originally designed
100                          * to take a number after a dash.
101                          */
102                         if (numlines != 0)
103                                 usage();
104                         numlines = ch - '0';
105                         p = optarg ? optarg : "";
106                         while (numlines >= 0 && *p >= '0' && *p <= '9')
107                                 numlines = numlines * 10 + *p++ - '0';
108                         if (numlines <= 0 || *p != '\0')
109                                 errx(EX_USAGE, "%c%s: illegal line count", ch,
110                                     optarg ? optarg : "");
111                         break;
112                 case 'a':               /* Suffix length */
113                         if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
114                                 errx(EX_USAGE,
115                                     "%s: illegal suffix length", optarg);
116                         autosfx = 0;
117                         break;
118                 case 'b':               /* Byte count. */
119                         errno = 0;
120                         error = expand_number(optarg, &bytecnt);
121                         if (error == -1)
122                                 errx(EX_USAGE, "%s: offset too large", optarg);
123                         break;
124                 case 'c':               /* Continue, don't overwrite output files. */
125                         clobber = false;
126                         break;
127                 case 'd':               /* Decimal suffix */
128                         dflag = true;
129                         break;
130                 case 'l':               /* Line count. */
131                         if (numlines != 0)
132                                 usage();
133                         if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
134                                 errx(EX_USAGE,
135                                     "%s: illegal line count", optarg);
136                         break;
137                 case 'n':               /* Chunks. */
138                         if (!isdigit((unsigned char)optarg[0]) ||
139                             (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
140                             *ep != '\0') {
141                                 errx(EX_USAGE, "%s: illegal number of chunks",
142                                      optarg);
143                         }
144                         break;
145
146                 case 'p':               /* pattern matching. */
147                         if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
148                                 errx(EX_USAGE, "%s: illegal regexp", optarg);
149                         pflag = 1;
150                         break;
151                 default:
152                         usage();
153                 }
154         argv += optind;
155         argc -= optind;
156
157         if (argc > 0) {                 /* Input file. */
158                 if (strcmp(*argv, "-") == 0)
159                         ifd = STDIN_FILENO;
160                 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
161                         err(EX_NOINPUT, "%s", *argv);
162                 ++argv;
163                 --argc;
164         }
165         if (argc > 0) {                 /* File name prefix. */
166                 if (strlcpy(fname, *argv, sizeof(fname)) >= sizeof(fname))
167                         errx(EX_USAGE, "file name prefix is too long: %s",
168                             *argv);
169                 ++argv;
170                 --argc;
171         }
172         if (argc > 0)
173                 usage();
174
175         if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
176                 errx(EX_USAGE, "suffix is too long");
177         if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
178                 usage();
179
180         if (numlines == 0)
181                 numlines = DEFLINE;
182         else if (bytecnt != 0 || chunks != 0)
183                 usage();
184
185         if (bytecnt && chunks)
186                 usage();
187
188         if (ifd == -1)                          /* Stdin by default. */
189                 ifd = 0;
190
191         if (bytecnt) {
192                 split1();
193                 exit (0);
194         } else if (chunks) {
195                 split3();
196                 exit (0);
197         }
198         split2();
199         if (pflag)
200                 regfree(&rgx);
201         exit(0);
202 }
203
204 /*
205  * split1 --
206  *      Split the input by bytes.
207  */
208 static void
209 split1(void)
210 {
211         static char bfr[MAXBSIZE];
212         off_t bcnt;
213         char *C;
214         ssize_t dist, len;
215         int nfiles;
216
217         nfiles = 0;
218
219         for (bcnt = 0;;)
220                 switch ((len = read(ifd, bfr, sizeof(bfr)))) {
221                 case 0:
222                         exit(0);
223                 case -1:
224                         err(EX_IOERR, "read");
225                         /* NOTREACHED */
226                 default:
227                         if (!file_open) {
228                                 if (!chunks || (nfiles < chunks)) {
229                                         newfile();
230                                         nfiles++;
231                                 }
232                         }
233                         if (bcnt + len >= bytecnt) {
234                                 dist = bytecnt - bcnt;
235                                 if (write(ofd, bfr, dist) != dist)
236                                         err(EX_IOERR, "write");
237                                 len -= dist;
238                                 for (C = bfr + dist; len >= bytecnt;
239                                     len -= bytecnt, C += bytecnt) {
240                                         if (!chunks || (nfiles < chunks)) {
241                                         newfile();
242                                                 nfiles++;
243                                         }
244                                         if (write(ofd,
245                                             C, bytecnt) != bytecnt)
246                                                 err(EX_IOERR, "write");
247                                 }
248                                 if (len != 0) {
249                                         if (!chunks || (nfiles < chunks)) {
250                                         newfile();
251                                                 nfiles++;
252                                         }
253                                         if (write(ofd, C, len) != len)
254                                                 err(EX_IOERR, "write");
255                                 } else
256                                         file_open = 0;
257                                 bcnt = len;
258                         } else {
259                                 bcnt += len;
260                                 if (write(ofd, bfr, len) != len)
261                                         err(EX_IOERR, "write");
262                         }
263                 }
264 }
265
266 /*
267  * split2 --
268  *      Split the input by lines.
269  */
270 static void
271 split2(void)
272 {
273         char *buf;
274         size_t bufsize;
275         ssize_t len;
276         long lcnt = 0;
277         FILE *infp;
278
279         buf = NULL;
280         bufsize = 0;
281
282         /* Stick a stream on top of input file descriptor */
283         if ((infp = fdopen(ifd, "r")) == NULL)
284                 err(EX_NOINPUT, "fdopen");
285
286         /* Process input one line at a time */
287         while ((errno = 0, len = getline(&buf, &bufsize, infp)) > 0) {
288                 /* Check if we need to start a new file */
289                 if (pflag) {
290                         regmatch_t pmatch;
291
292                         pmatch.rm_so = 0;
293                         pmatch.rm_eo = len - 1;
294                         if (regexec(&rgx, buf, 0, &pmatch, REG_STARTEND) == 0)
295                                 newfile();
296                 } else if (lcnt++ == numlines) {
297                         newfile();
298                         lcnt = 1;
299                 }
300
301                 /* Open output file if needed */
302                 if (!file_open)
303                         newfile();
304
305                 /* Write out line */
306                 if (write(ofd, buf, len) != len)
307                         err(EX_IOERR, "write");
308         }
309
310         /* EOF or error? */
311         if ((len == -1 && errno != 0) || ferror(infp))
312                 err(EX_IOERR, "read");
313         else
314                 exit(0);
315 }
316
317 /*
318  * split3 --
319  *      Split the input into specified number of chunks
320  */
321 static void
322 split3(void)
323 {
324         struct stat sb;
325
326         if (fstat(ifd, &sb) == -1) {
327                 err(1, "stat");
328                 /* NOTREACHED */
329         }
330
331         if (chunks > sb.st_size) {
332                 errx(1, "can't split into more than %d files",
333                     (int)sb.st_size);
334                 /* NOTREACHED */
335         }
336
337         bytecnt = sb.st_size / chunks;
338         split1();
339 }
340
341
342 /*
343  * newfile --
344  *      Open a new output file.
345  */
346 static void
347 newfile(void)
348 {
349         long i, maxfiles, tfnum;
350         static long fnum;
351         static char *fpnt;
352         char beg, end;
353         int pattlen;
354         int flags = O_WRONLY | O_CREAT | O_TRUNC;
355
356         if (!clobber)
357                 flags |= O_EXCL;
358
359         if (ofd == -1) {
360                 if (fname[0] == '\0') {
361                         fname[0] = 'x';
362                         fpnt = fname + 1;
363                 } else {
364                         fpnt = fname + strlen(fname);
365                 }
366         } else if (close(ofd) != 0)
367                 err(1, "%s", fname);
368
369         again:
370         if (dflag) {
371                 beg = '0';
372                 end = '9';
373         }
374         else {
375                 beg = 'a';
376                 end = 'z';
377         }
378         pattlen = end - beg + 1;
379
380         /*
381          * If '-a' is not specified, then we automatically expand the
382          * suffix length to accomodate splitting all input.  We do this
383          * by moving the suffix pointer (fpnt) forward and incrementing
384          * sufflen by one, thereby yielding an additional two characters
385          * and allowing all output files to sort such that 'cat *' yields
386          * the input in order.  I.e., the order is '... xyy xyz xzaaa
387          * xzaab ... xzyzy, xzyzz, xzzaaaa, xzzaaab' and so on.
388          */
389         if (!dflag && autosfx && (fpnt[0] == 'y') &&
390                         strspn(fpnt+1, "z") == strlen(fpnt+1)) {
391                 fpnt = fname + strlen(fname) - sufflen;
392                 fpnt[sufflen + 2] = '\0';
393                 fpnt[0] = end;
394                 fpnt[1] = beg;
395
396                 /*  Basename | Suffix
397                  *  before:
398                  *  x        | yz
399                  *  after:
400                  *  xz       | a.. */
401                 fpnt++;
402                 sufflen++;
403
404                 /* Reset so we start back at all 'a's in our extended suffix. */
405                 fnum = 0;
406         }
407
408         /* maxfiles = pattlen^sufflen, but don't use libm. */
409         for (maxfiles = 1, i = 0; i < sufflen; i++)
410                 if (LONG_MAX / pattlen < maxfiles)
411                         errx(EX_USAGE, "suffix is too long (max %ld)", i);
412                 else
413                         maxfiles *= pattlen;
414
415         if (fnum == maxfiles)
416                 errx(EX_DATAERR, "too many files");
417
418         /* Generate suffix of sufflen letters */
419         tfnum = fnum;
420         i = sufflen - 1;
421         do {
422                 fpnt[i] = tfnum % pattlen + beg;
423                 tfnum /= pattlen;
424         } while (i-- > 0);
425         fpnt[sufflen] = '\0';
426
427         ++fnum;
428         if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) {
429                 if (!clobber && errno == EEXIST)
430                         goto again;
431                 err(EX_IOERR, "%s", fname);
432         }
433         file_open = 1;
434 }
435
436 static void
437 usage(void)
438 {
439         (void)fprintf(stderr,
440 "usage: split [-cd] [-l line_count] [-a suffix_length] [file [prefix]]\n"
441 "       split [-cd] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
442 "       split [-cd] -n chunk_count [-a suffix_length] [file [prefix]]\n"
443 "       split [-cd] -p pattern [-a suffix_length] [file [prefix]]\n");
444         exit(EX_USAGE);
445 }