# [PATCH] When exec'ing sub-commands, fall back on execvp (the PATH)

2 messages from 2007-10-20 to 2007-10-20. Participants: Scott Parish, Johannes Schindelin.
Thread: https://gitlist.dev/t/10387

## Scott Parish, 2007-10-20 06:44

Subject: [PATCH] When exec'ing sub-commands, fall back on execvp (the PATH)
Message-ID: <20071020064459.GB2237@srparish.net>
URL: https://gitlist.dev/e/20071020064459.GB2237%40srparish.net

```
 Signed-off-by: Scott R Parish <srp@srparish.net>

---
 exec_cmd.c |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/exec_cmd.c b/exec_cmd.c
index 9b74ed2..674c9f3 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -34,15 +34,15 @@ int execv_git_cmd(const char **argv)
 {
 	char git_command[PATH_MAX + 1];
 	int i;
+	int rc;
 	const char *paths[] = { current_exec_path,
 				getenv(EXEC_PATH_ENVIRONMENT),
 				builtin_exec_path };
+	const char *tmp;
+	size_t len;
 
 	for (i = 0; i < ARRAY_SIZE(paths); ++i) {
-		size_t len;
-		int rc;
 		const char *exec_dir = paths[i];
-		const char *tmp;
 
 		if (!exec_dir || !*exec_dir) continue;
 
@@ -106,8 +106,26 @@ int execv_git_cmd(const char **argv)
 
 		argv[0] = tmp;
 	}
-	return -1;
 
+	rc = snprintf(git_command, sizeof(git_command), "git-%s", argv[0]);
+	if (rc < 0 || rc >= sizeof(git_command) - len) {
+		fprintf(stderr, "git: command name given is too long.\n");
+		return -1;
+	}
+
+	tmp = argv[0];
+	argv[0] = git_command;
+
+	trace_argv_printf(argv, -1, "trace: exec:");
+
+	/* execve() can only ever return if it fails */
+	execvp(git_command, (char **)argv);
+
+	trace_printf("trace: exec failed: %s\n", strerror(errno));
+
+	argv[0] = tmp;
+
+	return -1;
 }
 
 
-- 
1.5.3.GIT

```

## Johannes Schindelin, 2007-10-20 07:30

Subject: Re: [PATCH] When exec'ing sub-commands, fall back on execvp (the PATH)
Message-ID: <Pine.LNX.4.64.0710200824270.25221@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0710200824270.25221%40racer.site
In-Reply-To: <20071020064459.GB2237@srparish.net>

```
Hi,

On Fri, 19 Oct 2007, Scott Parish wrote:

> diff --git a/exec_cmd.c b/exec_cmd.c
> index 9b74ed2..674c9f3 100644
> --- a/exec_cmd.c
> +++ b/exec_cmd.c
> @@ -34,15 +34,15 @@ int execv_git_cmd(const char **argv)
>  {
>  	char git_command[PATH_MAX + 1];
>  	int i;
> +	int rc;
>  	const char *paths[] = { current_exec_path,
>  				getenv(EXEC_PATH_ENVIRONMENT),
>  				builtin_exec_path };
> +	const char *tmp;
> +	size_t len;
>  
>  	for (i = 0; i < ARRAY_SIZE(paths); ++i) {
> -		size_t len;
> -		int rc;
>  		const char *exec_dir = paths[i];
> -		const char *tmp;
>  
>  		if (!exec_dir || !*exec_dir) continue;
>  
> @@ -106,8 +106,26 @@ int execv_git_cmd(const char **argv)
>  
>  		argv[0] = tmp;
>  	}
> -	return -1;
>  
> +	rc = snprintf(git_command, sizeof(git_command), "git-%s", argv[0]);
> +	if (rc < 0 || rc >= sizeof(git_command) - len) {
> +		fprintf(stderr, "git: command name given is too long.\n");
> +		return -1;
> +	}
> +
> +	tmp = argv[0];
> +	argv[0] = git_command;
> +
> +	trace_argv_printf(argv, -1, "trace: exec:");
> +
> +	/* execve() can only ever return if it fails */
> +	execvp(git_command, (char **)argv);
> +
> +	trace_printf("trace: exec failed: %s\n", strerror(errno));
> +
> +	argv[0] = tmp;
> +
> +	return -1;
>  }

I am not sure that this is elegant enough: Something like this (completely 
untested) might be better:

diff --git a/exec_cmd.c b/exec_cmd.c
index 9b74ed2..c928f37 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -36,7 +36,8 @@ int execv_git_cmd(const char **argv)
 	int i;
 	const char *paths[] = { current_exec_path,
 				getenv(EXEC_PATH_ENVIRONMENT),
-				builtin_exec_path };
+				builtin_exec_path,
+				"" };
 
 	for (i = 0; i < ARRAY_SIZE(paths); ++i) {
 		size_t len;
@@ -44,9 +45,12 @@ int execv_git_cmd(const char **argv)
 		const char *exec_dir = paths[i];
 		const char *tmp;
 
-		if (!exec_dir || !*exec_dir) continue;
+		if (!exec_dir) continue;
 
-		if (*exec_dir != '/') {
+		if (!*exec_dir)
+			/* try PATH */
+			*git_command = '\0';
+		else if (*exec_dir != '/') {
 			if (!getcwd(git_command, sizeof(git_command))) {
 				fprintf(stderr, "git: cannot determine "
 					"current directory: %s\n",
@@ -81,7 +85,7 @@ int execv_git_cmd(const char **argv)
 
 		len = strlen(git_command);
 		rc = snprintf(git_command + len, sizeof(git_command) - len,
-			      "/git-%s", argv[0]);
+			      "%sgit-%s", *exec_dir ? "/" : "", argv[0]);
 		if (rc < 0 || rc >= sizeof(git_command) - len) {
 			fprintf(stderr,
 				"git: command name given is too long.\n");

Ciao,
Dscho

```
