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

1.60    ! millert     1: /*     $OpenBSD: sort.c,v 1.59 2015/04/01 20:58:13 millert Exp $       */
1.1       millert     2:
                      3: /*-
1.44      millert     4:  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
                      5:  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
                      6:  * All rights reserved.
1.1       millert     7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
1.44      millert    17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1.1       millert    18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.44      millert    20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.1       millert    21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
1.48      millert    30: #include <sys/resource.h>
1.44      millert    31: #include <sys/stat.h>
1.48      millert    32: #include <sys/sysctl.h>
1.44      millert    33: #include <sys/types.h>
1.1       millert    34:
1.44      millert    35: #include <err.h>
                     36: #include <errno.h>
                     37: #include <getopt.h>
                     38: #include <limits.h>
1.16      ericj      39: #include <locale.h>
1.44      millert    40: #include <md5.h>
                     41: #include <regex.h>
1.1       millert    42: #include <signal.h>
1.44      millert    43: #include <stdbool.h>
                     44: #include <stdio.h>
1.1       millert    45: #include <stdlib.h>
                     46: #include <string.h>
                     47: #include <unistd.h>
1.44      millert    48: #include <wchar.h>
                     49: #include <wctype.h>
                     50:
                     51: #include "coll.h"
                     52: #include "file.h"
                     53: #include "sort.h"
                     54:
                     55: #define        OPTIONS "bCcdfgHhik:Mmno:RrS:st:T:uVz"
                     56:
                     57: static bool need_random;
                     58: static const char *random_source;
                     59:
                     60: MD5_CTX md5_ctx;
                     61:
                     62: struct sort_opts sort_opts_vals;
                     63:
                     64: bool debug_sort;
                     65: bool need_hint;
                     66:
                     67: static bool gnusort_numeric_compatibility;
                     68:
                     69: static struct sort_mods default_sort_mods_object;
                     70: struct sort_mods * const default_sort_mods = &default_sort_mods_object;
                     71:
                     72: static bool print_symbols_on_debug;
                     73:
                     74: /*
                     75:  * Arguments from file (when file0-from option is used:
                     76:  */
                     77: static size_t argc_from_file0 = (size_t)-1;
                     78: static char **argv_from_file0;
                     79:
                     80: /*
                     81:  * Placeholder symbols for options which have no single-character equivalent
                     82:  */
                     83: enum {
                     84:        SORT_OPT = CHAR_MAX + 1,
                     85:        HELP_OPT,
                     86:        FF_OPT,
                     87:        BS_OPT,
                     88:        VERSION_OPT,
                     89:        DEBUG_OPT,
                     90:        RANDOMSOURCE_OPT,
                     91:        COMPRESSPROGRAM_OPT,
                     92:        QSORT_OPT,
                     93:        HEAPSORT_OPT,
                     94:        RADIXSORT_OPT,
                     95:        MMAP_OPT
                     96: };
                     97:
                     98: #define        NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6
                     99: static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' };
                    100:
                    101: static const struct option long_options[] = {
                    102:     { "batch-size", required_argument, NULL, BS_OPT },
                    103:     { "buffer-size", required_argument, NULL, 'S' },
                    104:     { "check", optional_argument, NULL, 'c' },
                    105:     { "check=silent|quiet", optional_argument, NULL, 'C' },
                    106:     { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT },
                    107:     { "debug", no_argument, NULL, DEBUG_OPT },
                    108:     { "dictionary-order", no_argument, NULL, 'd' },
                    109:     { "field-separator", required_argument, NULL, 't' },
                    110:     { "files0-from", required_argument, NULL, FF_OPT },
                    111:     { "general-numeric-sort", no_argument, NULL, 'g' },
                    112:     { "heapsort", no_argument, NULL, HEAPSORT_OPT },
                    113:     { "help", no_argument, NULL, HELP_OPT },
                    114:     { "human-numeric-sort", no_argument, NULL, 'h' },
                    115:     { "ignore-leading-blanks", no_argument, NULL, 'b' },
                    116:     { "ignore-case", no_argument, NULL, 'f' },
                    117:     { "ignore-nonprinting", no_argument, NULL, 'i' },
                    118:     { "key", required_argument, NULL, 'k' },
                    119:     { "merge", no_argument, NULL, 'm' },
                    120:     { "mergesort", no_argument, NULL, 'H' },
                    121:     { "mmap", no_argument, NULL, MMAP_OPT },
                    122:     { "month-sort", no_argument, NULL, 'M' },
                    123:     { "numeric-sort", no_argument, NULL, 'n' },
                    124:     { "output", required_argument, NULL, 'o' },
                    125:     { "qsort", no_argument, NULL, QSORT_OPT },
                    126:     { "radixsort", no_argument, NULL, RADIXSORT_OPT },
                    127:     { "random-sort", no_argument, NULL, 'R' },
                    128:     { "random-source", required_argument, NULL, RANDOMSOURCE_OPT },
                    129:     { "reverse", no_argument, NULL, 'r' },
                    130:     { "sort", required_argument, NULL, SORT_OPT },
                    131:     { "stable", no_argument, NULL, 's' },
                    132:     { "temporary-directory", required_argument, NULL, 'T' },
                    133:     { "unique", no_argument, NULL, 'u' },
                    134:     { "version", no_argument, NULL, VERSION_OPT },
                    135:     { "version-sort", no_argument, NULL, 'V' },
                    136:     { "zero-terminated", no_argument, NULL, 'z' },
                    137:     { NULL, no_argument, NULL, 0 }
                    138: };
                    139:
                    140: /*
                    141:  * Check where sort modifier is present
                    142:  */
                    143: static bool
                    144: sort_modifier_empty(struct sort_mods *sm)
                    145: {
                    146:        if (sm == NULL)
                    147:                return true;
                    148:        return !(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag ||
                    149:            sm->rflag || sm->Rflag || sm->hflag || sm->dflag || sm->fflag);
                    150: }
                    151:
                    152: /*
                    153:  * Print out usage text.
                    154:  */
                    155: static __dead void
                    156: usage(int exit_val)
                    157: {
                    158:        fprintf(exit_val ? stderr : stdout,
1.46      jmc       159:            "usage: %s [-bCcdfgHhiMmnRrsuVz] [-k field1[,field2]] [-o output] "
1.45      jmc       160:            "[-S size]\n\t[-T dir] [-t char] [file ...]\n", getprogname());
1.44      millert   161:        exit(exit_val);
                    162: }
1.4       millert   163:
1.1       millert   164: /*
1.44      millert   165:  * Read input file names from a file (file0-from option).
1.1       millert   166:  */
1.44      millert   167: static void
                    168: read_fns_from_file0(const char *fn)
                    169: {
1.47      millert   170:        FILE *f;
                    171:        char *line = NULL;
                    172:        size_t linesize = 0;
                    173:        ssize_t linelen;
                    174:
                    175:        if (fn == NULL)
                    176:                return;
                    177:
                    178:        f = fopen(fn, "r");
                    179:        if (f == NULL)
                    180:                err(2, "%s", fn);
                    181:
                    182:        while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
                    183:                if (*line != '\0') {
                    184:                        if (argc_from_file0 == (size_t)-1)
                    185:                                argc_from_file0 = 0;
                    186:                        ++argc_from_file0;
                    187:                        argv_from_file0 = sort_reallocarray(argv_from_file0,
                    188:                            argc_from_file0, sizeof(char *));
                    189:                        argv_from_file0[argc_from_file0 - 1] = line;
                    190:                } else {
                    191:                        free(line);
1.44      millert   192:                }
1.47      millert   193:                line = NULL;
                    194:                linesize = 0;
1.44      millert   195:        }
1.47      millert   196:        if (ferror(f))
                    197:                err(2, "%s: getdelim", fn);
                    198:
                    199:        closefile(f, fn);
1.44      millert   200: }
1.4       millert   201:
1.1       millert   202: /*
1.44      millert   203:  * Check how much RAM is available for the sort.
1.1       millert   204:  */
1.44      millert   205: static void
                    206: set_hw_params(void)
                    207: {
1.48      millert   208:        long long user_memory;
                    209:        struct rlimit rl;
                    210:        size_t len;
                    211:        int mib[] = { CTL_HW, HW_USERMEM64 };
                    212:
                    213:        /* Get total user (non-kernel) memory. */
                    214:        len = sizeof(user_memory);
                    215:        if (sysctl(mib, 2, &user_memory, &len, NULL, 0) == -1)
                    216:            user_memory = -1;
                    217:
                    218:        /* Increase our data size to the max */
                    219:        if (getrlimit(RLIMIT_DATA, &rl) == 0) {
                    220:                free_memory = (unsigned long long)rl.rlim_cur;
                    221:                rl.rlim_cur = rl.rlim_max;
                    222:                if (setrlimit(RLIMIT_DATA, &rl) == 0) {
                    223:                        free_memory = (unsigned long long)rl.rlim_max;
                    224:                } else {
                    225:                        warn("Can't set resource limit to max data size");
                    226:                }
                    227:        } else
                    228:                warn("Can't get resource limit for data size");
1.1       millert   229:
1.48      millert   230:        /* We prefer to use temp files rather than swap space. */
                    231:        if (user_memory != -1 && free_memory > user_memory)
                    232:                free_memory = user_memory;
1.44      millert   233:
                    234:        available_free_memory = free_memory / 2;
                    235: }
                    236:
                    237: /*
                    238:  * Convert "plain" symbol to wide symbol, with default value.
                    239:  */
                    240: static void
                    241: conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def)
                    242: {
                    243:        if (wc && c) {
                    244:                int res;
1.1       millert   245:
1.44      millert   246:                res = mbtowc(wc, c, MB_CUR_MAX);
                    247:                if (res < 1)
                    248:                        *wc = def;
1.12      millert   249:        }
1.44      millert   250: }
1.12      millert   251:
1.44      millert   252: /*
                    253:  * Set current locale symbols.
                    254:  */
                    255: static void
                    256: set_locale(void)
1.1       millert   257: {
1.44      millert   258:        struct lconv *lc;
                    259:        const char *locale;
1.4       millert   260:
1.16      ericj     261:        setlocale(LC_ALL, "");
                    262:
1.44      millert   263:        lc = localeconv();
                    264:
                    265:        if (lc) {
                    266:                /* obtain LC_NUMERIC info */
                    267:                /* Convert to wide char form */
                    268:                conv_mbtowc(&symbol_decimal_point, lc->decimal_point,
                    269:                    symbol_decimal_point);
                    270:                conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep,
                    271:                    symbol_thousands_sep);
                    272:                conv_mbtowc(&symbol_positive_sign, lc->positive_sign,
                    273:                    symbol_positive_sign);
                    274:                conv_mbtowc(&symbol_negative_sign, lc->negative_sign,
                    275:                    symbol_negative_sign);
                    276:        }
                    277:
                    278:        if (getenv("GNUSORT_NUMERIC_COMPATIBILITY"))
                    279:                gnusort_numeric_compatibility = true;
                    280:
                    281:        locale = setlocale(LC_COLLATE, NULL);
                    282:        if (locale != NULL) {
                    283:                char *tmpl;
                    284:                const char *byteclocale;
                    285:
                    286:                tmpl = sort_strdup(locale);
                    287:                byteclocale = setlocale(LC_COLLATE, "C");
                    288:                if (byteclocale && strcmp(byteclocale, tmpl) == 0) {
                    289:                        byte_sort = true;
                    290:                } else {
                    291:                        byteclocale = setlocale(LC_COLLATE, "POSIX");
                    292:                        if (byteclocale && strcmp(byteclocale, tmpl) == 0)
                    293:                                byte_sort = true;
                    294:                        else
                    295:                                setlocale(LC_COLLATE, tmpl);
                    296:                }
                    297:                sort_free(tmpl);
                    298:        }
                    299:        if (!byte_sort)
                    300:                sort_mb_cur_max = MB_CUR_MAX;
                    301: }
                    302:
                    303: /*
                    304:  * Set directory temporary files.
                    305:  */
                    306: static void
                    307: set_tmpdir(void)
                    308: {
1.53      millert   309:        if (!issetugid()) {
                    310:                char *td;
1.44      millert   311:
1.53      millert   312:                td = getenv("TMPDIR");
                    313:                if (td != NULL)
                    314:                        tmpdir = sort_strdup(td);
                    315:        }
1.44      millert   316: }
                    317:
                    318: /*
                    319:  * Parse -S option.
                    320:  */
                    321: static unsigned long long
                    322: parse_memory_buffer_value(const char *value)
                    323: {
                    324:        if (value == NULL)
                    325:                return available_free_memory;
                    326:        else {
                    327:                char *endptr;
                    328:                unsigned long long membuf;
                    329:
                    330:                membuf = strtoll(value, &endptr, 10);
1.55      millert   331:                if (endptr == value || (long long)membuf < 0 ||
                    332:                    (errno == ERANGE && membuf == LONG_MAX))
                    333:                        errx(2, "invalid memory buffer size: %s", value);
                    334:
                    335:                switch (*endptr) {
                    336:                case 'Y':
                    337:                        membuf *= 1024;
                    338:                        /* FALLTHROUGH */
                    339:                case 'Z':
                    340:                        membuf *= 1024;
                    341:                        /* FALLTHROUGH */
                    342:                case 'E':
                    343:                        membuf *= 1024;
                    344:                        /* FALLTHROUGH */
                    345:                case 'P':
                    346:                        membuf *= 1024;
                    347:                        /* FALLTHROUGH */
                    348:                case 'T':
                    349:                        membuf *= 1024;
                    350:                        /* FALLTHROUGH */
                    351:                case 'G':
                    352:                        membuf *= 1024;
                    353:                        /* FALLTHROUGH */
                    354:                case 'M':
                    355:                        membuf *= 1024;
                    356:                        /* FALLTHROUGH */
                    357:                case '\0':
                    358:                case 'K':
                    359:                        membuf *= 1024;
                    360:                        /* FALLTHROUGH */
                    361:                case 'b':
                    362:                        break;
                    363:                case '%':
                    364:                        membuf = (available_free_memory * membuf) /
                    365:                            100;
                    366:                        break;
                    367:                default:
                    368:                        warnc(EINVAL, "%s", optarg);
1.44      millert   369:                        membuf = available_free_memory;
                    370:                }
                    371:                return membuf;
                    372:        }
                    373: }
                    374:
                    375: /*
                    376:  * Signal handler that clears the temporary files.
                    377:  */
                    378: static void
1.49      millert   379: sig_handler(int sig __unused)
1.44      millert   380: {
                    381:        clear_tmp_files();
1.50      millert   382:        _exit(2);
1.44      millert   383: }
                    384:
                    385: /*
                    386:  * Set signal handler on panic signals.
                    387:  */
                    388: static void
                    389: set_signal_handler(void)
                    390: {
                    391:        struct sigaction sa;
1.49      millert   392:        int i, signals[] = {SIGTERM, SIGHUP, SIGINT, SIGQUIT, SIGUSR1, SIGUSR2,
                    393:            SIGPIPE, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, 0};
1.44      millert   394:
                    395:        memset(&sa, 0, sizeof(sa));
1.49      millert   396:        sigfillset(&sa.sa_mask);
                    397:        sa.sa_flags = SA_RESTART;
                    398:        sa.sa_handler = sig_handler;
                    399:
                    400:        for (i = 0; signals[i] != 0; i++) {
                    401:                if (sigaction(signals[i], &sa, NULL) < 0) {
                    402:                        warn("sigaction(%d)", i);
                    403:                        continue;
                    404:                }
1.44      millert   405:        }
                    406: }
                    407:
                    408: /*
                    409:  * Print "unknown" message and exit with status 2.
                    410:  */
                    411: static void
                    412: unknown(const char *what)
                    413: {
                    414:        errx(2, "Unknown feature: %s", what);
                    415: }
                    416:
                    417: /*
                    418:  * Check whether contradictory input options are used.
                    419:  */
                    420: static void
                    421: check_mutually_exclusive_flags(char c, bool *mef_flags)
                    422: {
                    423:        int i, fo_index, mec;
                    424:        bool found_others, found_this;
                    425:
                    426:        found_others = found_this =false;
                    427:        fo_index = 0;
                    428:
                    429:        for (i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) {
                    430:                mec = mutually_exclusive_flags[i];
                    431:
                    432:                if (mec != c) {
                    433:                        if (mef_flags[i]) {
                    434:                                if (found_this)
                    435:                                        errx(1, "%c:%c: mutually exclusive flags", c, mec);
                    436:                                found_others = true;
                    437:                                fo_index = i;
                    438:                        }
                    439:                } else {
                    440:                        if (found_others)
                    441:                                errx(1, "%c:%c: mutually exclusive flags", c, mutually_exclusive_flags[fo_index]);
                    442:                        mef_flags[i] = true;
                    443:                        found_this = true;
                    444:                }
                    445:        }
                    446: }
                    447:
                    448: /*
                    449:  * Initialise sort opts data.
                    450:  */
                    451: static void
                    452: set_sort_opts(void)
                    453: {
                    454:        memset(&default_sort_mods_object, 0,
                    455:            sizeof(default_sort_mods_object));
                    456:        memset(&sort_opts_vals, 0, sizeof(sort_opts_vals));
                    457:        default_sort_mods_object.func =
                    458:            get_sort_func(&default_sort_mods_object);
                    459: }
                    460:
                    461: /*
                    462:  * Set a sort modifier on a sort modifiers object.
                    463:  */
                    464: static bool
                    465: set_sort_modifier(struct sort_mods *sm, int c)
                    466: {
                    467:        if (sm) {
                    468:                switch (c){
                    469:                case 'b':
                    470:                        sm->bflag = true;
1.1       millert   471:                        break;
                    472:                case 'd':
1.44      millert   473:                        sm->dflag = true;
                    474:                        break;
1.4       millert   475:                case 'f':
1.44      millert   476:                        sm->fflag = true;
                    477:                        break;
                    478:                case 'g':
                    479:                        sm->gflag = true;
                    480:                        need_hint = true;
                    481:                        break;
1.1       millert   482:                case 'i':
1.44      millert   483:                        sm->iflag = true;
1.1       millert   484:                        break;
                    485:                case 'R':
1.44      millert   486:                        sm->Rflag = true;
                    487:                        need_random = true;
1.1       millert   488:                        break;
1.44      millert   489:                case 'M':
                    490:                        initialise_months();
                    491:                        sm->Mflag = true;
                    492:                        need_hint = true;
1.1       millert   493:                        break;
1.44      millert   494:                case 'n':
                    495:                        sm->nflag = true;
                    496:                        need_hint = true;
                    497:                        print_symbols_on_debug = true;
1.1       millert   498:                        break;
1.44      millert   499:                case 'r':
                    500:                        sm->rflag = true;
1.1       millert   501:                        break;
1.44      millert   502:                case 'V':
                    503:                        sm->Vflag = true;
1.1       millert   504:                        break;
1.44      millert   505:                case 'h':
                    506:                        sm->hflag = true;
                    507:                        need_hint = true;
                    508:                        print_symbols_on_debug = true;
1.1       millert   509:                        break;
1.8       deraadt   510:                default:
1.44      millert   511:                        return false;
1.1       millert   512:                }
1.44      millert   513:                sort_opts_vals.complex_sort = true;
                    514:                sm->func = get_sort_func(sm);
                    515:        }
                    516:        return true;
                    517: }
                    518:
                    519: /*
                    520:  * Parse POS in -k option.
                    521:  */
                    522: static int
                    523: parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second)
                    524: {
                    525:        regmatch_t pmatch[4];
                    526:        regex_t re;
                    527:        char *c, *f;
                    528:        const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$";
                    529:        size_t len, nmatch;
                    530:        int ret;
                    531:
                    532:        ret = -1;
                    533:        nmatch = 4;
                    534:        c = f = NULL;
                    535:
                    536:        if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
                    537:                return -1;
                    538:
                    539:        if (regexec(&re, s, nmatch, pmatch, 0) != 0)
                    540:                goto end;
                    541:
                    542:        if (pmatch[0].rm_eo <= pmatch[0].rm_so)
                    543:                goto end;
                    544:
                    545:        if (pmatch[1].rm_eo <= pmatch[1].rm_so)
                    546:                goto end;
                    547:
                    548:        len = pmatch[1].rm_eo - pmatch[1].rm_so;
                    549:
1.57      millert   550:        f = sort_malloc(len + 1);
                    551:        memcpy(f, s + pmatch[1].rm_so, len);
1.44      millert   552:        f[len] = '\0';
                    553:
                    554:        if (second) {
                    555:                errno = 0;
1.58      millert   556:                ks->f2 = (size_t)strtoul(f, NULL, 10);
1.44      millert   557:                if (errno != 0)
1.58      millert   558:                        goto end;
1.44      millert   559:                if (ks->f2 == 0) {
                    560:                        warn("0 field in key specs");
                    561:                        goto end;
                    562:                }
                    563:        } else {
                    564:                errno = 0;
1.58      millert   565:                ks->f1 = (size_t)strtoul(f, NULL, 10);
1.44      millert   566:                if (errno != 0)
1.58      millert   567:                        goto end;
1.44      millert   568:                if (ks->f1 == 0) {
                    569:                        warn("0 field in key specs");
                    570:                        goto end;
                    571:                }
                    572:        }
                    573:
                    574:        if (pmatch[2].rm_eo > pmatch[2].rm_so) {
                    575:                len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
                    576:
1.57      millert   577:                c = sort_malloc(len + 1);
                    578:                memcpy(c, s + pmatch[2].rm_so + 1, len);
1.44      millert   579:                c[len] = '\0';
                    580:
                    581:                if (second) {
                    582:                        errno = 0;
1.58      millert   583:                        ks->c2 = (size_t)strtoul(c, NULL, 10);
1.44      millert   584:                        if (errno != 0)
1.58      millert   585:                                goto end;
1.44      millert   586:                } else {
                    587:                        errno = 0;
1.58      millert   588:                        ks->c1 = (size_t)strtoul(c, NULL, 10);
1.44      millert   589:                        if (errno != 0)
1.58      millert   590:                                goto end;
1.44      millert   591:                        if (ks->c1 == 0) {
                    592:                                warn("0 column in key specs");
                    593:                                goto end;
                    594:                        }
                    595:                }
                    596:        } else {
                    597:                if (second)
                    598:                        ks->c2 = 0;
                    599:                else
                    600:                        ks->c1 = 1;
                    601:        }
                    602:
                    603:        if (pmatch[3].rm_eo > pmatch[3].rm_so) {
                    604:                regoff_t i = 0;
                    605:
                    606:                for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) {
                    607:                        check_mutually_exclusive_flags(s[i], mef_flags);
                    608:                        if (s[i] == 'b') {
                    609:                                if (second)
                    610:                                        ks->pos2b = true;
                    611:                                else
                    612:                                        ks->pos1b = true;
                    613:                        } else if (!set_sort_modifier(&(ks->sm), s[i]))
                    614:                                goto end;
                    615:                }
                    616:        }
                    617:
                    618:        ret = 0;
                    619:
                    620: end:
                    621:
                    622:        if (c)
                    623:                sort_free(c);
                    624:        if (f)
                    625:                sort_free(f);
                    626:        regfree(&re);
                    627:
                    628:        return ret;
                    629: }
                    630:
                    631: /*
                    632:  * Parse -k option value.
                    633:  */
                    634: static int
                    635: parse_k(const char *s, struct key_specs *ks)
                    636: {
                    637:        int ret = -1;
                    638:        bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
                    639:            { false, false, false, false, false, false };
                    640:
                    641:        if (s && *s) {
                    642:                char *sptr;
                    643:
                    644:                sptr = strchr(s, ',');
                    645:                if (sptr) {
                    646:                        size_t size1;
                    647:                        char *pos1, *pos2;
                    648:
                    649:                        size1 = sptr - s;
                    650:
                    651:                        if (size1 < 1)
                    652:                                return -1;
                    653:
1.57      millert   654:                        pos1 = sort_malloc(size1 + 1);
                    655:                        memcpy(pos1, s, size1);
1.44      millert   656:                        pos1[size1] = '\0';
                    657:
                    658:                        ret = parse_pos(pos1, ks, mef_flags, false);
                    659:
                    660:                        sort_free(pos1);
                    661:                        if (ret < 0)
                    662:                                return ret;
                    663:
                    664:                        pos2 = sort_strdup(sptr + 1);
                    665:                        ret = parse_pos(pos2, ks, mef_flags, true);
                    666:                        sort_free(pos2);
                    667:                } else
                    668:                        ret = parse_pos(s, ks, mef_flags, false);
1.1       millert   669:        }
1.4       millert   670:
1.44      millert   671:        return ret;
                    672: }
                    673:
                    674: /*
                    675:  * Parse POS in +POS -POS option.
                    676:  */
                    677: static int
1.56      millert   678: parse_pos_obs(const char *s, size_t *nf, size_t *nc, char *sopts,
                    679:     size_t sopts_size)
1.44      millert   680: {
                    681:        regex_t re;
                    682:        regmatch_t pmatch[4];
                    683:        char *c, *f;
                    684:        const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$";
                    685:        int ret;
                    686:        size_t len, nmatch;
                    687:
                    688:        ret = -1;
                    689:        nmatch = 4;
                    690:        c = f = NULL;
                    691:        *nc = *nf = 0;
                    692:
                    693:        if (regcomp(&re, sregexp, REG_EXTENDED) != 0)
                    694:                return -1;
                    695:
                    696:        if (regexec(&re, s, nmatch, pmatch, 0) != 0)
                    697:                goto end;
                    698:
                    699:        if (pmatch[0].rm_eo <= pmatch[0].rm_so)
                    700:                goto end;
                    701:
                    702:        if (pmatch[1].rm_eo <= pmatch[1].rm_so)
                    703:                goto end;
                    704:
                    705:        len = pmatch[1].rm_eo - pmatch[1].rm_so;
                    706:
1.57      millert   707:        f = sort_malloc(len + 1);
                    708:        memcpy(f, s + pmatch[1].rm_so, len);
1.44      millert   709:        f[len] = '\0';
                    710:
                    711:        errno = 0;
                    712:        *nf = (size_t) strtoul(f, NULL, 10);
                    713:        if (errno != 0)
                    714:                errx(2, "Invalid key position");
                    715:
                    716:        if (pmatch[2].rm_eo > pmatch[2].rm_so) {
                    717:                len = pmatch[2].rm_eo - pmatch[2].rm_so - 1;
1.57      millert   718:
1.44      millert   719:                c = sort_malloc((len + 1) * sizeof(char));
1.57      millert   720:                memcpy(c, s + pmatch[2].rm_so + 1, len);
1.44      millert   721:                c[len] = '\0';
                    722:
                    723:                errno = 0;
                    724:                *nc = (size_t) strtoul(c, NULL, 10);
                    725:                if (errno != 0)
                    726:                        errx(2, "Invalid key position");
                    727:        }
                    728:
                    729:        if (pmatch[3].rm_eo > pmatch[3].rm_so) {
                    730:
                    731:                len = pmatch[3].rm_eo - pmatch[3].rm_so;
1.4       millert   732:
1.56      millert   733:                if (len >= sopts_size)
                    734:                        errx(2, "Invalid key position");
1.57      millert   735:                memcpy(sopts, s + pmatch[3].rm_so, len);
1.44      millert   736:                sopts[len] = '\0';
1.1       millert   737:        }
1.4       millert   738:
1.44      millert   739:        ret = 0;
1.4       millert   740:
1.44      millert   741: end:
                    742:        if (c)
                    743:                sort_free(c);
                    744:        if (f)
                    745:                sort_free(f);
                    746:        regfree(&re);
                    747:
                    748:        return ret;
                    749: }
                    750:
                    751: /*
                    752:  * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax
                    753:  */
                    754: static void
                    755: fix_obsolete_keys(int *argc, char **argv)
                    756: {
                    757:        char sopt[129];
                    758:        int i;
                    759:
                    760:        for (i = 1; i < *argc; i++) {
1.60    ! millert   761:                const char *arg1 = argv[i];
1.44      millert   762:
1.60    ! millert   763:                if (arg1[0] == '+') {
1.56      millert   764:                        size_t c1, f1;
1.44      millert   765:                        char sopts1[128];
                    766:
                    767:                        sopts1[0] = 0;
                    768:                        c1 = f1 = 0;
                    769:
1.56      millert   770:                        if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1,
                    771:                            sizeof(sopts1)) < 0)
1.44      millert   772:                                continue;
1.60    ! millert   773:
        !           774:                        f1 += 1;
        !           775:                        c1 += 1;
        !           776:                        if (i + 1 < *argc) {
        !           777:                                const char *arg2 = argv[i + 1];
        !           778:
        !           779:                                if (arg2[0] == '-') {
        !           780:                                        size_t c2, f2;
        !           781:                                        char sopts2[128];
        !           782:
        !           783:                                        sopts2[0] = 0;
        !           784:                                        c2 = f2 = 0;
        !           785:
        !           786:                                        if (parse_pos_obs(arg2 + 1, &f2, &c2,
        !           787:                                            sopts2, sizeof(sopts2)) >= 0) {
        !           788:                                                int j;
        !           789:                                                if (c2 > 0)
        !           790:                                                        f2 += 1;
        !           791:                                                snprintf(sopt, sizeof(sopt),
        !           792:                                                    "-k%zu.%zu%s,%zu.%zu%s",
        !           793:                                                    f1, c1, sopts1, f2,
        !           794:                                                    c2, sopts2);
        !           795:                                                argv[i] = sort_strdup(sopt);
        !           796:                                                for (j = i + 1; j + 1 < *argc; j++)
        !           797:                                                        argv[j] = argv[j + 1];
        !           798:                                                *argc -= 1;
        !           799:                                                continue;
1.44      millert   800:                                        }
                    801:                                }
1.1       millert   802:                        }
1.60    ! millert   803:                        snprintf(sopt, sizeof(sopt), "-k%zu.%zu%s",
        !           804:                            f1, c1, sopts1);
        !           805:                        argv[i] = sort_strdup(sopt);
1.44      millert   806:                }
1.1       millert   807:        }
1.44      millert   808: }
                    809:
                    810: /*
                    811:  * Set random seed
                    812:  */
                    813: static void
                    814: set_random_seed(void)
                    815: {
                    816:        if (!need_random)
                    817:                return;
1.4       millert   818:
1.44      millert   819:        MD5Init(&md5_ctx);
                    820:        if (random_source != NULL) {
                    821:                unsigned char buf[BUFSIZ];
                    822:                size_t nr;
                    823:                FILE *fp;
                    824:
                    825:                if ((fp = fopen(random_source, "r")) == NULL)
                    826:                        err(2, "%s", random_source);
                    827:                while ((nr = fread(buf, 1, sizeof(buf), fp)) != 0)
                    828:                        MD5Update(&md5_ctx, buf, nr);
                    829:                if (ferror(fp))
                    830:                        err(2, "%s", random_source);
                    831:                fclose(fp);
1.1       millert   832:        } else {
1.44      millert   833:                unsigned char rsd[1024];
                    834:
                    835:                arc4random_buf(rsd, sizeof(rsd));
                    836:                MD5Update(&md5_ctx, rsd, sizeof(rsd));
                    837:        }
                    838: }
                    839:
                    840: /*
                    841:  * Main function.
                    842:  */
                    843: int
                    844: main(int argc, char *argv[])
                    845: {
                    846:        char *outfile, *real_outfile;
                    847:        int c, result;
                    848:        size_t i;
                    849:        bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] =
                    850:            { false, false, false, false, false, false };
                    851:
                    852:        result = 0;
1.51      millert   853:        outfile = "-";
1.44      millert   854:        real_outfile = NULL;
                    855:
                    856:        struct sort_mods *sm = &default_sort_mods_object;
                    857:
                    858:        init_tmp_files();
                    859:
                    860:        set_signal_handler();
                    861:
1.51      millert   862:        atexit(clear_tmp_files);
                    863:
1.44      millert   864:        set_hw_params();
                    865:        set_locale();
                    866:        set_tmpdir();
                    867:        set_sort_opts();
                    868:
                    869:        fix_obsolete_keys(&argc, argv);
                    870:
                    871:        while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
                    872:            != -1)) {
                    873:
                    874:                check_mutually_exclusive_flags(c, mef_flags);
                    875:
                    876:                if (!set_sort_modifier(sm, c)) {
                    877:
                    878:                        switch (c) {
                    879:                        case 'c':
                    880:                                sort_opts_vals.cflag = true;
                    881:                                if (optarg) {
                    882:                                        if (!strcmp(optarg, "diagnose-first"))
                    883:                                                ;
                    884:                                        else if (!strcmp(optarg, "silent") ||
                    885:                                            !strcmp(optarg, "quiet"))
                    886:                                                sort_opts_vals.csilentflag = true;
                    887:                                        else if (*optarg)
                    888:                                                unknown(optarg);
                    889:                                }
                    890:                                break;
                    891:                        case 'C':
                    892:                                sort_opts_vals.cflag = true;
                    893:                                sort_opts_vals.csilentflag = true;
                    894:                                break;
                    895:                        case 'k':
                    896:                        {
                    897:                                sort_opts_vals.complex_sort = true;
                    898:                                sort_opts_vals.kflag = true;
                    899:
                    900:                                keys_num++;
                    901:                                keys = sort_reallocarray(keys, keys_num,
                    902:                                    sizeof(struct key_specs));
                    903:                                memset(&(keys[keys_num - 1]), 0,
                    904:                                    sizeof(struct key_specs));
                    905:
                    906:                                if (parse_k(optarg, &(keys[keys_num - 1]))
                    907:                                    < 0) {
                    908:                                        errc(2, EINVAL, "-k %s", optarg);
                    909:                                }
                    910:
                    911:                                break;
                    912:                        }
                    913:                        case 'm':
                    914:                                sort_opts_vals.mflag = true;
                    915:                                break;
                    916:                        case 'o':
1.51      millert   917:                                outfile = optarg;
1.44      millert   918:                                break;
                    919:                        case 's':
                    920:                                sort_opts_vals.sflag = true;
                    921:                                break;
                    922:                        case 'S':
                    923:                                available_free_memory =
                    924:                                    parse_memory_buffer_value(optarg);
                    925:                                break;
                    926:                        case 'T':
                    927:                                tmpdir = sort_strdup(optarg);
                    928:                                break;
                    929:                        case 't':
                    930:                                while (strlen(optarg) > 1) {
                    931:                                        if (optarg[0] != '\\') {
                    932:                                                errc(2, EINVAL, "%s", optarg);
                    933:                                        }
                    934:                                        optarg += 1;
                    935:                                        if (*optarg == '0') {
                    936:                                                *optarg = 0;
                    937:                                                break;
                    938:                                        }
                    939:                                }
                    940:                                sort_opts_vals.tflag = true;
                    941:                                sort_opts_vals.field_sep = btowc(optarg[0]);
                    942:                                if (sort_opts_vals.field_sep == WEOF) {
                    943:                                        errno = EINVAL;
                    944:                                        err(2, NULL);
                    945:                                }
                    946:                                if (!gnusort_numeric_compatibility) {
                    947:                                        if (symbol_decimal_point == sort_opts_vals.field_sep)
                    948:                                                symbol_decimal_point = WEOF;
                    949:                                        if (symbol_thousands_sep == sort_opts_vals.field_sep)
                    950:                                                symbol_thousands_sep = WEOF;
                    951:                                        if (symbol_negative_sign == sort_opts_vals.field_sep)
                    952:                                                symbol_negative_sign = WEOF;
                    953:                                        if (symbol_positive_sign == sort_opts_vals.field_sep)
                    954:                                                symbol_positive_sign = WEOF;
                    955:                                }
                    956:                                break;
                    957:                        case 'u':
                    958:                                sort_opts_vals.uflag = true;
                    959:                                /* stable sort for the correct unique val */
                    960:                                sort_opts_vals.sflag = true;
                    961:                                break;
                    962:                        case 'z':
                    963:                                sort_opts_vals.zflag = true;
                    964:                                break;
                    965:                        case SORT_OPT:
                    966:                                if (optarg) {
                    967:                                        if (!strcmp(optarg, "general-numeric"))
                    968:                                                set_sort_modifier(sm, 'g');
                    969:                                        else if (!strcmp(optarg, "human-numeric"))
                    970:                                                set_sort_modifier(sm, 'h');
                    971:                                        else if (!strcmp(optarg, "numeric"))
                    972:                                                set_sort_modifier(sm, 'n');
                    973:                                        else if (!strcmp(optarg, "month"))
                    974:                                                set_sort_modifier(sm, 'M');
                    975:                                        else if (!strcmp(optarg, "random"))
                    976:                                                set_sort_modifier(sm, 'R');
                    977:                                        else
                    978:                                                unknown(optarg);
                    979:                                }
                    980:                                break;
                    981:                        case QSORT_OPT:
                    982:                                sort_opts_vals.sort_method = SORT_QSORT;
                    983:                                break;
                    984:                        case 'H':
                    985:                                sort_opts_vals.sort_method = SORT_MERGESORT;
                    986:                                break;
                    987:                        case MMAP_OPT:
                    988:                                use_mmap = true;
                    989:                                break;
                    990:                        case HEAPSORT_OPT:
                    991:                                sort_opts_vals.sort_method = SORT_HEAPSORT;
                    992:                                break;
                    993:                        case RADIXSORT_OPT:
                    994:                                sort_opts_vals.sort_method = SORT_RADIXSORT;
                    995:                                break;
                    996:                        case RANDOMSOURCE_OPT:
                    997:                                random_source = strdup(optarg);
                    998:                                break;
                    999:                        case COMPRESSPROGRAM_OPT:
                   1000:                                compress_program = strdup(optarg);
                   1001:                                break;
                   1002:                        case FF_OPT:
                   1003:                                read_fns_from_file0(optarg);
                   1004:                                break;
                   1005:                        case BS_OPT:
                   1006:                        {
1.54      millert  1007:                                const char *errstr;
                   1008:
                   1009:                                max_open_files = strtonum(optarg, 2,
                   1010:                                    UINT_MAX - 1, &errstr) + 1;
                   1011:                                if (errstr != NULL)
                   1012:                                        errx(2, "--batch-size argument is %s",
                   1013:                                            errstr);
                   1014:                                break;
1.44      millert  1015:                        }
                   1016:                        case VERSION_OPT:
                   1017:                                printf("%s\n", VERSION);
                   1018:                                exit(EXIT_SUCCESS);
                   1019:                                /* NOTREACHED */
                   1020:                                break;
                   1021:                        case DEBUG_OPT:
                   1022:                                debug_sort = true;
                   1023:                                break;
                   1024:                        case HELP_OPT:
                   1025:                                usage(0);
                   1026:                                /* NOTREACHED */
                   1027:                                break;
                   1028:                        default:
                   1029:                                usage(2);
                   1030:                                /* NOTREACHED */
                   1031:                        }
                   1032:                }
                   1033:        }
                   1034:
                   1035:        argc -= optind;
                   1036:        argv += optind;
                   1037:
                   1038:        if (keys_num == 0) {
                   1039:                keys_num = 1;
                   1040:                keys = sort_realloc(keys, sizeof(struct key_specs));
                   1041:                memset(&(keys[0]), 0, sizeof(struct key_specs));
                   1042:                keys[0].c1 = 1;
                   1043:                keys[0].pos1b = default_sort_mods->bflag;
                   1044:                keys[0].pos2b = default_sort_mods->bflag;
                   1045:                memcpy(&(keys[0].sm), default_sort_mods,
                   1046:                    sizeof(struct sort_mods));
                   1047:        }
                   1048:
                   1049:        for (i = 0; i < keys_num; i++) {
                   1050:                struct key_specs *ks;
                   1051:
                   1052:                ks = &(keys[i]);
                   1053:
                   1054:                if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) &&
                   1055:                    !(ks->pos2b)) {
                   1056:                        ks->pos1b = sm->bflag;
                   1057:                        ks->pos2b = sm->bflag;
                   1058:                        memcpy(&(ks->sm), sm, sizeof(struct sort_mods));
                   1059:                }
                   1060:
                   1061:                ks->sm.func = get_sort_func(&(ks->sm));
                   1062:        }
                   1063:
                   1064:        if (argv_from_file0) {
                   1065:                argc = argc_from_file0;
                   1066:                argv = argv_from_file0;
                   1067:        }
                   1068:
                   1069:        if (debug_sort) {
                   1070:                printf("Memory to be used for sorting: %llu\n",
                   1071:                    available_free_memory);
                   1072:                printf("Using collate rules of %s locale\n",
                   1073:                    setlocale(LC_COLLATE, NULL));
                   1074:                if (byte_sort)
                   1075:                        printf("Byte sort is used\n");
                   1076:                if (print_symbols_on_debug) {
                   1077:                        printf("Decimal Point: <%lc>\n", symbol_decimal_point);
                   1078:                        if (symbol_thousands_sep)
                   1079:                                printf("Thousands separator: <%lc>\n",
                   1080:                                    symbol_thousands_sep);
                   1081:                        printf("Positive sign: <%lc>\n", symbol_positive_sign);
                   1082:                        printf("Negative sign: <%lc>\n", symbol_negative_sign);
                   1083:                }
                   1084:        }
                   1085:
                   1086:        set_random_seed();
1.4       millert  1087:
1.44      millert  1088:        /* Case when the outfile equals one of the input files: */
1.51      millert  1089:        if (strcmp(outfile, "-") != 0) {
                   1090:                struct stat sb;
                   1091:                int fd, i;
1.44      millert  1092:
                   1093:                for (i = 0; i < argc; ++i) {
                   1094:                        if (strcmp(argv[i], outfile) == 0) {
1.51      millert  1095:                                if (stat(outfile, &sb) == -1)
                   1096:                                        err(2, "%s", outfile);
                   1097:                                if (access(outfile, W_OK) == -1)
                   1098:                                        err(2, "%s", outfile);
                   1099:                                real_outfile = outfile;
                   1100:                                sort_asprintf(&outfile, "%s.XXXXXXXXXX",
                   1101:                                    real_outfile);
                   1102:                                if ((fd = mkstemp(outfile)) == -1 ||
                   1103:                                    fchmod(fd, sb.st_mode & ALLPERMS) == -1)
                   1104:                                        err(2, "%s", outfile);
                   1105:                                close(fd);
1.44      millert  1106:                                tmp_file_atexit(outfile);
1.51      millert  1107:                                break;
1.44      millert  1108:                        }
                   1109:                }
                   1110:        }
                   1111:
                   1112:        if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) {
                   1113:                struct file_list fl;
                   1114:                struct sort_list list;
                   1115:
                   1116:                sort_list_init(&list);
                   1117:                file_list_init(&fl, true);
                   1118:
                   1119:                if (argc < 1)
                   1120:                        procfile("-", &list, &fl);
                   1121:                else {
                   1122:                        while (argc > 0) {
                   1123:                                procfile(*argv, &list, &fl);
                   1124:                                --argc;
                   1125:                                ++argv;
                   1126:                        }
                   1127:                }
                   1128:
                   1129:                if (fl.count < 1)
                   1130:                        sort_list_to_file(&list, outfile);
                   1131:                else {
                   1132:                        if (list.count > 0) {
                   1133:                                char *flast = new_tmp_file_name();
                   1134:
                   1135:                                sort_list_to_file(&list, flast);
                   1136:                                file_list_add(&fl, flast, false);
                   1137:                        }
                   1138:                        merge_files(&fl, outfile);
                   1139:                }
                   1140:
                   1141:                file_list_clean(&fl);
                   1142:
                   1143:                /*
                   1144:                 * We are about to exit the program, so we can ignore
                   1145:                 * the clean-up for speed
                   1146:                 *
                   1147:                 * sort_list_clean(&list);
                   1148:                 */
                   1149:
                   1150:        } else if (sort_opts_vals.cflag) {
                   1151:                result = (argc == 0) ? (check("-")) : (check(*argv));
                   1152:        } else if (sort_opts_vals.mflag) {
                   1153:                struct file_list fl;
                   1154:
                   1155:                file_list_init(&fl, false);
                   1156:                file_list_populate(&fl, argc, argv, true);
                   1157:                merge_files(&fl, outfile);
                   1158:                file_list_clean(&fl);
                   1159:        }
                   1160:
                   1161:        if (real_outfile) {
                   1162:                if (rename(outfile, real_outfile) < 0)
                   1163:                        err(2, "%s", real_outfile);
1.51      millert  1164:                sort_free(outfile);
1.44      millert  1165:        }
1.4       millert  1166:
1.44      millert  1167:        return result;
1.1       millert  1168: }