LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2007, 06:24 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
how to get current user home directory


Hello everyone,


How to use Linux API to get current user home directory, e.g. for user foo, the home directory is /home/foo, for user root, the home directory is /root.


thanks in advance,
George
 
Old 05-06-2007, 06:28 AM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
I like to use

getenv("HOME");

Don't know if it's the best way to do it.
 
Old 05-06-2007, 06:38 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Standard way to do this:
Code:
#include <sys/types/h>
#include <pwd.h>
#include <inistd.h>

char *get_login_dir(char *dest)
{
     struct passwd *retval=getpwuid(getuid() );
      
     dest=NULL;
     if(retval!=NULL)
          strcpy(dest, retval->pw_dir);
     return dest;
}
 
Old 05-06-2007, 01:31 PM   #4
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 58
In shell scripting you can just type $HOME
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create a new partition for current /home directory? parv Linux - Hardware 3 02-27-2007 09:21 AM
Active Directory User Cannot Write to Samba Home Directory jonwatson Linux - Networking 2 12-19-2006 12:40 PM
Getting the home directory of current user in c++ jimsproch Programming 2 04-10-2006 01:15 AM
jail user to /home/user directory confused_user Linux - Security 12 03-15-2006 09:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration