diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-07 00:21:41 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-07 00:21:41 +0000 |
commit | 0514d50e3d5847394388c781fad158999cf3b338 (patch) | |
tree | 107879fed239680db64187e43889648a79e71192 /coreutils/diff.c | |
parent | f2236c0885cb06c667239aead0697d45c999fa72 (diff) | |
download | busybox-w32-0514d50e3d5847394388c781fad158999cf3b338.tar.gz busybox-w32-0514d50e3d5847394388c781fad158999cf3b338.tar.bz2 busybox-w32-0514d50e3d5847394388c781fad158999cf3b338.zip |
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
git-svn-id: svn://busybox.net/trunk/busybox@17175 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/diff.c')
-rw-r--r-- | coreutils/diff.c | 90 |
1 files changed, 44 insertions, 46 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] */ |
70 | int context, status; | 70 | /* This is the default number of lines of context. */ |
71 | char *start, *label[2]; | 71 | int context = 3; |
72 | int status; | ||
73 | char *start; | ||
74 | const char *label1; | ||
75 | const char *label2; | ||
72 | struct stat stb1, stb2; | 76 | struct stat stb1, stb2; |
73 | char **dl; | 77 | char **dl; |
74 | USE_FEATURE_DIFF_DIR(static int dl_count;) | 78 | USE_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 | ||
736 | static void print_header(const char *file1, const char *file2) | 741 | static 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; |