LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 04-16-2007, 10:11 AM   #1
JPrieSL
LQ Newbie
 
Registered: Apr 2007
Posts: 3

Rep: Reputation: 0
Obtaining uid from username


Hi all -

I need to obtain a Unix user id from a given username in C/C++, something along the lines of:


int GetUID( const char *username );


There seem to be ways of doing this with other languages, however I'm having trouble finding a way to do this from a standard C/C++ application.

Does anyone know of any unix api functions that may accomplish this?



Thanks in advance -
Josh
 
Old 04-16-2007, 10:52 AM   #2
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
see if this helps

http://www.linuxquestions.org/questi...d.php?t=543727
 
Old 04-18-2007, 08:04 AM   #3
JPrieSL
LQ Newbie
 
Registered: Apr 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Hi - sorry for the delay in responding.

I saw this thread, however my problem is sort of the opposite direction. For any arbitrary username, I need to be able to extract the user ID. The previous thread would be great if I needed to go in the reverse direction, however I can't seem to determine how I could use that function, short of propagating all of the existing user IDs and matching the one which corresponds to the specified user name. Is there any better way to do this?


Thanks,
Josh
 
Old 04-18-2007, 08:09 AM   #4
Guttorm
Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 959

Rep: Reputation: 157Reputation: 157
Hi

There's a similar function "getpwnam" that does what you want. It's in the same man page in my system. But it doesn't return uid but a pointer to a structure that contains it

struct passwd *pointer = getpwnam("username");
int uid = pointer->pw_uid;
 
Old 04-20-2007, 08:31 AM   #5
JPrieSL
LQ Newbie
 
Registered: Apr 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks guys - getpwnam did the trick.

- Josh
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Effective UID and Real UID Milosevic Linux - Newbie 1 01-06-2007 10:41 AM
Changing the UID of a Physical Volume to a specific UID jambraun Linux - Newbie 2 02-09-2006 02:34 PM
OpenLdap PAM UID vs Username Authentication zerovice Linux - Enterprise 0 12-05-2005 09:38 PM
FREEBSD5.3 platform: PHP setcookie() successful, but $uid = $_COOKIE['uid']; fail tclwp Programming 1 03-30-2005 08:05 PM
Getting uid and/or username of logged in user rjstephens Programming 5 09-23-2004 05:02 PM


All times are GMT -5. The time now is 02:40 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration