-
April 22nd, 2005, 09:06 PM
#1
Senior Member
Quick question, linux programming
Quick and stupid question but I don't really know where to begin to search for this...
In linux, what function should I use to convert a uid to a user name, or a user name to a uid for comparason purposes?
Hopefully some of you linux gurus can help me out here
-
April 22nd, 2005, 09:49 PM
#2
In what language are you thinking ?
Or do you mean the command line..
In that casegives you the uid of a user.
I don't know if there is an easy way to get a username for an id.. but
Code:
grep :uid: /etc/passwd | cut -d ":" -f 1
should do the trick
ASCII stupid question, get a stupid ANSI.
When in Russia, pet a PETSCII.
Get your ass over to SLAYRadio the best station for C64 Remixes !
-
April 23rd, 2005, 02:17 AM
#3
Senior Member
sorry i should have specified...
I'm editing the debian pmount utility to work with gentoo, hal, and gvm, instead of using fstab-sync which is unsafe and just didn't work period for me, and want to allow users in the plugdev group to unmount devices. So far I can get the gid of the mounting user through /etc/mtab, and can get a vector of user names in that group through getgrgid(). I would like to check to see if the calling user (getuid()) is in the group that mounted the volume to allow that user to unmount it.
is there a way to use either method in c, or preferrebly a function that i can call to get either an id or a user name?
upon further googling, getpwnam() looks promising, i'll get back to you if it works
Thanks!
-
April 23rd, 2005, 03:33 PM
#4
Hi
You almost found the correct function. I just did an illustration:
Code:
#include <sys/types.h>
#include <pwd.h>
int main(int argv, char *argc[]){
struct passwd *pw;
uid_t uid;
pw=getpwnam(argc[1]);
if (pw) // assuming it is a username
printf("%s found %d:%d\n",argc[1],pw->pw_uid,pw->pw_gid);
else{ // trying ID
uid=atoi(argc[1]);
pw=getpwuid(uid);
if (pw) // assuming it is a uid
printf("%s found %d:%d\n",pw->pw_name,pw->pw_uid,pw->pw_gid);
}
return 0;
}
Start the above little program with either a username or the uid.
Of course, you simple can modify it using uid=getuid();
Cheers.
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
-
April 23rd, 2005, 05:57 PM
#5
Senior Member
Thanks a bunch sec_ware that did the trick
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|