[GRASS5] A GRASS user configuration directory...

I've done a bit of the work to handle a user's configuration directory
in $HOME/.grass. I don't know if this is something we want to mess with
now, or wait until later. Basically, I have one callable function
so far: G_rc_path(char *elems, char *item). It works similar to
the G_element() types of functions, but it does several things.

  1) creates $HOME/.grass if it doesn't exist...
  2) creates subdirectories "elems" of $HOME/.grass as need be, if
     "elems" is not NULL.
  3) Tries hard to make sure all directories are created drwx------
  4) Forbids any element starting with '.'
  4) Forbids any element starting with '/' or containing a '//'
  5) Strips trailing '/' from "elems" if it exists.
  6) Forbids any item (a file) from starting with a "."
  7) Forbids any item from containing a "/"
  8) Makes sure all "elems" are directories.
  9) Nested directories can be specified with elems == "foo/bar/baz"
  10) Allows either "elems" or "item" to be NULL, but not both. If
      "elems" is NULL, items will live in the "top level".
  11) Doesn't do any existence or lstat tests on "item".
  12) Returns a "path" which will either be to an existing directory
      (created as needed) or a file, or returns NULL on error.
  12) Works for me... :wink:

I still have to change a few assertions to errors, maybe add some
related functions?? Accepting comments...

P.S. I looked at using the G_home() functions. Argh! It does some
really bad stuff:
    fd = G_popen ("cd ; pwd", "r");

That one is up there with system("more"); Thank you, I prefer "less"!

--
Eric G. Miller <egm2@jps.net>

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'

Hi Eric

"Eric G. Miller" wrote:

I've done a bit of the work to handle a user's configuration directory
in $HOME/.grass. I don't know if this is something we want to mess
with now, or wait until later. Basically, I have one callable
function so far: G_rc_path(char *elems, char *item). It works similar
to the G_element() types of functions, but it does several things.

I'm not familiar with G_element(). Could you please provide a brief
overview of how G_rc_path() works? Maybe with an example?

Thanks.

--
Sincerely,

Jazzman (a.k.a. Justin Hickey) e-mail: jhickey@hpcc.nectec.or.th
High Performance Computing Center
National Electronics and Computer Technology Center (NECTEC)
Bangkok, Thailand

People who think they know everything are very irritating to those
of us who do. ---Anonymous

Jazz and Trek Rule!!!

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'

On Fri, Feb 23, 2001 at 06:56:51PM +0700, Justin Hickey wrote:

Hi Eric

"Eric G. Miller" wrote:
>
> I've done a bit of the work to handle a user's configuration directory
> in $HOME/.grass. I don't know if this is something we want to mess
> with now, or wait until later. Basically, I have one callable
> function so far: G_rc_path(char *elems, char *item). It works similar
> to the G_element() types of functions, but it does several things.

I'm not familiar with G_element(). Could you please provide a brief
overview of how G_rc_path() works? Maybe with an example?

Well, there really isn't a G_element() function, I was kind of
generalizing the concept of mapset elements used in various open/find
type routines. There is the G__make_mapset_element()...

Anyway, for a short example. Say I wanted to get the path for the x0
socket file, that lives or will live in $HOME/.grass/com. I'd just
do something like:

  char *path;
  ...
  path = G_rc_path ("com", "x0");

If $HOME/.grass didn't exist, it would be created.
If $HOME/.grass/com didn't exist, it would be created.
No test for existence is done for the file (since it's not known if it
will be a new file or what).

I've already tested it out here with the XDRIVER (using sockets
obviously) and it works. A similar approach could be done for temporary
files by having G_tempfile() do something like:

char *
G_tempfile(void)
{
  int i, status;
  pid_t pid;
  char *path, name[30];
  struct stat buf;

  pid = getpid();
  i = 0;
  path = NULL;
  do {
    snprintf (name, 30, "%d.%d", pid, i);
    G_free (path);
    path = G_rc_path ("tmp", name); /* should check for NULL return */
    status = lstat (path, &buf);
    i++;
  } while (status == 0); /* file exists... */
  
  return path;
}
  
At some point, it would return a path like $HOME/.grass/tmp/12345.7

All in all, it's pretty simple. Most the code I wrote for this spends
time doing sanity testing on input and making directories as needed.

--
Eric G. Miller <egm2@jps.net>

----------------------------------------
If you want to unsubscribe from GRASS Development Team mailing list write to:
minordomo@geog.uni-hannover.de with
subject 'unsubscribe grass5'