diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-05-05 18:39:22 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-05-05 18:39:22 +0200 |
commit | c5d16e92c16cdd5ee57101e1ce88b5855e8ece6c (patch) | |
tree | 5ace6fd1189eebba1c54242214e2b0998c5716f8 | |
parent | 0cc9b1843df8c4b6838446542b3f7d5780fb9e84 (diff) | |
download | busybox-w32-c5d16e92c16cdd5ee57101e1ce88b5855e8ece6c.tar.gz busybox-w32-c5d16e92c16cdd5ee57101e1ce88b5855e8ece6c.tar.bz2 busybox-w32-c5d16e92c16cdd5ee57101e1ce88b5855e8ece6c.zip |
diff: fix -N and nonexistent files. Closes 7454
function old new delta
diffreg 1253 1310 +57
diff_main 1329 1355 +26
create_J 1819 1821 +2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 85/0) Total: 85 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/diff.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/editors/diff.c b/editors/diff.c index 3304edb26..7687518f3 100644 --- a/editors/diff.c +++ b/editors/diff.c | |||
@@ -715,9 +715,19 @@ static int diffreg(char *file[2]) | |||
715 | fp[0] = stdin; | 715 | fp[0] = stdin; |
716 | fp[1] = stdin; | 716 | fp[1] = stdin; |
717 | for (i = 0; i < 2; i++) { | 717 | for (i = 0; i < 2; i++) { |
718 | int fd = open_or_warn_stdin(file[i]); | 718 | int fd = STDIN_FILENO; |
719 | if (fd == -1) | 719 | if (!LONE_DASH(file[i])) { |
720 | goto out; | 720 | if (!(option_mask32 & FLAG(N))) { |
721 | fd = open_or_warn(file[i], O_RDONLY); | ||
722 | if (fd == -1) | ||
723 | goto out; | ||
724 | } else { | ||
725 | /* -N: if some file does not exist compare it like empty */ | ||
726 | fd = open(file[i], O_RDONLY); | ||
727 | if (fd == -1) | ||
728 | fd = xopen("/dev/null", O_RDONLY); | ||
729 | } | ||
730 | } | ||
721 | /* Our diff implementation is using seek. | 731 | /* Our diff implementation is using seek. |
722 | * When we meet non-seekable file, we must make a temp copy. | 732 | * When we meet non-seekable file, we must make a temp copy. |
723 | */ | 733 | */ |
@@ -978,17 +988,23 @@ int diff_main(int argc UNUSED_PARAM, char **argv) | |||
978 | argv += optind; | 988 | argv += optind; |
979 | while (L_arg) | 989 | while (L_arg) |
980 | label[!!label[0]] = llist_pop(&L_arg); | 990 | label[!!label[0]] = llist_pop(&L_arg); |
991 | |||
992 | /* Compat: "diff file name_which_doesnt_exist" exits with 2 */ | ||
981 | xfunc_error_retval = 2; | 993 | xfunc_error_retval = 2; |
982 | for (i = 0; i < 2; i++) { | 994 | for (i = 0; i < 2; i++) { |
983 | file[i] = argv[i]; | 995 | file[i] = argv[i]; |
984 | /* Compat: "diff file name_which_doesnt_exist" exits with 2 */ | ||
985 | if (LONE_DASH(file[i])) { | 996 | if (LONE_DASH(file[i])) { |
986 | fstat(STDIN_FILENO, &stb[i]); | 997 | fstat(STDIN_FILENO, &stb[i]); |
987 | gotstdin++; | 998 | gotstdin++; |
988 | } else | 999 | } else if (option_mask32 & FLAG(N)) { |
1000 | if (stat(file[i], &stb[i])) | ||
1001 | xstat("/dev/null", &stb[i]); | ||
1002 | } else { | ||
989 | xstat(file[i], &stb[i]); | 1003 | xstat(file[i], &stb[i]); |
1004 | } | ||
990 | } | 1005 | } |
991 | xfunc_error_retval = 1; | 1006 | xfunc_error_retval = 1; |
1007 | |||
992 | if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode))) | 1008 | if (gotstdin && (S_ISDIR(stb[0].st_mode) || S_ISDIR(stb[1].st_mode))) |
993 | bb_error_msg_and_die("can't compare stdin to a directory"); | 1009 | bb_error_msg_and_die("can't compare stdin to a directory"); |
994 | 1010 | ||