1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
44 /* exercises some reentrant libc-fucntions */
48 struct tm
*localtime_r(const time_t *timep
, struct tm
*buffer
);
49 struct passwd
* getpwnam_r(char*, struct passwd
*, char *, int);
50 struct spwd
* getspnam_r(char*, struct spwd
*, char *, int);
51 struct hostent
*gethostbyname_r(const char *name
, struct hostent
*result
,
52 char *buffer
, int buflen
, int *h_errnop
);
59 extern "C" void *workfunc1(void*)
65 struct hostent sthostent
;
68 printf("starting thread 1 ...\n");
70 getpwnam_r("hr", &stpwd
, buffer
, sizeof(buffer
));
71 gethostbyname_r("blauwal", &sthostent
, buffer
, sizeof(buffer
), &nerr
);
73 localtime_r(&nepoch
, &sttm
);
78 extern "C" void *workfunc2(void*)
84 struct hostent sthostent
;
87 printf("starting thread 2 ...\n");
89 getpwnam_r("mh", &stpwd
, buffer
, sizeof(buffer
));
90 gethostbyname_r("hr-1242", &sthostent
, buffer
, sizeof(buffer
), &nerr
);
92 localtime_r(&nepoch
, &sttm
);
100 int main(int argc
, char *argv
[])
105 struct passwd
*pwd
, *pres1
;
107 struct spwd
*spwd
, *pres2
;
109 struct hostent
*phostent
, *pres3
;
117 pthread_create(&tid1
, NULL
, workfunc1
, &res1
);
118 pthread_create(&tid2
, NULL
, workfunc2
, &res2
);
120 pwd
= (struct passwd
*)malloc(sizeof(struct passwd
));
122 pres1
= getpwnam_r("hr", pwd
, buffer
, sizeof(buffer
));
127 printf("Name: %s\n", pwd
->pw_name
);
128 printf("Passwd: %s\n", pwd
->pw_passwd
);
129 printf("Uid: %d\n", pwd
->pw_uid
);
130 printf("Gid: %d\n", pwd
->pw_gid
);
132 printf("Change: %s", ctime(&pwd
->pw_change
));
133 printf("Class: %s\n", pwd
->pw_class
);
135 printf("Age: %s\n", pwd
->pw_age
);
136 printf("Comment: %s\n", pwd
->pw_comment
);
138 printf("Gecos: %s\n", pwd
->pw_gecos
);
139 printf("Dir: %s\n", pwd
->pw_dir
);
140 printf("Shell: %s\n", pwd
->pw_shell
);
143 printf("getpwnam_r() failed!\n");
148 spwd
= (struct spwd
*)malloc(sizeof(struct spwd
));
150 pres2
= getspnam_r("hr", spwd
, buffer
, sizeof(buffer
));
153 printf("Name: %s\n", spwd
->sp_namp
);
154 printf("Passwd: %s\n", spwd
->sp_pwdp
);
155 printf("Last Change: %ld\n", spwd
->sp_lstchg
);
156 printf("Min: %ld\n", spwd
->sp_min
);
157 printf("Max: %ld\n", spwd
->sp_max
);
160 printf("getspnam_r() failed!\n");
165 ptm
= (struct tm
*)malloc(sizeof(struct tm
));
168 localtime_r(&nepoch
, ptm
);
170 printf("Seconds: %d\n", ptm
->tm_sec
);
171 printf("Minutes: %d\n", ptm
->tm_min
);
172 printf("Hour: %d\n", ptm
->tm_hour
);
173 printf("Day of Month: %d\n", ptm
->tm_mday
);
174 printf("Month: %d\n", ptm
->tm_mon
);
175 printf("Year: %d\n", ptm
->tm_year
);
176 printf("Day of week: %d\n", ptm
->tm_wday
);
177 printf("Day in the year: %d\n", ptm
->tm_yday
);
178 printf("Daylight saving time: %d\n", ptm
->tm_isdst
);
180 printf("Timezone: %s\n", ptm
->tm_zone
);
182 printf("Timezone: %s\n", ptm
->tm_name
);
187 phostent
= (struct hostent
*)malloc(sizeof(struct hostent
));
189 pres3
= gethostbyname_r("blauwal", phostent
, buffer
, sizeof(buffer
), h_errno
);
192 printf("Official Hostname: %s\n", phostent
->h_name
);
193 for ( p
= phostent
->h_aliases
; *p
!= NULL
; p
++ )
194 printf("Alias: %s\n", *p
);
195 printf("Addresstype: %d\n", phostent
->h_addrtype
);
196 printf("Address length: %d\n", phostent
->h_length
);
197 if ( phostent
->h_addrtype
== AF_INET
) {
198 for ( p
= phostent
->h_addr_list
; *p
!= NULL
; *p
++ )
199 printf("Address: %s\n", inet_ntoa(**((in_addr
**)p
)));
204 /* test boundary conditions */
205 char smallbuf
[23]; /* buffer to small */
206 pres3
= gethostbyname_r("blauwal", phostent
, smallbuf
, sizeof(smallbuf
), h_errno
);
208 perror("Expect ERANGE");
212 printf("ERROR: Check for buffersize went wrong\n");
218 char exactbuf
[24]; /* should be exact the necessary size */
220 pres3
= gethostbyname_r("blauwal", phostent
, exactbuf
, sizeof(exactbuf
), &h_errno
);
222 perror("Check with exact buffersize");
226 printf("Boundary check ok\n");
229 /* test error conditions */
230 pres3
= gethostbyname_r("nohost", phostent
, buffer
, sizeof(buffer
), &h_errno
);
232 herror("Expect HOST_NOT_FOUND");
236 printf("failed to detect non existant host\n");
240 go
= 0; /* atomic enough for our purposes */
242 pthread_join(tid1
, NULL
);
243 pthread_join(tid2
, NULL
);