Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/testpw5.c
blob: bb79e3acfd623777ab948514ab46f0181a77e223 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

/* $Id$ */

/* This program just tests getpwent/getpwnam. You want to have nss_ldap
 * plugged in, so to speak, to test anything useful.
 */

#include "config.h"

#ifdef _REENTRANT
#ifdef HAVE_THREAD_H
#include <thread.h>
#else
#include <pthread.h>
#endif
#endif
#include <stdio.h>
#include <pwd.h>
#include <stdlib.h>

#if NeXT
#define uid_t int
#else
#include <dlfcn.h>		/* why? */
#endif

void test_passwd (void);

int ARGC;
char **ARGV;

#define MAX_THREADS  16

void
main (int argc, char **argv)
{
#ifdef _REENTRANT
  int i;
#endif
  ARGC = argc;
  ARGV = argv;

#ifdef _REENTRANT
  for (i = 0; i < MAX_THREADS; i++)
    {
      thread_t tid;
      thr_create (NULL, 0, test_passwd, NULL, 0, &tid);
      thr_continue (tid);
    }
  while (thr_join (NULL, NULL, NULL) == 0);
#else
  test_passwd ();
#endif
  exit (0);
}

#ifdef _REENTRANT
static void
ret (int status)
{
  thr_exit (&status);
}
#else
#define ret exit
#endif

void
test_passwd (void)
{
  struct passwd *pw;
  uid_t uid;
#ifdef _REENTRANT
  char buf[1024];
  struct passwd pbuf;
#endif



  printf (">>>>>> setpwent()\n");
  setpwent ();

  printf (">>>>>> getpwent()\n");
  scan_passwd ();

  printf (">>>>>> endpwent()\n");
  endpwent ();

  ret (0);
}

scan_passwd ()
{
  int i = 1;
  struct passwd *p;
#ifdef _REENTRANT
  char buf[1024];
  struct passwd pbuf;
  while ((p = getpwent_r (&pbuf, buf, sizeof (buf))) != NULL)
#else
  while ((p = getpwent ()) != NULL)
#endif
    {
      printf ("%s:%s:%d:%d:%s:%s:%s\n",
	      p->pw_name,
	      p->pw_passwd,
	      p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);

      if (p == NULL)
	ret (1);

#ifdef _REENTRANT
      p = getpwnam_r (p->pw_name, &pbuf, buf, sizeof (buf));
#else
      p = getpwnam (p->pw_name);
#endif

      if (p == NULL)
	ret (2);

      printf ("%s:%s:%d:%d:%s:%s:%s\n",
	      p->pw_name,
	      p->pw_passwd,
	      p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);

      i++;
    }
}