Results 1 to 5 of 5

Thread: Quick question, linux programming

  1. #1
    Senior Member
    Join Date
    Oct 2003
    Posts
    234

    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

  2. #2
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    In what language are you thinking ?

    Or do you mean the command line..
    In that case
    Code:
    id -u username
    gives 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 !

  3. #3
    Senior Member
    Join Date
    Oct 2003
    Posts
    234
    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!

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    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)

  5. #5
    Senior Member
    Join Date
    Oct 2003
    Posts
    234
    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
  •