"Fossies" - the Fresh Open Source Software Archive 
Member "nss_ldap-265/tests/testpw.c" (6 Nov 2009, 2759 Bytes) of package /linux/privat/old/nss_ldap-265.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1
2 /* $Id: testpw.c,v 1.2 2001/01/09 00:21:22 lukeh Exp $ */
3
4 /* This program just tests getpwent/getpwnam. You want to have nss_ldap
5 * plugged in, so to speak, to test anything useful.
6 */
7
8 #include "config.h"
9
10 #ifdef _REENTRANT
11 #ifdef HAVE_PTHREAD_H
12 #include <pthread.h>
13 #else
14 #include <thread.h>
15 #endif /* _REENTRANT */
16
17 #endif /* HAVE_PTHREAD_H */
18 #include <stdio.h>
19 #include <pwd.h>
20 #include <sys/types.h>
21
22 #if NeXT
23 #define uid_t int
24 #else
25 #include <dlfcn.h> /* why? */
26 #endif
27
28 void test_passwd (void);
29 void scan_passwd (void);
30
31 int ARGC;
32 char **ARGV;
33
34 #define MAX_THREADS 16
35
36 void
37 main (int argc, char **argv)
38 {
39 #ifdef _REENTRANT
40 int i;
41 #endif
42 ARGC = argc;
43 ARGV = argv;
44
45 #ifdef _REENTRANT
46 for (i = 0; i < MAX_THREADS; i++)
47 {
48 #ifdef HAVE_PTHREAD_H
49 pthread_t tid;
50 pthread_create (NULL, 0, test_passwd, NULL, 0, &tid);
51 pthread_continue (tid);
52 #else
53 thread_t tid;
54 thr_create (NULL, 0, test_passwd, NULL, 0, &tid);
55 thr_continue (tid);
56 #endif /* HAVE_PTHREAD_H */
57 }
58 while (thr_join (NULL, NULL, NULL) == 0);
59 #else
60 test_passwd ();
61 #endif
62 exit (0);
63 }
64
65 #ifdef _REENTRANT
66 static void
67 ret (int status)
68 {
69 thr_exit (&status);
70 }
71 #else
72 #define ret exit
73 #endif
74
75 void
76 test_passwd (void)
77 {
78 struct passwd *pw;
79 uid_t uid;
80 #ifdef _REENTRANT
81 char buf[1024];
82 struct passwd pbuf;
83 #endif
84
85 printf (">>>>>> getpwnam(\"%s\")\n", ARGC > 1 ? ARGV[1] : "root");
86 #ifdef _REENTRANT
87 pw = getpwnam_r (ARGC > 1 ? ARGV[1] : "root", &pbuf, buf, sizeof (buf));
88 #else
89 pw = getpwnam (ARGC > 1 ? ARGV[1] : "root");
90 #endif
91
92 if (!pw)
93 ret (1);
94
95 printf ("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid,
96 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
97 uid = pw->pw_uid;
98
99 printf (">>>>>> getpwuid(%d)\n", uid);
100
101 #ifdef _REENTRANT
102 pw = getpwuid_r (uid, &pbuf, buf, sizeof (buf));
103 #else
104 pw = getpwuid (uid);
105 #endif
106
107 if (!pw)
108 ret (1);
109
110 printf ("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid,
111 pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
112
113 if (ARGC > 2 && !strcmp (ARGV[2], "no"))
114 {
115 printf (">>>>>> Enumeration skipped.\n");
116 }
117 else
118 {
119 printf (">>>>>> setpwent()\n");
120 setpwent ();
121
122 printf (">>>>>> getpwent()\n");
123 scan_passwd ();
124
125 printf (">>>>>> endpwent()\n");
126 endpwent ();
127 }
128
129 ret (0);
130 }
131
132 void
133 scan_passwd (void)
134 {
135 int i = 1;
136 struct passwd *p;
137 #ifdef _REENTRANT
138 char buf[1024];
139 struct passwd pbuf;
140 while ((p = getpwent_r (&pbuf, buf, sizeof (buf))) != NULL)
141 #else
142 while ((p = getpwent ()) != NULL)
143 #endif
144 {
145 printf ("%s:%s:%d:%d:%s:%s:%s\n",
146 p->pw_name,
147 p->pw_passwd,
148 p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
149 i++;
150 }
151 }