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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-19-2006, 08:15 PM
|
#1
|
|
Member
Registered: Nov 2005
Posts: 337
Rep:
|
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
|
|
|
|
02-19-2006, 08:29 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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);
}
|
|
|
|
02-19-2006, 08:32 PM
|
#3
|
|
Member
Registered: Nov 2005
Posts: 337
Original Poster
Rep:
|
Great Info!
Thanks Paul
I need to get various info. from the SBC too. Are there some good books that you would recommend?
Jack
|
|
|
|
02-19-2006, 11:22 PM
|
#4
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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.
|
|
|
|
02-20-2006, 01:34 AM
|
#5
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,325
|
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);
...
|
|
|
|
02-20-2006, 01:46 AM
|
#6
|
|
Member
Registered: Nov 2005
Posts: 337
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:07 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|