diff options
-rw-r--r-- | editors/patch.c | 35 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/wfopen_input.c | 8 | ||||
-rwxr-xr-x | testsuite/patch.tests | 24 |
4 files changed, 56 insertions, 12 deletions
diff --git a/editors/patch.c b/editors/patch.c index 3ed4eba45..c40f54155 100644 --- a/editors/patch.c +++ b/editors/patch.c | |||
@@ -447,10 +447,21 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
447 | INIT_TT(); | 447 | INIT_TT(); |
448 | 448 | ||
449 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); | 449 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); |
450 | argv += optind; | ||
450 | reverse = opts & FLAG_REVERSE; | 451 | reverse = opts & FLAG_REVERSE; |
451 | TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! | 452 | TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! |
452 | if (opts & FLAG_INPUT) TT.filepatch = xopen(opt_i, O_RDONLY); | ||
453 | TT.filein = TT.fileout = -1; | 453 | TT.filein = TT.fileout = -1; |
454 | if (opts & FLAG_INPUT) { | ||
455 | TT.filepatch = xopen_stdin(opt_i); | ||
456 | } else { | ||
457 | if (argv[0] && argv[1]) { | ||
458 | TT.filepatch = xopen_stdin(argv[1]); | ||
459 | } | ||
460 | } | ||
461 | if (argv[0]) { | ||
462 | oldname = xstrdup(argv[0]); | ||
463 | newname = xstrdup(argv[0]); | ||
464 | } | ||
454 | 465 | ||
455 | // Loop through the lines in the patch | 466 | // Loop through the lines in the patch |
456 | for(;;) { | 467 | for(;;) { |
@@ -498,18 +509,20 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
498 | state = 1; | 509 | state = 1; |
499 | } | 510 | } |
500 | 511 | ||
501 | free(*name); | ||
502 | finish_oldfile(); | 512 | finish_oldfile(); |
503 | 513 | ||
504 | // Trim date from end of filename (if any). We don't care. | 514 | if (!argv[0]) { |
505 | for (s = patchline+4; *s && *s!='\t'; s++) | 515 | free(*name); |
506 | if (*s=='\\' && s[1]) s++; | 516 | // Trim date from end of filename (if any). We don't care. |
507 | i = atoi(s); | 517 | for (s = patchline+4; *s && *s!='\t'; s++) |
508 | if (i>1900 && i<=1970) | 518 | if (*s=='\\' && s[1]) s++; |
509 | *name = xstrdup("/dev/null"); | 519 | i = atoi(s); |
510 | else { | 520 | if (i>1900 && i<=1970) |
511 | *s = 0; | 521 | *name = xstrdup("/dev/null"); |
512 | *name = xstrdup(patchline+4); | 522 | else { |
523 | *s = 0; | ||
524 | *name = xstrdup(patchline+4); | ||
525 | } | ||
513 | } | 526 | } |
514 | 527 | ||
515 | // We defer actually opening the file because svn produces broken | 528 | // We defer actually opening the file because svn produces broken |
diff --git a/include/libbb.h b/include/libbb.h index ac818a9ea..3fd754511 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -417,6 +417,7 @@ int xopen3(const char *pathname, int flags, int mode) FAST_FUNC; | |||
417 | int open_or_warn(const char *pathname, int flags) FAST_FUNC; | 417 | int open_or_warn(const char *pathname, int flags) FAST_FUNC; |
418 | int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC; | 418 | int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC; |
419 | int open_or_warn_stdin(const char *pathname) FAST_FUNC; | 419 | int open_or_warn_stdin(const char *pathname) FAST_FUNC; |
420 | int xopen_stdin(const char *pathname) FAST_FUNC; | ||
420 | void xrename(const char *oldpath, const char *newpath) FAST_FUNC; | 421 | void xrename(const char *oldpath, const char *newpath) FAST_FUNC; |
421 | int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC; | 422 | int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC; |
422 | off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC; | 423 | off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC; |
diff --git a/libbb/wfopen_input.c b/libbb/wfopen_input.c index 7263c933a..422a58ecf 100644 --- a/libbb/wfopen_input.c +++ b/libbb/wfopen_input.c | |||
@@ -46,3 +46,11 @@ int FAST_FUNC open_or_warn_stdin(const char *filename) | |||
46 | 46 | ||
47 | return fd; | 47 | return fd; |
48 | } | 48 | } |
49 | |||
50 | int FAST_FUNC xopen_stdin(const char *filename) | ||
51 | { | ||
52 | int fd = open_or_warn_stdin(filename); | ||
53 | if (fd >= 0) | ||
54 | return fd; | ||
55 | xfunc_die(); /* We already output an error message. */ | ||
56 | } | ||
diff --git a/testsuite/patch.tests b/testsuite/patch.tests index cd0e965cf..e482304f6 100755 --- a/testsuite/patch.tests +++ b/testsuite/patch.tests | |||
@@ -129,7 +129,6 @@ abc | |||
129 | " \ | 129 | " \ |
130 | 130 | ||
131 | # testing "test name" "command(s)" "expected result" "file input" "stdin" | 131 | # testing "test name" "command(s)" "expected result" "file input" "stdin" |
132 | |||
133 | testing "patch -N ignores already applied hunk" \ | 132 | testing "patch -N ignores already applied hunk" \ |
134 | 'patch -N 2>&1; echo $?; cat input' \ | 133 | 'patch -N 2>&1; echo $?; cat input' \ |
135 | "\ | 134 | "\ |
@@ -153,6 +152,29 @@ def | |||
153 | 123 | 152 | 123 |
154 | " \ | 153 | " \ |
155 | 154 | ||
155 | # testing "test name" "command(s)" "expected result" "file input" "stdin" | ||
156 | testing "patch FILE PATCH" \ | ||
157 | 'cat >a.patch; patch input a.patch 2>&1; echo $?; cat input; rm a.patch' \ | ||
158 | "\ | ||
159 | patching file input | ||
160 | 0 | ||
161 | abc | ||
162 | def | ||
163 | 123 | ||
164 | " \ | ||
165 | "\ | ||
166 | abc | ||
167 | 123 | ||
168 | " \ | ||
169 | "\ | ||
170 | --- foo.old | ||
171 | +++ foo | ||
172 | @@ -1,2 +1,3 @@ | ||
173 | abc | ||
174 | +def | ||
175 | 123 | ||
176 | " \ | ||
177 | |||
156 | rm input.orig 2>/dev/null | 178 | rm input.orig 2>/dev/null |
157 | 179 | ||
158 | exit $FAILCOUNT | 180 | exit $FAILCOUNT |