- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Can I find a users home directory from within a c ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 03:51 AM
тАО10-06-2005 03:51 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 03:54 AM
тАО10-06-2005 03:54 AM
Re: Can I find a users home directory from within a c program?
USER=some_user_name
HOMEDIR=`grep $USER /etc/passwd | cut -d: -f6`
Hope this helps
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 04:42 AM
тАО10-06-2005 04:42 AM
SolutionUse 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 */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 06:05 AM
тАО10-06-2005 06:05 AM
Re: Can I find a users home directory from within a c program?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 06:07 AM
тАО10-06-2005 06:07 AM
Re: Can I find a users home directory from within a c program?
#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 ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 06:17 AM
тАО10-06-2005 06:17 AM
Re: Can I find a users home directory from within a c program?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 06:28 AM
тАО10-06-2005 06:28 AM
Re: Can I find a users home directory from within a c program?
getpwnam looks like ti would work but not all user have access
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-06-2005 06:40 AM
тАО10-06-2005 06:40 AM
Re: Can I find a users home directory from within a c program?
system("echo ~root");
returns the home directory of root.