[PATCH 1/3] After chdir to run grep, return to old directory

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: David Turner <dturner@xxxxxxxxxxx>

Signed-off-by: David Turner <dturner@xxxxxxxxxxx>
---
 builtin/grep.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 69ac2d8..e9fe040 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -355,15 +355,25 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
 {
 	struct string_list *path_list = opt->output_priv;
 	const char **argv = xmalloc(sizeof(const char *) * (path_list->nr + 1));
+	static char old_directory[PATH_MAX+1];
 	int i, status;
 
 	for (i = 0; i < path_list->nr; i++)
 		argv[i] = path_list->items[i].string;
 	argv[path_list->nr] = NULL;
 
-	if (prefix && chdir(prefix))
-		die(_("Failed to chdir: %s"), prefix);
+
+	if (prefix) {
+		if (!getcwd(old_directory, PATH_MAX+1))
+			die(_("Failed to get cwd: %s"), prefix);
+		if (chdir(prefix))
+			die(_("Failed to chdir: %s"), prefix);
+	}
 	status = run_command_v_opt(argv, RUN_USING_SHELL);
+	if (prefix)
+		if (chdir(old_directory))
+			die(_("Failed to chdir: %s"), old_directory);
+
 	if (status)
 		exit(status);
 	free(argv);
-- 
2.0.0.rc0.31.g69c1a2d

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]