Thanks Markus
> I need to find the homedir of the active user in c++. Could anyone help me?
>
> Thanks Markus
>
Recommended way:
char *homedir = getenv("HOME");
Another way:
char *homedir = getpwuid(getuid())->pw_dir;
The latter is discouraged because the user cannot override it at
runtime. Even worse is reading /etc/passwd directly, which may fail if
NIS is being used.
--
Andrew
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#homedir
--
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:aaa...@daimi.au.dk
for(_=52;_;(_%5)||(_/=5),(_%5)&&(_-=2))putchar(_);
It depends on whether Markus would allow his users to specify what is
considered the "home directory" or if he is interested in "the real home
directory" or. Sometimes the former ("cheating") might be desireable,
sometimes it isn't.
Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
Yes, if it is a suid executable you probably shouldn't be able to
specify an alternate home. But otherwise you should be able to.
> I need to find the homedir of the active user in c++. Could anyone help me?
In order of preference:
const char *homedir = getenv("HOME"); /* Get from environment */
uid_t id = getuid();
struct passwd *pass = getpwuid(id);
const char *homedir = pass->pw_dir; /* Get from user database */
After these two checks, you must stat(2) homedir and make sure a) it
exists and b) is a directory.
const char *homedir = "/"; /* Last resort if all else fails */
Obviously, you'll do proper error checking of each function call in
your own program. If the program needs to be secure, you might
disallow getting $HOME from the envirionment, since you won't want it
to be overridable.
HTH,
Roger
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848 available on public keyservers