diff options
author | Lukas Huba <Huba.Lukas@centrum.cz> | 2010-10-21 00:43:00 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-21 00:43:00 +0200 |
commit | 08187356d7f6a79d5fb4aa83b90476997e585ad3 (patch) | |
tree | 18f0beabe1b0deb038d3ca07bfa2de267bf21f83 | |
parent | e7599d1cc7885017fa53955e257af248d183f2e3 (diff) | |
download | busybox-w32-08187356d7f6a79d5fb4aa83b90476997e585ad3.tar.gz busybox-w32-08187356d7f6a79d5fb4aa83b90476997e585ad3.tar.bz2 busybox-w32-08187356d7f6a79d5fb4aa83b90476997e585ad3.zip |
patch: implement -E option
Signed-off-by: Lukas Huba <Huba.Lukas@centrum.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/patch.c | 25 | ||||
-rw-r--r-- | include/usage.src.h | 12 |
2 files changed, 23 insertions, 14 deletions
diff --git a/editors/patch.c b/editors/patch.c index 66a9474fe..fff06907f 100644 --- a/editors/patch.c +++ b/editors/patch.c | |||
@@ -17,7 +17,6 @@ | |||
17 | * -o outfile output here instead of in place | 17 | * -o outfile output here instead of in place |
18 | * -r rejectfile write rejected hunks to this file | 18 | * -r rejectfile write rejected hunks to this file |
19 | * | 19 | * |
20 | * -E remove empty files --remove-empty-files | ||
21 | * -f force (no questions asked) | 20 | * -f force (no questions asked) |
22 | * -F fuzz (number, default 2) | 21 | * -F fuzz (number, default 2) |
23 | * [file] which file to patch | 22 | * [file] which file to patch |
@@ -42,7 +41,7 @@ config PATCH | |||
42 | hunks to stderr, and exits with nonzero status if any hunks fail. | 41 | hunks to stderr, and exits with nonzero status if any hunks fail. |
43 | 42 | ||
44 | A file compared against /dev/null (or with a date <= the epoch) is | 43 | A file compared against /dev/null (or with a date <= the epoch) is |
45 | created/deleted as appropriate. | 44 | created or deleted if -E or --remove-empty-files set. |
46 | */ | 45 | */ |
47 | #include "libbb.h" | 46 | #include "libbb.h" |
48 | 47 | ||
@@ -243,15 +242,16 @@ struct globals { | |||
243 | } while (0) | 242 | } while (0) |
244 | 243 | ||
245 | 244 | ||
246 | #define FLAG_STR "Rup:i:Nx" | 245 | #define FLAG_STR "Rup:i:NEx" |
247 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ | 246 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ |
248 | #define FLAG_REVERSE (1 << 0) | 247 | #define FLAG_REVERSE (1 << 0) |
249 | #define FLAG_u (1 << 1) | 248 | #define FLAG_u (1 << 1) |
250 | #define FLAG_PATHLEN (1 << 2) | 249 | #define FLAG_PATHLEN (1 << 2) |
251 | #define FLAG_INPUT (1 << 3) | 250 | #define FLAG_INPUT (1 << 3) |
252 | #define FLAG_IGNORE (1 << 4) | 251 | #define FLAG_IGNORE (1 << 4) |
252 | #define FLAG_RMEMPTY (1 << 5) | ||
253 | //non-standard: | 253 | //non-standard: |
254 | #define FLAG_DEBUG (1 << 5) | 254 | #define FLAG_DEBUG (1 << 6) |
255 | 255 | ||
256 | // Dispose of a line of input, either by writing it out or discarding it. | 256 | // Dispose of a line of input, either by writing it out or discarding it. |
257 | 257 | ||
@@ -551,7 +551,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
551 | 551 | ||
552 | // If this is the first hunk, open the file. | 552 | // If this is the first hunk, open the file. |
553 | if (TT.filein == -1) { | 553 | if (TT.filein == -1) { |
554 | int oldsum, newsum, del = 0; | 554 | int oldsum, newsum, empty = 0; |
555 | char *name; | 555 | char *name; |
556 | 556 | ||
557 | oldsum = TT.oldline + TT.oldlen; | 557 | oldsum = TT.oldline + TT.oldlen; |
@@ -564,7 +564,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
564 | if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) | 564 | if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) |
565 | { | 565 | { |
566 | name = reverse ? newname : oldname; | 566 | name = reverse ? newname : oldname; |
567 | del++; | 567 | empty++; |
568 | } | 568 | } |
569 | 569 | ||
570 | // handle -p path truncation. | 570 | // handle -p path truncation. |
@@ -576,10 +576,17 @@ int patch_main(int argc UNUSED_PARAM, char **argv) | |||
576 | } | 576 | } |
577 | } | 577 | } |
578 | 578 | ||
579 | if (del) { | 579 | if (empty) { |
580 | printf("removing %s\n", name); | 580 | // File is empty after the patches have been applied |
581 | xunlink(name); | ||
582 | state = 0; | 581 | state = 0; |
582 | if (option_mask32 & FLAG_RMEMPTY) { | ||
583 | // If flag -E or --remove-empty-files is set | ||
584 | printf("removing %s\n", name); | ||
585 | xunlink(name); | ||
586 | } else { | ||
587 | printf("patching file %s\n", name); | ||
588 | xclose(xopen(name, O_WRONLY | O_TRUNC)); | ||
589 | } | ||
583 | // If we've got a file to open, do so. | 590 | // If we've got a file to open, do so. |
584 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { | 591 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { |
585 | // If the old file was null, we're creating a new one. | 592 | // If the old file was null, we're creating a new one. |
diff --git a/include/usage.src.h b/include/usage.src.h index 2445c1b9b..f5ddd7ba5 100644 --- a/include/usage.src.h +++ b/include/usage.src.h | |||
@@ -2857,17 +2857,19 @@ INSERT | |||
2857 | "[OPTIONS] [ORIGFILE [PATCHFILE]]" | 2857 | "[OPTIONS] [ORIGFILE [PATCHFILE]]" |
2858 | #define patch_full_usage "\n\n" \ | 2858 | #define patch_full_usage "\n\n" \ |
2859 | IF_LONG_OPTS( \ | 2859 | IF_LONG_OPTS( \ |
2860 | " -p,--strip N Strip N leading components from file names" \ | 2860 | " -p,--strip N Strip N leading components from file names" \ |
2861 | "\n -i,--input DIFF Read DIFF instead of stdin" \ | 2861 | "\n -i,--input DIFF Read DIFF instead of stdin" \ |
2862 | "\n -R,--reverse Reverse patch" \ | 2862 | "\n -R,--reverse Reverse patch" \ |
2863 | "\n -N,--forward Ignore already applied patches" \ | 2863 | "\n -N,--forward Ignore already applied patches" \ |
2864 | "\n --dry-run Don't actually change files" \ | 2864 | "\n --dry-run Don't actually change files" \ |
2865 | "\n -E,--remove-empty-files Remove output files if they become empty" \ | ||
2865 | ) \ | 2866 | ) \ |
2866 | IF_NOT_LONG_OPTS( \ | 2867 | IF_NOT_LONG_OPTS( \ |
2867 | " -p N Strip N leading components from file names" \ | 2868 | " -p N Strip N leading components from file names" \ |
2868 | "\n -i DIFF Read DIFF instead of stdin" \ | 2869 | "\n -i DIFF Read DIFF instead of stdin" \ |
2869 | "\n -R Reverse patch" \ | 2870 | "\n -R Reverse patch" \ |
2870 | "\n -N Ignore already applied patches" \ | 2871 | "\n -N Ignore already applied patches" \ |
2872 | "\n -E Remove output files if they become empty" \ | ||
2871 | ) | 2873 | ) |
2872 | 2874 | ||
2873 | #define patch_example_usage \ | 2875 | #define patch_example_usage \ |