LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-27-2007, 02:14 AM   #1
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
C: expanding "~user" notation


Does anyone know of a simple way to find the home directory of a user if the path is given as "~user/path/to/somewhere"? I'm looking for anything... personal code you've developed or a library routine you might know of.

I can't find anything appropriate with apropos and searching for "home directory program username" on Google still gives no meaningful results... not sure how else I could refine the search.

I can write a parsing routine, but don't want to reinvent the wheel, and I know the wheel has been invented somewhere out there.

I know I can call popen() with a piped sequence sequence of commands to pull the information out of /etc/passwd ("grep '^<username>:' /etc/passwd | cut -f 5 -d ':'"), but I want to leave that as a last resort.

I know the above might sound picky (maybe even a lot), but I would really appreciate any code or just a nudge in the right direction.

Thanks.
 
Old 02-27-2007, 03:33 AM   #2
tread
LQ Newbie
 
Registered: Feb 2007
Posts: 29

Rep: Reputation: 15
Is getpwnam what you are looking for?
man getpwname shows:

Code:
struct passwd *getpwnam(const char *name); /* name is user name*/

struct passwd {
    char    *pw_name;      /* user name */
    char    *pw_passwd;    /* user password */
    uid_t   pw_uid;        /* user id */
    gid_t   pw_gid;        /* group id */
    char    *pw_gecos;     /* real name */
    char    *pw_dir;       /* home directory */
    char    *pw_shell;     /* shell program */
};
 
Old 02-27-2007, 08:26 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

getenv("HOME") gives the home directory too.
 
Old 02-27-2007, 09:27 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Original Poster
Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
getpwnam() is exactly what I was looking for.

I use getenv() too, but if a user wants to specify another user's home directory, then getenv() won't work.

Thank you both.
 
  


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
chrooted user: "write" and "talk" don't work. ldp Linux - Software 2 04-12-2005 02:05 AM
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
"User" & "System" CPU load difference JJX Linux - General 3 06-06-2004 01:42 AM
Shorten a Qmail user name to "user" instead of "user@domain.com" anorman Linux - Software 0 12-12-2003 08:29 AM
klipper not expanding "%s" randallrayusa Linux - Newbie 1 05-15-2003 11:36 PM

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

All times are GMT -5. The time now is 06:27 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