C/C++ - Locating a user's home directory

Smoot Carl-Mitchell smoot at tic.com
Sat Jan 29 18:13:26 UTC 2011


On Sat, 2011-01-29 at 16:58 +0000, David Fletcher wrote:
> 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.

You can look up the home directory by doing a getpwuid(uid) call which
is in the C library.  Look at the getpwent man page for the structure of
the returned value.  The home directory is a field in the returned
structure.

Also if you run the program out of cron, the current working directory
of the program is the home directory of the user.

-- 
Smoot Carl-Mitchell
System/Network Architect
voice: +1 480 922-7313
cell: +1 602 421-9005
smoot at tic.com





More information about the ubuntu-users mailing list