1c66bbc91SGabor Kovesdan /*- 21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 31de7b4b8SPedro F. Giffuni * 4c66bbc91SGabor Kovesdan * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> 5c859c6ddSGabor Kovesdan * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com> 6c66bbc91SGabor Kovesdan * All rights reserved. 7c66bbc91SGabor Kovesdan * 8c66bbc91SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 9c66bbc91SGabor Kovesdan * modification, are permitted provided that the following conditions 10c66bbc91SGabor Kovesdan * are met: 11c66bbc91SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 12c66bbc91SGabor Kovesdan * notice, this list of conditions and the following disclaimer. 13c66bbc91SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 14c66bbc91SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 15c66bbc91SGabor Kovesdan * documentation and/or other materials provided with the distribution. 16c66bbc91SGabor Kovesdan * 17c66bbc91SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18c66bbc91SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19c66bbc91SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20c66bbc91SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21c66bbc91SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22c66bbc91SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23c66bbc91SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24c66bbc91SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25c66bbc91SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26c66bbc91SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27c66bbc91SGabor Kovesdan * SUCH DAMAGE. 28c66bbc91SGabor Kovesdan */ 29c66bbc91SGabor Kovesdan 30c66bbc91SGabor Kovesdan #include <sys/cdefs.h> 31c66bbc91SGabor Kovesdan __FBSDID("$FreeBSD$"); 32c66bbc91SGabor Kovesdan 33c66bbc91SGabor Kovesdan #include <sys/stat.h> 34c66bbc91SGabor Kovesdan #include <sys/sysctl.h> 35c66bbc91SGabor Kovesdan #include <sys/types.h> 36c66bbc91SGabor Kovesdan 37c66bbc91SGabor Kovesdan #include <err.h> 38c66bbc91SGabor Kovesdan #include <errno.h> 39c66bbc91SGabor Kovesdan #include <getopt.h> 40c66bbc91SGabor Kovesdan #include <limits.h> 41c66bbc91SGabor Kovesdan #include <locale.h> 42c66bbc91SGabor Kovesdan #include <md5.h> 43c66bbc91SGabor Kovesdan #include <regex.h> 44c66bbc91SGabor Kovesdan #include <signal.h> 45c66bbc91SGabor Kovesdan #include <stdbool.h> 46c66bbc91SGabor Kovesdan #include <stdio.h> 47c66bbc91SGabor Kovesdan #include <stdlib.h> 48c66bbc91SGabor Kovesdan #include <string.h> 49c66bbc91SGabor Kovesdan #include <unistd.h> 50c66bbc91SGabor Kovesdan #include <wchar.h> 51c66bbc91SGabor Kovesdan #include <wctype.h> 52c66bbc91SGabor Kovesdan 53c66bbc91SGabor Kovesdan #include "coll.h" 54c66bbc91SGabor Kovesdan #include "file.h" 55c66bbc91SGabor Kovesdan #include "sort.h" 56c66bbc91SGabor Kovesdan 57c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 58c66bbc91SGabor Kovesdan #include <nl_types.h> 59c66bbc91SGabor Kovesdan nl_catd catalog; 60c66bbc91SGabor Kovesdan #endif 61c66bbc91SGabor Kovesdan 62c66bbc91SGabor Kovesdan #define OPTIONS "bcCdfghik:Mmno:RrsS:t:T:uVz" 63c66bbc91SGabor Kovesdan 64c66bbc91SGabor Kovesdan #define DEFAULT_RANDOM_SORT_SEED_FILE ("/dev/random") 65c66bbc91SGabor Kovesdan #define MAX_DEFAULT_RANDOM_SEED_DATA_SIZE (1024) 66c66bbc91SGabor Kovesdan 67ce1e997fSGabor Kovesdan static bool need_random; 68c66bbc91SGabor Kovesdan static const char *random_source = DEFAULT_RANDOM_SORT_SEED_FILE; 69ce1e997fSGabor Kovesdan static const void *random_seed; 70ce1e997fSGabor Kovesdan static size_t random_seed_size; 71c66bbc91SGabor Kovesdan 72c66bbc91SGabor Kovesdan MD5_CTX md5_ctx; 73c66bbc91SGabor Kovesdan 74c66bbc91SGabor Kovesdan /* 75c66bbc91SGabor Kovesdan * Default messages to use when NLS is disabled or no catalogue 76c66bbc91SGabor Kovesdan * is found. 77c66bbc91SGabor Kovesdan */ 78c66bbc91SGabor Kovesdan const char *nlsstr[] = { "", 798818aa39SGabor Kovesdan /* 1*/"mutually exclusive flags", 80c66bbc91SGabor Kovesdan /* 2*/"extra argument not allowed with -c", 818818aa39SGabor Kovesdan /* 3*/"Unknown feature", 82c66bbc91SGabor Kovesdan /* 4*/"Wrong memory buffer specification", 83c66bbc91SGabor Kovesdan /* 5*/"0 field in key specs", 84c66bbc91SGabor Kovesdan /* 6*/"0 column in key specs", 85c66bbc91SGabor Kovesdan /* 7*/"Wrong file mode", 86c66bbc91SGabor Kovesdan /* 8*/"Cannot open file for reading", 87c66bbc91SGabor Kovesdan /* 9*/"Radix sort cannot be used with these sort options", 88c66bbc91SGabor Kovesdan /*10*/"The chosen sort method cannot be used with stable and/or unique sort", 89c66bbc91SGabor Kovesdan /*11*/"Invalid key position", 90c66bbc91SGabor Kovesdan /*12*/"Usage: %s [-bcCdfigMmnrsuz] [-kPOS1[,POS2] ... ] " 91c66bbc91SGabor Kovesdan "[+POS1 [-POS2]] [-S memsize] [-T tmpdir] [-t separator] " 92c66bbc91SGabor Kovesdan "[-o outfile] [--batch-size size] [--files0-from file] " 93c66bbc91SGabor Kovesdan "[--heapsort] [--mergesort] [--radixsort] [--qsort] " 945ca724dcSGabor Kovesdan "[--mmap] " 95c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 965d5151aeSGabor Kovesdan "[--parallel thread_no] " 97c66bbc91SGabor Kovesdan #endif 98c66bbc91SGabor Kovesdan "[--human-numeric-sort] " 99c66bbc91SGabor Kovesdan "[--version-sort] [--random-sort [--random-source file]] " 100c66bbc91SGabor Kovesdan "[--compress-program program] [file ...]\n" }; 101c66bbc91SGabor Kovesdan 102c66bbc91SGabor Kovesdan struct sort_opts sort_opts_vals; 103c66bbc91SGabor Kovesdan 104ce1e997fSGabor Kovesdan bool debug_sort; 105ce1e997fSGabor Kovesdan bool need_hint; 106c66bbc91SGabor Kovesdan 107c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 108ab28d4d3SGabor Kovesdan unsigned int ncpu = 1; 109c66bbc91SGabor Kovesdan size_t nthreads = 1; 110c66bbc91SGabor Kovesdan #endif 111c66bbc91SGabor Kovesdan 112ce1e997fSGabor Kovesdan static bool gnusort_numeric_compatibility; 113c66bbc91SGabor Kovesdan 114c66bbc91SGabor Kovesdan static struct sort_mods default_sort_mods_object; 115c66bbc91SGabor Kovesdan struct sort_mods * const default_sort_mods = &default_sort_mods_object; 116c66bbc91SGabor Kovesdan 117ce1e997fSGabor Kovesdan static bool print_symbols_on_debug; 118c66bbc91SGabor Kovesdan 119c66bbc91SGabor Kovesdan /* 120c66bbc91SGabor Kovesdan * Arguments from file (when file0-from option is used: 121c66bbc91SGabor Kovesdan */ 122e8da8c74SGabor Kovesdan static size_t argc_from_file0 = (size_t)-1; 123ce1e997fSGabor Kovesdan static char **argv_from_file0; 124c66bbc91SGabor Kovesdan 125c66bbc91SGabor Kovesdan /* 126c66bbc91SGabor Kovesdan * Placeholder symbols for options which have no single-character equivalent 127c66bbc91SGabor Kovesdan */ 128c66bbc91SGabor Kovesdan enum 129c66bbc91SGabor Kovesdan { 130c66bbc91SGabor Kovesdan SORT_OPT = CHAR_MAX + 1, 131c66bbc91SGabor Kovesdan HELP_OPT, 132c66bbc91SGabor Kovesdan FF_OPT, 133c66bbc91SGabor Kovesdan BS_OPT, 134c66bbc91SGabor Kovesdan VERSION_OPT, 135c66bbc91SGabor Kovesdan DEBUG_OPT, 136c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 1375d5151aeSGabor Kovesdan PARALLEL_OPT, 138c66bbc91SGabor Kovesdan #endif 139c66bbc91SGabor Kovesdan RANDOMSOURCE_OPT, 140c66bbc91SGabor Kovesdan COMPRESSPROGRAM_OPT, 141c66bbc91SGabor Kovesdan QSORT_OPT, 142c66bbc91SGabor Kovesdan MERGESORT_OPT, 143c66bbc91SGabor Kovesdan HEAPSORT_OPT, 1445ca724dcSGabor Kovesdan RADIXSORT_OPT, 1455ca724dcSGabor Kovesdan MMAP_OPT 146c66bbc91SGabor Kovesdan }; 147c66bbc91SGabor Kovesdan 148c66bbc91SGabor Kovesdan #define NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6 149c66bbc91SGabor Kovesdan static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' }; 150c66bbc91SGabor Kovesdan 151bf70beceSEd Schouten static struct option long_options[] = { 152c66bbc91SGabor Kovesdan { "batch-size", required_argument, NULL, BS_OPT }, 153c66bbc91SGabor Kovesdan { "buffer-size", required_argument, NULL, 'S' }, 154c66bbc91SGabor Kovesdan { "check", optional_argument, NULL, 'c' }, 155c66bbc91SGabor Kovesdan { "check=silent|quiet", optional_argument, NULL, 'C' }, 156c66bbc91SGabor Kovesdan { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT }, 157c66bbc91SGabor Kovesdan { "debug", no_argument, NULL, DEBUG_OPT }, 158c66bbc91SGabor Kovesdan { "dictionary-order", no_argument, NULL, 'd' }, 159c66bbc91SGabor Kovesdan { "field-separator", required_argument, NULL, 't' }, 160c66bbc91SGabor Kovesdan { "files0-from", required_argument, NULL, FF_OPT }, 161c66bbc91SGabor Kovesdan { "general-numeric-sort", no_argument, NULL, 'g' }, 162c66bbc91SGabor Kovesdan { "heapsort", no_argument, NULL, HEAPSORT_OPT }, 163c66bbc91SGabor Kovesdan { "help",no_argument, NULL, HELP_OPT }, 164c66bbc91SGabor Kovesdan { "human-numeric-sort", no_argument, NULL, 'h' }, 165c66bbc91SGabor Kovesdan { "ignore-leading-blanks", no_argument, NULL, 'b' }, 166c66bbc91SGabor Kovesdan { "ignore-case", no_argument, NULL, 'f' }, 167c66bbc91SGabor Kovesdan { "ignore-nonprinting", no_argument, NULL, 'i' }, 168c66bbc91SGabor Kovesdan { "key", required_argument, NULL, 'k' }, 169c66bbc91SGabor Kovesdan { "merge", no_argument, NULL, 'm' }, 170c66bbc91SGabor Kovesdan { "mergesort", no_argument, NULL, MERGESORT_OPT }, 1715ca724dcSGabor Kovesdan { "mmap", no_argument, NULL, MMAP_OPT }, 172c66bbc91SGabor Kovesdan { "month-sort", no_argument, NULL, 'M' }, 173c66bbc91SGabor Kovesdan { "numeric-sort", no_argument, NULL, 'n' }, 174c66bbc91SGabor Kovesdan { "output", required_argument, NULL, 'o' }, 175c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 1765d5151aeSGabor Kovesdan { "parallel", required_argument, NULL, PARALLEL_OPT }, 177c66bbc91SGabor Kovesdan #endif 178c66bbc91SGabor Kovesdan { "qsort", no_argument, NULL, QSORT_OPT }, 179c66bbc91SGabor Kovesdan { "radixsort", no_argument, NULL, RADIXSORT_OPT }, 180c66bbc91SGabor Kovesdan { "random-sort", no_argument, NULL, 'R' }, 181c66bbc91SGabor Kovesdan { "random-source", required_argument, NULL, RANDOMSOURCE_OPT }, 182c66bbc91SGabor Kovesdan { "reverse", no_argument, NULL, 'r' }, 183c66bbc91SGabor Kovesdan { "sort", required_argument, NULL, SORT_OPT }, 184c66bbc91SGabor Kovesdan { "stable", no_argument, NULL, 's' }, 185c66bbc91SGabor Kovesdan { "temporary-directory",required_argument, NULL, 'T' }, 186c66bbc91SGabor Kovesdan { "unique", no_argument, NULL, 'u' }, 187c66bbc91SGabor Kovesdan { "version", no_argument, NULL, VERSION_OPT }, 188c66bbc91SGabor Kovesdan { "version-sort",no_argument, NULL, 'V' }, 189c66bbc91SGabor Kovesdan { "zero-terminated", no_argument, NULL, 'z' }, 190c66bbc91SGabor Kovesdan { NULL, no_argument, NULL, 0 } 191c66bbc91SGabor Kovesdan }; 192c66bbc91SGabor Kovesdan 193c66bbc91SGabor Kovesdan void fix_obsolete_keys(int *argc, char **argv); 194c66bbc91SGabor Kovesdan 195c66bbc91SGabor Kovesdan /* 196c66bbc91SGabor Kovesdan * Check where sort modifier is present 197c66bbc91SGabor Kovesdan */ 198c66bbc91SGabor Kovesdan static bool 199c66bbc91SGabor Kovesdan sort_modifier_empty(struct sort_mods *sm) 200c66bbc91SGabor Kovesdan { 201e5f71a07SPedro F. Giffuni 202c66bbc91SGabor Kovesdan if (sm == NULL) 203c66bbc91SGabor Kovesdan return (true); 204c66bbc91SGabor Kovesdan return (!(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag || 205c66bbc91SGabor Kovesdan sm->rflag || sm->Rflag || sm->hflag || sm->dflag || sm->fflag)); 206c66bbc91SGabor Kovesdan } 207c66bbc91SGabor Kovesdan 208c66bbc91SGabor Kovesdan /* 209c66bbc91SGabor Kovesdan * Print out usage text. 210c66bbc91SGabor Kovesdan */ 211c66bbc91SGabor Kovesdan static void 212c66bbc91SGabor Kovesdan usage(bool opt_err) 213c66bbc91SGabor Kovesdan { 214c66bbc91SGabor Kovesdan FILE *out; 215c66bbc91SGabor Kovesdan 216c514c3edSXin LI out = opt_err ? stderr : stdout; 217c66bbc91SGabor Kovesdan 218c66bbc91SGabor Kovesdan fprintf(out, getstr(12), getprogname()); 219c66bbc91SGabor Kovesdan if (opt_err) 220c66bbc91SGabor Kovesdan exit(2); 221c66bbc91SGabor Kovesdan exit(0); 222c66bbc91SGabor Kovesdan } 223c66bbc91SGabor Kovesdan 224c66bbc91SGabor Kovesdan /* 225c66bbc91SGabor Kovesdan * Read input file names from a file (file0-from option). 226c66bbc91SGabor Kovesdan */ 227c66bbc91SGabor Kovesdan static void 228c66bbc91SGabor Kovesdan read_fns_from_file0(const char *fn) 229c66bbc91SGabor Kovesdan { 230c66bbc91SGabor Kovesdan FILE *f; 2310f4b9a90SPedro F. Giffuni char *line = NULL; 2320f4b9a90SPedro F. Giffuni size_t linesize = 0; 2330f4b9a90SPedro F. Giffuni ssize_t linelen; 2340f4b9a90SPedro F. Giffuni 2350f4b9a90SPedro F. Giffuni if (fn == NULL) 2360f4b9a90SPedro F. Giffuni return; 237c66bbc91SGabor Kovesdan 238c66bbc91SGabor Kovesdan f = fopen(fn, "r"); 239c66bbc91SGabor Kovesdan if (f == NULL) 2400f4b9a90SPedro F. Giffuni err(2, "%s", fn); 241c66bbc91SGabor Kovesdan 2420f4b9a90SPedro F. Giffuni while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) { 2430f4b9a90SPedro F. Giffuni if (*line != '\0') { 244e8da8c74SGabor Kovesdan if (argc_from_file0 == (size_t) - 1) 245e8da8c74SGabor Kovesdan argc_from_file0 = 0; 246c66bbc91SGabor Kovesdan ++argc_from_file0; 247c66bbc91SGabor Kovesdan argv_from_file0 = sort_realloc(argv_from_file0, 248c66bbc91SGabor Kovesdan argc_from_file0 * sizeof(char *)); 249c66bbc91SGabor Kovesdan if (argv_from_file0 == NULL) 250c66bbc91SGabor Kovesdan err(2, NULL); 2510f4b9a90SPedro F. Giffuni argv_from_file0[argc_from_file0 - 1] = line; 2520f4b9a90SPedro F. Giffuni } else { 2530f4b9a90SPedro F. Giffuni free(line); 254c66bbc91SGabor Kovesdan } 2550f4b9a90SPedro F. Giffuni line = NULL; 2560f4b9a90SPedro F. Giffuni linesize = 0; 257c66bbc91SGabor Kovesdan } 2580f4b9a90SPedro F. Giffuni if (ferror(f)) 2590f4b9a90SPedro F. Giffuni err(2, "%s: getdelim", fn); 2600f4b9a90SPedro F. Giffuni 261c66bbc91SGabor Kovesdan closefile(f, fn); 262c66bbc91SGabor Kovesdan } 263c66bbc91SGabor Kovesdan 264c66bbc91SGabor Kovesdan /* 265c66bbc91SGabor Kovesdan * Check how much RAM is available for the sort. 266c66bbc91SGabor Kovesdan */ 267c66bbc91SGabor Kovesdan static void 268c66bbc91SGabor Kovesdan set_hw_params(void) 269c66bbc91SGabor Kovesdan { 27055444243SGabor Kovesdan long pages, psize; 271c66bbc91SGabor Kovesdan 272c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 273c66bbc91SGabor Kovesdan ncpu = 1; 274c66bbc91SGabor Kovesdan #endif 275c66bbc91SGabor Kovesdan 27655444243SGabor Kovesdan pages = sysconf(_SC_PHYS_PAGES); 27755444243SGabor Kovesdan if (pages < 1) { 27855444243SGabor Kovesdan perror("sysconf pages"); 279665d2db3SXin LI pages = 1; 280c66bbc91SGabor Kovesdan } 28155444243SGabor Kovesdan psize = sysconf(_SC_PAGESIZE); 28255444243SGabor Kovesdan if (psize < 1) { 28355444243SGabor Kovesdan perror("sysconf psize"); 28455444243SGabor Kovesdan psize = 4096; 285c66bbc91SGabor Kovesdan } 286c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 28755444243SGabor Kovesdan ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN); 28855444243SGabor Kovesdan if (ncpu < 1) 289c66bbc91SGabor Kovesdan ncpu = 1; 290c66bbc91SGabor Kovesdan else if(ncpu > 32) 291c66bbc91SGabor Kovesdan ncpu = 32; 292c66bbc91SGabor Kovesdan 293c66bbc91SGabor Kovesdan nthreads = ncpu; 294c66bbc91SGabor Kovesdan #endif 295c66bbc91SGabor Kovesdan 296c66bbc91SGabor Kovesdan free_memory = (unsigned long long) pages * (unsigned long long) psize; 29755444243SGabor Kovesdan available_free_memory = free_memory / 2; 298ab28d4d3SGabor Kovesdan 299ab28d4d3SGabor Kovesdan if (available_free_memory < 1024) 300ab28d4d3SGabor Kovesdan available_free_memory = 1024; 301c66bbc91SGabor Kovesdan } 302c66bbc91SGabor Kovesdan 303c66bbc91SGabor Kovesdan /* 304c66bbc91SGabor Kovesdan * Convert "plain" symbol to wide symbol, with default value. 305c66bbc91SGabor Kovesdan */ 306c66bbc91SGabor Kovesdan static void 307c66bbc91SGabor Kovesdan conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def) 308c66bbc91SGabor Kovesdan { 309e5f71a07SPedro F. Giffuni 310c66bbc91SGabor Kovesdan if (wc && c) { 311c66bbc91SGabor Kovesdan int res; 312c66bbc91SGabor Kovesdan 313c66bbc91SGabor Kovesdan res = mbtowc(wc, c, MB_CUR_MAX); 314c66bbc91SGabor Kovesdan if (res < 1) 315c66bbc91SGabor Kovesdan *wc = def; 316c66bbc91SGabor Kovesdan } 317c66bbc91SGabor Kovesdan } 318c66bbc91SGabor Kovesdan 319c66bbc91SGabor Kovesdan /* 320c66bbc91SGabor Kovesdan * Set current locale symbols. 321c66bbc91SGabor Kovesdan */ 322c66bbc91SGabor Kovesdan static void 323c66bbc91SGabor Kovesdan set_locale(void) 324c66bbc91SGabor Kovesdan { 325c66bbc91SGabor Kovesdan struct lconv *lc; 326c66bbc91SGabor Kovesdan const char *locale; 327c66bbc91SGabor Kovesdan 328c66bbc91SGabor Kovesdan setlocale(LC_ALL, ""); 329c66bbc91SGabor Kovesdan 330c66bbc91SGabor Kovesdan lc = localeconv(); 331c66bbc91SGabor Kovesdan 332c66bbc91SGabor Kovesdan if (lc) { 333c66bbc91SGabor Kovesdan /* obtain LC_NUMERIC info */ 334c66bbc91SGabor Kovesdan /* Convert to wide char form */ 335c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_decimal_point, lc->decimal_point, 336c66bbc91SGabor Kovesdan symbol_decimal_point); 337c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep, 338c66bbc91SGabor Kovesdan symbol_thousands_sep); 339c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_positive_sign, lc->positive_sign, 340c66bbc91SGabor Kovesdan symbol_positive_sign); 341c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_negative_sign, lc->negative_sign, 342c66bbc91SGabor Kovesdan symbol_negative_sign); 343c66bbc91SGabor Kovesdan } 344c66bbc91SGabor Kovesdan 345c66bbc91SGabor Kovesdan if (getenv("GNUSORT_NUMERIC_COMPATIBILITY")) 346c66bbc91SGabor Kovesdan gnusort_numeric_compatibility = true; 347c66bbc91SGabor Kovesdan 348c66bbc91SGabor Kovesdan locale = setlocale(LC_COLLATE, NULL); 349c66bbc91SGabor Kovesdan 350c66bbc91SGabor Kovesdan if (locale) { 351c66bbc91SGabor Kovesdan char *tmpl; 352c66bbc91SGabor Kovesdan const char *cclocale; 353c66bbc91SGabor Kovesdan 354c66bbc91SGabor Kovesdan tmpl = sort_strdup(locale); 355c66bbc91SGabor Kovesdan cclocale = setlocale(LC_COLLATE, "C"); 356c66bbc91SGabor Kovesdan if (cclocale && !strcmp(cclocale, tmpl)) 357c66bbc91SGabor Kovesdan byte_sort = true; 358c66bbc91SGabor Kovesdan else { 359c66bbc91SGabor Kovesdan const char *pclocale; 360c66bbc91SGabor Kovesdan 361c66bbc91SGabor Kovesdan pclocale = setlocale(LC_COLLATE, "POSIX"); 362c66bbc91SGabor Kovesdan if (pclocale && !strcmp(pclocale, tmpl)) 363c66bbc91SGabor Kovesdan byte_sort = true; 364c66bbc91SGabor Kovesdan } 365c66bbc91SGabor Kovesdan setlocale(LC_COLLATE, tmpl); 366c66bbc91SGabor Kovesdan sort_free(tmpl); 367c66bbc91SGabor Kovesdan } 368c66bbc91SGabor Kovesdan } 369c66bbc91SGabor Kovesdan 370c66bbc91SGabor Kovesdan /* 371c66bbc91SGabor Kovesdan * Set directory temporary files. 372c66bbc91SGabor Kovesdan */ 373c66bbc91SGabor Kovesdan static void 374c66bbc91SGabor Kovesdan set_tmpdir(void) 375c66bbc91SGabor Kovesdan { 376c66bbc91SGabor Kovesdan char *td; 377c66bbc91SGabor Kovesdan 378c66bbc91SGabor Kovesdan td = getenv("TMPDIR"); 379c66bbc91SGabor Kovesdan if (td != NULL) 380c66bbc91SGabor Kovesdan tmpdir = sort_strdup(td); 381c66bbc91SGabor Kovesdan } 382c66bbc91SGabor Kovesdan 383c66bbc91SGabor Kovesdan /* 384c66bbc91SGabor Kovesdan * Parse -S option. 385c66bbc91SGabor Kovesdan */ 386c66bbc91SGabor Kovesdan static unsigned long long 387c66bbc91SGabor Kovesdan parse_memory_buffer_value(const char *value) 388c66bbc91SGabor Kovesdan { 389e5f71a07SPedro F. Giffuni 390c66bbc91SGabor Kovesdan if (value == NULL) 391c66bbc91SGabor Kovesdan return (available_free_memory); 392c66bbc91SGabor Kovesdan else { 393c66bbc91SGabor Kovesdan char *endptr; 394c66bbc91SGabor Kovesdan unsigned long long membuf; 395c66bbc91SGabor Kovesdan 396c66bbc91SGabor Kovesdan endptr = NULL; 397c66bbc91SGabor Kovesdan errno = 0; 398c66bbc91SGabor Kovesdan membuf = strtoll(value, &endptr, 10); 399c66bbc91SGabor Kovesdan 400c66bbc91SGabor Kovesdan if (errno != 0) { 4018818aa39SGabor Kovesdan warn("%s",getstr(4)); 402c66bbc91SGabor Kovesdan membuf = available_free_memory; 403c66bbc91SGabor Kovesdan } else { 404c66bbc91SGabor Kovesdan switch (*endptr){ 405c66bbc91SGabor Kovesdan case 'Y': 406c66bbc91SGabor Kovesdan membuf *= 1024; 407c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 408c66bbc91SGabor Kovesdan case 'Z': 409c66bbc91SGabor Kovesdan membuf *= 1024; 410c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 411c66bbc91SGabor Kovesdan case 'E': 412c66bbc91SGabor Kovesdan membuf *= 1024; 413c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 414c66bbc91SGabor Kovesdan case 'P': 415c66bbc91SGabor Kovesdan membuf *= 1024; 416c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 417c66bbc91SGabor Kovesdan case 'T': 418c66bbc91SGabor Kovesdan membuf *= 1024; 419c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 420c66bbc91SGabor Kovesdan case 'G': 421c66bbc91SGabor Kovesdan membuf *= 1024; 422c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 423c66bbc91SGabor Kovesdan case 'M': 424c66bbc91SGabor Kovesdan membuf *= 1024; 425c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 426c66bbc91SGabor Kovesdan case '\0': 427c66bbc91SGabor Kovesdan case 'K': 428c66bbc91SGabor Kovesdan membuf *= 1024; 429c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 430c66bbc91SGabor Kovesdan case 'b': 431c66bbc91SGabor Kovesdan break; 432c66bbc91SGabor Kovesdan case '%': 433c66bbc91SGabor Kovesdan membuf = (available_free_memory * membuf) / 434c66bbc91SGabor Kovesdan 100; 435c66bbc91SGabor Kovesdan break; 436c66bbc91SGabor Kovesdan default: 437f187ff08SGabor Kovesdan warnc(EINVAL, "%s", optarg); 438c66bbc91SGabor Kovesdan membuf = available_free_memory; 439c66bbc91SGabor Kovesdan } 440c66bbc91SGabor Kovesdan } 441c66bbc91SGabor Kovesdan return (membuf); 442c66bbc91SGabor Kovesdan } 443c66bbc91SGabor Kovesdan } 444c66bbc91SGabor Kovesdan 445c66bbc91SGabor Kovesdan /* 446c66bbc91SGabor Kovesdan * Signal handler that clears the temporary files. 447c66bbc91SGabor Kovesdan */ 448c66bbc91SGabor Kovesdan static void 4498818aa39SGabor Kovesdan sig_handler(int sig __unused, siginfo_t *siginfo __unused, 4508818aa39SGabor Kovesdan void *context __unused) 451c66bbc91SGabor Kovesdan { 452e5f71a07SPedro F. Giffuni 453c66bbc91SGabor Kovesdan clear_tmp_files(); 454c66bbc91SGabor Kovesdan exit(-1); 455c66bbc91SGabor Kovesdan } 456c66bbc91SGabor Kovesdan 457c66bbc91SGabor Kovesdan /* 458c66bbc91SGabor Kovesdan * Set signal handler on panic signals. 459c66bbc91SGabor Kovesdan */ 460c66bbc91SGabor Kovesdan static void 461c66bbc91SGabor Kovesdan set_signal_handler(void) 462c66bbc91SGabor Kovesdan { 463c66bbc91SGabor Kovesdan struct sigaction sa; 464c66bbc91SGabor Kovesdan 465c66bbc91SGabor Kovesdan memset(&sa, 0, sizeof(sa)); 466c66bbc91SGabor Kovesdan sa.sa_sigaction = &sig_handler; 467c66bbc91SGabor Kovesdan sa.sa_flags = SA_SIGINFO; 468c66bbc91SGabor Kovesdan 469c66bbc91SGabor Kovesdan if (sigaction(SIGTERM, &sa, NULL) < 0) { 470c66bbc91SGabor Kovesdan perror("sigaction"); 471c66bbc91SGabor Kovesdan return; 472c66bbc91SGabor Kovesdan } 473c66bbc91SGabor Kovesdan if (sigaction(SIGHUP, &sa, NULL) < 0) { 474c66bbc91SGabor Kovesdan perror("sigaction"); 475c66bbc91SGabor Kovesdan return; 476c66bbc91SGabor Kovesdan } 477c66bbc91SGabor Kovesdan if (sigaction(SIGINT, &sa, NULL) < 0) { 478c66bbc91SGabor Kovesdan perror("sigaction"); 479c66bbc91SGabor Kovesdan return; 480c66bbc91SGabor Kovesdan } 481c66bbc91SGabor Kovesdan if (sigaction(SIGQUIT, &sa, NULL) < 0) { 482c66bbc91SGabor Kovesdan perror("sigaction"); 483c66bbc91SGabor Kovesdan return; 484c66bbc91SGabor Kovesdan } 485c66bbc91SGabor Kovesdan if (sigaction(SIGABRT, &sa, NULL) < 0) { 486c66bbc91SGabor Kovesdan perror("sigaction"); 487c66bbc91SGabor Kovesdan return; 488c66bbc91SGabor Kovesdan } 489c66bbc91SGabor Kovesdan if (sigaction(SIGBUS, &sa, NULL) < 0) { 490c66bbc91SGabor Kovesdan perror("sigaction"); 491c66bbc91SGabor Kovesdan return; 492c66bbc91SGabor Kovesdan } 493c66bbc91SGabor Kovesdan if (sigaction(SIGSEGV, &sa, NULL) < 0) { 494c66bbc91SGabor Kovesdan perror("sigaction"); 495c66bbc91SGabor Kovesdan return; 496c66bbc91SGabor Kovesdan } 497c66bbc91SGabor Kovesdan if (sigaction(SIGUSR1, &sa, NULL) < 0) { 498c66bbc91SGabor Kovesdan perror("sigaction"); 499c66bbc91SGabor Kovesdan return; 500c66bbc91SGabor Kovesdan } 501c66bbc91SGabor Kovesdan if (sigaction(SIGUSR2, &sa, NULL) < 0) { 502c66bbc91SGabor Kovesdan perror("sigaction"); 503c66bbc91SGabor Kovesdan return; 504c66bbc91SGabor Kovesdan } 505c66bbc91SGabor Kovesdan } 506c66bbc91SGabor Kovesdan 507c66bbc91SGabor Kovesdan /* 508c66bbc91SGabor Kovesdan * Print "unknown" message and exit with status 2. 509c66bbc91SGabor Kovesdan */ 510c66bbc91SGabor Kovesdan static void 511c66bbc91SGabor Kovesdan unknown(const char *what) 512c66bbc91SGabor Kovesdan { 513e5f71a07SPedro F. Giffuni 5148818aa39SGabor Kovesdan errx(2, "%s: %s", getstr(3), what); 515c66bbc91SGabor Kovesdan } 516c66bbc91SGabor Kovesdan 517c66bbc91SGabor Kovesdan /* 518c66bbc91SGabor Kovesdan * Check whether contradictory input options are used. 519c66bbc91SGabor Kovesdan */ 520c66bbc91SGabor Kovesdan static void 521c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(char c, bool *mef_flags) 522c66bbc91SGabor Kovesdan { 523c66bbc91SGabor Kovesdan int fo_index, mec; 524c66bbc91SGabor Kovesdan bool found_others, found_this; 525c66bbc91SGabor Kovesdan 526c66bbc91SGabor Kovesdan found_others = found_this = false; 527c66bbc91SGabor Kovesdan fo_index = 0; 528c66bbc91SGabor Kovesdan 529c66bbc91SGabor Kovesdan for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) { 530c66bbc91SGabor Kovesdan mec = mutually_exclusive_flags[i]; 531c66bbc91SGabor Kovesdan 532c66bbc91SGabor Kovesdan if (mec != c) { 533c66bbc91SGabor Kovesdan if (mef_flags[i]) { 534c66bbc91SGabor Kovesdan if (found_this) 5358818aa39SGabor Kovesdan errx(1, "%c:%c: %s", c, mec, getstr(1)); 536c66bbc91SGabor Kovesdan found_others = true; 537c66bbc91SGabor Kovesdan fo_index = i; 538c66bbc91SGabor Kovesdan } 539c66bbc91SGabor Kovesdan } else { 540c66bbc91SGabor Kovesdan if (found_others) 5418818aa39SGabor Kovesdan errx(1, "%c:%c: %s", c, mutually_exclusive_flags[fo_index], getstr(1)); 542c66bbc91SGabor Kovesdan mef_flags[i] = true; 543c66bbc91SGabor Kovesdan found_this = true; 544c66bbc91SGabor Kovesdan } 545c66bbc91SGabor Kovesdan } 546c66bbc91SGabor Kovesdan } 547c66bbc91SGabor Kovesdan 548c66bbc91SGabor Kovesdan /* 549c66bbc91SGabor Kovesdan * Initialise sort opts data. 550c66bbc91SGabor Kovesdan */ 551c66bbc91SGabor Kovesdan static void 552c66bbc91SGabor Kovesdan set_sort_opts(void) 553c66bbc91SGabor Kovesdan { 554e5f71a07SPedro F. Giffuni 555c66bbc91SGabor Kovesdan memset(&default_sort_mods_object, 0, 556c66bbc91SGabor Kovesdan sizeof(default_sort_mods_object)); 557c66bbc91SGabor Kovesdan memset(&sort_opts_vals, 0, sizeof(sort_opts_vals)); 558c66bbc91SGabor Kovesdan default_sort_mods_object.func = 559c66bbc91SGabor Kovesdan get_sort_func(&default_sort_mods_object); 560c66bbc91SGabor Kovesdan } 561c66bbc91SGabor Kovesdan 562c66bbc91SGabor Kovesdan /* 563c66bbc91SGabor Kovesdan * Set a sort modifier on a sort modifiers object. 564c66bbc91SGabor Kovesdan */ 565c66bbc91SGabor Kovesdan static bool 566c66bbc91SGabor Kovesdan set_sort_modifier(struct sort_mods *sm, int c) 567c66bbc91SGabor Kovesdan { 568e5f71a07SPedro F. Giffuni 569*74504eefSConrad Meyer if (sm == NULL) 570*74504eefSConrad Meyer return (true); 571*74504eefSConrad Meyer 572c66bbc91SGabor Kovesdan switch (c){ 573c66bbc91SGabor Kovesdan case 'b': 574c66bbc91SGabor Kovesdan sm->bflag = true; 575c66bbc91SGabor Kovesdan break; 576c66bbc91SGabor Kovesdan case 'd': 577c66bbc91SGabor Kovesdan sm->dflag = true; 578c66bbc91SGabor Kovesdan break; 579c66bbc91SGabor Kovesdan case 'f': 580c66bbc91SGabor Kovesdan sm->fflag = true; 581c66bbc91SGabor Kovesdan break; 582c66bbc91SGabor Kovesdan case 'g': 583c66bbc91SGabor Kovesdan sm->gflag = true; 584c66bbc91SGabor Kovesdan need_hint = true; 585c66bbc91SGabor Kovesdan break; 586c66bbc91SGabor Kovesdan case 'i': 587c66bbc91SGabor Kovesdan sm->iflag = true; 588c66bbc91SGabor Kovesdan break; 589c66bbc91SGabor Kovesdan case 'R': 590c66bbc91SGabor Kovesdan sm->Rflag = true; 591c66bbc91SGabor Kovesdan need_random = true; 592c66bbc91SGabor Kovesdan break; 593c66bbc91SGabor Kovesdan case 'M': 594c66bbc91SGabor Kovesdan initialise_months(); 595c66bbc91SGabor Kovesdan sm->Mflag = true; 596c66bbc91SGabor Kovesdan need_hint = true; 597c66bbc91SGabor Kovesdan break; 598c66bbc91SGabor Kovesdan case 'n': 599c66bbc91SGabor Kovesdan sm->nflag = true; 600c66bbc91SGabor Kovesdan need_hint = true; 601c66bbc91SGabor Kovesdan print_symbols_on_debug = true; 602c66bbc91SGabor Kovesdan break; 603c66bbc91SGabor Kovesdan case 'r': 604c66bbc91SGabor Kovesdan sm->rflag = true; 605c66bbc91SGabor Kovesdan break; 606c66bbc91SGabor Kovesdan case 'V': 607c66bbc91SGabor Kovesdan sm->Vflag = true; 608c66bbc91SGabor Kovesdan break; 609c66bbc91SGabor Kovesdan case 'h': 610c66bbc91SGabor Kovesdan sm->hflag = true; 611c66bbc91SGabor Kovesdan need_hint = true; 612c66bbc91SGabor Kovesdan print_symbols_on_debug = true; 613c66bbc91SGabor Kovesdan break; 614c66bbc91SGabor Kovesdan default: 615*74504eefSConrad Meyer return (false); 616c66bbc91SGabor Kovesdan } 617*74504eefSConrad Meyer 618c66bbc91SGabor Kovesdan sort_opts_vals.complex_sort = true; 619c66bbc91SGabor Kovesdan sm->func = get_sort_func(sm); 620c66bbc91SGabor Kovesdan return (true); 621c66bbc91SGabor Kovesdan } 622c66bbc91SGabor Kovesdan 623c66bbc91SGabor Kovesdan /* 624c66bbc91SGabor Kovesdan * Parse POS in -k option. 625c66bbc91SGabor Kovesdan */ 626c66bbc91SGabor Kovesdan static int 627c66bbc91SGabor Kovesdan parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second) 628c66bbc91SGabor Kovesdan { 629c66bbc91SGabor Kovesdan regmatch_t pmatch[4]; 630c66bbc91SGabor Kovesdan regex_t re; 631c66bbc91SGabor Kovesdan char *c, *f; 632c66bbc91SGabor Kovesdan const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$"; 633c66bbc91SGabor Kovesdan size_t len, nmatch; 634c66bbc91SGabor Kovesdan int ret; 635c66bbc91SGabor Kovesdan 636c66bbc91SGabor Kovesdan ret = -1; 637c66bbc91SGabor Kovesdan nmatch = 4; 638c66bbc91SGabor Kovesdan c = f = NULL; 639c66bbc91SGabor Kovesdan 640c66bbc91SGabor Kovesdan if (regcomp(&re, sregexp, REG_EXTENDED) != 0) 641c66bbc91SGabor Kovesdan return (-1); 642c66bbc91SGabor Kovesdan 643c66bbc91SGabor Kovesdan if (regexec(&re, s, nmatch, pmatch, 0) != 0) 644c66bbc91SGabor Kovesdan goto end; 645c66bbc91SGabor Kovesdan 646c66bbc91SGabor Kovesdan if (pmatch[0].rm_eo <= pmatch[0].rm_so) 647c66bbc91SGabor Kovesdan goto end; 648c66bbc91SGabor Kovesdan 649c66bbc91SGabor Kovesdan if (pmatch[1].rm_eo <= pmatch[1].rm_so) 650c66bbc91SGabor Kovesdan goto end; 651c66bbc91SGabor Kovesdan 652c66bbc91SGabor Kovesdan len = pmatch[1].rm_eo - pmatch[1].rm_so; 653c66bbc91SGabor Kovesdan f = sort_malloc((len + 1) * sizeof(char)); 654c66bbc91SGabor Kovesdan 655c66bbc91SGabor Kovesdan strncpy(f, s + pmatch[1].rm_so, len); 656c66bbc91SGabor Kovesdan f[len] = '\0'; 657c66bbc91SGabor Kovesdan 658c66bbc91SGabor Kovesdan if (second) { 659c66bbc91SGabor Kovesdan errno = 0; 660c66bbc91SGabor Kovesdan ks->f2 = (size_t) strtoul(f, NULL, 10); 661c66bbc91SGabor Kovesdan if (errno != 0) 662f187ff08SGabor Kovesdan err(2, "-k"); 663c66bbc91SGabor Kovesdan if (ks->f2 == 0) { 6648818aa39SGabor Kovesdan warn("%s",getstr(5)); 665c66bbc91SGabor Kovesdan goto end; 666c66bbc91SGabor Kovesdan } 667c66bbc91SGabor Kovesdan } else { 668c66bbc91SGabor Kovesdan errno = 0; 669c66bbc91SGabor Kovesdan ks->f1 = (size_t) strtoul(f, NULL, 10); 670c66bbc91SGabor Kovesdan if (errno != 0) 671f187ff08SGabor Kovesdan err(2, "-k"); 672c66bbc91SGabor Kovesdan if (ks->f1 == 0) { 6738818aa39SGabor Kovesdan warn("%s",getstr(5)); 674c66bbc91SGabor Kovesdan goto end; 675c66bbc91SGabor Kovesdan } 676c66bbc91SGabor Kovesdan } 677c66bbc91SGabor Kovesdan 678c66bbc91SGabor Kovesdan if (pmatch[2].rm_eo > pmatch[2].rm_so) { 679c66bbc91SGabor Kovesdan len = pmatch[2].rm_eo - pmatch[2].rm_so - 1; 680c66bbc91SGabor Kovesdan c = sort_malloc((len + 1) * sizeof(char)); 681c66bbc91SGabor Kovesdan 682c66bbc91SGabor Kovesdan strncpy(c, s + pmatch[2].rm_so + 1, len); 683c66bbc91SGabor Kovesdan c[len] = '\0'; 684c66bbc91SGabor Kovesdan 685c66bbc91SGabor Kovesdan if (second) { 686c66bbc91SGabor Kovesdan errno = 0; 687c66bbc91SGabor Kovesdan ks->c2 = (size_t) strtoul(c, NULL, 10); 688c66bbc91SGabor Kovesdan if (errno != 0) 689f187ff08SGabor Kovesdan err(2, "-k"); 690c66bbc91SGabor Kovesdan } else { 691c66bbc91SGabor Kovesdan errno = 0; 692c66bbc91SGabor Kovesdan ks->c1 = (size_t) strtoul(c, NULL, 10); 693c66bbc91SGabor Kovesdan if (errno != 0) 694f187ff08SGabor Kovesdan err(2, "-k"); 695c66bbc91SGabor Kovesdan if (ks->c1 == 0) { 6968818aa39SGabor Kovesdan warn("%s",getstr(6)); 697c66bbc91SGabor Kovesdan goto end; 698c66bbc91SGabor Kovesdan } 699c66bbc91SGabor Kovesdan } 700c66bbc91SGabor Kovesdan } else { 701c66bbc91SGabor Kovesdan if (second) 702c66bbc91SGabor Kovesdan ks->c2 = 0; 703c66bbc91SGabor Kovesdan else 704c66bbc91SGabor Kovesdan ks->c1 = 1; 705c66bbc91SGabor Kovesdan } 706c66bbc91SGabor Kovesdan 707c66bbc91SGabor Kovesdan if (pmatch[3].rm_eo > pmatch[3].rm_so) { 708c66bbc91SGabor Kovesdan regoff_t i = 0; 709c66bbc91SGabor Kovesdan 710c66bbc91SGabor Kovesdan for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) { 711c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(s[i], mef_flags); 712c66bbc91SGabor Kovesdan if (s[i] == 'b') { 713c66bbc91SGabor Kovesdan if (second) 714c66bbc91SGabor Kovesdan ks->pos2b = true; 715c66bbc91SGabor Kovesdan else 716c66bbc91SGabor Kovesdan ks->pos1b = true; 717c66bbc91SGabor Kovesdan } else if (!set_sort_modifier(&(ks->sm), s[i])) 718c66bbc91SGabor Kovesdan goto end; 719c66bbc91SGabor Kovesdan } 720c66bbc91SGabor Kovesdan } 721c66bbc91SGabor Kovesdan 722c66bbc91SGabor Kovesdan ret = 0; 723c66bbc91SGabor Kovesdan 724c66bbc91SGabor Kovesdan end: 725c66bbc91SGabor Kovesdan 726c66bbc91SGabor Kovesdan if (c) 727c66bbc91SGabor Kovesdan sort_free(c); 728c66bbc91SGabor Kovesdan if (f) 729c66bbc91SGabor Kovesdan sort_free(f); 730c66bbc91SGabor Kovesdan regfree(&re); 731c66bbc91SGabor Kovesdan 732c66bbc91SGabor Kovesdan return (ret); 733c66bbc91SGabor Kovesdan } 734c66bbc91SGabor Kovesdan 735c66bbc91SGabor Kovesdan /* 736c66bbc91SGabor Kovesdan * Parse -k option value. 737c66bbc91SGabor Kovesdan */ 738c66bbc91SGabor Kovesdan static int 739c66bbc91SGabor Kovesdan parse_k(const char *s, struct key_specs *ks) 740c66bbc91SGabor Kovesdan { 741c66bbc91SGabor Kovesdan int ret = -1; 742c66bbc91SGabor Kovesdan bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = 743c66bbc91SGabor Kovesdan { false, false, false, false, false, false }; 744c66bbc91SGabor Kovesdan 745c66bbc91SGabor Kovesdan if (s && *s) { 746c66bbc91SGabor Kovesdan char *sptr; 747c66bbc91SGabor Kovesdan 748c66bbc91SGabor Kovesdan sptr = strchr(s, ','); 749c66bbc91SGabor Kovesdan if (sptr) { 750c66bbc91SGabor Kovesdan size_t size1; 751c66bbc91SGabor Kovesdan char *pos1, *pos2; 752c66bbc91SGabor Kovesdan 753c66bbc91SGabor Kovesdan size1 = sptr - s; 754c66bbc91SGabor Kovesdan 755c66bbc91SGabor Kovesdan if (size1 < 1) 756c66bbc91SGabor Kovesdan return (-1); 757c66bbc91SGabor Kovesdan pos1 = sort_malloc((size1 + 1) * sizeof(char)); 758c66bbc91SGabor Kovesdan 759c66bbc91SGabor Kovesdan strncpy(pos1, s, size1); 760c66bbc91SGabor Kovesdan pos1[size1] = '\0'; 761c66bbc91SGabor Kovesdan 762c66bbc91SGabor Kovesdan ret = parse_pos(pos1, ks, mef_flags, false); 763c66bbc91SGabor Kovesdan 764c66bbc91SGabor Kovesdan sort_free(pos1); 765c66bbc91SGabor Kovesdan if (ret < 0) 766c66bbc91SGabor Kovesdan return (ret); 767c66bbc91SGabor Kovesdan 768c66bbc91SGabor Kovesdan pos2 = sort_strdup(sptr + 1); 769c66bbc91SGabor Kovesdan ret = parse_pos(pos2, ks, mef_flags, true); 770c66bbc91SGabor Kovesdan sort_free(pos2); 771c66bbc91SGabor Kovesdan } else 772c66bbc91SGabor Kovesdan ret = parse_pos(s, ks, mef_flags, false); 773c66bbc91SGabor Kovesdan } 774c66bbc91SGabor Kovesdan 775c66bbc91SGabor Kovesdan return (ret); 776c66bbc91SGabor Kovesdan } 777c66bbc91SGabor Kovesdan 778c66bbc91SGabor Kovesdan /* 779c66bbc91SGabor Kovesdan * Parse POS in +POS -POS option. 780c66bbc91SGabor Kovesdan */ 781c66bbc91SGabor Kovesdan static int 782c66bbc91SGabor Kovesdan parse_pos_obs(const char *s, int *nf, int *nc, char* sopts) 783c66bbc91SGabor Kovesdan { 784c66bbc91SGabor Kovesdan regex_t re; 785c66bbc91SGabor Kovesdan regmatch_t pmatch[4]; 786c66bbc91SGabor Kovesdan char *c, *f; 787c66bbc91SGabor Kovesdan const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$"; 788c66bbc91SGabor Kovesdan int ret; 789c66bbc91SGabor Kovesdan size_t len, nmatch; 790c66bbc91SGabor Kovesdan 791c66bbc91SGabor Kovesdan ret = -1; 792c66bbc91SGabor Kovesdan nmatch = 4; 793c66bbc91SGabor Kovesdan c = f = NULL; 794c66bbc91SGabor Kovesdan *nc = *nf = 0; 795c66bbc91SGabor Kovesdan 796c66bbc91SGabor Kovesdan if (regcomp(&re, sregexp, REG_EXTENDED) != 0) 797c66bbc91SGabor Kovesdan return (-1); 798c66bbc91SGabor Kovesdan 799c66bbc91SGabor Kovesdan if (regexec(&re, s, nmatch, pmatch, 0) != 0) 800c66bbc91SGabor Kovesdan goto end; 801c66bbc91SGabor Kovesdan 802c66bbc91SGabor Kovesdan if (pmatch[0].rm_eo <= pmatch[0].rm_so) 803c66bbc91SGabor Kovesdan goto end; 804c66bbc91SGabor Kovesdan 805c66bbc91SGabor Kovesdan if (pmatch[1].rm_eo <= pmatch[1].rm_so) 806c66bbc91SGabor Kovesdan goto end; 807c66bbc91SGabor Kovesdan 808c66bbc91SGabor Kovesdan len = pmatch[1].rm_eo - pmatch[1].rm_so; 809c66bbc91SGabor Kovesdan f = sort_malloc((len + 1) * sizeof(char)); 810c66bbc91SGabor Kovesdan 811c66bbc91SGabor Kovesdan strncpy(f, s + pmatch[1].rm_so, len); 812c66bbc91SGabor Kovesdan f[len] = '\0'; 813c66bbc91SGabor Kovesdan 814c66bbc91SGabor Kovesdan errno = 0; 815c66bbc91SGabor Kovesdan *nf = (size_t) strtoul(f, NULL, 10); 816c66bbc91SGabor Kovesdan if (errno != 0) 8178818aa39SGabor Kovesdan errx(2, "%s", getstr(11)); 818c66bbc91SGabor Kovesdan 819c66bbc91SGabor Kovesdan if (pmatch[2].rm_eo > pmatch[2].rm_so) { 820c66bbc91SGabor Kovesdan len = pmatch[2].rm_eo - pmatch[2].rm_so - 1; 821c66bbc91SGabor Kovesdan c = sort_malloc((len + 1) * sizeof(char)); 822c66bbc91SGabor Kovesdan 823c66bbc91SGabor Kovesdan strncpy(c, s + pmatch[2].rm_so + 1, len); 824c66bbc91SGabor Kovesdan c[len] = '\0'; 825c66bbc91SGabor Kovesdan 826c66bbc91SGabor Kovesdan errno = 0; 827c66bbc91SGabor Kovesdan *nc = (size_t) strtoul(c, NULL, 10); 828c66bbc91SGabor Kovesdan if (errno != 0) 8298818aa39SGabor Kovesdan errx(2, "%s", getstr(11)); 830c66bbc91SGabor Kovesdan } 831c66bbc91SGabor Kovesdan 832c66bbc91SGabor Kovesdan if (pmatch[3].rm_eo > pmatch[3].rm_so) { 833c66bbc91SGabor Kovesdan 834c66bbc91SGabor Kovesdan len = pmatch[3].rm_eo - pmatch[3].rm_so; 835c66bbc91SGabor Kovesdan 836c66bbc91SGabor Kovesdan strncpy(sopts, s + pmatch[3].rm_so, len); 837c66bbc91SGabor Kovesdan sopts[len] = '\0'; 838c66bbc91SGabor Kovesdan } 839c66bbc91SGabor Kovesdan 840c66bbc91SGabor Kovesdan ret = 0; 841c66bbc91SGabor Kovesdan 842c66bbc91SGabor Kovesdan end: 843c66bbc91SGabor Kovesdan if (c) 844c66bbc91SGabor Kovesdan sort_free(c); 845c66bbc91SGabor Kovesdan if (f) 846c66bbc91SGabor Kovesdan sort_free(f); 847c66bbc91SGabor Kovesdan regfree(&re); 848c66bbc91SGabor Kovesdan 849c66bbc91SGabor Kovesdan return (ret); 850c66bbc91SGabor Kovesdan } 851c66bbc91SGabor Kovesdan 852c66bbc91SGabor Kovesdan /* 853c66bbc91SGabor Kovesdan * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax 854c66bbc91SGabor Kovesdan */ 855c66bbc91SGabor Kovesdan void 856c66bbc91SGabor Kovesdan fix_obsolete_keys(int *argc, char **argv) 857c66bbc91SGabor Kovesdan { 858c66bbc91SGabor Kovesdan char sopt[129]; 859c66bbc91SGabor Kovesdan 860c66bbc91SGabor Kovesdan for (int i = 1; i < *argc; i++) { 861c66bbc91SGabor Kovesdan char *arg1; 862c66bbc91SGabor Kovesdan 863c66bbc91SGabor Kovesdan arg1 = argv[i]; 864c66bbc91SGabor Kovesdan 865c66bbc91SGabor Kovesdan if (strlen(arg1) > 1 && arg1[0] == '+') { 866c66bbc91SGabor Kovesdan int c1, f1; 867c66bbc91SGabor Kovesdan char sopts1[128]; 868c66bbc91SGabor Kovesdan 869c66bbc91SGabor Kovesdan sopts1[0] = 0; 870c66bbc91SGabor Kovesdan c1 = f1 = 0; 871c66bbc91SGabor Kovesdan 872c66bbc91SGabor Kovesdan if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1) < 0) 873c66bbc91SGabor Kovesdan continue; 874c66bbc91SGabor Kovesdan else { 875c66bbc91SGabor Kovesdan f1 += 1; 876c66bbc91SGabor Kovesdan c1 += 1; 877c66bbc91SGabor Kovesdan if (i + 1 < *argc) { 878c66bbc91SGabor Kovesdan char *arg2 = argv[i + 1]; 879c66bbc91SGabor Kovesdan 880c66bbc91SGabor Kovesdan if (strlen(arg2) > 1 && 881c66bbc91SGabor Kovesdan arg2[0] == '-') { 882c66bbc91SGabor Kovesdan int c2, f2; 883c66bbc91SGabor Kovesdan char sopts2[128]; 884c66bbc91SGabor Kovesdan 885c66bbc91SGabor Kovesdan sopts2[0] = 0; 886c66bbc91SGabor Kovesdan c2 = f2 = 0; 887c66bbc91SGabor Kovesdan 888c66bbc91SGabor Kovesdan if (parse_pos_obs(arg2 + 1, 889c66bbc91SGabor Kovesdan &f2, &c2, sopts2) >= 0) { 890c66bbc91SGabor Kovesdan if (c2 > 0) 891c66bbc91SGabor Kovesdan f2 += 1; 892c66bbc91SGabor Kovesdan sprintf(sopt, "-k%d.%d%s,%d.%d%s", 893c66bbc91SGabor Kovesdan f1, c1, sopts1, f2, c2, sopts2); 894c66bbc91SGabor Kovesdan argv[i] = sort_strdup(sopt); 895c66bbc91SGabor Kovesdan for (int j = i + 1; j + 1 < *argc; j++) 896c66bbc91SGabor Kovesdan argv[j] = argv[j + 1]; 897c66bbc91SGabor Kovesdan *argc -= 1; 898c66bbc91SGabor Kovesdan continue; 899c66bbc91SGabor Kovesdan } 900c66bbc91SGabor Kovesdan } 901c66bbc91SGabor Kovesdan } 9023e16491dSBaptiste Daroussin sprintf(sopt, "-k%d.%d%s", f1, c1, sopts1); 903c66bbc91SGabor Kovesdan argv[i] = sort_strdup(sopt); 904c66bbc91SGabor Kovesdan } 905c66bbc91SGabor Kovesdan } 906c66bbc91SGabor Kovesdan } 907c66bbc91SGabor Kovesdan } 908c66bbc91SGabor Kovesdan 909c66bbc91SGabor Kovesdan /* 910c66bbc91SGabor Kovesdan * Set random seed 911c66bbc91SGabor Kovesdan */ 912c66bbc91SGabor Kovesdan static void 913c66bbc91SGabor Kovesdan set_random_seed(void) 914c66bbc91SGabor Kovesdan { 915c66bbc91SGabor Kovesdan if (strcmp(random_source, DEFAULT_RANDOM_SORT_SEED_FILE) == 0) { 916c66bbc91SGabor Kovesdan FILE* fseed; 917c66bbc91SGabor Kovesdan MD5_CTX ctx; 918c66bbc91SGabor Kovesdan char rsd[MAX_DEFAULT_RANDOM_SEED_DATA_SIZE]; 919c66bbc91SGabor Kovesdan size_t sz = 0; 920c66bbc91SGabor Kovesdan 921c66bbc91SGabor Kovesdan fseed = openfile(random_source, "r"); 922c66bbc91SGabor Kovesdan while (!feof(fseed)) { 923c66bbc91SGabor Kovesdan int cr; 924c66bbc91SGabor Kovesdan 925c66bbc91SGabor Kovesdan cr = fgetc(fseed); 926c66bbc91SGabor Kovesdan if (cr == EOF) 927c66bbc91SGabor Kovesdan break; 928c66bbc91SGabor Kovesdan 929c66bbc91SGabor Kovesdan rsd[sz++] = (char) cr; 930c66bbc91SGabor Kovesdan 931c66bbc91SGabor Kovesdan if (sz >= MAX_DEFAULT_RANDOM_SEED_DATA_SIZE) 932c66bbc91SGabor Kovesdan break; 933c66bbc91SGabor Kovesdan } 934c66bbc91SGabor Kovesdan 935c66bbc91SGabor Kovesdan closefile(fseed, random_source); 936c66bbc91SGabor Kovesdan 937c66bbc91SGabor Kovesdan MD5Init(&ctx); 938c66bbc91SGabor Kovesdan MD5Update(&ctx, rsd, sz); 939c66bbc91SGabor Kovesdan 940c66bbc91SGabor Kovesdan random_seed = MD5End(&ctx, NULL); 941c66bbc91SGabor Kovesdan random_seed_size = strlen(random_seed); 942c66bbc91SGabor Kovesdan 943c66bbc91SGabor Kovesdan } else { 944c66bbc91SGabor Kovesdan MD5_CTX ctx; 945c66bbc91SGabor Kovesdan char *b; 946c66bbc91SGabor Kovesdan 947c66bbc91SGabor Kovesdan MD5Init(&ctx); 948c66bbc91SGabor Kovesdan b = MD5File(random_source, NULL); 949c66bbc91SGabor Kovesdan if (b == NULL) 950c66bbc91SGabor Kovesdan err(2, NULL); 951c66bbc91SGabor Kovesdan 952c66bbc91SGabor Kovesdan random_seed = b; 953c66bbc91SGabor Kovesdan random_seed_size = strlen(b); 954c66bbc91SGabor Kovesdan } 955c66bbc91SGabor Kovesdan MD5Init(&md5_ctx); 956c66bbc91SGabor Kovesdan if(random_seed_size>0) { 957c66bbc91SGabor Kovesdan MD5Update(&md5_ctx, random_seed, random_seed_size); 958c66bbc91SGabor Kovesdan } 959c66bbc91SGabor Kovesdan } 960c66bbc91SGabor Kovesdan 961c66bbc91SGabor Kovesdan /* 962c66bbc91SGabor Kovesdan * Main function. 963c66bbc91SGabor Kovesdan */ 964c66bbc91SGabor Kovesdan int 965c66bbc91SGabor Kovesdan main(int argc, char **argv) 966c66bbc91SGabor Kovesdan { 967c66bbc91SGabor Kovesdan char *outfile, *real_outfile; 968c66bbc91SGabor Kovesdan int c, result; 969c66bbc91SGabor Kovesdan bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = 970c66bbc91SGabor Kovesdan { false, false, false, false, false, false }; 971c66bbc91SGabor Kovesdan 972c66bbc91SGabor Kovesdan result = 0; 973c66bbc91SGabor Kovesdan outfile = sort_strdup("-"); 974c66bbc91SGabor Kovesdan real_outfile = NULL; 975c66bbc91SGabor Kovesdan 976c66bbc91SGabor Kovesdan struct sort_mods *sm = &default_sort_mods_object; 977c66bbc91SGabor Kovesdan 978c66bbc91SGabor Kovesdan init_tmp_files(); 979c66bbc91SGabor Kovesdan 980c66bbc91SGabor Kovesdan set_signal_handler(); 981c66bbc91SGabor Kovesdan 982c66bbc91SGabor Kovesdan set_hw_params(); 983c66bbc91SGabor Kovesdan set_locale(); 984c66bbc91SGabor Kovesdan set_tmpdir(); 985c66bbc91SGabor Kovesdan set_sort_opts(); 986c66bbc91SGabor Kovesdan 987c66bbc91SGabor Kovesdan fix_obsolete_keys(&argc, argv); 988c66bbc91SGabor Kovesdan 989c66bbc91SGabor Kovesdan while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL)) 990c66bbc91SGabor Kovesdan != -1)) { 991c66bbc91SGabor Kovesdan 992c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(c, mef_flags); 993c66bbc91SGabor Kovesdan 994c66bbc91SGabor Kovesdan if (!set_sort_modifier(sm, c)) { 995c66bbc91SGabor Kovesdan 996c66bbc91SGabor Kovesdan switch (c) { 997c66bbc91SGabor Kovesdan case 'c': 998c66bbc91SGabor Kovesdan sort_opts_vals.cflag = true; 999c66bbc91SGabor Kovesdan if (optarg) { 1000c66bbc91SGabor Kovesdan if (!strcmp(optarg, "diagnose-first")) 1001c66bbc91SGabor Kovesdan ; 1002c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "silent") || 1003c66bbc91SGabor Kovesdan !strcmp(optarg, "quiet")) 1004c66bbc91SGabor Kovesdan sort_opts_vals.csilentflag = true; 1005c66bbc91SGabor Kovesdan else if (*optarg) 1006c66bbc91SGabor Kovesdan unknown(optarg); 1007c66bbc91SGabor Kovesdan } 1008c66bbc91SGabor Kovesdan break; 1009c66bbc91SGabor Kovesdan case 'C': 1010c66bbc91SGabor Kovesdan sort_opts_vals.cflag = true; 1011c66bbc91SGabor Kovesdan sort_opts_vals.csilentflag = true; 1012c66bbc91SGabor Kovesdan break; 1013c66bbc91SGabor Kovesdan case 'k': 1014c66bbc91SGabor Kovesdan { 1015c66bbc91SGabor Kovesdan sort_opts_vals.complex_sort = true; 1016c66bbc91SGabor Kovesdan sort_opts_vals.kflag = true; 1017c66bbc91SGabor Kovesdan 1018c66bbc91SGabor Kovesdan keys_num++; 1019c66bbc91SGabor Kovesdan keys = sort_realloc(keys, keys_num * 1020c66bbc91SGabor Kovesdan sizeof(struct key_specs)); 1021c66bbc91SGabor Kovesdan memset(&(keys[keys_num - 1]), 0, 1022c66bbc91SGabor Kovesdan sizeof(struct key_specs)); 1023c66bbc91SGabor Kovesdan 1024c66bbc91SGabor Kovesdan if (parse_k(optarg, &(keys[keys_num - 1])) 1025c66bbc91SGabor Kovesdan < 0) { 1026f187ff08SGabor Kovesdan errc(2, EINVAL, "-k %s", optarg); 1027c66bbc91SGabor Kovesdan } 1028c66bbc91SGabor Kovesdan 1029c66bbc91SGabor Kovesdan break; 1030c66bbc91SGabor Kovesdan } 1031c66bbc91SGabor Kovesdan case 'm': 1032c66bbc91SGabor Kovesdan sort_opts_vals.mflag = true; 1033c66bbc91SGabor Kovesdan break; 1034c66bbc91SGabor Kovesdan case 'o': 1035f50d9b2fSGabor Kovesdan outfile = sort_realloc(outfile, (strlen(optarg) + 1)); 1036f50d9b2fSGabor Kovesdan strcpy(outfile, optarg); 1037c66bbc91SGabor Kovesdan break; 1038c66bbc91SGabor Kovesdan case 's': 1039c66bbc91SGabor Kovesdan sort_opts_vals.sflag = true; 1040c66bbc91SGabor Kovesdan break; 1041c66bbc91SGabor Kovesdan case 'S': 1042c66bbc91SGabor Kovesdan available_free_memory = 1043c66bbc91SGabor Kovesdan parse_memory_buffer_value(optarg); 1044c66bbc91SGabor Kovesdan break; 1045c66bbc91SGabor Kovesdan case 'T': 1046c66bbc91SGabor Kovesdan tmpdir = sort_strdup(optarg); 1047c66bbc91SGabor Kovesdan break; 1048c66bbc91SGabor Kovesdan case 't': 10495ca724dcSGabor Kovesdan while (strlen(optarg) > 1) { 10505ca724dcSGabor Kovesdan if (optarg[0] != '\\') { 1051f187ff08SGabor Kovesdan errc(2, EINVAL, "%s", optarg); 1052c66bbc91SGabor Kovesdan } 10535ca724dcSGabor Kovesdan optarg += 1; 10545ca724dcSGabor Kovesdan if (*optarg == '0') { 1055c66bbc91SGabor Kovesdan *optarg = 0; 10565ca724dcSGabor Kovesdan break; 10575ca724dcSGabor Kovesdan } 1058c66bbc91SGabor Kovesdan } 1059c66bbc91SGabor Kovesdan sort_opts_vals.tflag = true; 1060c66bbc91SGabor Kovesdan sort_opts_vals.field_sep = btowc(optarg[0]); 1061c66bbc91SGabor Kovesdan if (sort_opts_vals.field_sep == WEOF) { 1062c66bbc91SGabor Kovesdan errno = EINVAL; 1063c66bbc91SGabor Kovesdan err(2, NULL); 1064c66bbc91SGabor Kovesdan } 1065c66bbc91SGabor Kovesdan if (!gnusort_numeric_compatibility) { 1066c66bbc91SGabor Kovesdan if (symbol_decimal_point == sort_opts_vals.field_sep) 1067c66bbc91SGabor Kovesdan symbol_decimal_point = WEOF; 1068c66bbc91SGabor Kovesdan if (symbol_thousands_sep == sort_opts_vals.field_sep) 1069c66bbc91SGabor Kovesdan symbol_thousands_sep = WEOF; 1070c66bbc91SGabor Kovesdan if (symbol_negative_sign == sort_opts_vals.field_sep) 1071c66bbc91SGabor Kovesdan symbol_negative_sign = WEOF; 1072c66bbc91SGabor Kovesdan if (symbol_positive_sign == sort_opts_vals.field_sep) 1073c66bbc91SGabor Kovesdan symbol_positive_sign = WEOF; 1074c66bbc91SGabor Kovesdan } 1075c66bbc91SGabor Kovesdan break; 1076c66bbc91SGabor Kovesdan case 'u': 1077c66bbc91SGabor Kovesdan sort_opts_vals.uflag = true; 1078c66bbc91SGabor Kovesdan /* stable sort for the correct unique val */ 1079c66bbc91SGabor Kovesdan sort_opts_vals.sflag = true; 1080c66bbc91SGabor Kovesdan break; 1081c66bbc91SGabor Kovesdan case 'z': 1082c66bbc91SGabor Kovesdan sort_opts_vals.zflag = true; 1083c66bbc91SGabor Kovesdan break; 1084c66bbc91SGabor Kovesdan case SORT_OPT: 1085c66bbc91SGabor Kovesdan if (optarg) { 1086c66bbc91SGabor Kovesdan if (!strcmp(optarg, "general-numeric")) 1087c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'g'); 1088c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "human-numeric")) 1089c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'h'); 1090c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "numeric")) 1091c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'n'); 1092c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "month")) 1093c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'M'); 1094c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "random")) 1095c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'R'); 1096c66bbc91SGabor Kovesdan else 1097c66bbc91SGabor Kovesdan unknown(optarg); 1098c66bbc91SGabor Kovesdan } 1099c66bbc91SGabor Kovesdan break; 1100c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 11015d5151aeSGabor Kovesdan case PARALLEL_OPT: 1102c66bbc91SGabor Kovesdan nthreads = (size_t)(atoi(optarg)); 1103c66bbc91SGabor Kovesdan if (nthreads < 1) 1104c66bbc91SGabor Kovesdan nthreads = 1; 1105c66bbc91SGabor Kovesdan if (nthreads > 1024) 1106c66bbc91SGabor Kovesdan nthreads = 1024; 1107c66bbc91SGabor Kovesdan break; 1108c66bbc91SGabor Kovesdan #endif 1109c66bbc91SGabor Kovesdan case QSORT_OPT: 1110c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_QSORT; 1111c66bbc91SGabor Kovesdan break; 1112c66bbc91SGabor Kovesdan case MERGESORT_OPT: 1113c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_MERGESORT; 1114c66bbc91SGabor Kovesdan break; 11155ca724dcSGabor Kovesdan case MMAP_OPT: 11165ca724dcSGabor Kovesdan use_mmap = true; 11175ca724dcSGabor Kovesdan break; 1118c66bbc91SGabor Kovesdan case HEAPSORT_OPT: 1119c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_HEAPSORT; 1120c66bbc91SGabor Kovesdan break; 1121c66bbc91SGabor Kovesdan case RADIXSORT_OPT: 1122c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_RADIXSORT; 1123c66bbc91SGabor Kovesdan break; 1124c66bbc91SGabor Kovesdan case RANDOMSOURCE_OPT: 1125c66bbc91SGabor Kovesdan random_source = strdup(optarg); 1126c66bbc91SGabor Kovesdan break; 1127c66bbc91SGabor Kovesdan case COMPRESSPROGRAM_OPT: 1128c66bbc91SGabor Kovesdan compress_program = strdup(optarg); 1129c66bbc91SGabor Kovesdan break; 1130c66bbc91SGabor Kovesdan case FF_OPT: 1131c66bbc91SGabor Kovesdan read_fns_from_file0(optarg); 1132c66bbc91SGabor Kovesdan break; 1133c66bbc91SGabor Kovesdan case BS_OPT: 1134c66bbc91SGabor Kovesdan { 1135c66bbc91SGabor Kovesdan errno = 0; 1136c66bbc91SGabor Kovesdan long mof = strtol(optarg, NULL, 10); 1137c66bbc91SGabor Kovesdan if (errno != 0) 1138f187ff08SGabor Kovesdan err(2, "--batch-size"); 1139c66bbc91SGabor Kovesdan if (mof >= 2) 1140c66bbc91SGabor Kovesdan max_open_files = (size_t) mof + 1; 1141c66bbc91SGabor Kovesdan } 1142c66bbc91SGabor Kovesdan break; 1143c66bbc91SGabor Kovesdan case VERSION_OPT: 1144c66bbc91SGabor Kovesdan printf("%s\n", VERSION); 1145c66bbc91SGabor Kovesdan exit(EXIT_SUCCESS); 1146c66bbc91SGabor Kovesdan /* NOTREACHED */ 1147c66bbc91SGabor Kovesdan break; 1148c66bbc91SGabor Kovesdan case DEBUG_OPT: 1149c66bbc91SGabor Kovesdan debug_sort = true; 1150c66bbc91SGabor Kovesdan break; 1151c66bbc91SGabor Kovesdan case HELP_OPT: 1152c66bbc91SGabor Kovesdan usage(false); 1153c66bbc91SGabor Kovesdan /* NOTREACHED */ 1154c66bbc91SGabor Kovesdan break; 1155c66bbc91SGabor Kovesdan default: 1156c66bbc91SGabor Kovesdan usage(true); 1157c66bbc91SGabor Kovesdan /* NOTREACHED */ 1158c66bbc91SGabor Kovesdan } 1159c66bbc91SGabor Kovesdan } 1160c66bbc91SGabor Kovesdan } 1161c66bbc91SGabor Kovesdan 1162c66bbc91SGabor Kovesdan argc -= optind; 1163c66bbc91SGabor Kovesdan argv += optind; 1164c66bbc91SGabor Kovesdan 1165a7bc1892SGabor Kovesdan if (argv_from_file0) { 1166a7bc1892SGabor Kovesdan argc = argc_from_file0; 1167a7bc1892SGabor Kovesdan argv = argv_from_file0; 1168a7bc1892SGabor Kovesdan } 1169a7bc1892SGabor Kovesdan 1170c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 1171c66bbc91SGabor Kovesdan catalog = catopen("sort", NL_CAT_LOCALE); 1172c66bbc91SGabor Kovesdan #endif 1173c66bbc91SGabor Kovesdan 1174c66bbc91SGabor Kovesdan if (sort_opts_vals.cflag && sort_opts_vals.mflag) 11758818aa39SGabor Kovesdan errx(1, "%c:%c: %s", 'm', 'c', getstr(1)); 1176c66bbc91SGabor Kovesdan 1177c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 1178c66bbc91SGabor Kovesdan catclose(catalog); 1179c66bbc91SGabor Kovesdan #endif 1180c66bbc91SGabor Kovesdan 1181c66bbc91SGabor Kovesdan if (keys_num == 0) { 1182c66bbc91SGabor Kovesdan keys_num = 1; 1183c66bbc91SGabor Kovesdan keys = sort_realloc(keys, sizeof(struct key_specs)); 1184c66bbc91SGabor Kovesdan memset(&(keys[0]), 0, sizeof(struct key_specs)); 1185c66bbc91SGabor Kovesdan keys[0].c1 = 1; 1186c66bbc91SGabor Kovesdan keys[0].pos1b = default_sort_mods->bflag; 1187c66bbc91SGabor Kovesdan keys[0].pos2b = default_sort_mods->bflag; 1188c66bbc91SGabor Kovesdan memcpy(&(keys[0].sm), default_sort_mods, 1189c66bbc91SGabor Kovesdan sizeof(struct sort_mods)); 1190c66bbc91SGabor Kovesdan } 1191c66bbc91SGabor Kovesdan 1192c66bbc91SGabor Kovesdan for (size_t i = 0; i < keys_num; i++) { 1193c66bbc91SGabor Kovesdan struct key_specs *ks; 1194c66bbc91SGabor Kovesdan 1195c66bbc91SGabor Kovesdan ks = &(keys[i]); 1196c66bbc91SGabor Kovesdan 1197c66bbc91SGabor Kovesdan if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) && 1198c66bbc91SGabor Kovesdan !(ks->pos2b)) { 1199c66bbc91SGabor Kovesdan ks->pos1b = sm->bflag; 1200c66bbc91SGabor Kovesdan ks->pos2b = sm->bflag; 1201c66bbc91SGabor Kovesdan memcpy(&(ks->sm), sm, sizeof(struct sort_mods)); 1202c66bbc91SGabor Kovesdan } 1203c66bbc91SGabor Kovesdan 1204c66bbc91SGabor Kovesdan ks->sm.func = get_sort_func(&(ks->sm)); 1205c66bbc91SGabor Kovesdan } 1206c66bbc91SGabor Kovesdan 1207c66bbc91SGabor Kovesdan if (debug_sort) { 120855444243SGabor Kovesdan printf("Memory to be used for sorting: %llu\n",available_free_memory); 1209c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 121055444243SGabor Kovesdan printf("Number of CPUs: %d\n",(int)ncpu); 1211c66bbc91SGabor Kovesdan nthreads = 1; 1212c66bbc91SGabor Kovesdan #endif 1213c66bbc91SGabor Kovesdan printf("Using collate rules of %s locale\n", 1214c66bbc91SGabor Kovesdan setlocale(LC_COLLATE, NULL)); 1215c66bbc91SGabor Kovesdan if (byte_sort) 1216c66bbc91SGabor Kovesdan printf("Byte sort is used\n"); 1217c66bbc91SGabor Kovesdan if (print_symbols_on_debug) { 1218c66bbc91SGabor Kovesdan printf("Decimal Point: <%lc>\n", symbol_decimal_point); 1219c66bbc91SGabor Kovesdan if (symbol_thousands_sep) 1220c66bbc91SGabor Kovesdan printf("Thousands separator: <%lc>\n", 1221c66bbc91SGabor Kovesdan symbol_thousands_sep); 1222c66bbc91SGabor Kovesdan printf("Positive sign: <%lc>\n", symbol_positive_sign); 1223c66bbc91SGabor Kovesdan printf("Negative sign: <%lc>\n", symbol_negative_sign); 1224c66bbc91SGabor Kovesdan } 1225c66bbc91SGabor Kovesdan } 1226c66bbc91SGabor Kovesdan 1227*74504eefSConrad Meyer if (need_random) 1228c66bbc91SGabor Kovesdan set_random_seed(); 1229c66bbc91SGabor Kovesdan 1230c66bbc91SGabor Kovesdan /* Case when the outfile equals one of the input files: */ 1231c66bbc91SGabor Kovesdan if (strcmp(outfile, "-")) { 1232c66bbc91SGabor Kovesdan 1233c66bbc91SGabor Kovesdan for(int i = 0; i < argc; ++i) { 1234c66bbc91SGabor Kovesdan if (strcmp(argv[i], outfile) == 0) { 1235c66bbc91SGabor Kovesdan real_outfile = sort_strdup(outfile); 1236c66bbc91SGabor Kovesdan for(;;) { 1237c66bbc91SGabor Kovesdan char* tmp = sort_malloc(strlen(outfile) + 1238c66bbc91SGabor Kovesdan strlen(".tmp") + 1); 1239c66bbc91SGabor Kovesdan 1240c66bbc91SGabor Kovesdan strcpy(tmp, outfile); 1241c66bbc91SGabor Kovesdan strcpy(tmp + strlen(tmp), ".tmp"); 1242c66bbc91SGabor Kovesdan sort_free(outfile); 1243c66bbc91SGabor Kovesdan outfile = tmp; 1244c66bbc91SGabor Kovesdan if (access(outfile, F_OK) < 0) 1245c66bbc91SGabor Kovesdan break; 1246c66bbc91SGabor Kovesdan } 1247c66bbc91SGabor Kovesdan tmp_file_atexit(outfile); 1248c66bbc91SGabor Kovesdan } 1249c66bbc91SGabor Kovesdan } 1250c66bbc91SGabor Kovesdan } 1251c66bbc91SGabor Kovesdan 12525ca724dcSGabor Kovesdan #if defined(SORT_THREADS) 12535ca724dcSGabor Kovesdan if ((argc < 1) || (strcmp(outfile, "-") == 0) || (*outfile == 0)) 12545ca724dcSGabor Kovesdan nthreads = 1; 12555ca724dcSGabor Kovesdan #endif 12565ca724dcSGabor Kovesdan 1257c66bbc91SGabor Kovesdan if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) { 1258c66bbc91SGabor Kovesdan struct file_list fl; 1259c66bbc91SGabor Kovesdan struct sort_list list; 1260c66bbc91SGabor Kovesdan 1261c66bbc91SGabor Kovesdan sort_list_init(&list); 1262c66bbc91SGabor Kovesdan file_list_init(&fl, true); 1263c66bbc91SGabor Kovesdan 1264c66bbc91SGabor Kovesdan if (argc < 1) 1265c66bbc91SGabor Kovesdan procfile("-", &list, &fl); 1266c66bbc91SGabor Kovesdan else { 1267c66bbc91SGabor Kovesdan while (argc > 0) { 1268c66bbc91SGabor Kovesdan procfile(*argv, &list, &fl); 1269c66bbc91SGabor Kovesdan --argc; 1270c66bbc91SGabor Kovesdan ++argv; 1271c66bbc91SGabor Kovesdan } 1272c66bbc91SGabor Kovesdan } 1273c66bbc91SGabor Kovesdan 1274c66bbc91SGabor Kovesdan if (fl.count < 1) 1275c66bbc91SGabor Kovesdan sort_list_to_file(&list, outfile); 1276c66bbc91SGabor Kovesdan else { 1277c66bbc91SGabor Kovesdan if (list.count > 0) { 1278c66bbc91SGabor Kovesdan char *flast = new_tmp_file_name(); 1279c66bbc91SGabor Kovesdan 1280c66bbc91SGabor Kovesdan sort_list_to_file(&list, flast); 1281c66bbc91SGabor Kovesdan file_list_add(&fl, flast, false); 1282c66bbc91SGabor Kovesdan } 1283c66bbc91SGabor Kovesdan merge_files(&fl, outfile); 1284c66bbc91SGabor Kovesdan } 1285c66bbc91SGabor Kovesdan 1286c66bbc91SGabor Kovesdan file_list_clean(&fl); 1287c66bbc91SGabor Kovesdan 1288c66bbc91SGabor Kovesdan /* 1289c66bbc91SGabor Kovesdan * We are about to exit the program, so we can ignore 1290c66bbc91SGabor Kovesdan * the clean-up for speed 1291c66bbc91SGabor Kovesdan * 1292c66bbc91SGabor Kovesdan * sort_list_clean(&list); 1293c66bbc91SGabor Kovesdan */ 1294c66bbc91SGabor Kovesdan 1295c66bbc91SGabor Kovesdan } else if (sort_opts_vals.cflag) { 1296c66bbc91SGabor Kovesdan result = (argc == 0) ? (check("-")) : (check(*argv)); 1297c66bbc91SGabor Kovesdan } else if (sort_opts_vals.mflag) { 1298c66bbc91SGabor Kovesdan struct file_list fl; 1299c66bbc91SGabor Kovesdan 1300c66bbc91SGabor Kovesdan file_list_init(&fl, false); 13017137597eSKyle Evans /* No file arguments remaining means "read from stdin." */ 13027137597eSKyle Evans if (argc == 0) 13037137597eSKyle Evans file_list_add(&fl, "-", true); 13047137597eSKyle Evans else 1305c66bbc91SGabor Kovesdan file_list_populate(&fl, argc, argv, true); 1306c66bbc91SGabor Kovesdan merge_files(&fl, outfile); 1307c66bbc91SGabor Kovesdan file_list_clean(&fl); 1308c66bbc91SGabor Kovesdan } 1309c66bbc91SGabor Kovesdan 1310c66bbc91SGabor Kovesdan if (real_outfile) { 1311c66bbc91SGabor Kovesdan unlink(real_outfile); 1312c66bbc91SGabor Kovesdan if (rename(outfile, real_outfile) < 0) 1313c66bbc91SGabor Kovesdan err(2, NULL); 1314c66bbc91SGabor Kovesdan sort_free(real_outfile); 1315c66bbc91SGabor Kovesdan } 1316c66bbc91SGabor Kovesdan 1317c66bbc91SGabor Kovesdan sort_free(outfile); 1318c66bbc91SGabor Kovesdan 1319c66bbc91SGabor Kovesdan return (result); 1320c66bbc91SGabor Kovesdan } 1321