Hello people,

This patch adds the -d/--directory argument to the patch applet. Pretty
straight-forward, just switches to the provided directory before the
main patch loop using the pre-existing chdir_or_warn() and
_exit_FAILURE() if it doesnt work out.

As far as I can see, this mirrors the basic functionality of the gnu
patch implementation: 
https://git.savannah.gnu.org/cgit/patch.git/tree/src/patch.c#n945

This is my first patch to the project, so have at me, want to improve.

Cheers!
diff --git a/editors/patch.c b/editors/patch.c
index 5a768b23f..df621184c 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -11,7 +11,6 @@
  * TODO:
  * -b backup
  * -l treat all whitespace as a single space
- * -d chdir first
  * -D define wrap #ifdef and #ifndef around changes
  * -o outfile output here instead of in place
  * -r rejectfile write rejected hunks to this file
@@ -34,6 +33,7 @@
 //usage:       "[-RNE] [-p N] [-i DIFF] [ORIGFILE [PATCHFILE]]"
 //usage:#define patch_full_usage "\n\n"
 //usage:       "	-p N	Strip N leading components from file names"
+//usage:     "\n	-d DIR  Switch to DIR before doing anything"
 //usage:     "\n	-i DIFF	Read DIFF instead of stdin"
 //usage:     "\n	-R	Reverse patch"
 //usage:     "\n	-N	Ignore already applied patches"
@@ -48,6 +48,7 @@
 //usage:       "$ patch -p0 -i example.diff"
 
 #include "libbb.h"
+#include <unistd.h>
 
 #define PATCH_DEBUG  0
 
@@ -114,7 +115,7 @@ struct globals {
 } while (0)
 
 
-#define FLAG_STR "Rup:i:NEfg"
+#define FLAG_STR "Rup:i:NEfgd:"
 /* FLAG_REVERSE must be == 1! Code uses this fact. */
 #define FLAG_REVERSE  (1 << 0)
 #define FLAG_u        (1 << 1)
@@ -124,7 +125,8 @@ struct globals {
 #define FLAG_RMEMPTY  (1 << 5)
 #define FLAG_f_unused (1 << 6)
 #define FLAG_g_unused (1 << 7)
-#define FLAG_dry_run  ((1 << 8) * ENABLE_LONG_OPTS)
+#define FLAG_DIR      (1 << 8)
+#define FLAG_dry_run  ((1 << 9) * ENABLE_LONG_OPTS)
 
 
 // Dispose of a line of input, either by writing it out or discarding it.
@@ -362,7 +364,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
 	int opts;
 	int reverse, state = 0;
 	char *oldname = NULL, *newname = NULL;
-	char *opt_p, *opt_i;
+	char *opt_p, *opt_i, *opt_d;
 	long oldlen = oldlen; /* for compiler */
 	long newlen = newlen; /* for compiler */
 
@@ -373,6 +375,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
 		"strip\0"                 Required_argument "p"
 		"input\0"                 Required_argument "i"
 		"forward\0"               No_argument       "N"
+		"directory\0"             Required_argument "d"
 # if ENABLE_DESKTOP
 		"remove-empty-files\0"    No_argument       "E" /*ignored*/
 		/* "debug"                Required_argument "x" */
@@ -399,7 +402,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
 	INIT_TT();
 
 #if ENABLE_LONG_OPTS
-	opts = getopt32long(argv, FLAG_STR, patch_longopts, &opt_p, &opt_i);
+	opts = getopt32long(argv, FLAG_STR, patch_longopts, &opt_p, &opt_i, &opt_d);
 #else
 	opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i);
 #endif
@@ -417,6 +420,12 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
 		}
 	}
 
+    if ((opts & FLAG_DIR) && opt_d) {
+        if (chdir_or_warn(opt_d) != 0) {
+            _exit_FAILURE();
+        }
+    }
+
 	// Loop through the lines in the patch
 	for (;;) {
 		char *patchline;
_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to