1c66bbc91SGabor Kovesdan /*- 2c66bbc91SGabor Kovesdan * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> 3c859c6ddSGabor Kovesdan * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.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] " 925ca724dcSGabor Kovesdan "[--mmap] " 93c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 945d5151aeSGabor Kovesdan "[--parallel 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) 106ab28d4d3SGabor Kovesdan unsigned int 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 */ 120e8da8c74SGabor Kovesdan static size_t argc_from_file0 = (size_t)-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) 1355d5151aeSGabor Kovesdan PARALLEL_OPT, 136c66bbc91SGabor Kovesdan #endif 137c66bbc91SGabor Kovesdan RANDOMSOURCE_OPT, 138c66bbc91SGabor Kovesdan COMPRESSPROGRAM_OPT, 139c66bbc91SGabor Kovesdan QSORT_OPT, 140c66bbc91SGabor Kovesdan MERGESORT_OPT, 141c66bbc91SGabor Kovesdan HEAPSORT_OPT, 1425ca724dcSGabor Kovesdan RADIXSORT_OPT, 1435ca724dcSGabor 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 149bf70beceSEd Schouten static 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 }, 1695ca724dcSGabor 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) 1745d5151aeSGabor Kovesdan { "parallel", required_argument, NULL, PARALLEL_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 { 199e5f71a07SPedro F. Giffuni 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 FILE *f; 233*0f4b9a90SPedro F. Giffuni char *line = NULL; 234*0f4b9a90SPedro F. Giffuni size_t linesize = 0; 235*0f4b9a90SPedro F. Giffuni ssize_t linelen; 236*0f4b9a90SPedro F. Giffuni 237*0f4b9a90SPedro F. Giffuni if (fn == NULL) 238*0f4b9a90SPedro F. Giffuni return; 239c66bbc91SGabor Kovesdan 240c66bbc91SGabor Kovesdan f = fopen(fn, "r"); 241c66bbc91SGabor Kovesdan if (f == NULL) 242*0f4b9a90SPedro F. Giffuni err(2, "%s", fn); 243c66bbc91SGabor Kovesdan 244*0f4b9a90SPedro F. Giffuni while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) { 245*0f4b9a90SPedro F. Giffuni if (*line != '\0') { 246e8da8c74SGabor Kovesdan if (argc_from_file0 == (size_t) - 1) 247e8da8c74SGabor Kovesdan argc_from_file0 = 0; 248c66bbc91SGabor Kovesdan ++argc_from_file0; 249c66bbc91SGabor Kovesdan argv_from_file0 = sort_realloc(argv_from_file0, 250c66bbc91SGabor Kovesdan argc_from_file0 * sizeof(char *)); 251c66bbc91SGabor Kovesdan if (argv_from_file0 == NULL) 252c66bbc91SGabor Kovesdan err(2, NULL); 253*0f4b9a90SPedro F. Giffuni argv_from_file0[argc_from_file0 - 1] = line; 254*0f4b9a90SPedro F. Giffuni } else { 255*0f4b9a90SPedro F. Giffuni free(line); 256c66bbc91SGabor Kovesdan } 257*0f4b9a90SPedro F. Giffuni line = NULL; 258*0f4b9a90SPedro F. Giffuni linesize = 0; 259c66bbc91SGabor Kovesdan } 260*0f4b9a90SPedro F. Giffuni if (ferror(f)) 261*0f4b9a90SPedro F. Giffuni err(2, "%s: getdelim", fn); 262*0f4b9a90SPedro F. Giffuni 263c66bbc91SGabor Kovesdan closefile(f, fn); 264c66bbc91SGabor Kovesdan } 265c66bbc91SGabor Kovesdan 266c66bbc91SGabor Kovesdan /* 267c66bbc91SGabor Kovesdan * Check how much RAM is available for the sort. 268c66bbc91SGabor Kovesdan */ 269c66bbc91SGabor Kovesdan static void 270c66bbc91SGabor Kovesdan set_hw_params(void) 271c66bbc91SGabor Kovesdan { 27255444243SGabor Kovesdan long pages, psize; 273c66bbc91SGabor Kovesdan 274c66bbc91SGabor Kovesdan pages = psize = 0; 27555444243SGabor Kovesdan 276c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 277c66bbc91SGabor Kovesdan ncpu = 1; 278c66bbc91SGabor Kovesdan #endif 279c66bbc91SGabor Kovesdan 28055444243SGabor Kovesdan pages = sysconf(_SC_PHYS_PAGES); 28155444243SGabor Kovesdan if (pages < 1) { 28255444243SGabor Kovesdan perror("sysconf pages"); 28355444243SGabor Kovesdan psize = 1; 284c66bbc91SGabor Kovesdan } 28555444243SGabor Kovesdan psize = sysconf(_SC_PAGESIZE); 28655444243SGabor Kovesdan if (psize < 1) { 28755444243SGabor Kovesdan perror("sysconf psize"); 28855444243SGabor Kovesdan psize = 4096; 289c66bbc91SGabor Kovesdan } 290c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 29155444243SGabor Kovesdan ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN); 29255444243SGabor Kovesdan if (ncpu < 1) 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; 30155444243SGabor Kovesdan available_free_memory = free_memory / 2; 302ab28d4d3SGabor Kovesdan 303ab28d4d3SGabor Kovesdan if (available_free_memory < 1024) 304ab28d4d3SGabor Kovesdan available_free_memory = 1024; 305c66bbc91SGabor Kovesdan } 306c66bbc91SGabor Kovesdan 307c66bbc91SGabor Kovesdan /* 308c66bbc91SGabor Kovesdan * Convert "plain" symbol to wide symbol, with default value. 309c66bbc91SGabor Kovesdan */ 310c66bbc91SGabor Kovesdan static void 311c66bbc91SGabor Kovesdan conv_mbtowc(wchar_t *wc, const char *c, const wchar_t def) 312c66bbc91SGabor Kovesdan { 313e5f71a07SPedro F. Giffuni 314c66bbc91SGabor Kovesdan if (wc && c) { 315c66bbc91SGabor Kovesdan int res; 316c66bbc91SGabor Kovesdan 317c66bbc91SGabor Kovesdan res = mbtowc(wc, c, MB_CUR_MAX); 318c66bbc91SGabor Kovesdan if (res < 1) 319c66bbc91SGabor Kovesdan *wc = def; 320c66bbc91SGabor Kovesdan } 321c66bbc91SGabor Kovesdan } 322c66bbc91SGabor Kovesdan 323c66bbc91SGabor Kovesdan /* 324c66bbc91SGabor Kovesdan * Set current locale symbols. 325c66bbc91SGabor Kovesdan */ 326c66bbc91SGabor Kovesdan static void 327c66bbc91SGabor Kovesdan set_locale(void) 328c66bbc91SGabor Kovesdan { 329c66bbc91SGabor Kovesdan struct lconv *lc; 330c66bbc91SGabor Kovesdan const char *locale; 331c66bbc91SGabor Kovesdan 332c66bbc91SGabor Kovesdan setlocale(LC_ALL, ""); 333c66bbc91SGabor Kovesdan 334c66bbc91SGabor Kovesdan lc = localeconv(); 335c66bbc91SGabor Kovesdan 336c66bbc91SGabor Kovesdan if (lc) { 337c66bbc91SGabor Kovesdan /* obtain LC_NUMERIC info */ 338c66bbc91SGabor Kovesdan /* Convert to wide char form */ 339c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_decimal_point, lc->decimal_point, 340c66bbc91SGabor Kovesdan symbol_decimal_point); 341c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_thousands_sep, lc->thousands_sep, 342c66bbc91SGabor Kovesdan symbol_thousands_sep); 343c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_positive_sign, lc->positive_sign, 344c66bbc91SGabor Kovesdan symbol_positive_sign); 345c66bbc91SGabor Kovesdan conv_mbtowc(&symbol_negative_sign, lc->negative_sign, 346c66bbc91SGabor Kovesdan symbol_negative_sign); 347c66bbc91SGabor Kovesdan } 348c66bbc91SGabor Kovesdan 349c66bbc91SGabor Kovesdan if (getenv("GNUSORT_NUMERIC_COMPATIBILITY")) 350c66bbc91SGabor Kovesdan gnusort_numeric_compatibility = true; 351c66bbc91SGabor Kovesdan 352c66bbc91SGabor Kovesdan locale = setlocale(LC_COLLATE, NULL); 353c66bbc91SGabor Kovesdan 354c66bbc91SGabor Kovesdan if (locale) { 355c66bbc91SGabor Kovesdan char *tmpl; 356c66bbc91SGabor Kovesdan const char *cclocale; 357c66bbc91SGabor Kovesdan 358c66bbc91SGabor Kovesdan tmpl = sort_strdup(locale); 359c66bbc91SGabor Kovesdan cclocale = setlocale(LC_COLLATE, "C"); 360c66bbc91SGabor Kovesdan if (cclocale && !strcmp(cclocale, tmpl)) 361c66bbc91SGabor Kovesdan byte_sort = true; 362c66bbc91SGabor Kovesdan else { 363c66bbc91SGabor Kovesdan const char *pclocale; 364c66bbc91SGabor Kovesdan 365c66bbc91SGabor Kovesdan pclocale = setlocale(LC_COLLATE, "POSIX"); 366c66bbc91SGabor Kovesdan if (pclocale && !strcmp(pclocale, tmpl)) 367c66bbc91SGabor Kovesdan byte_sort = true; 368c66bbc91SGabor Kovesdan } 369c66bbc91SGabor Kovesdan setlocale(LC_COLLATE, tmpl); 370c66bbc91SGabor Kovesdan sort_free(tmpl); 371c66bbc91SGabor Kovesdan } 372c66bbc91SGabor Kovesdan } 373c66bbc91SGabor Kovesdan 374c66bbc91SGabor Kovesdan /* 375c66bbc91SGabor Kovesdan * Set directory temporary files. 376c66bbc91SGabor Kovesdan */ 377c66bbc91SGabor Kovesdan static void 378c66bbc91SGabor Kovesdan set_tmpdir(void) 379c66bbc91SGabor Kovesdan { 380c66bbc91SGabor Kovesdan char *td; 381c66bbc91SGabor Kovesdan 382c66bbc91SGabor Kovesdan td = getenv("TMPDIR"); 383c66bbc91SGabor Kovesdan if (td != NULL) 384c66bbc91SGabor Kovesdan tmpdir = sort_strdup(td); 385c66bbc91SGabor Kovesdan } 386c66bbc91SGabor Kovesdan 387c66bbc91SGabor Kovesdan /* 388c66bbc91SGabor Kovesdan * Parse -S option. 389c66bbc91SGabor Kovesdan */ 390c66bbc91SGabor Kovesdan static unsigned long long 391c66bbc91SGabor Kovesdan parse_memory_buffer_value(const char *value) 392c66bbc91SGabor Kovesdan { 393e5f71a07SPedro F. Giffuni 394c66bbc91SGabor Kovesdan if (value == NULL) 395c66bbc91SGabor Kovesdan return (available_free_memory); 396c66bbc91SGabor Kovesdan else { 397c66bbc91SGabor Kovesdan char *endptr; 398c66bbc91SGabor Kovesdan unsigned long long membuf; 399c66bbc91SGabor Kovesdan 400c66bbc91SGabor Kovesdan endptr = NULL; 401c66bbc91SGabor Kovesdan errno = 0; 402c66bbc91SGabor Kovesdan membuf = strtoll(value, &endptr, 10); 403c66bbc91SGabor Kovesdan 404c66bbc91SGabor Kovesdan if (errno != 0) { 4058818aa39SGabor Kovesdan warn("%s",getstr(4)); 406c66bbc91SGabor Kovesdan membuf = available_free_memory; 407c66bbc91SGabor Kovesdan } else { 408c66bbc91SGabor Kovesdan switch (*endptr){ 409c66bbc91SGabor Kovesdan case 'Y': 410c66bbc91SGabor Kovesdan membuf *= 1024; 411c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 412c66bbc91SGabor Kovesdan case 'Z': 413c66bbc91SGabor Kovesdan membuf *= 1024; 414c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 415c66bbc91SGabor Kovesdan case 'E': 416c66bbc91SGabor Kovesdan membuf *= 1024; 417c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 418c66bbc91SGabor Kovesdan case 'P': 419c66bbc91SGabor Kovesdan membuf *= 1024; 420c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 421c66bbc91SGabor Kovesdan case 'T': 422c66bbc91SGabor Kovesdan membuf *= 1024; 423c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 424c66bbc91SGabor Kovesdan case 'G': 425c66bbc91SGabor Kovesdan membuf *= 1024; 426c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 427c66bbc91SGabor Kovesdan case 'M': 428c66bbc91SGabor Kovesdan membuf *= 1024; 429c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 430c66bbc91SGabor Kovesdan case '\0': 431c66bbc91SGabor Kovesdan case 'K': 432c66bbc91SGabor Kovesdan membuf *= 1024; 433c66bbc91SGabor Kovesdan /* FALLTHROUGH */ 434c66bbc91SGabor Kovesdan case 'b': 435c66bbc91SGabor Kovesdan break; 436c66bbc91SGabor Kovesdan case '%': 437c66bbc91SGabor Kovesdan membuf = (available_free_memory * membuf) / 438c66bbc91SGabor Kovesdan 100; 439c66bbc91SGabor Kovesdan break; 440c66bbc91SGabor Kovesdan default: 441f187ff08SGabor Kovesdan warnc(EINVAL, "%s", optarg); 442c66bbc91SGabor Kovesdan membuf = available_free_memory; 443c66bbc91SGabor Kovesdan } 444c66bbc91SGabor Kovesdan } 445c66bbc91SGabor Kovesdan return (membuf); 446c66bbc91SGabor Kovesdan } 447c66bbc91SGabor Kovesdan } 448c66bbc91SGabor Kovesdan 449c66bbc91SGabor Kovesdan /* 450c66bbc91SGabor Kovesdan * Signal handler that clears the temporary files. 451c66bbc91SGabor Kovesdan */ 452c66bbc91SGabor Kovesdan static void 4538818aa39SGabor Kovesdan sig_handler(int sig __unused, siginfo_t *siginfo __unused, 4548818aa39SGabor Kovesdan void *context __unused) 455c66bbc91SGabor Kovesdan { 456e5f71a07SPedro F. Giffuni 457c66bbc91SGabor Kovesdan clear_tmp_files(); 458c66bbc91SGabor Kovesdan exit(-1); 459c66bbc91SGabor Kovesdan } 460c66bbc91SGabor Kovesdan 461c66bbc91SGabor Kovesdan /* 462c66bbc91SGabor Kovesdan * Set signal handler on panic signals. 463c66bbc91SGabor Kovesdan */ 464c66bbc91SGabor Kovesdan static void 465c66bbc91SGabor Kovesdan set_signal_handler(void) 466c66bbc91SGabor Kovesdan { 467c66bbc91SGabor Kovesdan struct sigaction sa; 468c66bbc91SGabor Kovesdan 469c66bbc91SGabor Kovesdan memset(&sa, 0, sizeof(sa)); 470c66bbc91SGabor Kovesdan sa.sa_sigaction = &sig_handler; 471c66bbc91SGabor Kovesdan sa.sa_flags = SA_SIGINFO; 472c66bbc91SGabor Kovesdan 473c66bbc91SGabor Kovesdan if (sigaction(SIGTERM, &sa, NULL) < 0) { 474c66bbc91SGabor Kovesdan perror("sigaction"); 475c66bbc91SGabor Kovesdan return; 476c66bbc91SGabor Kovesdan } 477c66bbc91SGabor Kovesdan if (sigaction(SIGHUP, &sa, NULL) < 0) { 478c66bbc91SGabor Kovesdan perror("sigaction"); 479c66bbc91SGabor Kovesdan return; 480c66bbc91SGabor Kovesdan } 481c66bbc91SGabor Kovesdan if (sigaction(SIGINT, &sa, NULL) < 0) { 482c66bbc91SGabor Kovesdan perror("sigaction"); 483c66bbc91SGabor Kovesdan return; 484c66bbc91SGabor Kovesdan } 485c66bbc91SGabor Kovesdan if (sigaction(SIGQUIT, &sa, NULL) < 0) { 486c66bbc91SGabor Kovesdan perror("sigaction"); 487c66bbc91SGabor Kovesdan return; 488c66bbc91SGabor Kovesdan } 489c66bbc91SGabor Kovesdan if (sigaction(SIGABRT, &sa, NULL) < 0) { 490c66bbc91SGabor Kovesdan perror("sigaction"); 491c66bbc91SGabor Kovesdan return; 492c66bbc91SGabor Kovesdan } 493c66bbc91SGabor Kovesdan if (sigaction(SIGBUS, &sa, NULL) < 0) { 494c66bbc91SGabor Kovesdan perror("sigaction"); 495c66bbc91SGabor Kovesdan return; 496c66bbc91SGabor Kovesdan } 497c66bbc91SGabor Kovesdan if (sigaction(SIGSEGV, &sa, NULL) < 0) { 498c66bbc91SGabor Kovesdan perror("sigaction"); 499c66bbc91SGabor Kovesdan return; 500c66bbc91SGabor Kovesdan } 501c66bbc91SGabor Kovesdan if (sigaction(SIGUSR1, &sa, NULL) < 0) { 502c66bbc91SGabor Kovesdan perror("sigaction"); 503c66bbc91SGabor Kovesdan return; 504c66bbc91SGabor Kovesdan } 505c66bbc91SGabor Kovesdan if (sigaction(SIGUSR2, &sa, NULL) < 0) { 506c66bbc91SGabor Kovesdan perror("sigaction"); 507c66bbc91SGabor Kovesdan return; 508c66bbc91SGabor Kovesdan } 509c66bbc91SGabor Kovesdan } 510c66bbc91SGabor Kovesdan 511c66bbc91SGabor Kovesdan /* 512c66bbc91SGabor Kovesdan * Print "unknown" message and exit with status 2. 513c66bbc91SGabor Kovesdan */ 514c66bbc91SGabor Kovesdan static void 515c66bbc91SGabor Kovesdan unknown(const char *what) 516c66bbc91SGabor Kovesdan { 517e5f71a07SPedro F. Giffuni 5188818aa39SGabor Kovesdan errx(2, "%s: %s", getstr(3), what); 519c66bbc91SGabor Kovesdan } 520c66bbc91SGabor Kovesdan 521c66bbc91SGabor Kovesdan /* 522c66bbc91SGabor Kovesdan * Check whether contradictory input options are used. 523c66bbc91SGabor Kovesdan */ 524c66bbc91SGabor Kovesdan static void 525c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(char c, bool *mef_flags) 526c66bbc91SGabor Kovesdan { 527c66bbc91SGabor Kovesdan int fo_index, mec; 528c66bbc91SGabor Kovesdan bool found_others, found_this; 529c66bbc91SGabor Kovesdan 530c66bbc91SGabor Kovesdan found_others = found_this = false; 531c66bbc91SGabor Kovesdan fo_index = 0; 532c66bbc91SGabor Kovesdan 533c66bbc91SGabor Kovesdan for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) { 534c66bbc91SGabor Kovesdan mec = mutually_exclusive_flags[i]; 535c66bbc91SGabor Kovesdan 536c66bbc91SGabor Kovesdan if (mec != c) { 537c66bbc91SGabor Kovesdan if (mef_flags[i]) { 538c66bbc91SGabor Kovesdan if (found_this) 5398818aa39SGabor Kovesdan errx(1, "%c:%c: %s", c, mec, getstr(1)); 540c66bbc91SGabor Kovesdan found_others = true; 541c66bbc91SGabor Kovesdan fo_index = i; 542c66bbc91SGabor Kovesdan } 543c66bbc91SGabor Kovesdan } else { 544c66bbc91SGabor Kovesdan if (found_others) 5458818aa39SGabor Kovesdan errx(1, "%c:%c: %s", c, mutually_exclusive_flags[fo_index], getstr(1)); 546c66bbc91SGabor Kovesdan mef_flags[i] = true; 547c66bbc91SGabor Kovesdan found_this = true; 548c66bbc91SGabor Kovesdan } 549c66bbc91SGabor Kovesdan } 550c66bbc91SGabor Kovesdan } 551c66bbc91SGabor Kovesdan 552c66bbc91SGabor Kovesdan /* 553c66bbc91SGabor Kovesdan * Initialise sort opts data. 554c66bbc91SGabor Kovesdan */ 555c66bbc91SGabor Kovesdan static void 556c66bbc91SGabor Kovesdan set_sort_opts(void) 557c66bbc91SGabor Kovesdan { 558e5f71a07SPedro F. Giffuni 559c66bbc91SGabor Kovesdan memset(&default_sort_mods_object, 0, 560c66bbc91SGabor Kovesdan sizeof(default_sort_mods_object)); 561c66bbc91SGabor Kovesdan memset(&sort_opts_vals, 0, sizeof(sort_opts_vals)); 562c66bbc91SGabor Kovesdan default_sort_mods_object.func = 563c66bbc91SGabor Kovesdan get_sort_func(&default_sort_mods_object); 564c66bbc91SGabor Kovesdan } 565c66bbc91SGabor Kovesdan 566c66bbc91SGabor Kovesdan /* 567c66bbc91SGabor Kovesdan * Set a sort modifier on a sort modifiers object. 568c66bbc91SGabor Kovesdan */ 569c66bbc91SGabor Kovesdan static bool 570c66bbc91SGabor Kovesdan set_sort_modifier(struct sort_mods *sm, int c) 571c66bbc91SGabor Kovesdan { 572e5f71a07SPedro F. Giffuni 573c66bbc91SGabor Kovesdan if (sm) { 574c66bbc91SGabor Kovesdan switch (c){ 575c66bbc91SGabor Kovesdan case 'b': 576c66bbc91SGabor Kovesdan sm->bflag = true; 577c66bbc91SGabor Kovesdan break; 578c66bbc91SGabor Kovesdan case 'd': 579c66bbc91SGabor Kovesdan sm->dflag = true; 580c66bbc91SGabor Kovesdan break; 581c66bbc91SGabor Kovesdan case 'f': 582c66bbc91SGabor Kovesdan sm->fflag = true; 583c66bbc91SGabor Kovesdan break; 584c66bbc91SGabor Kovesdan case 'g': 585c66bbc91SGabor Kovesdan sm->gflag = true; 586c66bbc91SGabor Kovesdan need_hint = true; 587c66bbc91SGabor Kovesdan break; 588c66bbc91SGabor Kovesdan case 'i': 589c66bbc91SGabor Kovesdan sm->iflag = true; 590c66bbc91SGabor Kovesdan break; 591c66bbc91SGabor Kovesdan case 'R': 592c66bbc91SGabor Kovesdan sm->Rflag = true; 593c66bbc91SGabor Kovesdan need_random = true; 594c66bbc91SGabor Kovesdan break; 595c66bbc91SGabor Kovesdan case 'M': 596c66bbc91SGabor Kovesdan initialise_months(); 597c66bbc91SGabor Kovesdan sm->Mflag = true; 598c66bbc91SGabor Kovesdan need_hint = true; 599c66bbc91SGabor Kovesdan break; 600c66bbc91SGabor Kovesdan case 'n': 601c66bbc91SGabor Kovesdan sm->nflag = true; 602c66bbc91SGabor Kovesdan need_hint = true; 603c66bbc91SGabor Kovesdan print_symbols_on_debug = true; 604c66bbc91SGabor Kovesdan break; 605c66bbc91SGabor Kovesdan case 'r': 606c66bbc91SGabor Kovesdan sm->rflag = true; 607c66bbc91SGabor Kovesdan break; 608c66bbc91SGabor Kovesdan case 'V': 609c66bbc91SGabor Kovesdan sm->Vflag = true; 610c66bbc91SGabor Kovesdan break; 611c66bbc91SGabor Kovesdan case 'h': 612c66bbc91SGabor Kovesdan sm->hflag = true; 613c66bbc91SGabor Kovesdan need_hint = true; 614c66bbc91SGabor Kovesdan print_symbols_on_debug = true; 615c66bbc91SGabor Kovesdan break; 616c66bbc91SGabor Kovesdan default: 617c66bbc91SGabor Kovesdan return false; 618c66bbc91SGabor Kovesdan } 619c66bbc91SGabor Kovesdan sort_opts_vals.complex_sort = true; 620c66bbc91SGabor Kovesdan sm->func = get_sort_func(sm); 621c66bbc91SGabor Kovesdan } 622c66bbc91SGabor Kovesdan return (true); 623c66bbc91SGabor Kovesdan } 624c66bbc91SGabor Kovesdan 625c66bbc91SGabor Kovesdan /* 626c66bbc91SGabor Kovesdan * Parse POS in -k option. 627c66bbc91SGabor Kovesdan */ 628c66bbc91SGabor Kovesdan static int 629c66bbc91SGabor Kovesdan parse_pos(const char *s, struct key_specs *ks, bool *mef_flags, bool second) 630c66bbc91SGabor Kovesdan { 631c66bbc91SGabor Kovesdan regmatch_t pmatch[4]; 632c66bbc91SGabor Kovesdan regex_t re; 633c66bbc91SGabor Kovesdan char *c, *f; 634c66bbc91SGabor Kovesdan const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([bdfirMngRhV]+)?$"; 635c66bbc91SGabor Kovesdan size_t len, nmatch; 636c66bbc91SGabor Kovesdan int ret; 637c66bbc91SGabor Kovesdan 638c66bbc91SGabor Kovesdan ret = -1; 639c66bbc91SGabor Kovesdan nmatch = 4; 640c66bbc91SGabor Kovesdan c = f = NULL; 641c66bbc91SGabor Kovesdan 642c66bbc91SGabor Kovesdan if (regcomp(&re, sregexp, REG_EXTENDED) != 0) 643c66bbc91SGabor Kovesdan return (-1); 644c66bbc91SGabor Kovesdan 645c66bbc91SGabor Kovesdan if (regexec(&re, s, nmatch, pmatch, 0) != 0) 646c66bbc91SGabor Kovesdan goto end; 647c66bbc91SGabor Kovesdan 648c66bbc91SGabor Kovesdan if (pmatch[0].rm_eo <= pmatch[0].rm_so) 649c66bbc91SGabor Kovesdan goto end; 650c66bbc91SGabor Kovesdan 651c66bbc91SGabor Kovesdan if (pmatch[1].rm_eo <= pmatch[1].rm_so) 652c66bbc91SGabor Kovesdan goto end; 653c66bbc91SGabor Kovesdan 654c66bbc91SGabor Kovesdan len = pmatch[1].rm_eo - pmatch[1].rm_so; 655c66bbc91SGabor Kovesdan f = sort_malloc((len + 1) * sizeof(char)); 656c66bbc91SGabor Kovesdan 657c66bbc91SGabor Kovesdan strncpy(f, s + pmatch[1].rm_so, len); 658c66bbc91SGabor Kovesdan f[len] = '\0'; 659c66bbc91SGabor Kovesdan 660c66bbc91SGabor Kovesdan if (second) { 661c66bbc91SGabor Kovesdan errno = 0; 662c66bbc91SGabor Kovesdan ks->f2 = (size_t) strtoul(f, NULL, 10); 663c66bbc91SGabor Kovesdan if (errno != 0) 664f187ff08SGabor Kovesdan err(2, "-k"); 665c66bbc91SGabor Kovesdan if (ks->f2 == 0) { 6668818aa39SGabor Kovesdan warn("%s",getstr(5)); 667c66bbc91SGabor Kovesdan goto end; 668c66bbc91SGabor Kovesdan } 669c66bbc91SGabor Kovesdan } else { 670c66bbc91SGabor Kovesdan errno = 0; 671c66bbc91SGabor Kovesdan ks->f1 = (size_t) strtoul(f, NULL, 10); 672c66bbc91SGabor Kovesdan if (errno != 0) 673f187ff08SGabor Kovesdan err(2, "-k"); 674c66bbc91SGabor Kovesdan if (ks->f1 == 0) { 6758818aa39SGabor Kovesdan warn("%s",getstr(5)); 676c66bbc91SGabor Kovesdan goto end; 677c66bbc91SGabor Kovesdan } 678c66bbc91SGabor Kovesdan } 679c66bbc91SGabor Kovesdan 680c66bbc91SGabor Kovesdan if (pmatch[2].rm_eo > pmatch[2].rm_so) { 681c66bbc91SGabor Kovesdan len = pmatch[2].rm_eo - pmatch[2].rm_so - 1; 682c66bbc91SGabor Kovesdan c = sort_malloc((len + 1) * sizeof(char)); 683c66bbc91SGabor Kovesdan 684c66bbc91SGabor Kovesdan strncpy(c, s + pmatch[2].rm_so + 1, len); 685c66bbc91SGabor Kovesdan c[len] = '\0'; 686c66bbc91SGabor Kovesdan 687c66bbc91SGabor Kovesdan if (second) { 688c66bbc91SGabor Kovesdan errno = 0; 689c66bbc91SGabor Kovesdan ks->c2 = (size_t) strtoul(c, NULL, 10); 690c66bbc91SGabor Kovesdan if (errno != 0) 691f187ff08SGabor Kovesdan err(2, "-k"); 692c66bbc91SGabor Kovesdan } else { 693c66bbc91SGabor Kovesdan errno = 0; 694c66bbc91SGabor Kovesdan ks->c1 = (size_t) strtoul(c, NULL, 10); 695c66bbc91SGabor Kovesdan if (errno != 0) 696f187ff08SGabor Kovesdan err(2, "-k"); 697c66bbc91SGabor Kovesdan if (ks->c1 == 0) { 6988818aa39SGabor Kovesdan warn("%s",getstr(6)); 699c66bbc91SGabor Kovesdan goto end; 700c66bbc91SGabor Kovesdan } 701c66bbc91SGabor Kovesdan } 702c66bbc91SGabor Kovesdan } else { 703c66bbc91SGabor Kovesdan if (second) 704c66bbc91SGabor Kovesdan ks->c2 = 0; 705c66bbc91SGabor Kovesdan else 706c66bbc91SGabor Kovesdan ks->c1 = 1; 707c66bbc91SGabor Kovesdan } 708c66bbc91SGabor Kovesdan 709c66bbc91SGabor Kovesdan if (pmatch[3].rm_eo > pmatch[3].rm_so) { 710c66bbc91SGabor Kovesdan regoff_t i = 0; 711c66bbc91SGabor Kovesdan 712c66bbc91SGabor Kovesdan for (i = pmatch[3].rm_so; i < pmatch[3].rm_eo; i++) { 713c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(s[i], mef_flags); 714c66bbc91SGabor Kovesdan if (s[i] == 'b') { 715c66bbc91SGabor Kovesdan if (second) 716c66bbc91SGabor Kovesdan ks->pos2b = true; 717c66bbc91SGabor Kovesdan else 718c66bbc91SGabor Kovesdan ks->pos1b = true; 719c66bbc91SGabor Kovesdan } else if (!set_sort_modifier(&(ks->sm), s[i])) 720c66bbc91SGabor Kovesdan goto end; 721c66bbc91SGabor Kovesdan } 722c66bbc91SGabor Kovesdan } 723c66bbc91SGabor Kovesdan 724c66bbc91SGabor Kovesdan ret = 0; 725c66bbc91SGabor Kovesdan 726c66bbc91SGabor Kovesdan end: 727c66bbc91SGabor Kovesdan 728c66bbc91SGabor Kovesdan if (c) 729c66bbc91SGabor Kovesdan sort_free(c); 730c66bbc91SGabor Kovesdan if (f) 731c66bbc91SGabor Kovesdan sort_free(f); 732c66bbc91SGabor Kovesdan regfree(&re); 733c66bbc91SGabor Kovesdan 734c66bbc91SGabor Kovesdan return (ret); 735c66bbc91SGabor Kovesdan } 736c66bbc91SGabor Kovesdan 737c66bbc91SGabor Kovesdan /* 738c66bbc91SGabor Kovesdan * Parse -k option value. 739c66bbc91SGabor Kovesdan */ 740c66bbc91SGabor Kovesdan static int 741c66bbc91SGabor Kovesdan parse_k(const char *s, struct key_specs *ks) 742c66bbc91SGabor Kovesdan { 743c66bbc91SGabor Kovesdan int ret = -1; 744c66bbc91SGabor Kovesdan bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = 745c66bbc91SGabor Kovesdan { false, false, false, false, false, false }; 746c66bbc91SGabor Kovesdan 747c66bbc91SGabor Kovesdan if (s && *s) { 748c66bbc91SGabor Kovesdan char *sptr; 749c66bbc91SGabor Kovesdan 750c66bbc91SGabor Kovesdan sptr = strchr(s, ','); 751c66bbc91SGabor Kovesdan if (sptr) { 752c66bbc91SGabor Kovesdan size_t size1; 753c66bbc91SGabor Kovesdan char *pos1, *pos2; 754c66bbc91SGabor Kovesdan 755c66bbc91SGabor Kovesdan size1 = sptr - s; 756c66bbc91SGabor Kovesdan 757c66bbc91SGabor Kovesdan if (size1 < 1) 758c66bbc91SGabor Kovesdan return (-1); 759c66bbc91SGabor Kovesdan pos1 = sort_malloc((size1 + 1) * sizeof(char)); 760c66bbc91SGabor Kovesdan 761c66bbc91SGabor Kovesdan strncpy(pos1, s, size1); 762c66bbc91SGabor Kovesdan pos1[size1] = '\0'; 763c66bbc91SGabor Kovesdan 764c66bbc91SGabor Kovesdan ret = parse_pos(pos1, ks, mef_flags, false); 765c66bbc91SGabor Kovesdan 766c66bbc91SGabor Kovesdan sort_free(pos1); 767c66bbc91SGabor Kovesdan if (ret < 0) 768c66bbc91SGabor Kovesdan return (ret); 769c66bbc91SGabor Kovesdan 770c66bbc91SGabor Kovesdan pos2 = sort_strdup(sptr + 1); 771c66bbc91SGabor Kovesdan ret = parse_pos(pos2, ks, mef_flags, true); 772c66bbc91SGabor Kovesdan sort_free(pos2); 773c66bbc91SGabor Kovesdan } else 774c66bbc91SGabor Kovesdan ret = parse_pos(s, ks, mef_flags, false); 775c66bbc91SGabor Kovesdan } 776c66bbc91SGabor Kovesdan 777c66bbc91SGabor Kovesdan return (ret); 778c66bbc91SGabor Kovesdan } 779c66bbc91SGabor Kovesdan 780c66bbc91SGabor Kovesdan /* 781c66bbc91SGabor Kovesdan * Parse POS in +POS -POS option. 782c66bbc91SGabor Kovesdan */ 783c66bbc91SGabor Kovesdan static int 784c66bbc91SGabor Kovesdan parse_pos_obs(const char *s, int *nf, int *nc, char* sopts) 785c66bbc91SGabor Kovesdan { 786c66bbc91SGabor Kovesdan regex_t re; 787c66bbc91SGabor Kovesdan regmatch_t pmatch[4]; 788c66bbc91SGabor Kovesdan char *c, *f; 789c66bbc91SGabor Kovesdan const char *sregexp = "^([0-9]+)(\\.[0-9]+)?([A-Za-z]+)?$"; 790c66bbc91SGabor Kovesdan int ret; 791c66bbc91SGabor Kovesdan size_t len, nmatch; 792c66bbc91SGabor Kovesdan 793c66bbc91SGabor Kovesdan ret = -1; 794c66bbc91SGabor Kovesdan nmatch = 4; 795c66bbc91SGabor Kovesdan c = f = NULL; 796c66bbc91SGabor Kovesdan *nc = *nf = 0; 797c66bbc91SGabor Kovesdan 798c66bbc91SGabor Kovesdan if (regcomp(&re, sregexp, REG_EXTENDED) != 0) 799c66bbc91SGabor Kovesdan return (-1); 800c66bbc91SGabor Kovesdan 801c66bbc91SGabor Kovesdan if (regexec(&re, s, nmatch, pmatch, 0) != 0) 802c66bbc91SGabor Kovesdan goto end; 803c66bbc91SGabor Kovesdan 804c66bbc91SGabor Kovesdan if (pmatch[0].rm_eo <= pmatch[0].rm_so) 805c66bbc91SGabor Kovesdan goto end; 806c66bbc91SGabor Kovesdan 807c66bbc91SGabor Kovesdan if (pmatch[1].rm_eo <= pmatch[1].rm_so) 808c66bbc91SGabor Kovesdan goto end; 809c66bbc91SGabor Kovesdan 810c66bbc91SGabor Kovesdan len = pmatch[1].rm_eo - pmatch[1].rm_so; 811c66bbc91SGabor Kovesdan f = sort_malloc((len + 1) * sizeof(char)); 812c66bbc91SGabor Kovesdan 813c66bbc91SGabor Kovesdan strncpy(f, s + pmatch[1].rm_so, len); 814c66bbc91SGabor Kovesdan f[len] = '\0'; 815c66bbc91SGabor Kovesdan 816c66bbc91SGabor Kovesdan errno = 0; 817c66bbc91SGabor Kovesdan *nf = (size_t) strtoul(f, NULL, 10); 818c66bbc91SGabor Kovesdan if (errno != 0) 8198818aa39SGabor Kovesdan errx(2, "%s", getstr(11)); 820c66bbc91SGabor Kovesdan 821c66bbc91SGabor Kovesdan if (pmatch[2].rm_eo > pmatch[2].rm_so) { 822c66bbc91SGabor Kovesdan len = pmatch[2].rm_eo - pmatch[2].rm_so - 1; 823c66bbc91SGabor Kovesdan c = sort_malloc((len + 1) * sizeof(char)); 824c66bbc91SGabor Kovesdan 825c66bbc91SGabor Kovesdan strncpy(c, s + pmatch[2].rm_so + 1, len); 826c66bbc91SGabor Kovesdan c[len] = '\0'; 827c66bbc91SGabor Kovesdan 828c66bbc91SGabor Kovesdan errno = 0; 829c66bbc91SGabor Kovesdan *nc = (size_t) strtoul(c, NULL, 10); 830c66bbc91SGabor Kovesdan if (errno != 0) 8318818aa39SGabor Kovesdan errx(2, "%s", getstr(11)); 832c66bbc91SGabor Kovesdan } 833c66bbc91SGabor Kovesdan 834c66bbc91SGabor Kovesdan if (pmatch[3].rm_eo > pmatch[3].rm_so) { 835c66bbc91SGabor Kovesdan 836c66bbc91SGabor Kovesdan len = pmatch[3].rm_eo - pmatch[3].rm_so; 837c66bbc91SGabor Kovesdan 838c66bbc91SGabor Kovesdan strncpy(sopts, s + pmatch[3].rm_so, len); 839c66bbc91SGabor Kovesdan sopts[len] = '\0'; 840c66bbc91SGabor Kovesdan } 841c66bbc91SGabor Kovesdan 842c66bbc91SGabor Kovesdan ret = 0; 843c66bbc91SGabor Kovesdan 844c66bbc91SGabor Kovesdan end: 845c66bbc91SGabor Kovesdan if (c) 846c66bbc91SGabor Kovesdan sort_free(c); 847c66bbc91SGabor Kovesdan if (f) 848c66bbc91SGabor Kovesdan sort_free(f); 849c66bbc91SGabor Kovesdan regfree(&re); 850c66bbc91SGabor Kovesdan 851c66bbc91SGabor Kovesdan return (ret); 852c66bbc91SGabor Kovesdan } 853c66bbc91SGabor Kovesdan 854c66bbc91SGabor Kovesdan /* 855c66bbc91SGabor Kovesdan * "Translate" obsolete +POS1 -POS2 syntax into new -kPOS1,POS2 syntax 856c66bbc91SGabor Kovesdan */ 857c66bbc91SGabor Kovesdan void 858c66bbc91SGabor Kovesdan fix_obsolete_keys(int *argc, char **argv) 859c66bbc91SGabor Kovesdan { 860c66bbc91SGabor Kovesdan char sopt[129]; 861c66bbc91SGabor Kovesdan 862c66bbc91SGabor Kovesdan for (int i = 1; i < *argc; i++) { 863c66bbc91SGabor Kovesdan char *arg1; 864c66bbc91SGabor Kovesdan 865c66bbc91SGabor Kovesdan arg1 = argv[i]; 866c66bbc91SGabor Kovesdan 867c66bbc91SGabor Kovesdan if (strlen(arg1) > 1 && arg1[0] == '+') { 868c66bbc91SGabor Kovesdan int c1, f1; 869c66bbc91SGabor Kovesdan char sopts1[128]; 870c66bbc91SGabor Kovesdan 871c66bbc91SGabor Kovesdan sopts1[0] = 0; 872c66bbc91SGabor Kovesdan c1 = f1 = 0; 873c66bbc91SGabor Kovesdan 874c66bbc91SGabor Kovesdan if (parse_pos_obs(arg1 + 1, &f1, &c1, sopts1) < 0) 875c66bbc91SGabor Kovesdan continue; 876c66bbc91SGabor Kovesdan else { 877c66bbc91SGabor Kovesdan f1 += 1; 878c66bbc91SGabor Kovesdan c1 += 1; 879c66bbc91SGabor Kovesdan if (i + 1 < *argc) { 880c66bbc91SGabor Kovesdan char *arg2 = argv[i + 1]; 881c66bbc91SGabor Kovesdan 882c66bbc91SGabor Kovesdan if (strlen(arg2) > 1 && 883c66bbc91SGabor Kovesdan arg2[0] == '-') { 884c66bbc91SGabor Kovesdan int c2, f2; 885c66bbc91SGabor Kovesdan char sopts2[128]; 886c66bbc91SGabor Kovesdan 887c66bbc91SGabor Kovesdan sopts2[0] = 0; 888c66bbc91SGabor Kovesdan c2 = f2 = 0; 889c66bbc91SGabor Kovesdan 890c66bbc91SGabor Kovesdan if (parse_pos_obs(arg2 + 1, 891c66bbc91SGabor Kovesdan &f2, &c2, sopts2) >= 0) { 892c66bbc91SGabor Kovesdan if (c2 > 0) 893c66bbc91SGabor Kovesdan f2 += 1; 894c66bbc91SGabor Kovesdan sprintf(sopt, "-k%d.%d%s,%d.%d%s", 895c66bbc91SGabor Kovesdan f1, c1, sopts1, f2, c2, sopts2); 896c66bbc91SGabor Kovesdan argv[i] = sort_strdup(sopt); 897c66bbc91SGabor Kovesdan for (int j = i + 1; j + 1 < *argc; j++) 898c66bbc91SGabor Kovesdan argv[j] = argv[j + 1]; 899c66bbc91SGabor Kovesdan *argc -= 1; 900c66bbc91SGabor Kovesdan continue; 901c66bbc91SGabor Kovesdan } 902c66bbc91SGabor Kovesdan } 903c66bbc91SGabor Kovesdan } 9043e16491dSBaptiste Daroussin sprintf(sopt, "-k%d.%d%s", f1, c1, sopts1); 905c66bbc91SGabor Kovesdan argv[i] = sort_strdup(sopt); 906c66bbc91SGabor Kovesdan } 907c66bbc91SGabor Kovesdan } 908c66bbc91SGabor Kovesdan } 909c66bbc91SGabor Kovesdan } 910c66bbc91SGabor Kovesdan 911c66bbc91SGabor Kovesdan /* 912c66bbc91SGabor Kovesdan * Set random seed 913c66bbc91SGabor Kovesdan */ 914c66bbc91SGabor Kovesdan static void 915c66bbc91SGabor Kovesdan set_random_seed(void) 916c66bbc91SGabor Kovesdan { 917c66bbc91SGabor Kovesdan if (need_random) { 918c66bbc91SGabor Kovesdan 919c66bbc91SGabor Kovesdan if (strcmp(random_source, DEFAULT_RANDOM_SORT_SEED_FILE) == 0) { 920c66bbc91SGabor Kovesdan FILE* fseed; 921c66bbc91SGabor Kovesdan MD5_CTX ctx; 922c66bbc91SGabor Kovesdan char rsd[MAX_DEFAULT_RANDOM_SEED_DATA_SIZE]; 923c66bbc91SGabor Kovesdan size_t sz = 0; 924c66bbc91SGabor Kovesdan 925c66bbc91SGabor Kovesdan fseed = openfile(random_source, "r"); 926c66bbc91SGabor Kovesdan while (!feof(fseed)) { 927c66bbc91SGabor Kovesdan int cr; 928c66bbc91SGabor Kovesdan 929c66bbc91SGabor Kovesdan cr = fgetc(fseed); 930c66bbc91SGabor Kovesdan if (cr == EOF) 931c66bbc91SGabor Kovesdan break; 932c66bbc91SGabor Kovesdan 933c66bbc91SGabor Kovesdan rsd[sz++] = (char) cr; 934c66bbc91SGabor Kovesdan 935c66bbc91SGabor Kovesdan if (sz >= MAX_DEFAULT_RANDOM_SEED_DATA_SIZE) 936c66bbc91SGabor Kovesdan break; 937c66bbc91SGabor Kovesdan } 938c66bbc91SGabor Kovesdan 939c66bbc91SGabor Kovesdan closefile(fseed, random_source); 940c66bbc91SGabor Kovesdan 941c66bbc91SGabor Kovesdan MD5Init(&ctx); 942c66bbc91SGabor Kovesdan MD5Update(&ctx, rsd, sz); 943c66bbc91SGabor Kovesdan 944c66bbc91SGabor Kovesdan random_seed = MD5End(&ctx, NULL); 945c66bbc91SGabor Kovesdan random_seed_size = strlen(random_seed); 946c66bbc91SGabor Kovesdan 947c66bbc91SGabor Kovesdan } else { 948c66bbc91SGabor Kovesdan MD5_CTX ctx; 949c66bbc91SGabor Kovesdan char *b; 950c66bbc91SGabor Kovesdan 951c66bbc91SGabor Kovesdan MD5Init(&ctx); 952c66bbc91SGabor Kovesdan b = MD5File(random_source, NULL); 953c66bbc91SGabor Kovesdan if (b == NULL) 954c66bbc91SGabor Kovesdan err(2, NULL); 955c66bbc91SGabor Kovesdan 956c66bbc91SGabor Kovesdan random_seed = b; 957c66bbc91SGabor Kovesdan random_seed_size = strlen(b); 958c66bbc91SGabor Kovesdan } 959c66bbc91SGabor Kovesdan 960c66bbc91SGabor Kovesdan MD5Init(&md5_ctx); 961c66bbc91SGabor Kovesdan if(random_seed_size>0) { 962c66bbc91SGabor Kovesdan MD5Update(&md5_ctx, random_seed, random_seed_size); 963c66bbc91SGabor Kovesdan } 964c66bbc91SGabor Kovesdan } 965c66bbc91SGabor Kovesdan } 966c66bbc91SGabor Kovesdan 967c66bbc91SGabor Kovesdan /* 968c66bbc91SGabor Kovesdan * Main function. 969c66bbc91SGabor Kovesdan */ 970c66bbc91SGabor Kovesdan int 971c66bbc91SGabor Kovesdan main(int argc, char **argv) 972c66bbc91SGabor Kovesdan { 973c66bbc91SGabor Kovesdan char *outfile, *real_outfile; 974c66bbc91SGabor Kovesdan int c, result; 975c66bbc91SGabor Kovesdan bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = 976c66bbc91SGabor Kovesdan { false, false, false, false, false, false }; 977c66bbc91SGabor Kovesdan 978c66bbc91SGabor Kovesdan result = 0; 979c66bbc91SGabor Kovesdan outfile = sort_strdup("-"); 980c66bbc91SGabor Kovesdan real_outfile = NULL; 981c66bbc91SGabor Kovesdan 982c66bbc91SGabor Kovesdan struct sort_mods *sm = &default_sort_mods_object; 983c66bbc91SGabor Kovesdan 984c66bbc91SGabor Kovesdan init_tmp_files(); 985c66bbc91SGabor Kovesdan 986c66bbc91SGabor Kovesdan set_signal_handler(); 987c66bbc91SGabor Kovesdan 988c66bbc91SGabor Kovesdan set_hw_params(); 989c66bbc91SGabor Kovesdan set_locale(); 990c66bbc91SGabor Kovesdan set_tmpdir(); 991c66bbc91SGabor Kovesdan set_sort_opts(); 992c66bbc91SGabor Kovesdan 993c66bbc91SGabor Kovesdan fix_obsolete_keys(&argc, argv); 994c66bbc91SGabor Kovesdan 995c66bbc91SGabor Kovesdan while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL)) 996c66bbc91SGabor Kovesdan != -1)) { 997c66bbc91SGabor Kovesdan 998c66bbc91SGabor Kovesdan check_mutually_exclusive_flags(c, mef_flags); 999c66bbc91SGabor Kovesdan 1000c66bbc91SGabor Kovesdan if (!set_sort_modifier(sm, c)) { 1001c66bbc91SGabor Kovesdan 1002c66bbc91SGabor Kovesdan switch (c) { 1003c66bbc91SGabor Kovesdan case 'c': 1004c66bbc91SGabor Kovesdan sort_opts_vals.cflag = true; 1005c66bbc91SGabor Kovesdan if (optarg) { 1006c66bbc91SGabor Kovesdan if (!strcmp(optarg, "diagnose-first")) 1007c66bbc91SGabor Kovesdan ; 1008c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "silent") || 1009c66bbc91SGabor Kovesdan !strcmp(optarg, "quiet")) 1010c66bbc91SGabor Kovesdan sort_opts_vals.csilentflag = true; 1011c66bbc91SGabor Kovesdan else if (*optarg) 1012c66bbc91SGabor Kovesdan unknown(optarg); 1013c66bbc91SGabor Kovesdan } 1014c66bbc91SGabor Kovesdan break; 1015c66bbc91SGabor Kovesdan case 'C': 1016c66bbc91SGabor Kovesdan sort_opts_vals.cflag = true; 1017c66bbc91SGabor Kovesdan sort_opts_vals.csilentflag = true; 1018c66bbc91SGabor Kovesdan break; 1019c66bbc91SGabor Kovesdan case 'k': 1020c66bbc91SGabor Kovesdan { 1021c66bbc91SGabor Kovesdan sort_opts_vals.complex_sort = true; 1022c66bbc91SGabor Kovesdan sort_opts_vals.kflag = true; 1023c66bbc91SGabor Kovesdan 1024c66bbc91SGabor Kovesdan keys_num++; 1025c66bbc91SGabor Kovesdan keys = sort_realloc(keys, keys_num * 1026c66bbc91SGabor Kovesdan sizeof(struct key_specs)); 1027c66bbc91SGabor Kovesdan memset(&(keys[keys_num - 1]), 0, 1028c66bbc91SGabor Kovesdan sizeof(struct key_specs)); 1029c66bbc91SGabor Kovesdan 1030c66bbc91SGabor Kovesdan if (parse_k(optarg, &(keys[keys_num - 1])) 1031c66bbc91SGabor Kovesdan < 0) { 1032f187ff08SGabor Kovesdan errc(2, EINVAL, "-k %s", optarg); 1033c66bbc91SGabor Kovesdan } 1034c66bbc91SGabor Kovesdan 1035c66bbc91SGabor Kovesdan break; 1036c66bbc91SGabor Kovesdan } 1037c66bbc91SGabor Kovesdan case 'm': 1038c66bbc91SGabor Kovesdan sort_opts_vals.mflag = true; 1039c66bbc91SGabor Kovesdan break; 1040c66bbc91SGabor Kovesdan case 'o': 1041f50d9b2fSGabor Kovesdan outfile = sort_realloc(outfile, (strlen(optarg) + 1)); 1042f50d9b2fSGabor Kovesdan strcpy(outfile, optarg); 1043c66bbc91SGabor Kovesdan break; 1044c66bbc91SGabor Kovesdan case 's': 1045c66bbc91SGabor Kovesdan sort_opts_vals.sflag = true; 1046c66bbc91SGabor Kovesdan break; 1047c66bbc91SGabor Kovesdan case 'S': 1048c66bbc91SGabor Kovesdan available_free_memory = 1049c66bbc91SGabor Kovesdan parse_memory_buffer_value(optarg); 1050c66bbc91SGabor Kovesdan break; 1051c66bbc91SGabor Kovesdan case 'T': 1052c66bbc91SGabor Kovesdan tmpdir = sort_strdup(optarg); 1053c66bbc91SGabor Kovesdan break; 1054c66bbc91SGabor Kovesdan case 't': 10555ca724dcSGabor Kovesdan while (strlen(optarg) > 1) { 10565ca724dcSGabor Kovesdan if (optarg[0] != '\\') { 1057f187ff08SGabor Kovesdan errc(2, EINVAL, "%s", optarg); 1058c66bbc91SGabor Kovesdan } 10595ca724dcSGabor Kovesdan optarg += 1; 10605ca724dcSGabor Kovesdan if (*optarg == '0') { 1061c66bbc91SGabor Kovesdan *optarg = 0; 10625ca724dcSGabor Kovesdan break; 10635ca724dcSGabor Kovesdan } 1064c66bbc91SGabor Kovesdan } 1065c66bbc91SGabor Kovesdan sort_opts_vals.tflag = true; 1066c66bbc91SGabor Kovesdan sort_opts_vals.field_sep = btowc(optarg[0]); 1067c66bbc91SGabor Kovesdan if (sort_opts_vals.field_sep == WEOF) { 1068c66bbc91SGabor Kovesdan errno = EINVAL; 1069c66bbc91SGabor Kovesdan err(2, NULL); 1070c66bbc91SGabor Kovesdan } 1071c66bbc91SGabor Kovesdan if (!gnusort_numeric_compatibility) { 1072c66bbc91SGabor Kovesdan if (symbol_decimal_point == sort_opts_vals.field_sep) 1073c66bbc91SGabor Kovesdan symbol_decimal_point = WEOF; 1074c66bbc91SGabor Kovesdan if (symbol_thousands_sep == sort_opts_vals.field_sep) 1075c66bbc91SGabor Kovesdan symbol_thousands_sep = WEOF; 1076c66bbc91SGabor Kovesdan if (symbol_negative_sign == sort_opts_vals.field_sep) 1077c66bbc91SGabor Kovesdan symbol_negative_sign = WEOF; 1078c66bbc91SGabor Kovesdan if (symbol_positive_sign == sort_opts_vals.field_sep) 1079c66bbc91SGabor Kovesdan symbol_positive_sign = WEOF; 1080c66bbc91SGabor Kovesdan } 1081c66bbc91SGabor Kovesdan break; 1082c66bbc91SGabor Kovesdan case 'u': 1083c66bbc91SGabor Kovesdan sort_opts_vals.uflag = true; 1084c66bbc91SGabor Kovesdan /* stable sort for the correct unique val */ 1085c66bbc91SGabor Kovesdan sort_opts_vals.sflag = true; 1086c66bbc91SGabor Kovesdan break; 1087c66bbc91SGabor Kovesdan case 'z': 1088c66bbc91SGabor Kovesdan sort_opts_vals.zflag = true; 1089c66bbc91SGabor Kovesdan break; 1090c66bbc91SGabor Kovesdan case SORT_OPT: 1091c66bbc91SGabor Kovesdan if (optarg) { 1092c66bbc91SGabor Kovesdan if (!strcmp(optarg, "general-numeric")) 1093c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'g'); 1094c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "human-numeric")) 1095c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'h'); 1096c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "numeric")) 1097c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'n'); 1098c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "month")) 1099c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'M'); 1100c66bbc91SGabor Kovesdan else if (!strcmp(optarg, "random")) 1101c66bbc91SGabor Kovesdan set_sort_modifier(sm, 'R'); 1102c66bbc91SGabor Kovesdan else 1103c66bbc91SGabor Kovesdan unknown(optarg); 1104c66bbc91SGabor Kovesdan } 1105c66bbc91SGabor Kovesdan break; 1106c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 11075d5151aeSGabor Kovesdan case PARALLEL_OPT: 1108c66bbc91SGabor Kovesdan nthreads = (size_t)(atoi(optarg)); 1109c66bbc91SGabor Kovesdan if (nthreads < 1) 1110c66bbc91SGabor Kovesdan nthreads = 1; 1111c66bbc91SGabor Kovesdan if (nthreads > 1024) 1112c66bbc91SGabor Kovesdan nthreads = 1024; 1113c66bbc91SGabor Kovesdan break; 1114c66bbc91SGabor Kovesdan #endif 1115c66bbc91SGabor Kovesdan case QSORT_OPT: 1116c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_QSORT; 1117c66bbc91SGabor Kovesdan break; 1118c66bbc91SGabor Kovesdan case MERGESORT_OPT: 1119c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_MERGESORT; 1120c66bbc91SGabor Kovesdan break; 11215ca724dcSGabor Kovesdan case MMAP_OPT: 11225ca724dcSGabor Kovesdan use_mmap = true; 11235ca724dcSGabor Kovesdan break; 1124c66bbc91SGabor Kovesdan case HEAPSORT_OPT: 1125c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_HEAPSORT; 1126c66bbc91SGabor Kovesdan break; 1127c66bbc91SGabor Kovesdan case RADIXSORT_OPT: 1128c66bbc91SGabor Kovesdan sort_opts_vals.sort_method = SORT_RADIXSORT; 1129c66bbc91SGabor Kovesdan break; 1130c66bbc91SGabor Kovesdan case RANDOMSOURCE_OPT: 1131c66bbc91SGabor Kovesdan random_source = strdup(optarg); 1132c66bbc91SGabor Kovesdan break; 1133c66bbc91SGabor Kovesdan case COMPRESSPROGRAM_OPT: 1134c66bbc91SGabor Kovesdan compress_program = strdup(optarg); 1135c66bbc91SGabor Kovesdan break; 1136c66bbc91SGabor Kovesdan case FF_OPT: 1137c66bbc91SGabor Kovesdan read_fns_from_file0(optarg); 1138c66bbc91SGabor Kovesdan break; 1139c66bbc91SGabor Kovesdan case BS_OPT: 1140c66bbc91SGabor Kovesdan { 1141c66bbc91SGabor Kovesdan errno = 0; 1142c66bbc91SGabor Kovesdan long mof = strtol(optarg, NULL, 10); 1143c66bbc91SGabor Kovesdan if (errno != 0) 1144f187ff08SGabor Kovesdan err(2, "--batch-size"); 1145c66bbc91SGabor Kovesdan if (mof >= 2) 1146c66bbc91SGabor Kovesdan max_open_files = (size_t) mof + 1; 1147c66bbc91SGabor Kovesdan } 1148c66bbc91SGabor Kovesdan break; 1149c66bbc91SGabor Kovesdan case VERSION_OPT: 1150c66bbc91SGabor Kovesdan printf("%s\n", VERSION); 1151c66bbc91SGabor Kovesdan exit(EXIT_SUCCESS); 1152c66bbc91SGabor Kovesdan /* NOTREACHED */ 1153c66bbc91SGabor Kovesdan break; 1154c66bbc91SGabor Kovesdan case DEBUG_OPT: 1155c66bbc91SGabor Kovesdan debug_sort = true; 1156c66bbc91SGabor Kovesdan break; 1157c66bbc91SGabor Kovesdan case HELP_OPT: 1158c66bbc91SGabor Kovesdan usage(false); 1159c66bbc91SGabor Kovesdan /* NOTREACHED */ 1160c66bbc91SGabor Kovesdan break; 1161c66bbc91SGabor Kovesdan default: 1162c66bbc91SGabor Kovesdan usage(true); 1163c66bbc91SGabor Kovesdan /* NOTREACHED */ 1164c66bbc91SGabor Kovesdan } 1165c66bbc91SGabor Kovesdan } 1166c66bbc91SGabor Kovesdan } 1167c66bbc91SGabor Kovesdan 1168c66bbc91SGabor Kovesdan argc -= optind; 1169c66bbc91SGabor Kovesdan argv += optind; 1170c66bbc91SGabor Kovesdan 1171c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 1172c66bbc91SGabor Kovesdan catalog = catopen("sort", NL_CAT_LOCALE); 1173c66bbc91SGabor Kovesdan #endif 1174c66bbc91SGabor Kovesdan 1175c66bbc91SGabor Kovesdan if (sort_opts_vals.cflag && sort_opts_vals.mflag) 11768818aa39SGabor Kovesdan errx(1, "%c:%c: %s", 'm', 'c', getstr(1)); 1177c66bbc91SGabor Kovesdan 1178c66bbc91SGabor Kovesdan #ifndef WITHOUT_NLS 1179c66bbc91SGabor Kovesdan catclose(catalog); 1180c66bbc91SGabor Kovesdan #endif 1181c66bbc91SGabor Kovesdan 1182c66bbc91SGabor Kovesdan if (keys_num == 0) { 1183c66bbc91SGabor Kovesdan keys_num = 1; 1184c66bbc91SGabor Kovesdan keys = sort_realloc(keys, sizeof(struct key_specs)); 1185c66bbc91SGabor Kovesdan memset(&(keys[0]), 0, sizeof(struct key_specs)); 1186c66bbc91SGabor Kovesdan keys[0].c1 = 1; 1187c66bbc91SGabor Kovesdan keys[0].pos1b = default_sort_mods->bflag; 1188c66bbc91SGabor Kovesdan keys[0].pos2b = default_sort_mods->bflag; 1189c66bbc91SGabor Kovesdan memcpy(&(keys[0].sm), default_sort_mods, 1190c66bbc91SGabor Kovesdan sizeof(struct sort_mods)); 1191c66bbc91SGabor Kovesdan } 1192c66bbc91SGabor Kovesdan 1193c66bbc91SGabor Kovesdan for (size_t i = 0; i < keys_num; i++) { 1194c66bbc91SGabor Kovesdan struct key_specs *ks; 1195c66bbc91SGabor Kovesdan 1196c66bbc91SGabor Kovesdan ks = &(keys[i]); 1197c66bbc91SGabor Kovesdan 1198c66bbc91SGabor Kovesdan if (sort_modifier_empty(&(ks->sm)) && !(ks->pos1b) && 1199c66bbc91SGabor Kovesdan !(ks->pos2b)) { 1200c66bbc91SGabor Kovesdan ks->pos1b = sm->bflag; 1201c66bbc91SGabor Kovesdan ks->pos2b = sm->bflag; 1202c66bbc91SGabor Kovesdan memcpy(&(ks->sm), sm, sizeof(struct sort_mods)); 1203c66bbc91SGabor Kovesdan } 1204c66bbc91SGabor Kovesdan 1205c66bbc91SGabor Kovesdan ks->sm.func = get_sort_func(&(ks->sm)); 1206c66bbc91SGabor Kovesdan } 1207c66bbc91SGabor Kovesdan 1208e8da8c74SGabor Kovesdan if (argv_from_file0) { 1209c66bbc91SGabor Kovesdan argc = argc_from_file0; 1210c66bbc91SGabor Kovesdan argv = argv_from_file0; 1211c66bbc91SGabor Kovesdan } 1212c66bbc91SGabor Kovesdan 1213c66bbc91SGabor Kovesdan if (debug_sort) { 121455444243SGabor Kovesdan printf("Memory to be used for sorting: %llu\n",available_free_memory); 1215c66bbc91SGabor Kovesdan #if defined(SORT_THREADS) 121655444243SGabor Kovesdan printf("Number of CPUs: %d\n",(int)ncpu); 1217c66bbc91SGabor Kovesdan nthreads = 1; 1218c66bbc91SGabor Kovesdan #endif 1219c66bbc91SGabor Kovesdan printf("Using collate rules of %s locale\n", 1220c66bbc91SGabor Kovesdan setlocale(LC_COLLATE, NULL)); 1221c66bbc91SGabor Kovesdan if (byte_sort) 1222c66bbc91SGabor Kovesdan printf("Byte sort is used\n"); 1223c66bbc91SGabor Kovesdan if (print_symbols_on_debug) { 1224c66bbc91SGabor Kovesdan printf("Decimal Point: <%lc>\n", symbol_decimal_point); 1225c66bbc91SGabor Kovesdan if (symbol_thousands_sep) 1226c66bbc91SGabor Kovesdan printf("Thousands separator: <%lc>\n", 1227c66bbc91SGabor Kovesdan symbol_thousands_sep); 1228c66bbc91SGabor Kovesdan printf("Positive sign: <%lc>\n", symbol_positive_sign); 1229c66bbc91SGabor Kovesdan printf("Negative sign: <%lc>\n", symbol_negative_sign); 1230c66bbc91SGabor Kovesdan } 1231c66bbc91SGabor Kovesdan } 1232c66bbc91SGabor Kovesdan 1233c66bbc91SGabor Kovesdan set_random_seed(); 1234c66bbc91SGabor Kovesdan 1235c66bbc91SGabor Kovesdan /* Case when the outfile equals one of the input files: */ 1236c66bbc91SGabor Kovesdan if (strcmp(outfile, "-")) { 1237c66bbc91SGabor Kovesdan 1238c66bbc91SGabor Kovesdan for(int i = 0; i < argc; ++i) { 1239c66bbc91SGabor Kovesdan if (strcmp(argv[i], outfile) == 0) { 1240c66bbc91SGabor Kovesdan real_outfile = sort_strdup(outfile); 1241c66bbc91SGabor Kovesdan for(;;) { 1242c66bbc91SGabor Kovesdan char* tmp = sort_malloc(strlen(outfile) + 1243c66bbc91SGabor Kovesdan strlen(".tmp") + 1); 1244c66bbc91SGabor Kovesdan 1245c66bbc91SGabor Kovesdan strcpy(tmp, outfile); 1246c66bbc91SGabor Kovesdan strcpy(tmp + strlen(tmp), ".tmp"); 1247c66bbc91SGabor Kovesdan sort_free(outfile); 1248c66bbc91SGabor Kovesdan outfile = tmp; 1249c66bbc91SGabor Kovesdan if (access(outfile, F_OK) < 0) 1250c66bbc91SGabor Kovesdan break; 1251c66bbc91SGabor Kovesdan } 1252c66bbc91SGabor Kovesdan tmp_file_atexit(outfile); 1253c66bbc91SGabor Kovesdan } 1254c66bbc91SGabor Kovesdan } 1255c66bbc91SGabor Kovesdan } 1256c66bbc91SGabor Kovesdan 12575ca724dcSGabor Kovesdan #if defined(SORT_THREADS) 12585ca724dcSGabor Kovesdan if ((argc < 1) || (strcmp(outfile, "-") == 0) || (*outfile == 0)) 12595ca724dcSGabor Kovesdan nthreads = 1; 12605ca724dcSGabor Kovesdan #endif 12615ca724dcSGabor Kovesdan 1262c66bbc91SGabor Kovesdan if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) { 1263c66bbc91SGabor Kovesdan struct file_list fl; 1264c66bbc91SGabor Kovesdan struct sort_list list; 1265c66bbc91SGabor Kovesdan 1266c66bbc91SGabor Kovesdan sort_list_init(&list); 1267c66bbc91SGabor Kovesdan file_list_init(&fl, true); 1268c66bbc91SGabor Kovesdan 1269c66bbc91SGabor Kovesdan if (argc < 1) 1270c66bbc91SGabor Kovesdan procfile("-", &list, &fl); 1271c66bbc91SGabor Kovesdan else { 1272c66bbc91SGabor Kovesdan while (argc > 0) { 1273c66bbc91SGabor Kovesdan procfile(*argv, &list, &fl); 1274c66bbc91SGabor Kovesdan --argc; 1275c66bbc91SGabor Kovesdan ++argv; 1276c66bbc91SGabor Kovesdan } 1277c66bbc91SGabor Kovesdan } 1278c66bbc91SGabor Kovesdan 1279c66bbc91SGabor Kovesdan if (fl.count < 1) 1280c66bbc91SGabor Kovesdan sort_list_to_file(&list, outfile); 1281c66bbc91SGabor Kovesdan else { 1282c66bbc91SGabor Kovesdan if (list.count > 0) { 1283c66bbc91SGabor Kovesdan char *flast = new_tmp_file_name(); 1284c66bbc91SGabor Kovesdan 1285c66bbc91SGabor Kovesdan sort_list_to_file(&list, flast); 1286c66bbc91SGabor Kovesdan file_list_add(&fl, flast, false); 1287c66bbc91SGabor Kovesdan } 1288c66bbc91SGabor Kovesdan merge_files(&fl, outfile); 1289c66bbc91SGabor Kovesdan } 1290c66bbc91SGabor Kovesdan 1291c66bbc91SGabor Kovesdan file_list_clean(&fl); 1292c66bbc91SGabor Kovesdan 1293c66bbc91SGabor Kovesdan /* 1294c66bbc91SGabor Kovesdan * We are about to exit the program, so we can ignore 1295c66bbc91SGabor Kovesdan * the clean-up for speed 1296c66bbc91SGabor Kovesdan * 1297c66bbc91SGabor Kovesdan * sort_list_clean(&list); 1298c66bbc91SGabor Kovesdan */ 1299c66bbc91SGabor Kovesdan 1300c66bbc91SGabor Kovesdan } else if (sort_opts_vals.cflag) { 1301c66bbc91SGabor Kovesdan result = (argc == 0) ? (check("-")) : (check(*argv)); 1302c66bbc91SGabor Kovesdan } else if (sort_opts_vals.mflag) { 1303c66bbc91SGabor Kovesdan struct file_list fl; 1304c66bbc91SGabor Kovesdan 1305c66bbc91SGabor Kovesdan file_list_init(&fl, false); 1306c66bbc91SGabor Kovesdan file_list_populate(&fl, argc, argv, true); 1307c66bbc91SGabor Kovesdan merge_files(&fl, outfile); 1308c66bbc91SGabor Kovesdan file_list_clean(&fl); 1309c66bbc91SGabor Kovesdan } 1310c66bbc91SGabor Kovesdan 1311c66bbc91SGabor Kovesdan if (real_outfile) { 1312c66bbc91SGabor Kovesdan unlink(real_outfile); 1313c66bbc91SGabor Kovesdan if (rename(outfile, real_outfile) < 0) 1314c66bbc91SGabor Kovesdan err(2, NULL); 1315c66bbc91SGabor Kovesdan sort_free(real_outfile); 1316c66bbc91SGabor Kovesdan } 1317c66bbc91SGabor Kovesdan 1318c66bbc91SGabor Kovesdan sort_free(outfile); 1319c66bbc91SGabor Kovesdan 1320c66bbc91SGabor Kovesdan return (result); 1321c66bbc91SGabor Kovesdan } 1322