commit 72afef702f8814b3522037ad8c655e4cd92b7e32
Author: Dhruva Krishnamurthy <dhruvakm@gmail.com>
Date:   Wed Jun 25 19:18:15 2014 -0700

    Avoid expensive calls to stat() when calling with same path. Store path to allow same call semantics to test() with any argument

diff --git a/global/global.c b/global/global.c
index ed1f781..6723483 100644
--- a/global/global.c
+++ b/global/global.c
@@ -1352,7 +1352,7 @@ grep(const char *pattern, char *const *argv, const char *dbpath)
 				warning("'%s' is a directory. Ignored.", path);
 				continue;
 			}
-			if (!test("f", buf)) {
+			if (!test("f", NULL /* buf */)) {
 				warning("'%s' not found. Ignored.", path);
 				continue;
 			}
diff --git a/gozilla/gozilla.c b/gozilla/gozilla.c
index 5d9136b..bc63965 100644
--- a/gozilla/gozilla.c
+++ b/gozilla/gozilla.c
@@ -315,7 +315,7 @@ main(int argc, char **argv)
 			/*
 			 * Make a file URL.
 			 */
-			else if (test("fr", argument) || test("dr", argument)) {
+			else if (test("fr", argument) || test("dr", NULL /* argument */)) {
 				char cwd[MAXPATHLEN];
 				char result[MAXPATHLEN];
 
@@ -363,7 +363,7 @@ getdefinitionURL(const char *arg, const char *htmldir, STRBUF *URL)
 	if (!test("d", path))
 		die("'%s' not found. Please invoke htags(1) without the -D option.", path);
 	path = makepath(htmldir, "MAP", NULL);
-	if (!test("f", path))
+	if (!test("f", NULL /* path */))
 		die("'%s' not found. Please invoke htags(1) with the --map-file option.", path);
 	fp = fopen(path, "r");
 	if (!fp)
@@ -401,7 +401,7 @@ getURL(const char *file, const char *htmldir, STRBUF *URL)
 	char buf[MAXPATHLEN];
 	STRBUF *sb = strbuf_open(0);
 
-	if (!test("f", file) && !test("d", file))
+	if (!test("f", file) && !test("d", NULL /* file */))
 		die("file '%s' not found.", file);
 	p = normalize(file, get_root_with_slash(), cwd, buf, sizeof(buf));
 	if (p != NULL && convertpath(dbpath, htmldir, p, sb) == 0)
diff --git a/gtags/gtags.c b/gtags/gtags.c
index daa94a3..35c9fc4 100644
--- a/gtags/gtags.c
+++ b/gtags/gtags.c
@@ -345,9 +345,9 @@ main(int argc, char **argv)
 	if (file_list && strcmp(file_list, "-")) {
 		if (test("d", file_list))
 			die("'%s' is a directory.", file_list);
-		else if (!test("f", file_list))
+		else if (!test("f", NULL /* file_list */))
 			die("'%s' not found.", file_list);
-		else if (!test("r", file_list))
+		else if (!test("r", NULL /* file_list */))
 			die("'%s' is not readable.", file_list);
 	}
 	/*
@@ -682,7 +682,7 @@ incremental(const char *dbpath, const char *root)
 			 * assuming that it does not exist in the file system.
 			 */
 			if (type == GPATH_OTHER) {
-				if (!idset_contains(findset, id) || !test("f", path) || test("b", path))
+				if (!idset_contains(findset, id) || !test("f", path) || test("b", NULL /* path */))
 					strbuf_puts0(deletelist, path);
 			} else {
 				if (!idset_contains(findset, id) || !test("f", path)) {
diff --git a/libutil/conf.c b/libutil/conf.c
index ba4fe99..3188139 100644
--- a/libutil/conf.c
+++ b/libutil/conf.c
@@ -274,9 +274,9 @@ openconf(const char *rootdir)
 
 		if (test("d", config_path))
 			die("config file '%s' is a directory.", config_path);
-		if (!test("f", config_path))
+		if (!test("f", NULL /* config_path */))
 			die("config file '%s' not found.", config_path);
-		if (!test("r", config_path))
+		if (!test("r", NULL /* config_path */))
 			die("config file '%s' is not readable.", config_path);
 		if ((label = getenv("GTAGSLABEL")) == NULL)
 			label = "default";
diff --git a/libutil/find.c b/libutil/find.c
index 26618ab..8dc7d15 100644
--- a/libutil/find.c
+++ b/libutil/find.c
@@ -711,7 +711,7 @@ find_read_traverse(void)
 				 * o dead symbolic link
 				 */
 				if (!test("f", path)) {
-					if (test("d", path))
+					if (test("d", NULL /* path */))
 						warning("'%s' is a directory. ignored.", trimpath(path));
 					else
 						warning("'%s' not found. ignored.", trimpath(path));
@@ -816,7 +816,7 @@ find_read_filelist(void)
 		 * o dead symbolic link
 		 */
 		if (!test("f", path)) {
-			if (test("d", path))
+			if (test("d", NULL /* path */))
 				warning("'%s' is a directory. ignored.", trimpath(path));
 			else
 				warning("'%s' not found. ignored.", trimpath(path));
diff --git a/libutil/getdbpath.c b/libutil/getdbpath.c
index b098376..78cf1ae 100644
--- a/libutil/getdbpath.c
+++ b/libutil/getdbpath.c
@@ -100,7 +100,7 @@ getobjdir(const char *candidate, int verbose)
 		setupvariables(0);
 	snprintf(path, sizeof(path), "%s/%s", candidate, makeobjdir);
 	if (test("d", path)) {
-		if (!test("drw", path))
+		if (!test("drw", NULL /* path */))
 			die("Found objdir '%s', but you don't have read/write permission for it.", path);
 		if (verbose)
 			fprintf(stderr, "Using objdir '%s'.\n", path);
@@ -110,7 +110,7 @@ getobjdir(const char *candidate, int verbose)
 	if (test("d", makeobjdirprefix)) {
 		snprintf(path, sizeof(path), "%s%s", makeobjdirprefix, candidate);
 		if (test("d", path)) {
-			if (!test("drw", path))
+			if (!test("drw", NULL /* path */))
 				die("Found objdir '%s', but you don't have read/write permission for it.", path);
 			if (verbose)
 				fprintf(stderr, "Using objdir '%s'.\n", path);
diff --git a/libutil/path.c b/libutil/path.c
index 3ec04fd..d271b88 100644
--- a/libutil/path.c
+++ b/libutil/path.c
@@ -202,7 +202,7 @@ makedirectories(const char *base, const char *rest, int verbose)
 
 	if (!test("d", base))
 		return -1;
-	if (!test("drw", base))
+	if (!test("drw", NULL /* base */))
 		return -2;
 	sb = strbuf_open(0);
 	strbuf_puts(sb, base);
diff --git a/libutil/test.c b/libutil/test.c
index 542dfce..58c2e7e 100644
--- a/libutil/test.c
+++ b/libutil/test.c
@@ -91,14 +91,17 @@ is_binary(const char *path)
  * You can specify more than one character. It assumed 'AND' test.
  */
 int
-test(const char *flags, const char *path)
+test(const char *flags, const char *input_path)
 {
 	static struct stat sb;
+	static const char *path;
 	int c;
 
-	if (path != NULL)
+	if (input_path != NULL) {
+		path = input_path;
 		if (stat(path, &sb) < 0)
 			return 0;
+	}
 	while ((c = *flags++) != 0) {
 		switch (c) {
 		case 'b':
