1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tgetpwnam.cxx,v $
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"
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
47 /* exercises some reentrant libc-fucntions */
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
);
62 extern "C" void *workfunc1(void*)
68 struct hostent sthostent
;
71 printf("starting thread 1 ...\n");
73 getpwnam_r("hr", &stpwd
, buffer
, sizeof(buffer
));
74 gethostbyname_r("blauwal", &sthostent
, buffer
, sizeof(buffer
), &nerr
);
76 localtime_r(&nepoch
, &sttm
);
81 extern "C" void *workfunc2(void*)
87 struct hostent sthostent
;
90 printf("starting thread 2 ...\n");
92 getpwnam_r("mh", &stpwd
, buffer
, sizeof(buffer
));
93 gethostbyname_r("hr-1242", &sthostent
, buffer
, sizeof(buffer
), &nerr
);
95 localtime_r(&nepoch
, &sttm
);
103 int main(int argc
, char *argv
[])
108 struct passwd
*pwd
, *pres1
;
110 struct spwd
*spwd
, *pres2
;
112 struct hostent
*phostent
, *pres3
;
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
));
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
);
135 printf("Change: %s", ctime(&pwd
->pw_change
));
136 printf("Class: %s\n", pwd
->pw_class
);
138 printf("Age: %s\n", pwd
->pw_age
);
139 printf("Comment: %s\n", pwd
->pw_comment
);
141 printf("Gecos: %s\n", pwd
->pw_gecos
);
142 printf("Dir: %s\n", pwd
->pw_dir
);
143 printf("Shell: %s\n", pwd
->pw_shell
);
146 printf("getpwnam_r() failed!\n");
151 spwd
= (struct spwd
*)malloc(sizeof(struct spwd
));
153 pres2
= getspnam_r("hr", spwd
, buffer
, sizeof(buffer
));
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
);
163 printf("getspnam_r() failed!\n");
168 ptm
= (struct tm
*)malloc(sizeof(struct tm
));
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
);
183 printf("Timezone: %s\n", ptm
->tm_zone
);
185 printf("Timezone: %s\n", ptm
->tm_name
);
190 phostent
= (struct hostent
*)malloc(sizeof(struct hostent
));
192 pres3
= gethostbyname_r("blauwal", phostent
, buffer
, sizeof(buffer
), h_errno
);
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
);
211 perror("Expect ERANGE");
215 printf("ERROR: Check for buffersize went wrong\n");
221 char exactbuf
[24]; /* should be exact the necessary size */
223 pres3
= gethostbyname_r("blauwal", phostent
, exactbuf
, sizeof(exactbuf
), &h_errno
);
225 perror("Check with exact buffersize");
229 printf("Boundary check ok\n");
232 /* test error conditions */
233 pres3
= gethostbyname_r("nohost", phostent
, buffer
, sizeof(buffer
), &h_errno
);
235 herror("Expect HOST_NOT_FOUND");
239 printf("failed to detect non existant host\n");
243 go
= 0; /* atomic enough for our purposes */
245 pthread_join(tid1
, NULL
);
246 pthread_join(tid2
, NULL
);