From 8336f080cb921a8c64c4cea59463363144e97c1d Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sun, 7 Jan 2007 00:21:41 +0000 Subject: diff: small optimizations; do not try to diff non-seekable stream (currently we don't support that) sort: fixes. testsuites fixed: sort with non-default leading delim 1 sort with non-default leading delim 2 sort key doesn't strip leading blanks, disables fallback global sort --- coreutils/diff.c | 90 +++++++++++++++++++++++++--------------------------- coreutils/sort.c | 11 ++----- testsuite/sort.tests | 42 +++++++++++++++++------- 3 files changed, 77 insertions(+), 66 deletions(-) diff --git a/coreutils/diff.c b/coreutils/diff.c index 923239ab1..2ce681aff 100644 --- a/coreutils/diff.c +++ b/coreutils/diff.c @@ -67,8 +67,12 @@ /* XXX: FIXME: the following variables should be static, but gcc currently * creates a much bigger object if we do this. [which version of gcc? --vda] */ -int context, status; -char *start, *label[2]; +/* This is the default number of lines of context. */ +int context = 3; +int status; +char *start; +const char *label1; +const char *label2; struct stat stb1, stb2; char **dl; USE_FEATURE_DIFF_DIR(static int dl_count;) @@ -276,7 +280,8 @@ static void prepare(int i, FILE * fd, off_t filesize) sz = 100; p = xmalloc((sz + 3) * sizeof(struct line)); - for (j = 0; (h = readhash(fd));) { + j = 0; + while ((h = readhash(fd))) { if (j == sz) { sz = sz * 3 / 2; p = xrealloc(p, (sz + 3) * sizeof(struct line)); @@ -735,14 +740,14 @@ static void dump_unified_vec(FILE * f1, FILE * f2) static void print_header(const char *file1, const char *file2) { - if (label[0] != NULL) - printf("%s %s\n", "---", label[0]); + if (label1) + printf("--- %s\n", label1); else - printf("%s %s\t%s", "---", file1, ctime(&stb1.st_mtime)); - if (label[1] != NULL) - printf("%s %s\n", "+++", label[1]); + printf("--- %s\t%s", file1, ctime(&stb1.st_mtime)); + if (label2) + printf("+++ %s\n", label2); else - printf("%s %s\t%s", "+++", file2, ctime(&stb2.st_mtime)); + printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime)); } @@ -771,8 +776,7 @@ static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a, max_context <<= 1; context_vec_start = xrealloc(context_vec_start, - max_context * - sizeof(struct context_vec)); + max_context * sizeof(struct context_vec)); context_vec_end = context_vec_start + max_context; context_vec_ptr = context_vec_start + offset; } @@ -894,8 +898,8 @@ static int diffreg(char *ofile1, char *ofile2, int flags) { char *file1 = ofile1; char *file2 = ofile2; - FILE *f1 = NULL; - FILE *f2 = NULL; + FILE *f1; + FILE *f2; int rval = D_SAME; int i; @@ -912,12 +916,18 @@ static int diffreg(char *ofile1, char *ofile2, int flags) f1 = xfopen(bb_dev_null, "r"); else if (NOT_LONE_DASH(file1)) f1 = xfopen(file1, "r"); - if (flags & D_EMPTY2) f2 = xfopen(bb_dev_null, "r"); else if (NOT_LONE_DASH(file2)) f2 = xfopen(file2, "r"); +/* We can't diff non-seekable stream - we use rewind(), fseek(). + * This can be fixed (volunteers?). + * Meanwhile we should check it here by stat'ing input fds, + * but I am lazy and check that in main() instead. + * Check in main won't catch "diffing fifos buried in subdirectories" + * failure scenario - not very likely in real life... */ + i = files_differ(f1, f2, flags); if (i == 0) goto closem; @@ -1169,48 +1179,32 @@ int diff_main(int argc, char **argv) char *f1, *f2; llist_t *L_arg = NULL; - opt_complementary = "L::"; + /* exactly 2 params; collect multiple -L