summaryrefslogtreecommitdiff
path: root/testsuite/nsswitch/getgrent_r.c
blob: 3eac8aa218da39ea2f4c698093043d906f7bbf50 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Use set/get/endgrent calls from two processes to iterate over the
 * password database.  This checks the multithreaded stuff works.
 */

#include <stdio.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <wait.h>

void dump_grent(char *id)
{
    struct group *gr;
    char fname[255];
    FILE *fptr;

    /* Open results file */

    sprintf(fname, "/tmp/getgrent_r-%s.out-%d", id, getpid());

    if ((fptr = fopen(fname, "w")) < 0) {
        fprintf(stderr, "ERROR: could not open file %s: %s\n", fname,
                sys_errlist[errno]);
        return;
    }

    /* Dump group database */

    setgrent();
        
    while((gr = getgrent()) != NULL) {
        fprintf(fptr,"%s:%s:%d:%d\n", gr->gr_name, gr->gr_passwd,
                gr->gr_gid);
    }
        
    endgrent();

    /* Close results file */

    fclose(fptr);
}

int main(int argc, char **argv)
{
    pid_t pid;

    /* Check args */

    if (argc != 2) {
        printf("ERROR: must specify output file identifier\n");
        return 1;
    }

    /* Fork child process */

    if ((pid = fork()) == -1) {
        printf("ERROR: unable to fork\n");
        return 1;
    }

    /* Handle test case */

    if (pid > 0) {
        int status;

        /* Parent */

        dump_grent(argv[1]);
        wait(&status);

    } else {

        /* Child */

        dump_grent(argv[1]);
        return 0;
    }

    printf("PASS: run getgrent_r.c\n");
    return 0;
}