C/C++ - Locating a user's home directory
David Fletcher
dave at thefletchers.net
Sat Jan 29 16:58:31 UTC 2011
I'm writing a small C++ application that I want to run once a day as an
ordinary user on my server as a cron job. The application is almost
complete, except that what I want to do is get it to use config files in
the user's home directory.
My question is, does anybody know of a g++ function that returns the
path to the home directory of the user running the program? I have found
out how to get the uid number of the user by calling getuid(). There is
a function called by the source code of gpass which returns the path to
the home directory but this is a gnome function which obviously I don't
have or want to have on my server.
The other source I looked at is that of Joe's Own Editor in which I
found the line
s = (unsigned char *)getenv("HOME");
BUT does this work when the user on whose behalf the program is being
run by cron isn't logged in? If not, I can easily enough add to my
config class to get it to search through /etc/passwd for the name of the
home directory given that I know the uid, but surely this must have been
done before? Assuming that getuid() works on a server with a program
running as a cron job on behalf of a not logged in user.
Hope that's clear enough to convey what I'm asking about.
Dave
More information about the ubuntu-users
mailing list