Re: [PATCH 6/7 v2] Compute prefix at runtime if RUNTIME_PREFIX is set

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

 



Hi,

On Sun, 18 Jan 2009, Steffen Prohaska wrote:

> +	if (!prefix) {
> +		const char *strip[] = {
> +			GIT_EXEC_PATH,
> +			BINDIR,
> +			0
> +		};
> +		const char **s;
> +
> +		for (s = strip; *s; s++) {
> +			const char *sargv = argv0_path + strlen(argv0_path);

This does not need to be recomputed all the time, right?

> +			const char *ss = *s + strlen(*s);
> +			while (argv0_path < sargv && *s < ss
> +				&& (*sargv == *ss ||
> +				    (is_dir_sep(*sargv) && is_dir_sep(*ss)))) {
> +				sargv--;
> +				ss--;
> +			}
> +			if (*s == ss) {
> +				struct strbuf d = STRBUF_INIT;
> +				/* We also skip the trailing directory separator. */
> +				assert(sargv - argv0_path - 1 >= 0);
> +				strbuf_add(&d, argv0_path, sargv - argv0_path - 1);
> +				prefix = strbuf_detach(&d, NULL);
> +				break;
> +			}
> +		}
> +	}

I have a definite feeling that this code would be easier to read if it 
resembled this:

/* sets prefix if the suffix matches (modulo separator changes) */
static int strip_path_suffix(const char *path, const char *suffix,
	char **prefix)
{
	int path_len = strlen(path), suffix_len = strlen(suffix);

	while (suffix_len) {
		if (!path_len)
			return 1;

		path_len--; suffix_len--;
		/* strip arbitrary amount of directory separators */
		if (is_dir_sep(path[path_len]) && 
				is_dir_sep(suffix[suffix_len])) {
			while (path_len && is_dir_sep(path[path_len]))
				path_len--;
			while (suffix_len && is_dir_sep(suffix[suffix_len]))
				suffix_len--;
		}
		else if (path[path_len] != suffix[suffix_len])
			return 1;
	}

	while (path_len && is_dir_sep(path[path_len - 1]))
		path_len--;

	prefix = xstrndup(path, path_len);
	return 0;
}

...

	if (!prefix && strip_path_suffix(argv0_path, GIT_EXEC_PATH) &&
			strip_path_suffix(argv0_path, BINDIR)) {
		prefix = PREFIX;
		...

Note: this function could be exported in path.c, as it also handles the 
"a//bin" vs "a/bin" case.

Ciao,
Dscho

--
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]

  Powered by Linux