r23797: started fixing old FSF addresses. Fixed pcap2nbench COPYING file
[ira/wip.git] / testsuite / nsswitch / getgrent_r.c
1 /*
2  * Use set/get/endgrent calls from two processes to iterate over the
3  * password database.  This checks the multithreaded stuff works.
4  */
5
6 #include <stdio.h>
7 #include <grp.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <wait.h>
13
14 void dump_grent(char *id)
15 {
16     struct group *gr;
17     char fname[255];
18     FILE *fptr;
19
20     /* Open results file */
21
22     sprintf(fname, "/tmp/getgrent_r-%s.out-%d", id, getpid());
23
24     if ((fptr = fopen(fname, "w")) < 0) {
25         fprintf(stderr, "ERROR: could not open file %s: %s\n", fname,
26                 sys_errlist[errno]);
27         return;
28     }
29
30     /* Dump group database */
31
32     setgrent();
33         
34     while((gr = getgrent()) != NULL) {
35         fprintf(fptr,"%s:%s:%d:%d\n", gr->gr_name, gr->gr_passwd,
36                 gr->gr_gid);
37     }
38         
39     endgrent();
40
41     /* Close results file */
42
43     fclose(fptr);
44 }
45
46 int main(int argc, char **argv)
47 {
48     pid_t pid;
49
50     /* Check args */
51
52     if (argc != 2) {
53         printf("ERROR: must specify output file identifier\n");
54         return 1;
55     }
56
57     /* Fork child process */
58
59     if ((pid = fork()) == -1) {
60         printf("ERROR: unable to fork\n");
61         return 1;
62     }
63
64     /* Handle test case */
65
66     if (pid > 0) {
67         int status;
68
69         /* Parent */
70
71         dump_grent(argv[1]);
72         wait(&status);
73
74     } else {
75
76         /* Child */
77
78         dump_grent(argv[1]);
79         return 0;
80     }
81
82     printf("PASS: run getgrent_r.c\n");
83     return 0;
84 }