git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Steffen Prohaska <prohaska@zib.de>
Cc: Johannes Sixt <johannes.sixt@telecom.at>,
	git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] Set up argv0_path correctly, even when argv[0] is just the basename
Date: Sat, 26 Jul 2008 16:14:33 +0200 (CEST)	[thread overview]
Message-ID: <alpine.DEB.1.00.0807261613120.26810@eeepc-johanness> (raw)
In-Reply-To: <1217065304-27815-1-git-send-email-prohaska@zib.de>


When the program 'git' is in the PATH, the argv[0] is set to the basename.
However, argv0_path needs the full path, so add a function to discover the
program by traversing the PATH manually.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---

	So it is not easily possible to reuse this function in 
	compat/mingw.c, as Junio said that compat/ should not depend
	(at least too much) on libgit.a.

	Of course, we could try to follow a symlinked git, too, but I 
	think this is overkill until someone proves me wrong.

 exec_cmd.c |   22 ++++++++++++++++++++++
 exec_cmd.h |    1 +
 git.c      |    6 ++++++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/exec_cmd.c b/exec_cmd.c
index 0ed768d..048f3ca 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -125,3 +125,25 @@ int execl_git_cmd(const char *cmd,...)
 	argv[argc] = NULL;
 	return execv_git_cmd(argv);
 }
+
+char *lookup_program_in_path(const char *program)
+{
+	struct strbuf buf = STRBUF_INIT;
+	const char *path = getenv("PATH");
+
+	if (!path || !*path)
+		return NULL;
+
+	for (;;) {
+		const char *colon = strchrnul(path, PATH_SEP);
+
+		strbuf_setlen(&buf, 0);
+		strbuf_addf(&buf, "%.*s/%s",
+				(int)(colon - path), path, program);
+		if (!access(buf.buf, X_OK))
+			return strbuf_detach(&buf, NULL);
+		if (!*colon)
+			return NULL;
+		path = colon + 1;
+	}
+}
diff --git a/exec_cmd.h b/exec_cmd.h
index 0c46cd5..4548390 100644
--- a/exec_cmd.h
+++ b/exec_cmd.h
@@ -8,5 +8,6 @@ extern void setup_path(void);
 extern int execv_git_cmd(const char **argv); /* NULL terminated */
 extern int execl_git_cmd(const char *cmd, ...);
 extern const char *system_path(const char *path);
+extern char *lookup_program_in_path(const char *program);
 
 #endif /* GIT_EXEC_CMD_H */
diff --git a/git.c b/git.c
index 54c5bfa..0ec8ee1 100644
--- a/git.c
+++ b/git.c
@@ -428,6 +428,12 @@ int main(int argc, const char **argv)
 	do
 		--slash;
 	while (cmd <= slash && !is_dir_sep(*slash));
+	if (slash < cmd) {
+		cmd = lookup_program_in_path(cmd);
+		for (slash = (char *)cmd + strlen(cmd) - 1;
+				cmd <= slash && !is_dir_sep(*slash); slash--)
+			; /* do nothing */
+	}
 	if (cmd <= slash) {
 		*slash++ = 0;
 		git_set_argv0_path(cmd);
-- 
1.5.6.2.516.g22071

  parent reply	other threads:[~2008-07-26 14:14 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-26  9:41 [PATCH] Modify mingw_main() workaround to avoid link errors Steffen Prohaska
2008-07-26 13:17 ` Johannes Schindelin
2008-07-26 16:07   ` Steffen Prohaska
2008-07-26 14:14 ` Johannes Schindelin [this message]
2008-07-26 14:54   ` [PATCH] Set up argv0_path correctly, even when argv[0] is just the basename Rene Herman
2008-07-26 15:10     ` Johannes Schindelin
2008-07-26 15:19       ` Rene Herman
2008-07-26 15:35         ` Johannes Schindelin
2008-07-26 15:53           ` Rene Herman
2008-07-26 17:31   ` Junio C Hamano
2008-07-26 17:42     ` Johannes Schindelin
2008-08-03 20:25   ` Jan Hudec
2008-08-03 20:43     ` Junio C Hamano
2008-07-26 20:37 ` [PATCH] Modify mingw_main() workaround to avoid link errors Johannes Sixt
2008-07-26 21:36   ` Steffen Prohaska
2008-07-27 19:24     ` Johannes Sixt
2008-07-29  4:46       ` Steffen Prohaska
2008-07-29  8:33         ` Johannes Sixt
2008-07-29 19:46           ` Steffen Prohaska
2008-08-03 19:55 ` Johannes Sixt
2008-08-03 21:21   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.1.00.0807261613120.26810@eeepc-johanness \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.sixt@telecom.at \
    --cc=prohaska@zib.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).