From b3b4eccfd36c8dfed8155299b4bb181283388646 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Fri, 3 Apr 2015 10:37:25 +0000 Subject: The combination of -c and -o is not specified by POSIX. In fact, the call "sort -o file -c file" has unspecified behavior and would leave an empty file behind if it was sorted, the original file it was not. If -c (or -C) has been specified, only perform that action and ignore -o among other arguments. While at it, clean up check() internals. with input by and ok millert@ --- usr.bin/sort/file.c | 37 +++++++++---------------------------- usr.bin/sort/sort.c | 16 ++++++++-------- 2 files changed, 17 insertions(+), 36 deletions(-) (limited to 'usr.bin/sort') diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c index 1ae477e81ac..d7cf00460b0 100644 --- a/usr.bin/sort/file.c +++ b/usr.bin/sort/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.19 2015/04/02 22:14:51 deraadt Exp $ */ +/* $OpenBSD: file.c,v 1.20 2015/04/03 10:37:24 tobias Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan @@ -344,13 +344,13 @@ sort_list_dump(struct sort_list *l, const char *fn) int check(const char *fn) { - struct bwstring *s1, *s2, *s1disorder, *s2disorder; + struct bwstring *s1, *s2; struct file_reader *fr; struct keys_array *ka1, *ka2; int res; size_t pos, posdisorder; - s1 = s2 = s1disorder = s2disorder = NULL; + s1 = s2 = NULL; ka1 = ka2 = NULL; fr = file_reader_init(fn); @@ -359,10 +359,8 @@ check(const char *fn) pos = 1; posdisorder = 1; - if (fr == NULL) { + if (fr == NULL) err(2, "%s", fn); - goto end; - } s1 = file_reader_readline(fr); if (s1 == NULL) @@ -399,10 +397,10 @@ check(const char *fn) if ((sort_opts_vals.uflag && (cmp <= 0)) || (cmp < 0)) { if (!(sort_opts_vals.csilentflag)) { - s2disorder = bwsdup(s2); + bws_disorder_warnx(s2, fn, posdisorder); posdisorder = pos; if (debug_sort) - s1disorder = bwsdup(s1); + bws_disorder_warnx(s1, fn, posdisorder); } res = 1; goto end; @@ -432,16 +430,14 @@ end: sort_free(ka1); } - if (s1) - bwsfree(s1); + bwsfree(s1); if (ka2) { clean_keys_array(s2, ka2); sort_free(ka2); } - if (s2) - bwsfree(s2); + bwsfree(s2); if (fn == NULL || *fn == 0 || strcmp(fn, "-") == 0) { for (;;) { @@ -454,22 +450,7 @@ end: file_reader_free(fr); - if (s2disorder) { - bws_disorder_warnx(s2disorder, fn, posdisorder); - if (s1disorder) { - bws_disorder_warnx(s1disorder, fn, posdisorder); - if (s1disorder != s2disorder) - bwsfree(s1disorder); - } - bwsfree(s2disorder); - s1disorder = NULL; - s2disorder = NULL; - } - - if (res) - exit(res); - - return 0; + return res; } /* diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c index 2a959198770..49ea740f426 100644 --- a/usr.bin/sort/sort.c +++ b/usr.bin/sort/sort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sort.c,v 1.75 2015/04/03 10:07:25 tobias Exp $ */ +/* $OpenBSD: sort.c,v 1.76 2015/04/03 10:37:24 tobias Exp $ */ /*- * Copyright (C) 2009 Gabor Kovesdan @@ -857,12 +857,11 @@ int main(int argc, char *argv[]) { char *outfile, *real_outfile, *sflag; - int c, result; + int c; size_t i; bool mef_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { false, false, false, false, false, false }; - result = 0; outfile = "-"; real_outfile = NULL; sflag = NULL; @@ -1094,6 +1093,9 @@ main(int argc, char *argv[]) } } + if (sort_opts_vals.cflag) + return check(argc ? *argv : "-"); + set_random_seed(); /* Case when the outfile equals one of the input files: */ @@ -1120,7 +1122,7 @@ main(int argc, char *argv[]) } } - if (!sort_opts_vals.cflag && !sort_opts_vals.mflag) { + if (!sort_opts_vals.mflag) { struct file_list fl; struct sort_list list; @@ -1158,9 +1160,7 @@ main(int argc, char *argv[]) * sort_list_clean(&list); */ - } else if (sort_opts_vals.cflag) { - result = (argc == 0) ? (check("-")) : (check(*argv)); - } else if (sort_opts_vals.mflag) { + } else { struct file_list fl; file_list_init(&fl, false); @@ -1175,5 +1175,5 @@ main(int argc, char *argv[]) sort_free(outfile); } - return result; + return 0; } -- cgit v1.2.3