update dev300-m57
[ooovba.git] / sal / workben / tgetpwnam.cxx
blob9270621a9248bcf7952b435f2a0a404c4bf2901f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tgetpwnam.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 #include <pwd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <netdb.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #ifndef NETBSD
44 #include <shadow.h>
45 #endif
47 /* exercises some reentrant libc-fucntions */
49 extern "C"
51 struct tm *localtime_r(const time_t *timep, struct tm *buffer);
52 struct passwd* getpwnam_r(char*, struct passwd*, char *, int);
53 struct spwd* getspnam_r(char*, struct spwd*, char *, int);
54 struct hostent *gethostbyname_r(const char *name, struct hostent *result,
55 char *buffer, int buflen, int *h_errnop);
58 static int go;
62 extern "C" void *workfunc1(void*)
64 char buffer[256];
65 struct tm sttm;
66 time_t nepoch;
67 struct passwd stpwd;
68 struct hostent sthostent;
69 int nerr;
71 printf("starting thread 1 ...\n");
72 while (go) {
73 getpwnam_r("hr", &stpwd, buffer, sizeof(buffer));
74 gethostbyname_r("blauwal", &sthostent, buffer, sizeof(buffer), &nerr);
75 time(&nepoch);
76 localtime_r(&nepoch, &sttm);
78 return 0;
81 extern "C" void *workfunc2(void*)
83 char buffer[256];
84 struct tm sttm;
85 time_t nepoch;
86 struct passwd stpwd;
87 struct hostent sthostent;
88 int nerr;
90 printf("starting thread 2 ...\n");
91 while(go) {
92 getpwnam_r("mh", &stpwd, buffer, sizeof(buffer));
93 gethostbyname_r("hr-1242", &sthostent, buffer, sizeof(buffer), &nerr);
94 time(&nepoch);
95 localtime_r(&nepoch, &sttm);
97 return 0;
101 extern int h_errno;
103 int main(int argc, char *argv[])
105 char buffer[256];
106 struct tm *ptm;
107 time_t nepoch;
108 struct passwd *pwd, *pres1;
109 #ifndef NETBSD
110 struct spwd *spwd, *pres2;
111 #endif
112 struct hostent *phostent, *pres3;
113 char **p;
115 pthread_t tid1,tid2;
116 int res1,res2;
118 go = 1;
120 pthread_create(&tid1, NULL, workfunc1, &res1);
121 pthread_create(&tid2, NULL, workfunc2, &res2);
123 pwd = (struct passwd*)malloc(sizeof(struct passwd));
125 pres1 = getpwnam_r("hr", pwd, buffer, sizeof(buffer));
127 sleep(3);
129 if (pres1) {
130 printf("Name: %s\n", pwd->pw_name);
131 printf("Passwd: %s\n", pwd->pw_passwd);
132 printf("Uid: %d\n", pwd->pw_uid);
133 printf("Gid: %d\n", pwd->pw_gid);
134 #ifdef NETBSD
135 printf("Change: %s", ctime(&pwd->pw_change));
136 printf("Class: %s\n", pwd->pw_class);
137 #else
138 printf("Age: %s\n", pwd->pw_age);
139 printf("Comment: %s\n", pwd->pw_comment);
140 #endif
141 printf("Gecos: %s\n", pwd->pw_gecos);
142 printf("Dir: %s\n", pwd->pw_dir);
143 printf("Shell: %s\n", pwd->pw_shell);
145 else
146 printf("getpwnam_r() failed!\n");
148 free(pwd);
150 #ifndef NETBSD
151 spwd = (struct spwd*)malloc(sizeof(struct spwd));
153 pres2 = getspnam_r("hr", spwd, buffer, sizeof(buffer));
155 if (pres2) {
156 printf("Name: %s\n", spwd->sp_namp);
157 printf("Passwd: %s\n", spwd->sp_pwdp);
158 printf("Last Change: %ld\n", spwd->sp_lstchg);
159 printf("Min: %ld\n", spwd->sp_min);
160 printf("Max: %ld\n", spwd->sp_max);
162 else
163 printf("getspnam_r() failed!\n");
165 free(spwd);
166 #endif
168 ptm = (struct tm*)malloc(sizeof(struct tm));
170 time(&nepoch);
171 localtime_r(&nepoch, ptm);
173 printf("Seconds: %d\n", ptm->tm_sec);
174 printf("Minutes: %d\n", ptm->tm_min);
175 printf("Hour: %d\n", ptm->tm_hour);
176 printf("Day of Month: %d\n", ptm->tm_mday);
177 printf("Month: %d\n", ptm->tm_mon);
178 printf("Year: %d\n", ptm->tm_year);
179 printf("Day of week: %d\n", ptm->tm_wday);
180 printf("Day in the year: %d\n", ptm->tm_yday);
181 printf("Daylight saving time: %d\n", ptm->tm_isdst);
182 #ifdef NETBSD
183 printf("Timezone: %s\n", ptm->tm_zone);
184 #else
185 printf("Timezone: %s\n", ptm->tm_name);
186 #endif
188 free(ptm);
190 phostent = (struct hostent*)malloc(sizeof(struct hostent));
192 pres3 = gethostbyname_r("blauwal", phostent, buffer, sizeof(buffer), h_errno);
194 if (pres3) {
195 printf("Official Hostname: %s\n", phostent->h_name);
196 for ( p = phostent->h_aliases; *p != NULL; p++ )
197 printf("Alias: %s\n", *p);
198 printf("Addresstype: %d\n", phostent->h_addrtype);
199 printf("Address length: %d\n", phostent->h_length);
200 if ( phostent->h_addrtype == AF_INET ) {
201 for ( p = phostent->h_addr_list; *p != NULL; *p++ )
202 printf("Address: %s\n", inet_ntoa(**((in_addr**)p)));
207 /* test boundary conditions */
208 char smallbuf[23]; /* buffer to small */
209 pres3 = gethostbyname_r("blauwal", phostent, smallbuf, sizeof(smallbuf), h_errno);
210 if (!pres3) {
211 perror("Expect ERANGE");
213 else
215 printf("ERROR: Check for buffersize went wrong\n");
218 #ifdef NETBSD
219 char exactbuf[35];
220 #else
221 char exactbuf[24]; /* should be exact the necessary size */
222 #endif
223 pres3 = gethostbyname_r("blauwal", phostent, exactbuf, sizeof(exactbuf), &h_errno);
224 if (!pres3) {
225 perror("Check with exact buffersize");
227 else
229 printf("Boundary check ok\n");
232 /* test error conditions */
233 pres3 = gethostbyname_r("nohost", phostent, buffer, sizeof(buffer), &h_errno);
234 if (!pres3) {
235 herror("Expect HOST_NOT_FOUND");
237 else
239 printf("failed to detect non existant host\n");
242 free(phostent);
243 go = 0; /* atomic enough for our purposes */
245 pthread_join(tid1, NULL);
246 pthread_join(tid2, NULL);
248 exit(0);