1c66bbc91SGabor Kovesdan /*- 2c66bbc91SGabor Kovesdan * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> 3c66bbc91SGabor Kovesdan * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko@citrix.com> 4c66bbc91SGabor Kovesdan * All rights reserved. 5c66bbc91SGabor Kovesdan * 6c66bbc91SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 7c66bbc91SGabor Kovesdan * modification, are permitted provided that the following conditions 8c66bbc91SGabor Kovesdan * are met: 9c66bbc91SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 10c66bbc91SGabor Kovesdan * notice, this list of conditions and the following disclaimer. 11c66bbc91SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 12c66bbc91SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 13c66bbc91SGabor Kovesdan * documentation and/or other materials provided with the distribution. 14c66bbc91SGabor Kovesdan * 15c66bbc91SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16c66bbc91SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17c66bbc91SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18c66bbc91SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19c66bbc91SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20c66bbc91SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21c66bbc91SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22c66bbc91SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23c66bbc91SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24c66bbc91SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25c66bbc91SGabor Kovesdan * SUCH DAMAGE. 26c66bbc91SGabor Kovesdan */ 27c66bbc91SGabor Kovesdan 28c66bbc91SGabor Kovesdan #include <sys/cdefs.h> 29c66bbc91SGabor Kovesdan __FBSDID("$FreeBSD$"); 30c66bbc91SGabor Kovesdan 31c66bbc91SGabor Kovesdan #include <sys/stat.h> 32c66bbc91SGabor Kovesdan #include <sys/sysctl.h> 33c66bbc91SGabor Kovesdan #include <sys/types.h> 34c66bbc91SGabor Kovesdan 35c66bbc91SGabor Kovesdan #include <err.h> 36c66bbc91SGabor Kovesdan #include <errno.h> 37c66bbc91SGabor Kovesdan #include <getopt.h> 38c66bbc91SGabor Kovesdan #include <limits.h> 39c66bbc91SGabor Kovesdan #include <locale.h> 40c66bbc91SGabor Kovesdan #include <md5.h> 41c66bbc91SGabor Kovesdan #include <regex.h> 42c66bbc91SGabor Kovesdan #include <signal.h> 43c66bbc91SGabor Kovesdan #include <stdbool.h> 44c66bbc91SGabor Kovesdan #include <stdio.h> 45c66bbc91SGabor Kovesdan #include <stdlib.h> 46c66bbc91SGabor Kovesdan #include <string.h> 47c66bbc91SGabor Kovesdan #include <unistd.h> 48c66bbc91SGabor Kovesdan #include <wchar.h> 49c66bbc91SGabor Kovesdan #include <wctype.h> 50c66bbc91SGabor Kovesdan 51c66bbc91SGabor Kovesdan #include "coll.h" 52c66bbc91SGabor Kovesdan #include "file.h" 53c66bbc91SGabor Kovesdan #include "sort.h" 54c66bbc91SGabor Kovesdan 55c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 56c66bbc91SGabor Kovesdan #include <nl_types.h> 57c66bbc91SGabor Kovesdan nl_catd catalog; 58c66bbc91SGabor Kovesdan #endif 59c66bbc91SGabor Kovesdan 60c66bbc91SGabor Kovesdan #define OPTIONS "bcCdfghik:Mmno:RrsS:t:T:uVz" 61c66bbc91SGabor Kovesdan 62c66bbc91SGabor Kovesdan #define DEFAULT_RANDOM_SORT_SEED_FILE ("/dev/random") 63c66bbc91SGabor Kovesdan #define MAX_DEFAULT_RANDOM_SEED_DATA_SIZE (1024) 64c66bbc91SGabor Kovesdan 65ce1e997fSGabor Kovesdan static bool need_random; 66c66bbc91SGabor Kovesdan static const char *random_source = DEFAULT_RANDOM_SORT_SEED_FILE; 67ce1e997fSGabor Kovesdan static const void *random_seed; 68ce1e997fSGabor Kovesdan static size_t random_seed_size; 69c66bbc91SGabor Kovesdan 70c66bbc91SGabor Kovesdan MD5_CTX md5_ctx; 71c66bbc91SGabor Kovesdan 72c66bbc91SGabor Kovesdan /* 73c66bbc91SGabor Kovesdan * Default messages to use when NLS is disabled or no catalogue 74c66bbc91SGabor Kovesdan * is found. 75c66bbc91SGabor Kovesdan */ 76c66bbc91SGabor Kovesdan const char *nlsstr[] = { "", 778818aa39SGabor Kovesdan /* 1*/"mutually exclusive flags", 78c66bbc91SGabor Kovesdan /* 2*/"extra argument not allowed with -c", 798818aa39SGabor Kovesdan /* 3*/"Unknown feature", 80c66bbc91SGabor Kovesdan /* 4*/"Wrong memory buffer specification", 81c66bbc91SGabor Kovesdan /* 5*/"0 field in key specs", 82c66bbc91SGabor Kovesdan /* 6*/"0 column in key specs", 83c66bbc91SGabor Kovesdan /* 7*/"Wrong file mode", 84c66bbc91SGabor Kovesdan /* 8*/"Cannot open file for reading", 85c66bbc91SGabor Kovesdan /* 9*/"Radix sort cannot be used with these sort options", 86c66bbc91SGabor Kovesdan /*10*/"The chosen sort method cannot be used with stable and/or unique sort", 87c66bbc91SGabor Kovesdan /*11*/"Invalid key position", 88c66bbc91SGabor Kovesdan /*12*/"Usage: %s [-bcCdfigMmnrsuz] [-kPOS1[,POS2] ... ] " 89c66bbc91SGabor Kovesdan "[+POS1 [-POS2]] [-S memsize] [-T tmpdir] [-t separator] " 90c66bbc91SGabor Kovesdan "[-o outfile] [--batch-size size] [--files0-from file] " 91c66bbc91SGabor Kovesdan "[--heapsort] [--mergesort] [--radixsort] [--qsort] " 92*5ca724dcSGabor Kovesdan "[--mmap] " 93c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 94c66bbc91SGabor Kovesdan "[--nthreads thread_no] " 95c66bbc91SGabor Kovesdan #endif 96c66bbc91SGabor Kovesdan "[--human-numeric-sort] " 97c66bbc91SGabor Kovesdan "[--version-sort] [--random-sort [--random-source file]] " 98c66bbc91SGabor Kovesdan "[--compress-program program] [file ...]\n" }; 99c66bbc91SGabor Kovesdan 100c66bbc91SGabor Kovesdan struct sort_opts sort_opts_vals; 101c66bbc91SGabor Kovesdan 102ce1e997fSGabor Kovesdan bool debug_sort; 103ce1e997fSGabor Kovesdan bool need_hint; 104c66bbc91SGabor Kovesdan 105c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 106c66bbc91SGabor Kovesdan size_t ncpu = 1; 107c66bbc91SGabor Kovesdan size_t nthreads = 1; 108c66bbc91SGabor Kovesdan #endif 109c66bbc91SGabor Kovesdan 110ce1e997fSGabor Kovesdan static bool gnusort_numeric_compatibility; 111c66bbc91SGabor Kovesdan 112c66bbc91SGabor Kovesdan static struct sort_mods default_sort_mods_object; 113c66bbc91SGabor Kovesdan struct sort_mods * const default_sort_mods = &default_sort_mods_object; 114c66bbc91SGabor Kovesdan 115ce1e997fSGabor Kovesdan static bool print_symbols_on_debug; 116c66bbc91SGabor Kovesdan 117c66bbc91SGabor Kovesdan /* 118c66bbc91SGabor Kovesdan * Arguments from file (when file0-from option is used: 119c66bbc91SGabor Kovesdan */ 120c66bbc91SGabor Kovesdan static int argc_from_file0 = -1; 121ce1e997fSGabor Kovesdan static char **argv_from_file0; 122c66bbc91SGabor Kovesdan 123c66bbc91SGabor Kovesdan /* 124c66bbc91SGabor Kovesdan * Placeholder symbols for options which have no single-character equivalent 125c66bbc91SGabor Kovesdan */ 126c66bbc91SGabor Kovesdan enum 127c66bbc91SGabor Kovesdan { 128c66bbc91SGabor Kovesdan SORT_OPT = CHAR_MAX + 1, 129c66bbc91SGabor Kovesdan HELP_OPT, 130c66bbc91SGabor Kovesdan FF_OPT, 131c66bbc91SGabor Kovesdan BS_OPT, 132c66bbc91SGabor Kovesdan VERSION_OPT, 133c66bbc91SGabor Kovesdan DEBUG_OPT, 134c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 135c66bbc91SGabor Kovesdan NTHREADS_OPT, 136c66bbc91SGabor Kovesdan #endif 137c66bbc91SGabor Kovesdan RANDOMSOURCE_OPT, 138c66bbc91SGabor Kovesdan COMPRESSPROGRAM_OPT, 139c66bbc91SGabor Kovesdan QSORT_OPT, 140c66bbc91SGabor Kovesdan MERGESORT_OPT, 141c66bbc91SGabor Kovesdan HEAPSORT_OPT, 142*5ca724dcSGabor Kovesdan RADIXSORT_OPT, 143*5ca724dcSGabor Kovesdan MMAP_OPT 144c66bbc91SGabor Kovesdan }; 145c66bbc91SGabor Kovesdan 146c66bbc91SGabor Kovesdan #define NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6 147c66bbc91SGabor Kovesdan static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' }; 148c66bbc91SGabor Kovesdan 149c66bbc91SGabor Kovesdan struct option long_options[] = { 150c66bbc91SGabor Kovesdan { "batch-size", required_argument, NULL, BS_OPT }, 151c66bbc91SGabor Kovesdan { "buffer-size", required_argument, NULL, 'S' }, 152c66bbc91SGabor Kovesdan { "check", optional_argument, NULL, 'c' }, 153c66bbc91SGabor Kovesdan { "check=silent|quiet", optional_argument, NULL, 'C' }, 154c66bbc91SGabor Kovesdan { "compress-program", required_argument, NULL, COMPRESSPROGRAM_OPT }, 155c66bbc91SGabor Kovesdan { "debug", no_argument, NULL, DEBUG_OPT }, 156c66bbc91SGabor Kovesdan { "dictionary-order", no_argument, NULL, 'd' }, 157c66bbc91SGabor Kovesdan { "field-separator", required_argument, NULL, 't' }, 158c66bbc91SGabor Kovesdan { "files0-from", required_argument, NULL, FF_OPT }, 159c66bbc91SGabor Kovesdan { "general-numeric-sort", no_argument, NULL, 'g' }, 160c66bbc91SGabor Kovesdan { "heapsort", no_argument, NULL, HEAPSORT_OPT }, 161c66bbc91SGabor Kovesdan { "help",no_argument, NULL, HELP_OPT }, 162c66bbc91SGabor Kovesdan { "human-numeric-sort", no_argument, NULL, 'h' }, 163c66bbc91SGabor Kovesdan { "ignore-leading-blanks", no_argument, NULL, 'b' }, 164c66bbc91SGabor Kovesdan { "ignore-case", no_argument, NULL, 'f' }, 165c66bbc91SGabor Kovesdan { "ignore-nonprinting", no_argument, NULL, 'i' }, 166c66bbc91SGabor Kovesdan { "key", required_argument, NULL, 'k' }, 167c66bbc91SGabor Kovesdan { "merge", no_argument, NULL, 'm' }, 168c66bbc91SGabor Kovesdan { "mergesort", no_argument, NULL, MERGESORT_OPT }, 169*5ca724dcSGabor Kovesdan { "mmap", no_argument, NULL, MMAP_OPT }, 170c66bbc91SGabor Kovesdan { "month-sort", no_argument, NULL, 'M' }, 171c66bbc91SGabor Kovesdan { "numeric-sort", no_argument, NULL, 'n' }, 172c66bbc91SGabor Kovesdan { "output", required_argument, NULL, 'o' }, 173c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 174c66bbc91SGabor Kovesdan { "nthreads", required_argument, NULL, NTHREADS_OPT }, 175c66bbc91SGabor Kovesdan #endif 176c66bbc91SGabor Kovesdan { "qsort", no_argument, NULL, QSORT_OPT }, 177c66bbc91SGabor Kovesdan { "radixsort", no_argument, NULL, RADIXSORT_OPT }, 178c66bbc91SGabor Kovesdan { "random-sort", no_argument, NULL, 'R' }, 179c66bbc91SGabor Kovesdan { "random-source", required_argument, NULL, RANDOMSOURCE_OPT }, 180c66bbc91SGabor Kovesdan { "reverse", no_argument, NULL, 'r' }, 181c66bbc91SGabor Kovesdan { "sort", required_argument, NULL, SORT_OPT }, 182c66bbc91SGabor Kovesdan { "stable", no_argument, NULL, 's' }, 183c66bbc91SGabor Kovesdan { "temporary-directory",required_argument, NULL, 'T' }, 184c66bbc91SGabor Kovesdan { "unique", no_argument, NULL, 'u' }, 185c66bbc91SGabor Kovesdan { "version", no_argument, NULL, VERSION_OPT }, 186c66bbc91SGabor Kovesdan { "version-sort",no_argument, NULL, 'V' }, 187c66bbc91SGabor Kovesdan { "zero-terminated", no_argument, NULL, 'z' }, 188c66bbc91SGabor Kovesdan { NULL, no_argument, NULL, 0 } 189c66bbc91SGabor Kovesdan }; 190c66bbc91SGabor Kovesdan 191c66bbc91SGabor Kovesdan void fix_obsolete_keys(int *argc, char **argv); 192c66bbc91SGabor Kovesdan 193c66bbc91SGabor Kovesdan /* 194c66bbc91SGabor Kovesdan * Check where sort modifier is present 195c66bbc91SGabor Kovesdan */ 196c66bbc91SGabor Kovesdan static bool 197c66bbc91SGabor Kovesdan sort_modifier_empty(struct sort_mods *sm) 198c66bbc91SGabor Kovesdan { 199c66bbc91SGabor Kovesdan 200c66bbc91SGabor Kovesdan if (sm == NULL) 201c66bbc91SGabor Kovesdan return (true); 202c66bbc91SGabor Kovesdan return (!(sm->Mflag || sm->Vflag || sm->nflag || sm->gflag || 203c66bbc91SGabor Kovesdan sm->rflag || sm->Rflag || sm->hflag || sm->dflag || sm->fflag)); 204c66bbc91SGabor Kovesdan } 205c66bbc91SGabor Kovesdan 206c66bbc91SGabor Kovesdan /* 207c66bbc91SGabor Kovesdan * Print out usage text. 208c66bbc91SGabor Kovesdan */ 209c66bbc91SGabor Kovesdan static void 210c66bbc91SGabor Kovesdan usage(bool opt_err) 211c66bbc91SGabor Kovesdan { 212c66bbc91SGabor Kovesdan struct option *o; 213c66bbc91SGabor Kovesdan FILE *out; 214c66bbc91SGabor Kovesdan 215c66bbc91SGabor Kovesdan out = stdout; 216c66bbc91SGabor Kovesdan o = &(long_options[0]); 217c66bbc91SGabor Kovesdan 218c66bbc91SGabor Kovesdan if (opt_err) 219c66bbc91SGabor Kovesdan out = stderr; 220c66bbc91SGabor Kovesdan fprintf(out, getstr(12), getprogname()); 221c66bbc91SGabor Kovesdan if (opt_err) 222c66bbc91SGabor Kovesdan exit(2); 223c66bbc91SGabor Kovesdan exit(0); 224c66bbc91SGabor Kovesdan } 225c66bbc91SGabor Kovesdan 226c66bbc91SGabor Kovesdan /* 227c66bbc91SGabor Kovesdan * Read input file names from a file (file0-from option). 228c66bbc91SGabor Kovesdan */ 229c66bbc91SGabor Kovesdan static void 230c66bbc91SGabor Kovesdan read_fns_from_file0(const char *fn) 231c66bbc91SGabor Kovesdan { 232c66bbc91SGabor Kovesdan if (fn) { 233c66bbc91SGabor Kovesdan struct file0_reader f0r; 234c66bbc91SGabor Kovesdan FILE *f; 235c66bbc91SGabor Kovesdan 236c66bbc91SGabor Kovesdan f = fopen(fn, "r"); 237c66bbc91SGabor Kovesdan if (f == NULL) 238c66bbc91SGabor Kovesdan err(2, NULL); 239c66bbc91SGabor Kovesdan 240c66bbc91SGabor Kovesdan memset(&f0r, 0, sizeof(f0r)); 241c66bbc91SGabor Kovesdan f0r.f = f; 242c66bbc91SGabor Kovesdan 243c66bbc91SGabor Kovesdan while (!feof(f)) { 244c66bbc91SGabor Kovesdan char *line = read_file0_line(&f0r); 245c66bbc91SGabor Kovesdan 246c66bbc91SGabor Kovesdan if (line && *line) { 247c66bbc91SGabor Kovesdan ++argc_from_file0; 248c66bbc91SGabor Kovesdan if (argc_from_file0 < 1) 249c66bbc91SGabor Kovesdan argc_from_file0 = 1; 250c66bbc91SGabor Kovesdan argv_from_file0 = sort_realloc(argv_from_file0, 251c66bbc91SGabor Kovesdan argc_from_file0 * sizeof(char *)); 252c66bbc91SGabor Kovesdan if (argv_from_file0 == NULL) 253c66bbc91SGabor Kovesdan err(2, NULL); 254c66bbc91SGabor Kovesdan argv_from_file0[argc_from_file0 - 1] = 255c66bbc91SGabor Kovesdan sort_strdup(line); 256c66bbc91SGabor Kovesdan } 257c66bbc91SGabor Kovesdan } 258c66bbc91SGabor Kovesdan closefile(f, fn); 259c66bbc91SGabor Kovesdan } 260c66bbc91SGabor Kovesdan } 261c66bbc91SGabor Kovesdan 262c66bbc91SGabor Kovesdan /* 263c66bbc91SGabor Kovesdan * Check how much RAM is available for the sort. 264c66bbc91SGabor Kovesdan */ 265c66bbc91SGabor Kovesdan static void 266c66bbc91SGabor Kovesdan set_hw_params(void) 267c66bbc91SGabor Kovesdan { 268c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 269c66bbc91SGabor Kovesdan size_t ncpusz; 270c66bbc91SGabor Kovesdan #endif 271c66bbc91SGabor Kovesdan size_t pages, psize, psz, pszsz; 272c66bbc91SGabor Kovesdan 273c66bbc91SGabor Kovesdan pages = psize = 0; 274c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 275c66bbc91SGabor Kovesdan ncpu = 1; 276c66bbc91SGabor Kovesdan ncpusz = sizeof(size_t); 277c66bbc91SGabor Kovesdan #endif 278c66bbc91SGabor Kovesdan psz = pszsz = sizeof(size_t); 279c66bbc91SGabor Kovesdan 280c66bbc91SGabor Kovesdan if (sysctlbyname("vm.stats.vm.v_free_count", &pages, &psz, 281c66bbc91SGabor Kovesdan NULL, 0) < 0) { 282c66bbc91SGabor Kovesdan perror("vm.stats.vm.v_free_count"); 283c66bbc91SGabor Kovesdan return; 284c66bbc91SGabor Kovesdan } 285c66bbc91SGabor Kovesdan if (sysctlbyname("vm.stats.vm.v_page_size", &psize, &pszsz, 286c66bbc91SGabor Kovesdan NULL, 0) < 0) { 287c66bbc91SGabor Kovesdan perror("vm.stats.vm.v_page_size"); 288c66bbc91SGabor Kovesdan return; 289c66bbc91SGabor Kovesdan } 290c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 291c66bbc91SGabor Kovesdan if (sysctlbyname("hw.ncpu", &ncpu, &ncpusz, 292c66bbc91SGabor Kovesdan NULL, 0) < 0) 293c66bbc91SGabor Kovesdan ncpu = 1; 294c66bbc91SGabor Kovesdan else if(ncpu > 32) 295c66bbc91SGabor Kovesdan ncpu = 32; 296c66bbc91SGabor Kovesdan 297c66bbc91SGabor Kovesdan nthreads = ncpu; 298c66bbc91SGabor Kovesdan #endif 299c66bbc91SGabor Kovesdan 300c66bbc91SGabor Kovesdan free_memory = (unsigned long long) pages * (unsigned long long) psize; 301c66bbc91SGabor Kovesdan available_free_memory = (free_memory * 9) / 10; 302c66bbc91SGabor Kovesdan } 303c66bbc91SGabor Kovesdan 304c66bbc91SGabor Kovesdan /* 305c66bbc91SGabor Kovesdan * Convert "plain" symbol to wide symbol, with default value. 306c66bbc91SGabor Kovesdan */ 307c66bbc91SGabor Kovesdan static void 308c66bbc91SGabor Kovesdan conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def) 309c66bbc91SGabor Kovesdan { 310c66bbc91SGabor Kovesdan 311c66bbc91SGabor Kovesdan if (wc && c) { 312c66bbc91SGabor Kovesdan int res; 313c66bbc91SGabor Kovesdan 314c66bbc91SGabor Kovesdan res = mbtowc(wc, c, MB_CUR_MAX); 315c66bbc91SGabor Kovesdan if (res < 1) 316c66bbc91SGabor Kovesdan *wc = def; 317c66bbc91SGabor Kovesdan } 318c66bbc91SGabor Kovesdan } 319c66bbc91SGabor Kovesdan 320c66bbc91SGabor Kovesdan /* 321c66bbc91SGabor Kovesdan * Set current locale symbols. 322c66bbc91SGabor Kovesdan */ 323c66bbc91SGabor Kovesdan static void 324c66bbc91SGabor Kovesdan set_locale(void) 325c66bbc91SGabor Kovesdan { 326c66bbc91SGabor Kovesdan struct lconv *lc; 327c66bbc91SGabor Kovesdan const char *locale; 328c66bbc91SGabor Kovesdan 329c66bbc91SGabor Kovesdan setlocale(LC_ALL, ""); 330c66bbc91SGabor Kovesdan 331c66bbc91SGabor Kovesdan lc = localeconv(); 332c66bbc91SGabor Kovesdan 333c66bbc91SGabor Kovesdan if (lc) { 334c66bbc91SGabor Kovesdan /* obtain LC_NUMERIC info */ 335c66bbc91SGabor Kovesdan /* Convert to wide char form */ 336c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_decimal_point, lc->decimal_point, 337c66bbc91SGabor Kovesdan symbol_decimal_point); 338c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep, 339c66bbc91SGabor Kovesdan symbol_thousands_sep); 340c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_positive_sign, lc->positive_sign, 341c66bbc91SGabor Kovesdan symbol_positive_sign); 342c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_negative_sign, lc->negative_sign, 343c66bbc91SGabor Kovesdan symbol_negative_sign); 344c66bbc91SGabor Kovesdan } 345c66bbc91SGabor Kovesdan 346c66bbc91SGabor Kovesdan if (getenv("GNUSORT_NUMERIC_COMPATIBILITY")) 347c66bbc91SGabor Kovesdan gnusort_numeric_compatibility = true; 348c66bbc91SGabor Kovesdan 349c66bbc91SGabor Kovesdan locale = setlocale(LC_COLLATE, NULL); 350c66bbc91SGabor Kovesdan 351c66bbc91SGabor Kovesdan if (locale) { 352c66bbc91SGabor Kovesdan char *tmpl; 353c66bbc91SGabor Kovesdan const char *cclocale; 354c66bbc91SGabor Kovesdan 355c66bbc91SGabor Kovesdan tmpl = sort_strdup(locale); 356c66bbc91SGabor Kovesdan cclocale = setlocale(LC_COLLATE, "C"); 357c66bbc91SGabor Kovesdan if (cclocale && !strcmp(cclocale, tmpl)) 358c66bbc91SGabor Kovesdan byte_sort = true; 359c66bbc91SGabor Kovesdan else { 360c66bbc91SGabor Kovesdan const char *pclocale; 361c66bbc91SGabor Kovesdan 362c66bbc91SGabor Kovesdan pclocale = setlocale(LC_COLLATE, "POSIX"); 363c66bbc91SGabor Kovesdan if (pclocale && !strcmp(pclocale, tmpl)) 364c66bbc91SGabor Kovesdan byte_sort = true; 365c66bbc91SGabor Kovesdan } 366c66bbc91SGabor Kovesdan setlocale(LC_COLLATE, tmpl); 367c66bbc91SGabor Kovesdan sort_free(tmpl); 368c66bbc91SGabor Kovesdan } 369c66bbc91SGabor Kovesdan } 370c66bbc91SGabor Kovesdan 371c66bbc91SGabor Kovesdan /* 372c66bbc91SGabor Kovesdan * Set directory temporary files. 373c66bbc91SGabor Kovesdan */ 374c66bbc91SGabor Kovesdan static void 375c66bbc91SGabor Kovesdan set_tmpdir(void) 376c66bbc91SGabor Kovesdan { 377c66bbc91SGabor Kovesdan char *td; 378c66bbc91SGabor Kovesdan 379c66bbc91SGabor Kovesdan td = getenv("TMPDIR"); 380c66bbc91SGabor Kovesdan if (td != NULL) 381c66bbc91SGabor Kovesdan tmpdir = sort_strdup(td); 382c66bbc91SGabor Kovesdan } 383c66bbc91SGabor Kovesdan 384c66bbc91SGabor Kovesdan /* 385c66bbc91SGabor Kovesdan * Parse -S option. 386c66bbc91SGabor Kovesdan */ 387c66bbc91SGabor Kovesdan static unsigned long long 388c66bbc91SGabor Kovesdan parse_memory_buffer_value(const char *value) 389c66bbc91SGabor Kovesdan { 390c66bbc91SGabor Kovesdan 391c66bbc91SGabor Kovesdan if (value == NULL) 392c66bbc91SGabor Kovesdan return (available_free_memory); 393c66bbc91SGabor Kovesdan else { 394c66bbc91SGabor Kovesdan char *endptr; 395c66bbc91SGabor Kovesdan unsigned long long membuf; 396c66bbc91SGabor Kovesdan 397c66bbc91SGabor Kovesdan endptr = NULL; 398c66bbc91SGabor Kovesdan errno = 0; 399c66bbc91SGabor Kovesdan membuf = strtoll(value, &endptr, 10); 400c66bbc91SGabor Kovesdan 401c66bbc91SGabor Kovesdan if (errno != 0) { 4028818aa39SGabor Kovesdan warn("%s",getstr(4)); 403c66bbc91SGabor Kovesdan membuf = available_free_memory; 404c66bbc91SGabor Kovesdan } else { 405c66bbc91SGabor Kovesdan switch (*endptr){ 406c66bbc91SGabor Kovesdan case 'Y': 407c66bbc91SGabor Kovesdan membuf *= 1024; 408c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 409c66bbc91SGabor Kovesdan case 'Z': 410c66bbc91SGabor Kovesdan membuf *= 1024; 411c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 412c66bbc91SGabor Kovesdan case 'E': 413c66bbc91SGabor Kovesdan membuf *= 1024; 414c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 415c66bbc91SGabor Kovesdan case 'P': 416c66bbc91SGabor Kovesdan membuf *= 1024; 417c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 418c66bbc91SGabor Kovesdan case 'T': 419c66bbc91SGabor Kovesdan membuf *= 1024; 420c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 421c66bbc91SGabor Kovesdan case 'G': 422c66bbc91SGabor Kovesdan membuf *= 1024; 423c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 424c66bbc91SGabor Kovesdan case 'M': 425c66bbc91SGabor Kovesdan membuf *= 1024; 426c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 427c66bbc91SGabor Kovesdan case '\0': 428c66bbc91SGabor Kovesdan case 'K': 429c66bbc91SGabor Kovesdan membuf *= 1024; 430c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 431c66bbc91SGabor Kovesdan case 'b': 432c66bbc91SGabor Kovesdan break; 433c66bbc91SGabor Kovesdan case '%': 434c66bbc91SGabor Kovesdan membuf = (available_free_memory * membuf) / 435c66bbc91SGabor Kovesdan 100; 436c66bbc91SGabor Kovesdan break; 437c66bbc91SGabor Kovesdan default: 438c66bbc91SGabor Kovesdan fprintf(stderr, "%s: %s\n", strerror(EINVAL), 439c66bbc91SGabor Kovesdan optarg); 440c66bbc91SGabor Kovesdan membuf = available_free_memory; 441c66bbc91SGabor Kovesdan } 442c66bbc91SGabor Kovesdan } 443c66bbc91SGabor Kovesdan return (membuf); 444c66bbc91SGabor Kovesdan } 445c66bbc91SGabor Kovesdan } 446c66bbc91SGabor Kovesdan 447c66bbc91SGabor Kovesdan /* 448c66bbc91SGabor Kovesdan * Signal handler that clears the temporary files. 449c66bbc91SGabor Kovesdan */ 450c66bbc91SGabor Kovesdan static void 4518818aa39SGabor Kovesdan sig_handler(int sig __unused, siginfo_t *siginfo __unused, 4528818aa39SGabor Kovesdan void *context __unused) 453c66bbc91SGabor Kovesdan { 454c66bbc91SGabor Kovesdan 455c66bbc91SGabor Kovesdan clear_tmp_files(); 456c66bbc91SGabor Kovesdan exit(-1); 457c66bbc91SGabor Kovesdan } 458c66bbc91SGabor Kovesdan 459c66bbc91SGabor Kovesdan /* 460c66bbc91SGabor Kovesdan * Set signal handler on panic signals. 461c66bbc91SGabor Kovesdan */ 462c66bbc91SGabor Kovesdan static void 463c66bbc91SGabor Kovesdan set_signal_handler(void) 464c66bbc91SGabor Kovesdan { 465c66bbc91SGabor Kovesdan struct sigaction sa; 466c66bbc91SGabor Kovesdan 467c66bbc91SGabor Kovesdan memset(&sa, 0, sizeof(sa)); 468c66bbc91SGabor Kovesdan sa.sa_sigaction = &sig_handler; 469c66bbc91SGabor Kovesdan sa.sa_flags = SA_SIGINFO; 470c66bbc91SGabor Kovesdan 471c66bbc91SGabor Kovesdan if (sigaction(SIGTERM, &sa, NULL) < 0) { 472c66bbc91SGabor Kovesdan perror("sigaction"); 473c66bbc91SGabor Kovesdan return; 474c66bbc91SGabor Kovesdan } 475c66bbc91SGabor Kovesdan if (sigaction(SIGHUP, &sa, NULL) < 0) { 476c66bbc91SGabor Kovesdan perror("sigaction"); 477c66bbc91SGabor Kovesdan return; 478c66bbc91SGabor Kovesdan } 479c66bbc91SGabor Kovesdan if (sigaction(SIGINT, &sa, NULL) < 0) { 480c66bbc91SGabor Kovesdan perror("sigaction"); 481c66bbc91SGabor Kovesdan return; 482c66bbc91SGabor Kovesdan } 483c66bbc91SGabor Kovesdan if (sigaction(SIGQUIT, &sa, NULL) < 0) { 484c66bbc91SGabor Kovesdan perror("sigaction"); 485c66bbc91SGabor Kovesdan return; 486c66bbc91SGabor Kovesdan } 487c66bbc91SGabor Kovesdan if (sigaction(SIGABRT, &sa, NULL) < 0) { 488c66bbc91SGabor Kovesdan perror("sigaction"); 489c66bbc91SGabor Kovesdan return; 490c66bbc91SGabor Kovesdan } 491c66bbc91SGabor Kovesdan if (sigaction(SIGBUS, &sa, NULL) < 0) { 492c66bbc91SGabor Kovesdan perror("sigaction"); 493c66bbc91SGabor Kovesdan return; 494c66bbc91SGabor Kovesdan } 495c66bbc91SGabor Kovesdan if (sigaction(SIGSEGV, &sa, NULL) < 0) { 496c66bbc91SGabor Kovesdan perror("sigaction"); 497c66bbc91SGabor Kovesdan return; 498c66bbc91SGabor Kovesdan } 499c66bbc91SGabor Kovesdan if (sigaction(SIGUSR1, &sa, NULL) < 0) { 500c66bbc91SGabor Kovesdan perror("sigaction"); 501c66bbc91SGabor Kovesdan return; 502c66bbc91SGabor Kovesdan } 503c66bbc91SGabor Kovesdan if (sigaction(SIGUSR2, &sa, NULL) < 0) { 504c66bbc91SGabor Kovesdan perror("sigaction"); 505c66bbc91SGabor Kovesdan return; 506c66bbc91SGabor Kovesdan } 507c66bbc91SGabor Kovesdan } 508c66bbc91SGabor Kovesdan 509c66bbc91SGabor Kovesdan /* 510c66bbc91SGabor Kovesdan * Print "unknown" message and exit with status 2. 511c66bbc91SGabor Kovesdan */ 512c66bbc91SGabor Kovesdan static void 513c66bbc91SGabor Kovesdan unknown(const char *what) 514c66bbc91SGabor Kovesdan { 515c66bbc91SGabor Kovesdan 5168818aa39SGabor Kovesdan errx(2, "%s: %s", getstr(3), what); 517c66bbc91SGabor Kovesdan } 518c66bbc91SGabor Kovesdan 519c66bbc91SGabor Kovesdan /* 520c66bbc91SGabor Kovesdan * Check whether contradictory input options are used. 521c66bbc91SGabor Kovesdan */ 522c66bbc91SGabor Kovesdan static void 523c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(char c, bool *mef_flags) 524c66bbc91SGabor Kovesdan { 525c66bbc91SGabor Kovesdan int fo_index, mec; 526c66bbc91SGabor Kovesdan bool found_others, found_this; 527c66bbc91SGabor Kovesdan 528c66bbc91SGabor Kovesdan found_others = found_this =false; 529c66bbc91SGabor Kovesdan fo_index = 0; 530c66bbc91SGabor Kovesdan 531c66bbc91SGabor Kovesdan for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) { 532c66bbc91SGabor Kovesdan mec = mutually_exclusive_flags[i]; 533c66bbc91SGabor Kovesdan 534c66bbc91SGabor Kovesdan if (mec != c) { 535c66bbc91SGabor Kovesdan if (mef_flags[i]) { 536c66bbc91SGabor Kovesdan if (found_this) 5378818aa39SGabor Kovesdan errx(1, "%c:%c: %s", c, mec, getstr(1)); 538c66bbc91SGabor Kovesdan found_others = true; 539c66bbc91SGabor Kovesdan fo_index = i; 540c66bbc91SGabor Kovesdan } 541c66bbc91SGabor Kovesdan } else { 542c66bbc91SGabor Kovesdan if (found_others) 5438818aa39SGabor Kovesdan errx(1, "%c:%c: %s", c, mutually_exclusive_flags[fo_index], getstr(1)); 544c66bbc91SGabor Kovesdan mef_flags[i] = true; 545c66bbc91SGabor Kovesdan found_this = true; 546c66bbc91SGabor Kovesdan } 547c66bbc91SGabor Kovesdan } 548c66bbc91SGabor Kovesdan } 549c66bbc91SGabor Kovesdan 550c66bbc91SGabor Kovesdan /* 551c66bbc91SGabor Kovesdan * Initialise sort opts data. 552c66bbc91SGabor Kovesdan */ 553c66bbc91SGabor Kovesdan static void 554c66bbc91SGabor Kovesdan set_sort_opts(void) 555c66bbc91SGabor Kovesdan { 556c66bbc91SGabor Kovesdan 557c66bbc91SGabor Kovesdan memset(&default_sort_mods_object, 0, 558c66bbc91SGabor Kovesdan sizeof(default_sort_mods_object)); 559c66bbc91SGabor Kovesdan memset(&sort_opts_vals, 0, sizeof(sort_opts_vals)); 560c66bbc91SGabor Kovesdan default_sort_mods_object.func = 561c66bbc91SGabor Kovesdan get_sort_func(&default_sort_mods_object); 562c66bbc91SGabor Kovesdan } 563c66bbc91SGabor Kovesdan 564c66bbc91SGabor Kovesdan /* 565c66bbc91SGabor Kovesdan * Set a sort modifier on a sort modifiers object. 566c66bbc91SGabor Kovesdan */ 567c66bbc91SGabor Kovesdan static bool 568c66bbc91SGabor Kovesdan set_sort_modifier(struct sort_mods *sm, int c) 569c66bbc91SGabor Kovesdan { 570c66bbc91SGabor Kovesdan 571c66bbc91SGabor Kovesdan if (sm) { 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: 615c66bbc91SGabor Kovesdan return false; 616c66bbc91SGabor Kovesdan } 617c66bbc91SGabor Kovesdan sort_opts_vals.complex_sort = true; 618c66bbc91SGabor Kovesdan sm->func = get_sort_func(sm); 619c66bbc91SGabor Kovesdan } 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) 662c66bbc91SGabor Kovesdan errx(2, "%s: -k", strerror(errno)); 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) 671c66bbc91SGabor Kovesdan errx(2, "%s: -k", strerror(errno)); 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) 689c66bbc91SGabor Kovesdan errx(2, "%s: -k", strerror(errno)); 690c66bbc91SGabor Kovesdan } else { 691c66bbc91SGabor Kovesdan errno = 0; 692c66bbc91SGabor Kovesdan ks->c1 = (size_t) strtoul(c, NULL, 10); 693c66bbc91SGabor Kovesdan if (errno != 0) 694c66bbc91SGabor Kovesdan errx(2, "%s: -k", strerror(errno)); 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 } 902c66bbc91SGabor Kovesdan sprintf(sopt, "-k%d.%d", f1, c1); 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 (need_random) { 916c66bbc91SGabor Kovesdan 917c66bbc91SGabor Kovesdan if (strcmp(random_source, DEFAULT_RANDOM_SORT_SEED_FILE) == 0) { 918c66bbc91SGabor Kovesdan FILE* fseed; 919c66bbc91SGabor Kovesdan MD5_CTX ctx; 920c66bbc91SGabor Kovesdan char rsd[MAX_DEFAULT_RANDOM_SEED_DATA_SIZE]; 921c66bbc91SGabor Kovesdan size_t sz = 0; 922c66bbc91SGabor Kovesdan 923c66bbc91SGabor Kovesdan fseed = openfile(random_source, "r"); 924c66bbc91SGabor Kovesdan while (!feof(fseed)) { 925c66bbc91SGabor Kovesdan int cr; 926c66bbc91SGabor Kovesdan 927c66bbc91SGabor Kovesdan cr = fgetc(fseed); 928c66bbc91SGabor Kovesdan if (cr == EOF) 929c66bbc91SGabor Kovesdan break; 930c66bbc91SGabor Kovesdan 931c66bbc91SGabor Kovesdan rsd[sz++] = (char) cr; 932c66bbc91SGabor Kovesdan 933c66bbc91SGabor Kovesdan if (sz >= MAX_DEFAULT_RANDOM_SEED_DATA_SIZE) 934c66bbc91SGabor Kovesdan break; 935c66bbc91SGabor Kovesdan } 936c66bbc91SGabor Kovesdan 937c66bbc91SGabor Kovesdan closefile(fseed, random_source); 938c66bbc91SGabor Kovesdan 939c66bbc91SGabor Kovesdan MD5Init(&ctx); 940c66bbc91SGabor Kovesdan MD5Update(&ctx, rsd, sz); 941c66bbc91SGabor Kovesdan 942c66bbc91SGabor Kovesdan random_seed = MD5End(&ctx, NULL); 943c66bbc91SGabor Kovesdan random_seed_size = strlen(random_seed); 944c66bbc91SGabor Kovesdan 945c66bbc91SGabor Kovesdan } else { 946c66bbc91SGabor Kovesdan MD5_CTX ctx; 947c66bbc91SGabor Kovesdan char *b; 948c66bbc91SGabor Kovesdan 949c66bbc91SGabor Kovesdan MD5Init(&ctx); 950c66bbc91SGabor Kovesdan b = MD5File(random_source, NULL); 951c66bbc91SGabor Kovesdan if (b == NULL) 952c66bbc91SGabor Kovesdan err(2, NULL); 953c66bbc91SGabor Kovesdan 954c66bbc91SGabor Kovesdan random_seed = b; 955c66bbc91SGabor Kovesdan random_seed_size = strlen(b); 956c66bbc91SGabor Kovesdan } 957c66bbc91SGabor Kovesdan 958c66bbc91SGabor Kovesdan MD5Init(&md5_ctx); 959c66bbc91SGabor Kovesdan if(random_seed_size>0) { 960c66bbc91SGabor Kovesdan MD5Update(&md5_ctx, random_seed, random_seed_size); 961c66bbc91SGabor Kovesdan } 962c66bbc91SGabor Kovesdan } 963c66bbc91SGabor Kovesdan } 964c66bbc91SGabor Kovesdan 965c66bbc91SGabor Kovesdan /* 966c66bbc91SGabor Kovesdan * Main function. 967c66bbc91SGabor Kovesdan */ 968c66bbc91SGabor Kovesdan int 969c66bbc91SGabor Kovesdan main(int argc, char **argv) 970c66bbc91SGabor Kovesdan { 971c66bbc91SGabor Kovesdan char *outfile, *real_outfile; 972c66bbc91SGabor Kovesdan int c, result; 973c66bbc91SGabor Kovesdan bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = 974c66bbc91SGabor Kovesdan { false, false, false, false, false, false }; 975c66bbc91SGabor Kovesdan 976c66bbc91SGabor Kovesdan result = 0; 977c66bbc91SGabor Kovesdan outfile = sort_strdup("-"); 978c66bbc91SGabor Kovesdan real_outfile = NULL; 979c66bbc91SGabor Kovesdan 980c66bbc91SGabor Kovesdan struct sort_mods *sm = &default_sort_mods_object; 981c66bbc91SGabor Kovesdan 982c66bbc91SGabor Kovesdan init_tmp_files(); 983c66bbc91SGabor Kovesdan 984c66bbc91SGabor Kovesdan set_signal_handler(); 985c66bbc91SGabor Kovesdan 986c66bbc91SGabor Kovesdan set_hw_params(); 987c66bbc91SGabor Kovesdan set_locale(); 988c66bbc91SGabor Kovesdan set_tmpdir(); 989c66bbc91SGabor Kovesdan set_sort_opts(); 990c66bbc91SGabor Kovesdan 991c66bbc91SGabor Kovesdan #if 0 992c66bbc91SGabor Kovesdan { 993c66bbc91SGabor Kovesdan static int counter = 0; 994c66bbc91SGabor Kovesdan char fn[128]; 995c66bbc91SGabor Kovesdan sprintf(fn, "/var/tmp/debug.sort.%d", counter++); 996c66bbc91SGabor Kovesdan FILE* f = fopen(fn, "w"); 997c66bbc91SGabor Kovesdan fprintf(f, ">>sort>>"); 998c66bbc91SGabor Kovesdan for (int i = 0; i < argc; i++) { 999c66bbc91SGabor Kovesdan fprintf(f, "<%s>", argv[i]); 1000c66bbc91SGabor Kovesdan } 1001c66bbc91SGabor Kovesdan fprintf(f, "<<sort<<\n"); 1002c66bbc91SGabor Kovesdan fclose(f); 1003c66bbc91SGabor Kovesdan } 1004c66bbc91SGabor Kovesdan #endif 1005c66bbc91SGabor Kovesdan 1006c66bbc91SGabor Kovesdan fix_obsolete_keys(&argc, argv); 1007c66bbc91SGabor Kovesdan 1008c66bbc91SGabor Kovesdan while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL)) 1009c66bbc91SGabor Kovesdan != -1)) { 1010c66bbc91SGabor Kovesdan 1011c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(c, mef_flags); 1012c66bbc91SGabor Kovesdan 1013c66bbc91SGabor Kovesdan if (!set_sort_modifier(sm, c)) { 1014c66bbc91SGabor Kovesdan 1015c66bbc91SGabor Kovesdan switch (c) { 1016c66bbc91SGabor Kovesdan case 'c': 1017c66bbc91SGabor Kovesdan sort_opts_vals.cflag = true; 1018c66bbc91SGabor Kovesdan if (optarg) { 1019c66bbc91SGabor Kovesdan if (!strcmp(optarg, "diagnose-first")) 1020c66bbc91SGabor Kovesdan ; 1021c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "silent") || 1022c66bbc91SGabor Kovesdan !strcmp(optarg, "quiet")) 1023c66bbc91SGabor Kovesdan sort_opts_vals.csilentflag = true; 1024c66bbc91SGabor Kovesdan else if (*optarg) 1025c66bbc91SGabor Kovesdan unknown(optarg); 1026c66bbc91SGabor Kovesdan } 1027c66bbc91SGabor Kovesdan break; 1028c66bbc91SGabor Kovesdan case 'C': 1029c66bbc91SGabor Kovesdan sort_opts_vals.cflag = true; 1030c66bbc91SGabor Kovesdan sort_opts_vals.csilentflag = true; 1031c66bbc91SGabor Kovesdan break; 1032c66bbc91SGabor Kovesdan case 'k': 1033c66bbc91SGabor Kovesdan { 1034c66bbc91SGabor Kovesdan sort_opts_vals.complex_sort = true; 1035c66bbc91SGabor Kovesdan sort_opts_vals.kflag = true; 1036c66bbc91SGabor Kovesdan 1037c66bbc91SGabor Kovesdan keys_num++; 1038c66bbc91SGabor Kovesdan keys = sort_realloc(keys, keys_num * 1039c66bbc91SGabor Kovesdan sizeof(struct key_specs)); 1040c66bbc91SGabor Kovesdan memset(&(keys[keys_num - 1]), 0, 1041c66bbc91SGabor Kovesdan sizeof(struct key_specs)); 1042c66bbc91SGabor Kovesdan 1043c66bbc91SGabor Kovesdan if (parse_k(optarg, &(keys[keys_num - 1])) 1044c66bbc91SGabor Kovesdan < 0) { 1045c66bbc91SGabor Kovesdan errx(2, "%s: -k %s\n", 1046c66bbc91SGabor Kovesdan strerror(EINVAL), optarg); 1047c66bbc91SGabor Kovesdan } 1048c66bbc91SGabor Kovesdan 1049c66bbc91SGabor Kovesdan break; 1050c66bbc91SGabor Kovesdan } 1051c66bbc91SGabor Kovesdan case 'm': 1052c66bbc91SGabor Kovesdan sort_opts_vals.mflag = true; 1053c66bbc91SGabor Kovesdan break; 1054c66bbc91SGabor Kovesdan case 'o': 1055f50d9b2fSGabor Kovesdan outfile = sort_realloc(outfile, (strlen(optarg) + 1)); 1056f50d9b2fSGabor Kovesdan strcpy(outfile, optarg); 1057c66bbc91SGabor Kovesdan break; 1058c66bbc91SGabor Kovesdan case 's': 1059c66bbc91SGabor Kovesdan sort_opts_vals.sflag = true; 1060c66bbc91SGabor Kovesdan break; 1061c66bbc91SGabor Kovesdan case 'S': 1062c66bbc91SGabor Kovesdan available_free_memory = 1063c66bbc91SGabor Kovesdan parse_memory_buffer_value(optarg); 1064c66bbc91SGabor Kovesdan break; 1065c66bbc91SGabor Kovesdan case 'T': 1066c66bbc91SGabor Kovesdan tmpdir = sort_strdup(optarg); 1067c66bbc91SGabor Kovesdan break; 1068c66bbc91SGabor Kovesdan case 't': 1069*5ca724dcSGabor Kovesdan while (strlen(optarg) > 1) { 1070*5ca724dcSGabor Kovesdan if (optarg[0] != '\\') { 1071c66bbc91SGabor Kovesdan errx(2, "%s: %s\n", 1072c66bbc91SGabor Kovesdan strerror(EINVAL), optarg); 1073c66bbc91SGabor Kovesdan } 1074*5ca724dcSGabor Kovesdan optarg += 1; 1075*5ca724dcSGabor Kovesdan if (*optarg == '0') { 1076c66bbc91SGabor Kovesdan *optarg = 0; 1077*5ca724dcSGabor Kovesdan break; 1078*5ca724dcSGabor Kovesdan } 1079c66bbc91SGabor Kovesdan } 1080c66bbc91SGabor Kovesdan sort_opts_vals.tflag = true; 1081c66bbc91SGabor Kovesdan sort_opts_vals.field_sep = btowc(optarg[0]); 1082c66bbc91SGabor Kovesdan if (sort_opts_vals.field_sep == WEOF) { 1083c66bbc91SGabor Kovesdan errno = EINVAL; 1084c66bbc91SGabor Kovesdan err(2, NULL); 1085c66bbc91SGabor Kovesdan } 1086c66bbc91SGabor Kovesdan if (!gnusort_numeric_compatibility) { 1087c66bbc91SGabor Kovesdan if (symbol_decimal_point == sort_opts_vals.field_sep) 1088c66bbc91SGabor Kovesdan symbol_decimal_point = WEOF; 1089c66bbc91SGabor Kovesdan if (symbol_thousands_sep == sort_opts_vals.field_sep) 1090c66bbc91SGabor Kovesdan symbol_thousands_sep = WEOF; 1091c66bbc91SGabor Kovesdan if (symbol_negative_sign == sort_opts_vals.field_sep) 1092c66bbc91SGabor Kovesdan symbol_negative_sign = WEOF; 1093c66bbc91SGabor Kovesdan if (symbol_positive_sign == sort_opts_vals.field_sep) 1094c66bbc91SGabor Kovesdan symbol_positive_sign = WEOF; 1095c66bbc91SGabor Kovesdan } 1096c66bbc91SGabor Kovesdan break; 1097c66bbc91SGabor Kovesdan case 'u': 1098c66bbc91SGabor Kovesdan sort_opts_vals.uflag = true; 1099c66bbc91SGabor Kovesdan /* stable sort for the correct unique val */ 1100c66bbc91SGabor Kovesdan sort_opts_vals.sflag = true; 1101c66bbc91SGabor Kovesdan break; 1102c66bbc91SGabor Kovesdan case 'z': 1103c66bbc91SGabor Kovesdan sort_opts_vals.zflag = true; 1104c66bbc91SGabor Kovesdan break; 1105c66bbc91SGabor Kovesdan case SORT_OPT: 1106c66bbc91SGabor Kovesdan if (optarg) { 1107c66bbc91SGabor Kovesdan if (!strcmp(optarg, "general-numeric")) 1108c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'g'); 1109c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "human-numeric")) 1110c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'h'); 1111c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "numeric")) 1112c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'n'); 1113c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "month")) 1114c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'M'); 1115c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "random")) 1116c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'R'); 1117c66bbc91SGabor Kovesdan else 1118c66bbc91SGabor Kovesdan unknown(optarg); 1119c66bbc91SGabor Kovesdan } 1120c66bbc91SGabor Kovesdan break; 1121c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 1122c66bbc91SGabor Kovesdan case NTHREADS_OPT: 1123c66bbc91SGabor Kovesdan nthreads = (size_t)(atoi(optarg)); 1124c66bbc91SGabor Kovesdan if (nthreads < 1) 1125c66bbc91SGabor Kovesdan nthreads = 1; 1126c66bbc91SGabor Kovesdan if (nthreads > 1024) 1127c66bbc91SGabor Kovesdan nthreads = 1024; 1128c66bbc91SGabor Kovesdan break; 1129c66bbc91SGabor Kovesdan #endif 1130c66bbc91SGabor Kovesdan case QSORT_OPT: 1131c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_QSORT; 1132c66bbc91SGabor Kovesdan break; 1133c66bbc91SGabor Kovesdan case MERGESORT_OPT: 1134c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_MERGESORT; 1135c66bbc91SGabor Kovesdan break; 1136*5ca724dcSGabor Kovesdan case MMAP_OPT: 1137*5ca724dcSGabor Kovesdan use_mmap = true; 1138*5ca724dcSGabor Kovesdan break; 1139c66bbc91SGabor Kovesdan case HEAPSORT_OPT: 1140c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_HEAPSORT; 1141c66bbc91SGabor Kovesdan break; 1142c66bbc91SGabor Kovesdan case RADIXSORT_OPT: 1143c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_RADIXSORT; 1144c66bbc91SGabor Kovesdan break; 1145c66bbc91SGabor Kovesdan case RANDOMSOURCE_OPT: 1146c66bbc91SGabor Kovesdan random_source = strdup(optarg); 1147c66bbc91SGabor Kovesdan break; 1148c66bbc91SGabor Kovesdan case COMPRESSPROGRAM_OPT: 1149c66bbc91SGabor Kovesdan compress_program = strdup(optarg); 1150c66bbc91SGabor Kovesdan break; 1151c66bbc91SGabor Kovesdan case FF_OPT: 1152c66bbc91SGabor Kovesdan read_fns_from_file0(optarg); 1153c66bbc91SGabor Kovesdan break; 1154c66bbc91SGabor Kovesdan case BS_OPT: 1155c66bbc91SGabor Kovesdan { 1156c66bbc91SGabor Kovesdan errno = 0; 1157c66bbc91SGabor Kovesdan long mof = strtol(optarg, NULL, 10); 1158c66bbc91SGabor Kovesdan if (errno != 0) 1159c66bbc91SGabor Kovesdan errx(2, "--batch-size: %s", 1160c66bbc91SGabor Kovesdan strerror(errno)); 1161c66bbc91SGabor Kovesdan if (mof >= 2) 1162c66bbc91SGabor Kovesdan max_open_files = (size_t) mof + 1; 1163c66bbc91SGabor Kovesdan } 1164c66bbc91SGabor Kovesdan break; 1165c66bbc91SGabor Kovesdan case VERSION_OPT: 1166c66bbc91SGabor Kovesdan printf("%s\n", VERSION); 1167c66bbc91SGabor Kovesdan exit(EXIT_SUCCESS); 1168c66bbc91SGabor Kovesdan /* NOTREACHED */ 1169c66bbc91SGabor Kovesdan break; 1170c66bbc91SGabor Kovesdan case DEBUG_OPT: 1171c66bbc91SGabor Kovesdan debug_sort = true; 1172c66bbc91SGabor Kovesdan break; 1173c66bbc91SGabor Kovesdan case HELP_OPT: 1174c66bbc91SGabor Kovesdan usage(false); 1175c66bbc91SGabor Kovesdan /* NOTREACHED */ 1176c66bbc91SGabor Kovesdan break; 1177c66bbc91SGabor Kovesdan default: 1178c66bbc91SGabor Kovesdan usage(true); 1179c66bbc91SGabor Kovesdan /* NOTREACHED */ 1180c66bbc91SGabor Kovesdan } 1181c66bbc91SGabor Kovesdan } 1182c66bbc91SGabor Kovesdan } 1183c66bbc91SGabor Kovesdan 1184c66bbc91SGabor Kovesdan argc -= optind; 1185c66bbc91SGabor Kovesdan argv += optind; 1186c66bbc91SGabor Kovesdan 1187c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 1188c66bbc91SGabor Kovesdan catalog = catopen("sort", NL_CAT_LOCALE); 1189c66bbc91SGabor Kovesdan #endif 1190c66bbc91SGabor Kovesdan 1191c66bbc91SGabor Kovesdan if (sort_opts_vals.cflag && sort_opts_vals.mflag) 11928818aa39SGabor Kovesdan errx(1, "%c:%c: %s", 'm', 'c', getstr(1)); 1193c66bbc91SGabor Kovesdan 1194c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 1195c66bbc91SGabor Kovesdan catclose(catalog); 1196c66bbc91SGabor Kovesdan #endif 1197c66bbc91SGabor Kovesdan 1198c66bbc91SGabor Kovesdan if (keys_num == 0) { 1199c66bbc91SGabor Kovesdan keys_num = 1; 1200c66bbc91SGabor Kovesdan keys = sort_realloc(keys, sizeof(struct key_specs)); 1201c66bbc91SGabor Kovesdan memset(&(keys[0]), 0, sizeof(struct key_specs)); 1202c66bbc91SGabor Kovesdan keys[0].c1 = 1; 1203c66bbc91SGabor Kovesdan keys[0].pos1b = default_sort_mods->bflag; 1204c66bbc91SGabor Kovesdan keys[0].pos2b = default_sort_mods->bflag; 1205c66bbc91SGabor Kovesdan memcpy(&(keys[0].sm), default_sort_mods, 1206c66bbc91SGabor Kovesdan sizeof(struct sort_mods)); 1207c66bbc91SGabor Kovesdan } 1208c66bbc91SGabor Kovesdan 1209c66bbc91SGabor Kovesdan for (size_t i = 0; i < keys_num; i++) { 1210c66bbc91SGabor Kovesdan struct key_specs *ks; 1211c66bbc91SGabor Kovesdan 1212c66bbc91SGabor Kovesdan ks = &(keys[i]); 1213c66bbc91SGabor Kovesdan 1214c66bbc91SGabor Kovesdan if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) && 1215c66bbc91SGabor Kovesdan !(ks->pos2b)) { 1216c66bbc91SGabor Kovesdan ks->pos1b = sm->bflag; 1217c66bbc91SGabor Kovesdan ks->pos2b = sm->bflag; 1218c66bbc91SGabor Kovesdan memcpy(&(ks->sm), sm, sizeof(struct sort_mods)); 1219c66bbc91SGabor Kovesdan } 1220c66bbc91SGabor Kovesdan 1221c66bbc91SGabor Kovesdan ks->sm.func = get_sort_func(&(ks->sm)); 1222c66bbc91SGabor Kovesdan } 1223c66bbc91SGabor Kovesdan 1224c66bbc91SGabor Kovesdan if (argc_from_file0 >= 0) { 1225c66bbc91SGabor Kovesdan argc = argc_from_file0; 1226c66bbc91SGabor Kovesdan argv = argv_from_file0; 1227c66bbc91SGabor Kovesdan } 1228c66bbc91SGabor Kovesdan 1229c66bbc91SGabor Kovesdan if (debug_sort) { 1230c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 1231c66bbc91SGabor Kovesdan nthreads = 1; 1232c66bbc91SGabor Kovesdan #endif 1233c66bbc91SGabor Kovesdan printf("Using collate rules of %s locale\n", 1234c66bbc91SGabor Kovesdan setlocale(LC_COLLATE, NULL)); 1235c66bbc91SGabor Kovesdan if (byte_sort) 1236c66bbc91SGabor Kovesdan printf("Byte sort is used\n"); 1237c66bbc91SGabor Kovesdan if (print_symbols_on_debug) { 1238c66bbc91SGabor Kovesdan printf("Decimal Point: <%lc>\n", symbol_decimal_point); 1239c66bbc91SGabor Kovesdan if (symbol_thousands_sep) 1240c66bbc91SGabor Kovesdan printf("Thousands separator: <%lc>\n", 1241c66bbc91SGabor Kovesdan symbol_thousands_sep); 1242c66bbc91SGabor Kovesdan printf("Positive sign: <%lc>\n", symbol_positive_sign); 1243c66bbc91SGabor Kovesdan printf("Negative sign: <%lc>\n", symbol_negative_sign); 1244c66bbc91SGabor Kovesdan } 1245c66bbc91SGabor Kovesdan } 1246c66bbc91SGabor Kovesdan 1247c66bbc91SGabor Kovesdan set_random_seed(); 1248c66bbc91SGabor Kovesdan 1249c66bbc91SGabor Kovesdan /* Case when the outfile equals one of the input files: */ 1250c66bbc91SGabor Kovesdan if (strcmp(outfile, "-")) { 1251c66bbc91SGabor Kovesdan 1252c66bbc91SGabor Kovesdan for(int i = 0; i < argc; ++i) { 1253c66bbc91SGabor Kovesdan if (strcmp(argv[i], outfile) == 0) { 1254c66bbc91SGabor Kovesdan real_outfile = sort_strdup(outfile); 1255c66bbc91SGabor Kovesdan for(;;) { 1256c66bbc91SGabor Kovesdan char* tmp = sort_malloc(strlen(outfile) + 1257c66bbc91SGabor Kovesdan strlen(".tmp") + 1); 1258c66bbc91SGabor Kovesdan 1259c66bbc91SGabor Kovesdan strcpy(tmp, outfile); 1260c66bbc91SGabor Kovesdan strcpy(tmp + strlen(tmp), ".tmp"); 1261c66bbc91SGabor Kovesdan sort_free(outfile); 1262c66bbc91SGabor Kovesdan outfile = tmp; 1263c66bbc91SGabor Kovesdan if (access(outfile, F_OK) < 0) 1264c66bbc91SGabor Kovesdan break; 1265c66bbc91SGabor Kovesdan } 1266c66bbc91SGabor Kovesdan tmp_file_atexit(outfile); 1267c66bbc91SGabor Kovesdan } 1268c66bbc91SGabor Kovesdan } 1269c66bbc91SGabor Kovesdan } 1270c66bbc91SGabor Kovesdan 1271*5ca724dcSGabor Kovesdan #if defined(SORT_THREADS) 1272*5ca724dcSGabor Kovesdan if ((argc < 1) || (strcmp(outfile, "-") == 0) || (*outfile == 0)) 1273*5ca724dcSGabor Kovesdan nthreads = 1; 1274*5ca724dcSGabor Kovesdan #endif 1275*5ca724dcSGabor Kovesdan 1276c66bbc91SGabor Kovesdan if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) { 1277c66bbc91SGabor Kovesdan struct file_list fl; 1278c66bbc91SGabor Kovesdan struct sort_list list; 1279c66bbc91SGabor Kovesdan 1280c66bbc91SGabor Kovesdan sort_list_init(&list); 1281c66bbc91SGabor Kovesdan file_list_init(&fl, true); 1282c66bbc91SGabor Kovesdan 1283c66bbc91SGabor Kovesdan if (argc < 1) 1284c66bbc91SGabor Kovesdan procfile("-", &list, &fl); 1285c66bbc91SGabor Kovesdan else { 1286c66bbc91SGabor Kovesdan while (argc > 0) { 1287c66bbc91SGabor Kovesdan procfile(*argv, &list, &fl); 1288c66bbc91SGabor Kovesdan --argc; 1289c66bbc91SGabor Kovesdan ++argv; 1290c66bbc91SGabor Kovesdan } 1291c66bbc91SGabor Kovesdan } 1292c66bbc91SGabor Kovesdan 1293c66bbc91SGabor Kovesdan if (fl.count < 1) 1294c66bbc91SGabor Kovesdan sort_list_to_file(&list, outfile); 1295c66bbc91SGabor Kovesdan else { 1296c66bbc91SGabor Kovesdan if (list.count > 0) { 1297c66bbc91SGabor Kovesdan char *flast = new_tmp_file_name(); 1298c66bbc91SGabor Kovesdan 1299c66bbc91SGabor Kovesdan sort_list_to_file(&list, flast); 1300c66bbc91SGabor Kovesdan file_list_add(&fl, flast, false); 1301c66bbc91SGabor Kovesdan } 1302c66bbc91SGabor Kovesdan merge_files(&fl, outfile); 1303c66bbc91SGabor Kovesdan } 1304c66bbc91SGabor Kovesdan 1305c66bbc91SGabor Kovesdan file_list_clean(&fl); 1306c66bbc91SGabor Kovesdan 1307c66bbc91SGabor Kovesdan /* 1308c66bbc91SGabor Kovesdan * We are about to exit the program, so we can ignore 1309c66bbc91SGabor Kovesdan * the clean-up for speed 1310c66bbc91SGabor Kovesdan * 1311c66bbc91SGabor Kovesdan * sort_list_clean(&list); 1312c66bbc91SGabor Kovesdan */ 1313c66bbc91SGabor Kovesdan 1314c66bbc91SGabor Kovesdan } else if (sort_opts_vals.cflag) { 1315c66bbc91SGabor Kovesdan result = (argc == 0) ? (check("-")) : (check(*argv)); 1316c66bbc91SGabor Kovesdan } else if (sort_opts_vals.mflag) { 1317c66bbc91SGabor Kovesdan struct file_list fl; 1318c66bbc91SGabor Kovesdan 1319c66bbc91SGabor Kovesdan file_list_init(&fl, false); 1320c66bbc91SGabor Kovesdan file_list_populate(&fl, argc, argv, true); 1321c66bbc91SGabor Kovesdan merge_files(&fl, outfile); 1322c66bbc91SGabor Kovesdan file_list_clean(&fl); 1323c66bbc91SGabor Kovesdan } 1324c66bbc91SGabor Kovesdan 1325c66bbc91SGabor Kovesdan if (real_outfile) { 1326c66bbc91SGabor Kovesdan unlink(real_outfile); 1327c66bbc91SGabor Kovesdan if (rename(outfile, real_outfile) < 0) 1328c66bbc91SGabor Kovesdan err(2, NULL); 1329c66bbc91SGabor Kovesdan sort_free(real_outfile); 1330c66bbc91SGabor Kovesdan } 1331c66bbc91SGabor Kovesdan 1332c66bbc91SGabor Kovesdan sort_free(outfile); 1333c66bbc91SGabor Kovesdan 1334c66bbc91SGabor Kovesdan return (result); 1335c66bbc91SGabor Kovesdan } 1336