Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Linux Application Development    (ISBN: 0201308215)


 

 List Price: $47.95
 Our Price: $33.57
 Used Price: $11.50

 Release Date: 20 April, 1998
 Manufacturer: Addison-Wesley Pub Co (Hardcover)
 Sales Rank: 6,653

 Author: Michael K. Johnson, Erik W. Troan









More Info

 Get GID Name 2002-12-01 15:38:16
 
Category: source:c:linux
Description: Convert a gid into its proper name, lists users in your group from /etc/group, and retrieves the passwd string.
Platform: unix
Author: detour
Viewed: 2098
Rating: 5/5 (3 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
getgrgid.c
/* getgrgid.c by detour@metalshell.com
 *
 * Convert a gid into its name, list all the users
 * in the same group, and retrieve the passwd string.
 *
 * http://www.metalshell.com/
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <grp.h>

int main() {
    struct group* grp;
    gid_t gid;
    char** users;
    int x = 1;

    // Get the group id of whoever runs this.
    gid = getgid();
    printf("Group ID: %d\n", gid);

    // If NULL is returned there was an error
    if((grp = getgrgid(gid)) == NULL )
      return 1;

    // Your group name in char format
    printf("Group: %s\n", grp->gr_name );

    // List users in your group, from /etc/group
    printf("Users in your group\n");
    printf("-------------------\n");

    for( users = grp->gr_mem; *users != NULL; users++,++x )
      printf( "%d.  %s\n", x, *users );

    printf("-------------------\n");

    // Encrypted passwd as seen in /etc/passwd.
    if(strcmp(grp->gr_passwd,"x") == 0)
      printf("Password is protected by shadow file.\n");
    else
      printf("Password: %s\n", grp->gr_passwd);

    return 0;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?