[GRASS-dev] etc file finder, take 2

Glynn Clements glynn at gclements.plus.com
Wed Apr 18 12:16:09 EDT 2007


William Kyngesburye wrote:

> Here's what I came up with.  It uses an env var, GRASS_ADDON_ETC,  
> much like the PATH and GRASS_ADDON_PATH vars - a colon-delimited list  
> of paths to look in.  And finally checks the GRASS application etc/.
> 
> It returns the full path to the found file or folder, or null if not  
> found.

The mechanism seems reasonable, although I have quite a few comments
on the implementation.

The main one is that I'd suggest using G_tokenize() to do most of the
work for you. Also, make the argument "const char *" (you don't need
to modify it) and use GPATH_MAX as the size of the path buffer (I've
been replacing various random constants with that value as I find
them; also for GNAME_MAX and GMAPSET_MAX)..

E.g. (untested):

static char *G__find_etc(const char *name)
{
	const char *pathlist = getenv("GRASS_ADDON_ETC");
	char path[GPATH_MAX];

	if (pathlist)
	{
		char **dirs = G_tokenize(pathlist, ":");
		char *result = NULL;
		int i;

		for (i = 0; dirs[i]; i++)
		{
			sprintf(path, "%s/%s", dirs[i], name);

			if (access(path, 0) == 0)
			{
				result = G_store(path);
				break;
			}
		}
	
		G_free_tokens(dirs);
	
		if (result)
			return result;
	}

	sprintf(path, "%s/etc/%s", G_gisbase(), name);
	if (access(path, 0) == 0)
		return G_store(path);

	return NULL;
}

Also:

> #include <grass/etc.h>

Not needed; add it to gisdefs.h.

> And a companion g.findetc for use in scripts:

> 	exit(fpath==NULL);

	exit(fpath ? EXIT_SUCCESS : EXIT_FAILURE);

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list