?? sort.c
字號:
{ int i = n_temp_files; struct tempnode *node; char **tempfiles = (char **) xmalloc (n_temp_files * sizeof (char *)); for (node = temphead; i > 0; node = node->next) tempfiles[--i] = node->name; merge (tempfiles, n_temp_files, NMERGE, output_file); free ((char *) tempfiles); }}/* Insert key KEY at the end of the key list. */static voidinsertkey (struct keyfield *key){ struct keyfield **p; for (p = &keylist; *p; p = &(*p)->next) continue; *p = key; key->next = NULL;}/* Report a bad field specification SPEC, with extra info MSGID. */static void badfieldspec (char const *, char const *) ATTRIBUTE_NORETURN;static voidbadfieldspec (char const *spec, char const *msgid){ error (SORT_FAILURE, 0, _("%s: invalid field specification `%s'"), _(msgid), spec); abort ();}/* Parse the leading integer in STRING and store the resulting value (which must fit into size_t) into *VAL. Return the address of the suffix after the integer. If MSGID is NULL, return NULL after failure; otherwise, report MSGID and exit on failure. */static char const *parse_field_count (char const *string, size_t *val, char const *msgid){ char *suffix; uintmax_t n; switch (xstrtoumax (string, &suffix, 10, &n, "")) { case LONGINT_OK: case LONGINT_INVALID_SUFFIX_CHAR: *val = n; if (*val == n) break; /* Fall through. */ case LONGINT_OVERFLOW: if (msgid) error (SORT_FAILURE, 0, _("%s: count `%.*s' too large"), _(msgid), (int) (suffix - string), string); return NULL; case LONGINT_INVALID: if (msgid) error (SORT_FAILURE, 0, _("%s: invalid count at start of `%s'"), _(msgid), string); return NULL; } return suffix;}/* Handle interrupts and hangups. */static voidsighandler (int sig){#ifndef SA_NOCLDSTOP signal (sig, SIG_IGN);#endif cleanup ();#ifdef SA_NOCLDSTOP { struct sigaction sigact; sigact.sa_handler = SIG_DFL; sigemptyset (&sigact.sa_mask); sigact.sa_flags = 0; sigaction (sig, &sigact, NULL); }#else signal (sig, SIG_DFL);#endif raise (sig);}/* Set the ordering options for KEY specified in S. Return the address of the first character in S that is not a valid ordering option. BLANKTYPE is the kind of blanks that 'b' should skip. */static char *set_ordering (register const char *s, struct keyfield *key, enum blanktype blanktype){ while (*s) { switch (*s) { case 'b': if (blanktype == bl_start || blanktype == bl_both) key->skipsblanks = 1; if (blanktype == bl_end || blanktype == bl_both) key->skipeblanks = 1; break; case 'd': key->ignore = nondictionary; break; case 'f': key->translate = fold_toupper; break; case 'g': key->general_numeric = 1; break; case 'i': key->ignore = nonprinting; break; case 'M': key->month = 1; break; case 'n': key->numeric = 1; break; case 'r': key->reverse = 1; break; default: return (char *) s; } ++s; } return (char *) s;}static struct keyfield *new_key (void){ struct keyfield *key = (struct keyfield *) xcalloc (1, sizeof *key); key->eword = -1; return key;}intmain (int argc, char **argv){ struct keyfield *key; struct keyfield gkey; char const *s; int c = 0; int checkonly = 0, mergeonly = 0, nfiles = 0; int posix_pedantic = (getenv ("POSIXLY_CORRECT") != NULL); bool obsolete_usage = (posix2_version () < 200112); char const *short_options = (obsolete_usage ? COMMON_SHORT_OPTIONS "y::" : COMMON_SHORT_OPTIONS "y:"); char *minus = "-", **files; char const *outfile = minus; static int const sigs[] = { SIGHUP, SIGINT, SIGPIPE, SIGTERM }; unsigned nsigs = sizeof sigs / sizeof *sigs;#ifdef SA_NOCLDSTOP struct sigaction oldact, newact;#endif program_name = argv[0]; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (cleanup); hard_LC_COLLATE = hard_locale (LC_COLLATE);#if HAVE_NL_LANGINFO hard_LC_TIME = hard_locale (LC_TIME);#endif#if HAVE_SETLOCALE /* Let's get locale's representation of the decimal point */ { struct lconv *lconvp = localeconv (); /* If the locale doesn't define a decimal point, or if the decimal point is multibyte, use the C decimal point. We don't support multibyte decimal points yet. */ decimal_point = *lconvp->decimal_point; if (! decimal_point || lconvp->decimal_point[1]) decimal_point = C_DECIMAL_POINT; /* We don't support multibyte thousands separators yet. */ th_sep = *lconvp->thousands_sep; if (! th_sep || lconvp->thousands_sep[1]) th_sep = CHAR_MAX + 1; }#endif have_read_stdin = 0; inittables (); /* Change the way library functions fail. */ xalloc_exit_failure = SORT_FAILURE; xmemcoll_exit_failure = SORT_FAILURE;#ifdef SA_NOCLDSTOP { unsigned i; sigemptyset (&caught_signals); for (i = 0; i < nsigs; i++) sigaddset (&caught_signals, sigs[i]); newact.sa_handler = sighandler; newact.sa_mask = caught_signals; newact.sa_flags = 0; }#endif { unsigned i; for (i = 0; i < nsigs; i++) { int sig = sigs[i];#ifdef SA_NOCLDSTOP if (oldact.sa_handler != SIG_IGN) sigaction (sig, &newact, NULL);#else if (signal (sig, SIG_IGN) != SIG_IGN) signal (sig, sighandler);#endif } } gkey.sword = gkey.eword = -1; gkey.ignore = NULL; gkey.translate = NULL; gkey.numeric = gkey.general_numeric = gkey.month = gkey.reverse = 0; gkey.skipsblanks = gkey.skipeblanks = 0; files = (char **) xmalloc (sizeof (char *) * argc); for (;;) { /* Parse an operand as a file after "--" was seen; or if pedantic and a file was seen, unless the POSIX version predates 1003.1-2001 and -c was not seen and the operand is "-o FILE" or "-oFILE". */ if (c == -1 || (posix_pedantic && nfiles != 0 && ! (obsolete_usage && ! checkonly && optind != argc && argv[optind][0] == '-' && argv[optind][1] == 'o' && (argv[optind][2] || optind + 1 != argc))) || ((c = getopt_long (argc, argv, short_options, long_options, NULL)) == -1)) { if (optind == argc) break; files[nfiles++] = argv[optind++]; } else switch (c) { case 1: key = NULL; if (obsolete_usage && optarg[0] == '+') { /* Treat +POS1 [-POS2] as a key if possible; but silently treat an operand as a file if it is not a valid +POS1. */ key = new_key (); s = parse_field_count (optarg + 1, &key->sword, NULL); if (s && *s == '.') s = parse_field_count (s + 1, &key->schar, NULL); if (! (key->sword | key->schar)) key->sword = -1; if (! s || *set_ordering (s, key, bl_start)) { free (key); key = NULL; } else { if (optind != argc && argv[optind][0] == '-' && ISDIGIT (argv[optind][1])) { char const *optarg1 = argv[optind++]; s = parse_field_count (optarg1 + 1, &key->eword, N_("invalid number after `-'")); if (*s == '.') s = parse_field_count (s + 1, &key->echar, N_("invalid number after `.'")); if (*set_ordering (s, key, bl_end)) badfieldspec (optarg1, N_("stray character in field spec")); } insertkey (key); } } if (! key) files[nfiles++] = optarg; break; case 'b': case 'd': case 'f': case 'g': case 'i': case 'M': case 'n': case 'r': { char str[2]; str[0] = c; str[1] = '\0'; set_ordering (str, &gkey, bl_both); } break; case 'c': checkonly = 1; break; case 'k': key = new_key (); /* Get POS1. */ s = parse_field_count (optarg, &key->sword, N_("invalid number at field start")); if (! key->sword--) { /* Provoke with `sort -k0' */ badfieldspec (optarg, N_("field number is zero")); } if (*s == '.') { s = parse_field_count (s + 1, &key->schar, N_("invalid number after `.'")); if (! key->schar--) { /* Provoke with `sort -k1.0' */ badfieldspec (optarg, N_("character offset is zero")); } } if (! (key->sword | key->schar)) key->sword = -1; s = set_ordering (s, key, bl_start); if (*s != ',') { key->eword = -1; key->echar = 0; } else { /* Get POS2. */ s = parse_field_count (s + 1, &key->eword, N_("invalid number after `,'")); if (! key->eword--) { /* Provoke with `sort -k1,0' */ badfieldspec (optarg, N_("field number is zero")); } if (*s == '.') s = parse_field_count (s + 1, &key->echar, N_("invalid number after `.'")); else { /* `-k 2,3' is equivalent to `+1 -3'. */ key->eword++; } s = set_ordering (s, key, bl_end); } if (*s) badfieldspec (optarg, N_("stray character in field spec")); insertkey (key); break; case 'm': mergeonly = 1; break; case 'o': outfile = optarg; break; case 's': stable = 1; break; case 'S': specify_sort_size (optarg); break; case 't': tab = optarg[0]; if (tab && optarg[1]) { /* Provoke with `sort -txx'. Complain about "multi-character tab" instead of "multibyte tab", so that the diagnostic's wording does not need to be changed once multibyte characters are supported. */ error (SORT_FAILURE, 0, _("multi-character tab `%s'"), optarg); } break; case 'T': add_temp_dir (optarg); break; case 'u': unique = 1; break; case 'y': /* Accept and ignore e.g. -y0 for compatibility with Solaris 2.x through Solaris 7. -y is marked as obsolete starting with Solaris 8. */ break; case 'z': eolchar = 0; break; case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); default: usage (SORT_FAILURE); } } /* Inheritance of global options to individual keys. */ for (key = keylist; key; key = key->next) if (!key->ignore && !key->translate && !key->skipsblanks && !key->reverse && !key->skipeblanks && !key->month && !key->numeric && !key->general_numeric) { key->ignore = gkey.ignore; key->translate = gkey.translate; key->skipsblanks = gkey.skipsblanks; key->skipeblanks = gkey.skipeblanks; key->month = gkey.month; key->numeric = gkey.numeric; key->general_numeric = gkey.general_numeric; key->reverse = gkey.reverse; } if (!keylist && (gkey.ignore || gkey.translate || gkey.skipsblanks || gkey.skipeblanks || gkey.month || gkey.numeric || gkey.general_numeric)) insertkey (&gkey); reverse = gkey.reverse; if (temp_dir_count == 0) { char const *tmp_dir = getenv ("TMPDIR"); add_temp_dir (tmp_dir ? tmp_dir : DEFAULT_TMPDIR); } if (nfiles == 0) { nfiles = 1; files = − } if (checkonly) { if (nfiles > 1) error (SORT_FAILURE, 0, _("extra operand `%s' not allowed with -c"), files[1]); /* POSIX requires that sort return 1 IFF invoked with -c and the input is not properly sorted. */ exit (check (files, nfiles) == 0 ? EXIT_SUCCESS : SORT_OUT_OF_ORDER); } if (mergeonly) { int max_merge = first_same_file (files, MIN (nfiles, NMERGE), outfile); merge (files, nfiles, max_merge, outfile); } else sort (files, nfiles, outfile); if (have_read_stdin && fclose (stdin) == EOF) die (_("close failed"), "-"); exit (EXIT_SUCCESS);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -