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

1.30    ! naddy       1: /*     $OpenBSD: sort.c,v 1.29 2006/10/18 23:30:43 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.
1.20      millert    18:  * 3. Neither the name of the University nor the names of its contributors
1.1       millert    19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef lint
                     36: static char copyright[] =
                     37: "@(#) Copyright (c) 1993\n\
                     38:        The Regents of the University of California.  All rights reserved.\n";
                     39: #endif /* not lint */
                     40:
                     41: #ifndef lint
                     42: #if 0
                     43: static char sccsid[] = "@(#)sort.c     8.1 (Berkeley) 6/6/93";
                     44: #else
1.30    ! naddy      45: static char rcsid[] = "$OpenBSD: sort.c,v 1.29 2006/10/18 23:30:43 millert Exp $";
1.1       millert    46: #endif
                     47: #endif /* not lint */
                     48:
1.4       millert    49: /*
                     50:  * Sort sorts a file using an optional user-defined key.
1.1       millert    51:  * Sort uses radix sort for internal sorting, and allows
                     52:  * a choice of merge sort and radix sort for external sorting.
                     53:  */
                     54:
                     55: #include "sort.h"
                     56: #include "fsort.h"
                     57: #include "pathnames.h"
                     58:
1.13      espie      59: #include <sys/types.h>
                     60: #include <sys/stat.h>
1.16      ericj      61: #include <locale.h>
1.1       millert    62: #include <paths.h>
                     63: #include <signal.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
                     66: #include <unistd.h>
1.10      mickey     67: #include <err.h>
1.1       millert    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;
1.12      millert    84: struct coldesc *clist;
1.1       millert    85: int ncols = 0;
1.12      millert    86: int ND = 10;                   /* limit on number of -k options. */
1.1       millert    87:
1.19      millert    88: char *devstdin = _PATH_STDIN;
1.1       millert    89: char *tmpdir = _PATH_VARTMP;
1.19      millert    90: char toutpath[PATH_MAX];
1.1       millert    91:
1.18      millert    92: static void cleanup(void);
                     93: static void onsig(int);
                     94: static void usage(char *);
1.1       millert    95:
1.12      millert    96: #define CHECK_NFIELDS                                          \
                     97:        if (++nfields == ND) {                                  \
                     98:                ND += 10;                                       \
                     99:                if ((p = realloc(fldtab, ND)) == NULL)          \
                    100:                        errx(2, "cannot allocate memory");      \
                    101:                ftpos = p + (ftpos - fldtab);                   \
                    102:                fldtab = p;                                     \
                    103:        }
                    104:
1.1       millert   105: int
1.21      deraadt   106: main(int argc, char *argv[])
1.1       millert   107: {
1.22      deraadt   108:        int (*get)(int, union f_handle, int, RECHEADER *, u_char *, struct field *);
1.1       millert   109:        int ch, i, stdinflag = 0, tmp = 0;
1.12      millert   110:        char nfields = 0, cflag = 0, mflag = 0;
1.1       millert   111:        char *outfile, *outpath = 0;
1.12      millert   112:        struct field *fldtab, *ftpos;
1.1       millert   113:        union f_handle filelist;
                    114:        FILE *outfp = NULL;
1.12      millert   115:        void *p;
1.4       millert   116:
1.16      ericj     117:        setlocale(LC_ALL, "");
                    118:
1.12      millert   119:        if ((clist = calloc((ND+1)*2, sizeof(struct coldesc))) == NULL ||
                    120:            (ftpos = fldtab = calloc(ND+2, sizeof(struct field))) == NULL)
                    121:                errx(2, "cannot allocate memory");
1.1       millert   122:        memset(d_mask, 0, NBINS);
                    123:        d_mask[REC_D = '\n'] = REC_D_F;
                    124:        d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
                    125:        fixit(&argc, argv);
                    126:        if (!issetugid() && (outfile = getenv("TMPDIR")))
                    127:                tmpdir = outfile;
1.26      dlg       128:        while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:z")) != -1) {
1.4       millert   129:                switch (ch) {
1.1       millert   130:                case 'b': fldtab->flags |= BI | BT;
                    131:                        break;
                    132:                case 'd':
1.4       millert   133:                case 'f':
1.1       millert   134:                case 'i':
1.5       millert   135:                case 'n':
1.1       millert   136:                case 'r': tmp |= optval(ch, 0);
                    137:                        if (tmp & R && tmp & F)
                    138:                                fldtab->weights = RFtable;
                    139:                        else if (tmp & F)
                    140:                                fldtab->weights = Ftable;
1.4       millert   141:                        else if (tmp & R)
1.1       millert   142:                                fldtab->weights = Rascii;
                    143:                        fldtab->flags |= tmp;
                    144:                        break;
                    145:                case 'o':
                    146:                        outpath = optarg;
                    147:                        break;
                    148:                case 'k':
1.12      millert   149:                        CHECK_NFIELDS;
1.5       millert   150:                        setfield(optarg, ++ftpos, fldtab->flags);
1.1       millert   151:                        break;
                    152:                case 't':
                    153:                        if (SEP_FLAG)
                    154:                                usage("multiple field delimiters");
                    155:                        SEP_FLAG = 1;
                    156:                        d_mask[' '] &= ~FLD_D;
                    157:                        d_mask['\t'] &= ~FLD_D;
                    158:                        d_mask[(int)*optarg] |= FLD_D;
                    159:                        if (d_mask[(int)*optarg] & REC_D_F)
                    160:                                err(2, "record/field delimiter clash");
                    161:                        break;
                    162:                case 'R':
                    163:                        if (REC_D != '\n')
                    164:                                usage("multiple record delimiters");
                    165:                        if ('\n' == (REC_D = *optarg))
                    166:                                break;
                    167:                        d_mask['\n'] = d_mask[' '];
                    168:                        d_mask[REC_D] = REC_D_F;
                    169:                        break;
                    170:                case 'T':
                    171:                        tmpdir = optarg;
                    172:                        break;
                    173:                case 'u':
                    174:                        UNIQUE = 1;
                    175:                        break;
                    176:                case 'c':
                    177:                        cflag = 1;
                    178:                        break;
                    179:                case 'm':
                    180:                        mflag = 1;
                    181:                        break;
                    182:                case 'H':
                    183:                        PANIC = 0;
                    184:                        break;
                    185:                case 'y':
                    186:                        /* accept -y for backwards compat. */
                    187:                        break;
1.26      dlg       188:                case 'z':
                    189:                        if (REC_D != '\n')
                    190:                                usage("multiple record delimiters");
                    191:                        REC_D = '\0';
                    192:                        d_mask['\n'] = d_mask[' '];
                    193:                        d_mask[REC_D] = REC_D_F;
                    194:                        break;
1.1       millert   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:                um = umask(S_IWGRP|S_IWOTH);
1.25      moritz    281:                (void)umask(um);
1.13      espie     282:                if ((outfd = mkstemp(toutpath)) == -1 ||
                    283:                    fchmod(outfd, DEFFILEMODE & ~um) == -1 ||
1.1       millert   284:                    (outfp = fdopen(outfd, "w")) == 0)
1.14      millert   285:                        err(2, "%s", toutpath);
1.1       millert   286:                outfile = toutpath;
1.4       millert   287:
1.1       millert   288:                (void)atexit(cleanup);
1.17      millert   289:                sigfillset(&act.sa_mask);
                    290:                act.sa_flags = SA_RESTART;
                    291:                act.sa_handler = onsig;
1.1       millert   292:                for (i = 0; sigtable[i]; ++i)   /* always unlink toutpath */
                    293:                        sigaction(sigtable[i], &act, 0);
                    294:        } else
                    295:                outfile = outpath;
                    296:        if (outfp == NULL && (outfp = fopen(outfile, "w")) == NULL)
1.14      millert   297:                err(2, "%s", outfile);
1.1       millert   298:        if (mflag)
                    299:                fmerge(-1, filelist, argc-optind, get, outfp, putline, fldtab);
                    300:        else
                    301:                fsort(-1, 0, filelist, argc-optind, outfp, fldtab);
                    302:        if (outfile != outpath) {
                    303:                if (access(outfile, 0))
1.14      millert   304:                        err(2, "%s", outfile);
1.1       millert   305:                (void)unlink(outpath);
                    306:                if (link(outfile, outpath))
                    307:                        err(2, "cannot link %s: output left in %s",
                    308:                            outpath, outfile);
                    309:                (void)unlink(outfile);
                    310:        }
                    311:        exit(0);
                    312: }
                    313:
1.24      deraadt   314: /* ARGSUSED */
1.1       millert   315: static void
1.21      deraadt   316: onsig(int signo)
1.1       millert   317: {
1.4       millert   318:
1.1       millert   319:        cleanup();
1.15      deraadt   320:        _exit(2);                       /* return 2 on error/interrupt */
1.1       millert   321: }
                    322:
                    323: static void
1.21      deraadt   324: cleanup(void)
1.1       millert   325: {
1.4       millert   326:
1.1       millert   327:        if (toutpath[0])
                    328:                (void)unlink(toutpath);
                    329: }
                    330:
                    331: static void
1.21      deraadt   332: usage(char *msg)
1.1       millert   333: {
1.8       deraadt   334:        extern char *__progname;
1.4       millert   335:
1.16      ericj     336:        if (msg != NULL)
1.14      millert   337:                warnx("%s", msg);
1.27      jmc       338:        (void)fprintf(stderr, "usage: %s [-bcdfHimnruz] "
1.28      jmc       339:            "[-k field1[,field2]] [-o output] [-R char]\n"
1.27      jmc       340:            "\t[-T dir] [-t char] [file ...]\n", __progname);
1.1       millert   341:        exit(2);
                    342: }