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

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