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

1.6     ! millert     1: /*     $OpenBSD: sort.c,v 1.5 1997/06/16 02:39:15 millert 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.6     ! millert    49: static char rcsid[] = "$OpenBSD: sort.c,v 1.5 1997/06/16 02:39:15 millert 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 '?':
                    179:                default: usage("");
                    180:                }
                    181:        }
1.4       millert   182:
1.1       millert   183:        if (cflag && argc > optind+1)
                    184:                errx(2, "too many input files for -c option");
1.4       millert   185:
1.1       millert   186:        if (argc - 2 > optind && !strcmp(argv[argc-2], "-o")) {
                    187:                outpath = argv[argc-1];
                    188:                argc -= 2;
                    189:        }
1.4       millert   190:
1.1       millert   191:        if (mflag && argc - optind > (MAXFCT - (16+1))*16)
                    192:                errx(2, "too many input files for -m option");
1.4       millert   193:
1.1       millert   194:        for (i = optind; i < argc; i++) {
                    195:                /* allow one occurrence of /dev/stdin */
                    196:                if (!strcmp(argv[i], "-") || !strcmp(argv[i], devstdin)) {
                    197:                        if (stdinflag)
                    198:                                warnx("ignoring extra \"%s\" in file list",
                    199:                                    argv[i]);
                    200:                        else {
                    201:                                stdinflag = 1;
                    202:                                argv[i] = devstdin;
                    203:                        }
                    204:                } else if ((ch = access(argv[i], R_OK)))
1.2       millert   205:                        err(2, argv[i]);
1.1       millert   206:        }
1.4       millert   207:
1.5       millert   208:        if (!(fldtab->flags & (I|D|N) || fldtab[1].icol.num)) {
1.1       millert   209:                SINGL_FLD = 1;
                    210:                fldtab[0].icol.num = 1;
                    211:        } else {
                    212:                if (!fldtab[1].icol.num) {
                    213:                        fldtab[0].flags &= ~(BI|BT);
                    214:                        setfield("1", ++ftpos, fldtab->flags);
                    215:                }
                    216:                fldreset(fldtab);
                    217:                fldtab[0].flags &= ~F;
                    218:        }
                    219:        settables(fldtab[0].flags);
                    220:        num_init();
                    221:        fldtab->weights = gweights;
1.4       millert   222:
1.3       deraadt   223:        if (optind == argc) {
                    224:                static char *names[2];
                    225:
                    226:                names[0] = devstdin;
                    227:                names[1] = NULL;
                    228:                filelist.names = names;
                    229:                optind--;
                    230:        } else
                    231:                filelist.names = argv+optind;
1.4       millert   232:
1.1       millert   233:        if (SINGL_FLD)
                    234:                get = makeline;
                    235:        else
                    236:                get = makekey;
1.4       millert   237:
1.1       millert   238:        if (cflag) {
                    239:                order(filelist, get, fldtab);
                    240:                /* NOT REACHED */
                    241:        }
1.4       millert   242:
1.1       millert   243:        if (!outpath) {
                    244:                (void)snprintf(toutpath,
                    245:                    sizeof(toutpath), "%sstdout", _PATH_DEV);
                    246:                outfile = outpath = toutpath;
                    247:        } else if (!(ch = access(outpath, 0)) &&
                    248:            strncmp(_PATH_DEV, outpath, 5)) {
                    249:                struct sigaction act = {0, SIG_BLOCK, 6};
                    250:                int sigtable[] = {SIGHUP, SIGINT, SIGPIPE, SIGXCPU, SIGXFSZ,
                    251:                    SIGVTALRM, SIGPROF, 0};
                    252:                int outfd;
1.4       millert   253:
1.1       millert   254:                errno = 0;
1.4       millert   255:
1.1       millert   256:                if (access(outpath, W_OK))
1.2       millert   257:                        err(2, outpath);
1.1       millert   258:                act.sa_handler = onsig;
1.4       millert   259:                (void)snprintf(toutpath, sizeof(toutpath), "%sXXXXXXXXXX",
                    260:                    outpath);
1.1       millert   261:                if ((outfd = mkstemp(toutpath)) < 0 ||
                    262:                    (outfp = fdopen(outfd, "w")) == 0)
                    263:                        err(2, toutpath);
                    264:                outfile = toutpath;
1.4       millert   265:
1.1       millert   266:                (void)atexit(cleanup);
                    267:                for (i = 0; sigtable[i]; ++i)   /* always unlink toutpath */
                    268:                        sigaction(sigtable[i], &act, 0);
                    269:        } else
                    270:                outfile = outpath;
                    271:        if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
                    272:                err(2, outfile);
                    273:        if (mflag)
                    274:                fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
                    275:        else
                    276:                fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
                    277:        if (outfile != outpath) {
                    278:                if (access(outfile, 0))
1.2       millert   279:                        err(2, outfile);
1.1       millert   280:                (void)unlink(outpath);
                    281:                if (link(outfile, outpath))
                    282:                        err(2, "cannot link %s: output left in %s",
                    283:                            outpath, outfile);
                    284:                (void)unlink(outfile);
                    285:        }
                    286:        exit(0);
                    287: }
                    288:
                    289: static void
                    290: onsig(s)
                    291:        int s;
                    292: {
1.4       millert   293:
1.1       millert   294:        cleanup();
                    295:        exit(2);                        /* return 2 on error/interrupt */
                    296: }
                    297:
                    298: static void
                    299: cleanup()
                    300: {
1.4       millert   301:
1.1       millert   302:        if (toutpath[0])
                    303:                (void)unlink(toutpath);
                    304: }
                    305:
                    306: static void
                    307: usage(msg)
                    308:        char *msg;
                    309: {
1.4       millert   310:
1.1       millert   311:        if (msg)
                    312:                (void)fprintf(stderr, "sort: %s\n", msg);
1.2       millert   313:        (void)fprintf(stderr, "usage: [-T dir] [-o output] [-cmubdfinr] ");
                    314:        (void)fprintf(stderr, "[-t char] [-R char] [-k keydef] ... [files]\n");
1.1       millert   315:        exit(2);
                    316: }