Operating System - HP-UX
1754319 Members
3160 Online
108813 Solutions
New Discussion юеВ

Can I find a users home directory from within a c program?

 
SOLVED
Go to solution
Renda Skandier
Frequent Advisor

Can I find a users home directory from within a c program?

Hi,
Seems like there should be an easy way to find the home directory of the user running the c program. The only way I can think of is to read an external file containing it.
Any ideas?
tia Renda
7 REPLIES 7
Mel Burslan
Honored Contributor

Re: Can I find a users home directory from within a c program?

I am not a c programmer but here is how you determine it in shell scripting:

USER=some_user_name
HOMEDIR=`grep $USER /etc/passwd | cut -d: -f6`

Hope this helps
________________________________
UNIX because I majored in cryptology...
Alan Sengillo_1
Occasional Advisor
Solution

Re: Can I find a users home directory from within a c program?


Use getpwnam() to get the password entry. Use the field pw_dir in the password entry for the home directory.


struct passwd *passwordEntry;

passwordEntry = getpwnam ("user account name goes here");

passwordEntry->pw_dir /* Home directory */
Volker Borowski
Honored Contributor

Re: Can I find a users home directory from within a c program?

I am not a tight programmer,
but I would have thought of accessing the environment variable "~", but I think the getpasswd solution is more elegant.
It just might not work in case you need to use a s-uid bit on your program ?
Would you receive the HOME of the s-bit owner in this case, or the password-entry of the real user ?
This is more a question to Alan than a solution for you, so no points please.
Thanks
Volker
A. Clay Stephenson
Acclaimed Contributor

Re: Can I find a users home directory from within a c program?

The getpwnam(0 function is a good approach. Another is simply

#include


char *home = NULL;
home = getenv("HOME");
if (home != NULL)
{
(void) printf("Homedir: %s\n",home);
}
else
{
(void) fprintf(stderr,"getenv() failed\n");
}

You should note that getenv() uses a static buffer so subsequent calls will overwrite the buffer even if used to getenv("MYVAR"), for example. This means that *home will suddenly change to whatever "MYVAR" was set to. As with all functions of this type, you should copy the thingy to another vaiable if subsequent getenv() calls are expected.
This same fixed-buffer design is used by getpwnam() as well so ...



If it ain't broke, I can fix that.
Alan Sengillo_1
Occasional Advisor

Re: Can I find a users home directory from within a c program?

You pass the login name of the account to getpwnam() so the s-bit would not have any effect on the call.
Renda Skandier
Frequent Advisor

Re: Can I find a users home directory from within a c program?

thanks... getenv is actually what I was thinking of but couldn't remember.

getpwnam looks like ti would work but not all user have access
Tom Danzig
Honored Contributor

Re: Can I find a users home directory from within a c program?

You could also make use of the system() function and the ~user substitution


system("echo ~root");

returns the home directory of root.