[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.8

1.8     ! deraadt     1: /*     $OpenBSD: sort.c,v 1.7 1998/07/20 19:14:37 mickey 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.8     ! deraadt    49: static char rcsid[] = "$OpenBSD: sort.c,v 1.7 1998/07/20 19:14:37 mickey 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>
                     68:
                     69: int REC_D = '\n';
                     70: u_char d_mask[NBINS];          /* flags for rec_d, field_d, <blank> */
1.4       millert    71:
1.1       millert    72: /*
                     73:  * weight tables.  Gweights is one of ascii, Rascii..
                     74:  * modified to weight rec_d = 0 (or 255)
                     75:  */
                     76: extern u_char gweights[NBINS];
                     77: u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
1.4       millert    78:
1.1       millert    79: /*
                     80:  * masks of ignored characters.  Alltable is 256 ones
                     81:  */
                     82: u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
                     83: int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
                     84: struct coldesc clist[(ND+1)*2];
                     85: int ncols = 0;
                     86: extern struct coldesc clist[(ND+1)*2];
                     87: extern int ncols;
                     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:
                     97: int
                     98: main(argc, argv)
                     99:        int argc;
                    100:        char *argv[];
                    101: {
                    102:        int (*get)();
                    103:        int ch, i, stdinflag = 0, tmp = 0;
1.6       millert   104:        char cflag = 0, mflag = 0;
1.1       millert   105:        char *outfile, *outpath = 0;
                    106:        struct field fldtab[ND+2], *ftpos;
                    107:        union f_handle filelist;
                    108:        FILE *outfp = NULL;
1.4       millert   109:
1.1       millert   110:        memset(fldtab, 0, (ND+2)*sizeof(struct field));
                    111:        memset(d_mask, 0, NBINS);
                    112:        d_mask[REC_D = '\n'] = REC_D_F;
                    113:        SINGL_FLD = SEP_FLAG = 0;
                    114:        d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
                    115:        ftpos = fldtab;
                    116:        fixit(&argc, argv);
                    117:        if (!issetugid() && (outfile = getenv("TMPDIR")))
                    118:                tmpdir = outfile;
                    119:        while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:")) != -1) {
1.4       millert   120:                switch (ch) {
1.1       millert   121:                case 'b': fldtab->flags |= BI | BT;
                    122:                        break;
                    123:                case 'd':
1.4       millert   124:                case 'f':
1.1       millert   125:                case 'i':
1.5       millert   126:                case 'n':
1.1       millert   127:                case 'r': tmp |= optval(ch, 0);
                    128:                        if (tmp & R && tmp & F)
                    129:                                fldtab->weights = RFtable;
                    130:                        else if (tmp & F)
                    131:                                fldtab->weights = Ftable;
1.4       millert   132:                        else if (tmp & R)
1.1       millert   133:                                fldtab->weights = Rascii;
                    134:                        fldtab->flags |= tmp;
                    135:                        break;
                    136:                case 'o':
                    137:                        outpath = optarg;
                    138:                        break;
                    139:                case 'k':
1.5       millert   140:                        setfield(optarg, ++ftpos, fldtab->flags);
1.1       millert   141:                        break;
                    142:                case 't':
                    143:                        if (SEP_FLAG)
                    144:                                usage("multiple field delimiters");
                    145:                        SEP_FLAG = 1;
                    146:                        d_mask[' '] &= ~FLD_D;
                    147:                        d_mask['\t'] &= ~FLD_D;
                    148:                        d_mask[(int)*optarg] |= FLD_D;
                    149:                        if (d_mask[(int)*optarg] & REC_D_F)
                    150:                                err(2, "record/field delimiter clash");
                    151:                        break;
                    152:                case 'R':
                    153:                        if (REC_D != '\n')
                    154:                                usage("multiple record delimiters");
                    155:                        if ('\n' == (REC_D = *optarg))
                    156:                                break;
                    157:                        d_mask['\n'] = d_mask[' '];
                    158:                        d_mask[REC_D] = REC_D_F;
                    159:                        break;
                    160:                case 'T':
                    161:                        tmpdir = optarg;
                    162:                        break;
                    163:                case 'u':
                    164:                        UNIQUE = 1;
                    165:                        break;
                    166:                case 'c':
                    167:                        cflag = 1;
                    168:                        break;
                    169:                case 'm':
                    170:                        mflag = 1;
                    171:                        break;
                    172:                case 'H':
                    173:                        PANIC = 0;
                    174:                        break;
                    175:                case 'y':
                    176:                        /* accept -y for backwards compat. */
                    177:                        break;
                    178:                case '?':
1.8     ! deraadt   179:                default:
        !           180:                        usage(NULL);
1.1       millert   181:                }
                    182:        }
1.4       millert   183:
1.1       millert   184:        if (cflag && argc > optind+1)
                    185:                errx(2, "too many input files for -c option");
1.4       millert   186:
1.1       millert   187:        if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
                    188:                outpath = argv[argc-1];
                    189:                argc -= 2;
                    190:        }
1.4       millert   191:
1.1       millert   192:        if (mflag && argc - optind > (MAXFCT - (16+1))*16)
                    193:                errx(2, "too many input files for -m option");
1.4       millert   194:
1.1       millert   195:        for (i = optind; i < argc; i++) {
                    196:                /* allow one occurrence of /dev/stdin */
                    197:                if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
                    198:                        if (stdinflag)
                    199:                                warnx("ignoring extra \"%s\" in file list",
                    200:                                    argv[i]);
                    201:                        else {
                    202:                                stdinflag = 1;
                    203:                                argv[i] = devstdin;
                    204:                        }
                    205:                } else if ((ch = access(argv[i], R_OK)))
1.2       millert   206:                        err(2, argv[i]);
1.1       millert   207:        }
1.4       millert   208:
1.5       millert   209:        if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
1.1       millert   210:                SINGL_FLD = 1;
                    211:                fldtab[0].icol.num = 1;
                    212:        } else {
                    213:                if (!fldtab[1].icol.num) {
                    214:                        fldtab[0].flags &= ~(BI|BT);
                    215:                        setfield("1", ++ftpos, fldtab->flags);
                    216:                }
                    217:                fldreset(fldtab);
                    218:                fldtab[0].flags &= ~F;
                    219:        }
                    220:        settables(fldtab[0].flags);
                    221:        num_init();
                    222:        fldtab->weights = gweights;
1.4       millert   223:
1.3       deraadt   224:        if (optind == argc) {
                    225:                static char *names[2];
                    226:
                    227:                names[0] = devstdin;
                    228:                names[1] = NULL;
                    229:                filelist.names = names;
                    230:                optind--;
                    231:        } else
                    232:                filelist.names = argv+optind;
1.4       millert   233:
1.1       millert   234:        if (SINGL_FLD)
                    235:                get = makeline;
                    236:        else
                    237:                get = makekey;
1.4       millert   238:
1.1       millert   239:        if (cflag) {
                    240:                order(filelist, get, fldtab);
                    241:                /* NOT REACHED */
                    242:        }
1.4       millert   243:
1.1       millert   244:        if (!outpath) {
                    245:                (void)snprintf(toutpath,
                    246:                    sizeof(toutpath), "%sstdout", _PATH_DEV);
                    247:                outfile = outpath = toutpath;
                    248:        } else if (!(ch = access(outpath, 0)) &&
                    249:            strncmp(_PATH_DEV, outpath, 5)) {
                    250:                struct sigaction act = {0, SIG_BLOCK, 6};
                    251:                int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
                    252:                    SIGVTALRM, SIGPROF, 0};
                    253:                int outfd;
1.4       millert   254:
1.1       millert   255:                errno = 0;
1.4       millert   256:
1.1       millert   257:                if (access(outpath, W_OK))
1.2       millert   258:                        err(2, outpath);
1.1       millert   259:                act.sa_handler = onsig;
1.4       millert   260:                (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
                    261:                    outpath);
1.1       millert   262:                if ((outfd = mkstemp(toutpath)) < 0 ||
                    263:                    (outfp = fdopen(outfd, "w")) == 0)
                    264:                        err(2, toutpath);
                    265:                outfile = toutpath;
1.4       millert   266:
1.1       millert   267:                (void)atexit(cleanup);
                    268:                for (i = 0; sigtable[i]; ++i)   /* always unlink toutpath */
                    269:                        sigaction(sigtable[i], &act, 0);
                    270:        } else
                    271:                outfile = outpath;
                    272:        if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
                    273:                err(2, outfile);
                    274:        if (mflag)
                    275:                fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
                    276:        else
                    277:                fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
                    278:        if (outfile != outpath) {
                    279:                if (access(outfile, 0))
1.2       millert   280:                        err(2, outfile);
1.1       millert   281:                (void)unlink(outpath);
                    282:                if (link(outfile, outpath))
                    283:                        err(2, "cannot link %s: output left in %s",
                    284:                            outpath, outfile);
                    285:                (void)unlink(outfile);
                    286:        }
                    287:        exit(0);
                    288: }
                    289:
                    290: static void
                    291: onsig(s)
                    292:        int s;
                    293: {
1.4       millert   294:
1.1       millert   295:        cleanup();
                    296:        exit(2);                        /* return 2 on error/interrupt */
                    297: }
                    298:
                    299: static void
                    300: cleanup()
                    301: {
1.4       millert   302:
1.1       millert   303:        if (toutpath[0])
                    304:                (void)unlink(toutpath);
                    305: }
                    306:
                    307: static void
                    308: usage(msg)
                    309:        char *msg;
                    310: {
1.8     ! deraadt   311:        extern char *__progname;
1.4       millert   312:
1.1       millert   313:        if (msg)
                    314:                (void)fprintf(stderr, "sort: %s\n", msg);
1.8     ! deraadt   315:        (void)fprintf(stderr, "usage: %s [-T dir] [-o output] [-cmubdfinr] "
        !           316:        "[-t char] [-R char] [-k keydef] ... [files]\n", __progname);
1.1       millert   317:        exit(2);
                    318: }