[BACK]Return to split.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / split

Annotation of src/usr.bin/split/split.c, Revision 1.8

1.8     ! millert     1: /*     $OpenBSD: split.c,v 1.7 2002/12/08 16:50:07 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.8     ! millert    43: static char rcsid[] = "$OpenBSD: split.c,v 1.7 2002/12/08 16:50:07 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>
                     53: #include <stdio.h>
                     54: #include <stdlib.h>
                     55: #include <string.h>
                     56: #include <unistd.h>
1.4       millert    57: #include <regex.h>
                     58: #include <sysexits.h>
1.1       deraadt    59:
                     60: #define DEFLINE        1000                    /* Default num lines per file. */
                     61:
                     62: long    bytecnt;                       /* Byte count to split on. */
                     63: long    numlines;                      /* Line count to split on. */
                     64: int     file_open;                     /* If a file open. */
                     65: int     ifd = -1, ofd = -1;            /* Input/output file descriptors. */
                     66: char    bfr[MAXBSIZE];                 /* I/O buffer. */
                     67: char    fname[MAXPATHLEN];             /* File name prefix. */
1.4       millert    68: regex_t         rgx;
                     69: int     pflag;
1.1       deraadt    70:
1.6       millert    71: void newfile(void);
                     72: void split1(void);
                     73: void split2(void);
                     74: void usage(void);
1.1       deraadt    75:
                     76: int
                     77: main(argc, argv)
                     78:        int argc;
                     79:        char *argv[];
                     80: {
                     81:        int ch;
                     82:        char *ep, *p;
                     83:
1.7       millert    84:        while ((ch = getopt(argc, argv, "0123456789b:l:p:-")) != -1)
1.1       deraadt    85:                switch (ch) {
                     86:                case '0': case '1': case '2': case '3': case '4':
                     87:                case '5': case '6': case '7': case '8': case '9':
                     88:                        /*
                     89:                         * Undocumented kludge: split was originally designed
                     90:                         * to take a number after a dash.
                     91:                         */
                     92:                        if (numlines == 0) {
                     93:                                p = argv[optind - 1];
                     94:                                if (p[0] == '-' && p[1] == ch && !p[2])
                     95:                                        numlines = strtol(++p, &ep, 10);
                     96:                                else
                     97:                                        numlines =
                     98:                                            strtol(argv[optind] + 1, &ep, 10);
                     99:                                if (numlines <= 0 || *ep)
1.4       millert   100:                                        errx(EX_USAGE,
                    101:                                            "%s: illegal line count", optarg);
1.1       deraadt   102:                        }
                    103:                        break;
                    104:                case '-':               /* Undocumented: historic stdin flag. */
                    105:                        if (ifd != -1)
                    106:                                usage();
                    107:                        ifd = 0;
                    108:                        break;
                    109:                case 'b':               /* Byte count. */
                    110:                        if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 ||
1.4       millert   111:                            (*ep != '\0' && *ep != 'k' && *ep != 'm'))
                    112:                                errx(EX_USAGE,
                    113:                                    "%s: illegal byte count", optarg);
1.1       deraadt   114:                        if (*ep == 'k')
                    115:                                bytecnt *= 1024;
                    116:                        else if (*ep == 'm')
                    117:                                bytecnt *= 1048576;
                    118:                        break;
1.4       millert   119:                case 'p' :      /* pattern matching. */
                    120:                        if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
                    121:                                errx(EX_USAGE, "%s: illegal regexp", optarg);
                    122:                        pflag = 1;
                    123:                        break;
1.1       deraadt   124:                case 'l':               /* Line count. */
                    125:                        if (numlines != 0)
                    126:                                usage();
                    127:                        if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
1.4       millert   128:                                errx(EX_USAGE,
                    129:                                    "%s: illegal line count", optarg);
1.1       deraadt   130:                        break;
                    131:                default:
                    132:                        usage();
                    133:                }
                    134:        argv += optind;
                    135:        argc -= optind;
                    136:
                    137:        if (*argv != NULL)
                    138:                if (ifd == -1) {                /* Input file. */
                    139:                        if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
1.4       millert   140:                                err(EX_NOINPUT, "%s", *argv);
1.1       deraadt   141:                        ++argv;
                    142:                }
                    143:        if (*argv != NULL)                      /* File name prefix. */
1.5       deraadt   144:                (void)strlcpy(fname, *argv++, sizeof(fname));
1.1       deraadt   145:        if (*argv != NULL)
                    146:                usage();
                    147:
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
                    174: split1()
                    175: {
                    176:        long bcnt;
                    177:        int dist, len;
                    178:        char *C;
                    179:
                    180:        for (bcnt = 0;;)
1.4       millert   181:                switch ((len = read(ifd, bfr, MAXBSIZE))) {
1.1       deraadt   182:                case 0:
                    183:                        exit(0);
                    184:                case -1:
1.4       millert   185:                        err(EX_IOERR, "read");
1.1       deraadt   186:                        /* NOTREACHED */
                    187:                default:
1.4       millert   188:                        if (!file_open)
1.1       deraadt   189:                                newfile();
                    190:                        if (bcnt + len >= bytecnt) {
                    191:                                dist = bytecnt - bcnt;
                    192:                                if (write(ofd, bfr, dist) != dist)
1.4       millert   193:                                        err(EX_IOERR, "write");
1.1       deraadt   194:                                len -= dist;
                    195:                                for (C = bfr + dist; len >= bytecnt;
                    196:                                    len -= bytecnt, C += bytecnt) {
                    197:                                        newfile();
                    198:                                        if (write(ofd,
                    199:                                            C, (int)bytecnt) != bytecnt)
1.4       millert   200:                                                err(EX_IOERR, "write");
1.1       deraadt   201:                                }
1.4       millert   202:                                if (len != 0) {
1.1       deraadt   203:                                        newfile();
                    204:                                        if (write(ofd, C, len) != len)
1.4       millert   205:                                                err(EX_IOERR, "write");
1.1       deraadt   206:                                } else
                    207:                                        file_open = 0;
                    208:                                bcnt = len;
                    209:                        } else {
                    210:                                bcnt += len;
                    211:                                if (write(ofd, bfr, len) != len)
1.4       millert   212:                                        err(EX_IOERR, "write");
1.1       deraadt   213:                        }
                    214:                }
                    215: }
                    216:
                    217: /*
                    218:  * split2 --
                    219:  *     Split the input by lines.
                    220:  */
                    221: void
                    222: split2()
                    223: {
1.4       millert   224:        long lcnt = 0;
                    225:        FILE *infp;
1.1       deraadt   226:
1.4       millert   227:        /* Stick a stream on top of input file descriptor */
                    228:        if ((infp = fdopen(ifd, "r")) == NULL)
                    229:                err(EX_NOINPUT, "fdopen");
                    230:
                    231:        /* Process input one line at a time */
                    232:        while (fgets(bfr, sizeof(bfr), infp) != NULL) {
                    233:                const int len = strlen(bfr);
                    234:
                    235:                /* If line is too long to deal with, just write it out */
                    236:                if (bfr[len - 1] != '\n')
                    237:                        goto writeit;
                    238:
                    239:                /* Check if we need to start a new file */
                    240:                if (pflag) {
                    241:                        regmatch_t pmatch;
                    242:
                    243:                        pmatch.rm_so = 0;
                    244:                        pmatch.rm_eo = len - 1;
                    245:                        if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
1.1       deraadt   246:                                newfile();
1.4       millert   247:                } else if (lcnt++ == numlines) {
                    248:                        newfile();
                    249:                        lcnt = 1;
1.1       deraadt   250:                }
1.4       millert   251:
                    252: writeit:
                    253:                /* Open output file if needed */
                    254:                if (!file_open)
                    255:                        newfile();
                    256:
                    257:                /* Write out line */
                    258:                if (write(ofd, bfr, len) != len)
                    259:                        err(EX_IOERR, "write");
                    260:        }
                    261:
                    262:        /* EOF or error? */
                    263:        if (ferror(infp))
                    264:                err(EX_IOERR, "read");
                    265:        else
                    266:                exit(0);
1.1       deraadt   267: }
                    268:
                    269: /*
                    270:  * newfile --
                    271:  *     Open a new output file.
                    272:  */
                    273: void
                    274: newfile()
                    275: {
                    276:        static long fnum;
                    277:        static int defname;
                    278:        static char *fpnt;
                    279:
                    280:        if (ofd == -1) {
                    281:                if (fname[0] == '\0') {
                    282:                        fname[0] = 'x';
                    283:                        fpnt = fname + 1;
                    284:                        defname = 1;
                    285:                } else {
                    286:                        fpnt = fname + strlen(fname);
                    287:                        defname = 0;
                    288:                }
                    289:                ofd = fileno(stdout);
                    290:        }
                    291:        /*
                    292:         * Hack to increase max files; original code wandered through
                    293:         * magic characters.  Maximum files is 3 * 26 * 26 == 2028
                    294:         */
                    295: #define MAXFILES       676
                    296:        if (fnum == MAXFILES) {
                    297:                if (!defname || fname[0] == 'z')
1.4       millert   298:                        errx(EX_DATAERR, "too many files");
1.1       deraadt   299:                ++fname[0];
                    300:                fnum = 0;
                    301:        }
                    302:        fpnt[0] = fnum / 26 + 'a';
                    303:        fpnt[1] = fnum % 26 + 'a';
                    304:        ++fnum;
                    305:        if (!freopen(fname, "w", stdout))
1.4       millert   306:                err(EX_IOERR, "%s", fname);
                    307:        file_open = 1;
1.1       deraadt   308: }
                    309:
                    310: void
                    311: usage()
                    312: {
1.4       millert   313:        extern char *__progname;
                    314:
1.1       deraadt   315:        (void)fprintf(stderr,
1.4       millert   316: "usage: %s [-b byte_count] [-l line_count] [-p pattern] [file [prefix]]\n",
                    317: __progname);
                    318:        exit(EX_USAGE);
1.1       deraadt   319: }