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

Annotation of src/usr.bin/sort/sort.c, Revision 1.12

1.12    ! millert     1: /*     $OpenBSD: sort.c,v 1.11 1998/07/24 00:32:24 deraadt Exp $       */
1.1       millert     2:
                      3: /*-
                      4:  * Copyright (c) 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Peter McIlroy.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: static char copyright[] =
                     41: "@(#) Copyright (c) 1993\n\
                     42:        The Regents of the University of California.  All rights reserved.\n";
                     43: #endif /* not lint */
                     44:
                     45: #ifndef lint
                     46: #if 0
                     47: static char sccsid[] = "@(#)sort.c     8.1 (Berkeley) 6/6/93";
                     48: #else
1.12    ! millert    49: static char rcsid[] = "$OpenBSD: sort.c,v 1.11 1998/07/24 00:32:24 deraadt Exp $";
1.1       millert    50: #endif
                     51: #endif /* not lint */
                     52:
1.4       millert    53: /*
                     54:  * Sort sorts a file using an optional user-defined key.
1.1       millert    55:  * Sort uses radix sort for internal sorting, and allows
                     56:  * a choice of merge sort and radix sort for external sorting.
                     57:  */
                     58:
                     59: #include "sort.h"
                     60: #include "fsort.h"
                     61: #include "pathnames.h"
                     62:
                     63: #include <paths.h>
                     64: #include <signal.h>
                     65: #include <stdlib.h>
                     66: #include <string.h>
                     67: #include <unistd.h>
1.10      mickey     68: #include <err.h>
1.1       millert    69:
                     70: int REC_D = '\n';
                     71: u_char d_mask[NBINS];          /* flags for rec_d, field_d, <blank> */
1.4       millert    72:
1.1       millert    73: /*
                     74:  * weight tables.  Gweights is one of ascii, Rascii..
                     75:  * modified to weight rec_d = 0 (or 255)
                     76:  */
                     77: extern u_char gweights[NBINS];
                     78: u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
1.4       millert    79:
1.1       millert    80: /*
                     81:  * masks of ignored characters.  Alltable is 256 ones
                     82:  */
                     83: u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
                     84: int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
1.12    ! millert    85: struct coldesc *clist;
1.1       millert    86: int ncols = 0;
1.12    ! millert    87: int ND = 10;                   /* limit on number of -k options. */
1.1       millert    88:
                     89: char devstdin[] = _PATH_STDIN;
                     90: char toutpath[_POSIX_PATH_MAX];
                     91: char *tmpdir = _PATH_VARTMP;
                     92:
                     93: static void cleanup __P((void));
                     94: static void onsig __P((int));
                     95: static void usage __P((char *));
                     96:
1.12    ! millert    97: #define CHECK_NFIELDS                                          \
        !            98:        if (++nfields == ND) {                                  \
        !            99:                ND += 10;                                       \
        !           100:                if ((p = realloc(fldtab, ND)) == NULL)          \
        !           101:                        errx(2, "cannot allocate memory");      \
        !           102:                ftpos = p + (ftpos - fldtab);                   \
        !           103:                fldtab = p;                                     \
        !           104:        }
        !           105:
1.1       millert   106: int
                    107: main(argc, argv)
                    108:        int argc;
                    109:        char *argv[];
                    110: {
                    111:        int (*get)();
                    112:        int ch, i, stdinflag = 0, tmp = 0;
1.12    ! millert   113:        char nfields = 0, cflag = 0, mflag = 0;
1.1       millert   114:        char *outfile, *outpath = 0;
1.12    ! millert   115:        struct field *fldtab, *ftpos;
1.1       millert   116:        union f_handle filelist;
                    117:        FILE *outfp = NULL;
1.12    ! millert   118:        void *p;
1.4       millert   119:
1.12    ! millert   120:        if ((clist = calloc((ND+1)*2, sizeof(struct coldesc))) == NULL ||
        !           121:            (ftpos = fldtab = calloc(ND+2, sizeof(struct field))) == NULL)
        !           122:                errx(2, "cannot allocate memory");
1.1       millert   123:        memset(d_mask, 0, NBINS);
                    124:        d_mask[REC_D = '\n'] = REC_D_F;
                    125:        SINGL_FLD = SEP_FLAG = 0;
                    126:        d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
                    127:        fixit(&argc, argv);
                    128:        if (!issetugid() && (outfile = getenv("TMPDIR")))
                    129:                tmpdir = outfile;
                    130:        while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:")) != -1) {
1.4       millert   131:                switch (ch) {
1.1       millert   132:                case 'b': fldtab->flags |= BI | BT;
                    133:                        break;
                    134:                case 'd':
1.4       millert   135:                case 'f':
1.1       millert   136:                case 'i':
1.5       millert   137:                case 'n':
1.1       millert   138:                case 'r': tmp |= optval(ch, 0);
                    139:                        if (tmp & R && tmp & F)
                    140:                                fldtab->weights = RFtable;
                    141:                        else if (tmp & F)
                    142:                                fldtab->weights = Ftable;
1.4       millert   143:                        else if (tmp & R)
1.1       millert   144:                                fldtab->weights = Rascii;
                    145:                        fldtab->flags |= tmp;
                    146:                        break;
                    147:                case 'o':
                    148:                        outpath = optarg;
                    149:                        break;
                    150:                case 'k':
1.12    ! millert   151:                        CHECK_NFIELDS;
1.5       millert   152:                        setfield(optarg, ++ftpos, fldtab->flags);
1.1       millert   153:                        break;
                    154:                case 't':
                    155:                        if (SEP_FLAG)
                    156:                                usage("multiple field delimiters");
                    157:                        SEP_FLAG = 1;
                    158:                        d_mask[' '] &= ~FLD_D;
                    159:                        d_mask['\t'] &= ~FLD_D;
                    160:                        d_mask[(int)*optarg] |= FLD_D;
                    161:                        if (d_mask[(int)*optarg] & REC_D_F)
                    162:                                err(2, "record/field delimiter clash");
                    163:                        break;
                    164:                case 'R':
                    165:                        if (REC_D != '\n')
                    166:                                usage("multiple record delimiters");
                    167:                        if ('\n' == (REC_D = *optarg))
                    168:                                break;
                    169:                        d_mask['\n'] = d_mask[' '];
                    170:                        d_mask[REC_D] = REC_D_F;
                    171:                        break;
                    172:                case 'T':
                    173:                        tmpdir = optarg;
                    174:                        break;
                    175:                case 'u':
                    176:                        UNIQUE = 1;
                    177:                        break;
                    178:                case 'c':
                    179:                        cflag = 1;
                    180:                        break;
                    181:                case 'm':
                    182:                        mflag = 1;
                    183:                        break;
                    184:                case 'H':
                    185:                        PANIC = 0;
                    186:                        break;
                    187:                case 'y':
                    188:                        /* accept -y for backwards compat. */
                    189:                        break;
                    190:                case '?':
1.8       deraadt   191:                default:
                    192:                        usage(NULL);
1.1       millert   193:                }
                    194:        }
1.4       millert   195:
1.1       millert   196:        if (cflag && argc > optind+1)
                    197:                errx(2, "too many input files for -c option");
1.4       millert   198:
1.1       millert   199:        if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
                    200:                outpath = argv[argc-1];
                    201:                argc -= 2;
                    202:        }
1.4       millert   203:
1.1       millert   204:        if (mflag && argc - optind > (MAXFCT - (16+1))*16)
                    205:                errx(2, "too many input files for -m option");
1.4       millert   206:
1.1       millert   207:        for (i = optind; i < argc; i++) {
                    208:                /* allow one occurrence of /dev/stdin */
                    209:                if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
                    210:                        if (stdinflag)
                    211:                                warnx("ignoring extra \"%s\" in file list",
                    212:                                    argv[i]);
                    213:                        else {
                    214:                                stdinflag = 1;
                    215:                                argv[i] = devstdin;
                    216:                        }
                    217:                } else if ((ch = access(argv[i], R_OK)))
1.2       millert   218:                        err(2, argv[i]);
1.1       millert   219:        }
1.4       millert   220:
1.5       millert   221:        if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
1.1       millert   222:                SINGL_FLD = 1;
                    223:                fldtab[0].icol.num = 1;
                    224:        } else {
                    225:                if (!fldtab[1].icol.num) {
1.12    ! millert   226:                        CHECK_NFIELDS;
1.1       millert   227:                        fldtab[0].flags &= ~(BI|BT);
                    228:                        setfield("1", ++ftpos, fldtab->flags);
                    229:                }
                    230:                fldreset(fldtab);
                    231:                fldtab[0].flags &= ~F;
                    232:        }
                    233:        settables(fldtab[0].flags);
                    234:        num_init();
                    235:        fldtab->weights = gweights;
1.4       millert   236:
1.3       deraadt   237:        if (optind == argc) {
                    238:                static char *names[2];
                    239:
                    240:                names[0] = devstdin;
                    241:                names[1] = NULL;
                    242:                filelist.names = names;
                    243:                optind--;
                    244:        } else
                    245:                filelist.names = argv+optind;
1.4       millert   246:
1.1       millert   247:        if (SINGL_FLD)
                    248:                get = makeline;
                    249:        else
                    250:                get = makekey;
1.4       millert   251:
1.1       millert   252:        if (cflag) {
                    253:                order(filelist, get, fldtab);
                    254:                /* NOT REACHED */
                    255:        }
1.4       millert   256:
1.1       millert   257:        if (!outpath) {
                    258:                (void)snprintf(toutpath,
                    259:                    sizeof(toutpath), "%sstdout", _PATH_DEV);
                    260:                outfile = outpath = toutpath;
                    261:        } else if (!(ch = access(outpath, 0)) &&
                    262:            strncmp(_PATH_DEV, outpath, 5)) {
                    263:                struct sigaction act = {0, SIG_BLOCK, 6};
                    264:                int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
                    265:                    SIGVTALRM, SIGPROF, 0};
                    266:                int outfd;
1.4       millert   267:
1.1       millert   268:                errno = 0;
1.4       millert   269:
1.1       millert   270:                if (access(outpath, W_OK))
1.2       millert   271:                        err(2, outpath);
1.1       millert   272:                act.sa_handler = onsig;
1.4       millert   273:                (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
                    274:                    outpath);
1.1       millert   275:                if ((outfd = mkstemp(toutpath)) < 0 ||
                    276:                    (outfp = fdopen(outfd, "w")) == 0)
                    277:                        err(2, toutpath);
                    278:                outfile = toutpath;
1.4       millert   279:
1.1       millert   280:                (void)atexit(cleanup);
                    281:                for (i = 0; sigtable[i]; ++i)   /* always unlink toutpath */
                    282:                        sigaction(sigtable[i], &act, 0);
                    283:        } else
                    284:                outfile = outpath;
                    285:        if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
                    286:                err(2, outfile);
                    287:        if (mflag)
                    288:                fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
                    289:        else
                    290:                fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
                    291:        if (outfile != outpath) {
                    292:                if (access(outfile, 0))
1.2       millert   293:                        err(2, outfile);
1.1       millert   294:                (void)unlink(outpath);
                    295:                if (link(outfile, outpath))
                    296:                        err(2, "cannot link %s: output left in %s",
                    297:                            outpath, outfile);
                    298:                (void)unlink(outfile);
                    299:        }
                    300:        exit(0);
                    301: }
                    302:
                    303: static void
                    304: onsig(s)
                    305:        int s;
                    306: {
1.4       millert   307:
1.1       millert   308:        cleanup();
                    309:        exit(2);                        /* return 2 on error/interrupt */
                    310: }
                    311:
                    312: static void
                    313: cleanup()
                    314: {
1.4       millert   315:
1.1       millert   316:        if (toutpath[0])
                    317:                (void)unlink(toutpath);
                    318: }
                    319:
                    320: static void
                    321: usage(msg)
                    322:        char *msg;
                    323: {
1.8       deraadt   324:        extern char *__progname;
1.4       millert   325:
1.1       millert   326:        if (msg)
1.10      mickey    327:                warnx(msg);
1.11      deraadt   328:        (void)fprintf(stderr, "usage: %s [-T dir] [-o output] [-cmubdfinrH] "
1.9       deraadt   329:            "[-t char] [-R char] [-k keydef] ... [files]\n", __progname);
1.1       millert   330:        exit(2);
                    331: }