aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/patch.c25
1 files changed, 16 insertions, 9 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.