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

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