FAQ > Environment and user data


Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > Linux/Unix Specific > Environment and user data

This item was added on: 2003/03/07

  • How do I get my UID?

  • How do I get the user's login name?

  • How do I get user information from /etc/passwd without parsing?

  • How do I test a plain text password against an /etc/passwd entry?


  • How do I get my UID?

    
    #include <sys/types.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    
    uid_t u = getuid();
    printf("Real UID: %u\n", u);
    u = geteuid();
    printf("Effective UID: %u\n", u);
    
    



    How do I get the user's login name?

    
    #include <unistd.h> 
    #include <stdio.h> 
    
    char *p = getlogin();
    if( p ) 
    {
      printf( "Hello %s\n", p );
    }
    
    



    How do I get user information from /etc/passwd without parsing?

    
    #include <sys/types.h> 
    #include <pwd.h> 
    
    struct passwd *p;
    p = getpwnam( getlogin() );
    p = getpwuid( getuid() );
    /* ... */
    
    



    How do I test a plain text password against an /etc/passwd entry?

    
    #include <sys/types.h> 
    #include <pwd.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    
    struct passwd *p;
    p = getpwnam( "username" );
    if (p) 
    {
      if( strcmp( crypt( "Password", p->pw_passwd ), p->pw_passwd ) == 0 ) 
      {
        puts( "match" );
      } 
      else 
      {
        puts( "no match" );
      }
    }
    
    



    Credit: vVv

    Script provided by SmartCGIs