LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-19-2006, 08:15 PM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
How to get home directory of a login user


From Fedora 4, how do you get the home dir of a login user. (C/C++ Programming) Say I login as john I get
/home/John
If I login as Kim I get
/home/Kim
Couldn't find it on Google
Thanks
Jack
 
Old 02-19-2006, 08:29 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
1. If you want the home for the user running the program:
Code:
char *s = getenv ("HOME");
2. If you want the home for any arbitrary user, then you'll need to "grep" /etc/passwd:
EXAMPLE:
Code:
  char cmd[256], homedir[256];
  sprintf (cmd, "grep %s /etc/passwd|awk -F : '{print $6}'", username);
  FILE *fp = popen (cmd, "r");
  if (fp)
  {
    fgets (homedir, sizeof homedir, fp);
    pclose (fp);
  }
 
Old 02-19-2006, 08:32 PM   #3
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
Great Info!
Thanks Paul
I need to get various info. from the SBC too. Are there some good books that you would recommend?
Jack
 
Old 02-19-2006, 11:22 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

By "SBC", I assume you mean "Single Board Computer", as in "Embedded System". Correct?

I don't know - you're probably more conversant with the options than I am at this point.

Off the top of my head, "/proc" has a lot of great info - if your SBC's Linux kernel is new enough to support the /proc virtual filesystem:

http://www.comptechdoc.org/os/linux/...ux_hlproc.html
http://www.redhat.com/docs/manuals/l...e/ch-proc.html
<= I'd also suggest looking at /proc for your other question, about memory cache

Although I think I might have already recommended them, a couple of books that might be of use to you include:

Building Embedded Linux Systems, Karim Yaghmour
http://www.bookpool.com/sm/059600222X

Linux Kernel Development, 2nd Ed, Robert Love
http://www.bookpool.com/ss?qs=linux+...t+love&x=0&y=0

'Hope that helps .. PSM

Last edited by paulsm4; 02-19-2006 at 11:24 PM.
 
Old 02-20-2006, 01:34 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by paulsm4
2. If you want the home for any arbitrary user, then you'll need to "grep" /etc/passwd:
EXAMPLE:
Code:
  char cmd[256], homedir[256];
  sprintf (cmd, "grep %s /etc/passwd|awk -F : '{print $6}'", username);
  FILE *fp = popen (cmd, "r");
  if (fp)
  {
    fgets (homedir, sizeof homedir, fp);
    pclose (fp);
  }
A simpler, better and more secure approach would be to use getpwnam.
Code:
#include <pwd.h>

...
        struct passwd *p;
        p=getpwnam(username);
        printf("%s\n", p->pw_dir);
...
 
Old 02-20-2006, 01:46 AM   #6
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
Hi Paul,
Yes. I am working on an application for the Single Board Computer. I need to obtain info like platform that the user is using, cache memory available and other configuration info. I'm now working hard to figure out the rest...
Thanks a lot
Jack
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Limit a user to their home directory redgore Linux - General 2 08-04-2009 10:45 AM
jail user to /home/user directory confused_user Linux - Security 12 03-15-2006 09:56 AM
login error: home directory supposedly doesn't exist jmelton Linux - General 3 11-30-2003 12:42 PM
How is your /home/user directory organized cyris Linux - General 4 04-09-2002 05:31 PM
Restricting user home directory ? chuck77 Linux - General 3 11-27-2001 06:44 AM

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

All times are GMT -5. The time now is 05:54 PM.

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