File: getgrnam.c

package info (click to toggle)
samba-doc-ja 2.2.8a%2Bja1.0-0.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 34,192 kB
  • ctags: 22,693
  • sloc: ansic: 197,587; sh: 8,073; perl: 5,377; makefile: 1,489; awk: 1,146; exp: 1,143; csh: 216
file content (51 lines) | stat: -rw-r--r-- 972 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Lookup a group by name
 */
 
#include <stdio.h>
#include <grp.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
    struct group *gr;
    
    /* Check args */

    if (argc != 2) {
        printf("ERROR: no arg specified\n");
        exit(1);
    }

    /* Do getgrnam() */

    if ((gr = getgrnam(argv[1])) == NULL) {
        printf("FAIL: group %s does not exist\n", argv[1]);
        exit(1);
    }

    /* Print group info */

    printf("PASS: group %s exists\n", argv[1]);
    printf("gr_name = %s\n", gr->gr_name);
    printf("gr_passwd = %s\n", gr->gr_passwd);
    printf("gr_gid = %d\n", gr->gr_gid);
    
    /* Group membership */

    if (gr->gr_mem != NULL) {
        int i = 0;

        printf("gr_mem = ");
        while(gr->gr_mem[i] != NULL) {
            printf("%s", gr->gr_mem[i]);
            i++;
            if (gr->gr_mem != NULL) {
                printf(",");
            }
        }
        printf("\n");
    }

    exit(0);
}