aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/diff.c90
-rw-r--r--coreutils/sort.c11
-rwxr-xr-xtestsuite/sort.tests42
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 @@
67 67
68/* XXX: FIXME: the following variables should be static, but gcc currently 68/* XXX: FIXME: the following variables should be static, but gcc currently
69 * creates a much bigger object if we do this. [which version of gcc? --vda] */ 69 * creates a much bigger object if we do this. [which version of gcc? --vda] */
70int context, status; 70/* This is the default number of lines of context. */
71char *start, *label[2]; 71int context = 3;
72int status;
73char *start;
74const char *label1;
75const char *label2;
72struct stat stb1, stb2; 76struct stat stb1, stb2;
73char **dl; 77char **dl;
74USE_FEATURE_DIFF_DIR(static int dl_count;) 78USE_FEATURE_DIFF_DIR(static int dl_count;)
@@ -276,7 +280,8 @@ static void prepare(int i, FILE * fd, off_t filesize)
276 sz = 100; 280 sz = 100;
277 281
278 p = xmalloc((sz + 3) * sizeof(struct line)); 282 p = xmalloc((sz + 3) * sizeof(struct line));
279 for (j = 0; (h = readhash(fd));) { 283 j = 0;
284 while ((h = readhash(fd))) {
280 if (j == sz) { 285 if (j == sz) {
281 sz = sz * 3 / 2; 286 sz = sz * 3 / 2;
282 p = xrealloc(p, (sz + 3) * sizeof(struct line)); 287 p = xrealloc(p, (sz + 3) * sizeof(struct line));
@@ -735,14 +740,14 @@ static void dump_unified_vec(FILE * f1, FILE * f2)
735 740
736static void print_header(const char *file1, const char *file2) 741static void print_header(const char *file1, const char *file2)
737{ 742{
738 if (label[0] != NULL) 743 if (label1)
739 printf("%s %s\n", "---", label[0]); 744 printf("--- %s\n", label1);
740 else 745 else
741 printf("%s %s\t%s", "---", file1, ctime(&stb1.st_mtime)); 746 printf("--- %s\t%s", file1, ctime(&stb1.st_mtime));
742 if (label[1] != NULL) 747 if (label2)
743 printf("%s %s\n", "+++", label[1]); 748 printf("+++ %s\n", label2);
744 else 749 else
745 printf("%s %s\t%s", "+++", file2, ctime(&stb2.st_mtime)); 750 printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime));
746} 751}
747 752
748 753
@@ -771,8 +776,7 @@ static void change(char *file1, FILE * f1, char *file2, FILE * f2, int a,
771 776
772 max_context <<= 1; 777 max_context <<= 1;
773 context_vec_start = xrealloc(context_vec_start, 778 context_vec_start = xrealloc(context_vec_start,
774 max_context * 779 max_context * sizeof(struct context_vec));
775 sizeof(struct context_vec));
776 context_vec_end = context_vec_start + max_context; 780 context_vec_end = context_vec_start + max_context;
777 context_vec_ptr = context_vec_start + offset; 781 context_vec_ptr = context_vec_start + offset;
778 } 782 }
@@ -894,8 +898,8 @@ static int diffreg(char *ofile1, char *ofile2, int flags)
894{ 898{
895 char *file1 = ofile1; 899 char *file1 = ofile1;
896 char *file2 = ofile2; 900 char *file2 = ofile2;
897 FILE *f1 = NULL; 901 FILE *f1;
898 FILE *f2 = NULL; 902 FILE *f2;
899 int rval = D_SAME; 903 int rval = D_SAME;
900 int i; 904 int i;
901 905
@@ -912,12 +916,18 @@ static int diffreg(char *ofile1, char *ofile2, int flags)
912 f1 = xfopen(bb_dev_null, "r"); 916 f1 = xfopen(bb_dev_null, "r");
913 else if (NOT_LONE_DASH(file1)) 917 else if (NOT_LONE_DASH(file1))
914 f1 = xfopen(file1, "r"); 918 f1 = xfopen(file1, "r");
915
916 if (flags & D_EMPTY2) 919 if (flags & D_EMPTY2)
917 f2 = xfopen(bb_dev_null, "r"); 920 f2 = xfopen(bb_dev_null, "r");
918 else if (NOT_LONE_DASH(file2)) 921 else if (NOT_LONE_DASH(file2))
919 f2 = xfopen(file2, "r"); 922 f2 = xfopen(file2, "r");
920 923
924/* We can't diff non-seekable stream - we use rewind(), fseek().
925 * This can be fixed (volunteers?).
926 * Meanwhile we should check it here by stat'ing input fds,
927 * but I am lazy and check that in main() instead.
928 * Check in main won't catch "diffing fifos buried in subdirectories"
929 * failure scenario - not very likely in real life... */
930
921 i = files_differ(f1, f2, flags); 931 i = files_differ(f1, f2, flags);
922 if (i == 0) 932 if (i == 0)
923 goto closem; 933 goto closem;
@@ -1169,48 +1179,32 @@ int diff_main(int argc, char **argv)
1169 char *f1, *f2; 1179 char *f1, *f2;
1170 llist_t *L_arg = NULL; 1180 llist_t *L_arg = NULL;
1171 1181
1172 opt_complementary = "L::"; 1182 /* exactly 2 params; collect multiple -L <label> */
1183 opt_complementary = "=2:L::";
1173 getopt32(argc, argv, "abdiL:NqrsS:tTU:wu" 1184 getopt32(argc, argv, "abdiL:NqrsS:tTU:wu"
1174 "p" /* ignored (for compatibility) */, 1185 "p" /* ignored (for compatibility) */,
1175 &L_arg, &start, &U_opt); 1186 &L_arg, &start, &U_opt);
1176 1187 /*argc -= optind;*/
1177 if (option_mask32 & FLAG_L) { 1188 argv += optind;
1178 while (L_arg) { 1189 while (L_arg) {
1179 if (label[0] == NULL) 1190 if (label1 && label2)
1180 label[0] = L_arg->data; 1191 bb_show_usage();
1181 else if (label[1] == NULL) 1192 if (!label1)
1182 label[1] = L_arg->data; 1193 label1 = L_arg->data;
1183 else 1194 else { /* then label2 is NULL */
1184 bb_show_usage(); 1195 label2 = label1;
1185 1196 label1 = L_arg->data;
1186 L_arg = L_arg->link;
1187 }
1188
1189 /* If both label[0] and label[1] were set, they need to be swapped. */
1190 if (label[0] && label[1]) {
1191 char *tmp;
1192
1193 tmp = label[1];
1194 label[1] = label[0];
1195 label[0] = tmp;
1196 } 1197 }
1198 /* we leak L_arg here... */
1199 L_arg = L_arg->link;
1197 } 1200 }
1198 1201 if (option_mask32 & FLAG_U)
1199 context = 3; /* This is the default number of lines of context. */
1200 if (option_mask32 & FLAG_U) {
1201 context = xatou_range(U_opt, 1, INT_MAX); 1202 context = xatou_range(U_opt, 1, INT_MAX);
1202 }
1203 argc -= optind;
1204 argv += optind;
1205 1203
1206 /* 1204 /*
1207 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 1205 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
1208 * driver routine. Both drivers use the contents of stb1 and stb2. 1206 * driver routine. Both drivers use the contents of stb1 and stb2.
1209 */ 1207 */
1210 if (argc < 2) {
1211 bb_error_msg("missing filename");
1212 bb_show_usage();
1213 }
1214 1208
1215 f1 = argv[0]; 1209 f1 = argv[0];
1216 f2 = argv[1]; 1210 f2 = argv[1];
@@ -1239,8 +1233,12 @@ int diff_main(int argc, char **argv)
1239 } 1233 }
1240 if (S_ISDIR(stb2.st_mode)) { 1234 if (S_ISDIR(stb2.st_mode)) {
1241 f2 = concat_path_file(f2, f1); 1235 f2 = concat_path_file(f2, f1);
1242 xstat(argv[1], &stb2); 1236 xstat(f2, &stb2);
1243 } 1237 }
1238/* We can't diff e.g. stdin supplied by a pipe - we use rewind(), fseek().
1239 * This can be fixed (volunteers?) */
1240 if (!S_ISREG(stb1.st_mode) || !S_ISREG(stb2.st_mode))
1241 bb_error_msg_and_die("can't diff non-seekable stream");
1244 print_status(diffreg(f1, f2, 0), f1, f2, NULL); 1242 print_status(diffreg(f1, f2, 0), f1, f2, NULL);
1245 } 1243 }
1246 return status; 1244 return status;
diff --git a/coreutils/sort.c b/coreutils/sort.c
index c7abc3355..29cadf5f6 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -75,14 +75,10 @@ static char *get_key(char *str, struct sort_key *key, int flags)
75 end = 0; 75 end = 0;
76 for (i = 1; i < key->range[2*j] + j; i++) { 76 for (i = 1; i < key->range[2*j] + j; i++) {
77 if (key_separator) { 77 if (key_separator) {
78 /* Skip first separator */ 78 /* Skip body of key and separator */
79 while (str[end] == key_separator)
80 end++;
81 /* Skip body of key */
82 while (str[end]) { 79 while (str[end]) {
83 if (str[end] == key_separator) 80 if (str[end++] == key_separator)
84 break; 81 break;
85 end++;
86 } 82 }
87 } else { 83 } else {
88 /* Skip leading blanks */ 84 /* Skip leading blanks */
@@ -99,9 +95,6 @@ static char *get_key(char *str, struct sort_key *key, int flags)
99 } 95 }
100 if (!j) start = end; 96 if (!j) start = end;
101 } 97 }
102 /* Key with explicit separator starts after separator */
103 if (key_separator && str[start] == key_separator)
104 start++;
105 /* Strip leading whitespace if necessary */ 98 /* Strip leading whitespace if necessary */
106//XXX: skip_whitespace() 99//XXX: skip_whitespace()
107 if (flags & FLAG_b) 100 if (flags & FLAG_b)
diff --git a/testsuite/sort.tests b/testsuite/sort.tests
index 5a4937b58..df5f7c7dd 100755
--- a/testsuite/sort.tests
+++ b/testsuite/sort.tests
@@ -66,18 +66,38 @@ testing "sort key range with multiple options" "sort -k2,3rn input" \
66egg 1 2 papyrus 66egg 1 2 papyrus
67" "$data" "" 67" "$data" ""
68 68
69testing "sort with non-default leading delim 1" "sort -n -k2 -t/ input" "\
70/a/2
71/b/1
72" "\
73/a/2
74/b/1
75" ""
76
77testing "sort with non-default leading delim 2" "sort -n -k3 -t/ input" "\
78/b/1
79/a/2
80" "\
81/b/1
82/a/2
83" ""
84
85testing "sort with non-default leading delim 3" "sort -n -k3 -t/ input" "\
86//a/2
87//b/1
88" "\
89//a/2
90//b/1
91" ""
92
93testing "sort -u should consider field only when discarding" "sort -u -k2 input" "\
94a c
95" "\
96a c
97b c
98" ""
99
69testing "sort key doesn't strip leading blanks, disables fallback global sort" \ 100testing "sort key doesn't strip leading blanks, disables fallback global sort" \
70"sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n" 101"sort -n -k2 -t ' '" " a \n 1 \n 2 \n" "" " 2 \n 1 \n a \n"
71 102
72testing "sort key edge case with -t" "sort -n -k4 -t/" \
73"/usr/lib/finish-install.d/1
74/usr/lib/finish-install.d/4
75/usr/lib/prebaseconfig.d/2
76/usr/lib/prebaseconfig.d/6
77" "" "/usr/lib/finish-install.d/1
78/usr/lib/prebaseconfig.d/2
79/usr/lib/finish-install.d/4
80/usr/lib/prebaseconfig.d/6
81"
82
83exit $FAILCOUNT 103exit $FAILCOUNT