lib: Fix a valgrind error
[Samba.git] / testsuite / nsswitch / getpwent_r.c
blob5e774911de73fc56d188711dfe7faaa4445e5bca
1 /*
2 * Use set/get/endpwent calls from two processes to iterate over the
3 * password database. This checks the multithreaded stuff works.
4 */
6 #include <stdio.h>
7 #include <pwd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <wait.h>
14 void dump_pwent(char *id)
16 struct passwd *pw;
17 char fname[255];
18 FILE *fptr;
20 /* Open results file */
22 sprintf(fname, "/tmp/getpwent_r-%s.out-%d", id, getpid());
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;
30 /* Dump passwd database */
32 setpwent();
34 while((pw = getpwent()) != NULL) {
35 fprintf(fptr,"%s:%s:%s:%d:%d\n", pw->pw_name, pw->pw_passwd,
36 pw->pw_gecos, pw->pw_uid, pw->pw_gid);
39 endpwent();
41 /* Close results file */
43 fclose(fptr);
46 #define NUM_FORKS 2
48 int main(int argc, char **argv)
50 pid_t pids[NUM_FORKS];
51 int i, status;
53 /* Check args */
55 if (argc != 2) {
56 printf("ERROR: must specify output file identifier\n");
57 return 1;
60 for(i = 0; i < NUM_FORKS; i++) {
62 /* Fork off lots */
64 if ((pids[i] = fork()) == -1) {
65 perror("fork");
66 return 1;
69 /* Child does tests */
71 if (pids[i] == 0) {
72 dump_pwent(argv[1]);
73 return 0;
77 /* Wait for everyone to finish */
79 for (i = 0; i < NUM_FORKS; i++) {
80 waitpid(pids[i], &status, 0);
83 printf("PASS: getpwent_r.c\n");
84 return 0;