| .1> getenv(3) - it should provide what you need.
Anna, in case it's not clear, what Lee means is that, in your program, you can
use getenv() to translate $HOME into "/usr/users/anna", and then you can append
"/test" to that and specify the result to open().
Webb
|
| > Anna, in case it's not clear, what Lee means is that, in your program, you can
> use getenv() to translate $HOME into "/usr/users/anna", and then you can append
> "/test" to that and specify the result to open().
Of course you only want to append "/test" if HOME is defined :-)
Example:
char *home = getenv("HOME");
char filespec[MAXPATHLEN];
if( home )
sprintf(filespec, "%s/test", home);
else
strcpy(filespec, "test"); /* relative to cwd then */
actually however one would normally also not prepend the HOME
directory if you are actually getting the filename you are appending
to the home directory from the user if they specified an absolute
filespec, such that you would modify the test above to:
if( home && usersuppliedfilename[0] != '/' )
also if the HOME environment variable is not defined (or you
are in a situation where you can't trust it, since the user
can set environment variables to anything), you can use
the getpwent family of routines (such as getpwuid(getuid()))
and use the pw_dir field of the passwd structure.
|