aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-08-22 05:39:15 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-08-22 05:39:15 +0200
commite7b0a9e5bc60617fb00c321430253d7771d40fd3 (patch)
tree5152c7e606fb72423de8d56c5c97a977c6852033
parentfd27fa83094a85b3e8cb0485467ffa65b572b923 (diff)
downloadbusybox-w32-e7b0a9e5bc60617fb00c321430253d7771d40fd3.tar.gz
busybox-w32-e7b0a9e5bc60617fb00c321430253d7771d40fd3.tar.bz2
busybox-w32-e7b0a9e5bc60617fb00c321430253d7771d40fd3.zip
patch: support "patch [FILE [PATCH]]" format
function old new delta xopen_stdin - 15 +15 patch_main 2075 2041 -34 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/patch.c35
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/wfopen_input.c8
-rwxr-xr-xtestsuite/patch.tests24
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;
417int open_or_warn(const char *pathname, int flags) FAST_FUNC; 417int open_or_warn(const char *pathname, int flags) FAST_FUNC;
418int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC; 418int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
419int open_or_warn_stdin(const char *pathname) FAST_FUNC; 419int open_or_warn_stdin(const char *pathname) FAST_FUNC;
420int xopen_stdin(const char *pathname) FAST_FUNC;
420void xrename(const char *oldpath, const char *newpath) FAST_FUNC; 421void xrename(const char *oldpath, const char *newpath) FAST_FUNC;
421int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC; 422int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC;
422off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC; 423off_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
50int 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
133testing "patch -N ignores already applied hunk" \ 132testing "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"
156testing "patch FILE PATCH" \
157 'cat >a.patch; patch input a.patch 2>&1; echo $?; cat input; rm a.patch' \
158"\
159patching file input
1600
161abc
162def
163123
164" \
165"\
166abc
167123
168" \
169"\
170--- foo.old
171+++ foo
172@@ -1,2 +1,3 @@
173 abc
174+def
175 123
176" \
177
156rm input.orig 2>/dev/null 178rm input.orig 2>/dev/null
157 179
158exit $FAILCOUNT 180exit $FAILCOUNT