merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / workben / tgetpwnam.cxx
blob912c6b951532d4b32413de59f0055c3eac0b4f00
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"
31 #include <pwd.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <pthread.h>
36 #include <netdb.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #ifndef NETBSD
41 #include <shadow.h>
42 #endif
44 /* exercises some reentrant libc-fucntions */
46 extern "C"
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);
55 static int go;
59 extern "C" void *workfunc1(void*)
61 char buffer[256];
62 struct tm sttm;
63 time_t nepoch;
64 struct passwd stpwd;
65 struct hostent sthostent;
66 int nerr;
68 printf("starting thread 1 ...\n");
69 while (go) {
70 getpwnam_r("hr", &stpwd, buffer, sizeof(buffer));
71 gethostbyname_r("blauwal", &sthostent, buffer, sizeof(buffer), &nerr);
72 time(&nepoch);
73 localtime_r(&nepoch, &sttm);
75 return 0;
78 extern "C" void *workfunc2(void*)
80 char buffer[256];
81 struct tm sttm;
82 time_t nepoch;
83 struct passwd stpwd;
84 struct hostent sthostent;
85 int nerr;
87 printf("starting thread 2 ...\n");
88 while(go) {
89 getpwnam_r("mh", &stpwd, buffer, sizeof(buffer));
90 gethostbyname_r("hr-1242", &sthostent, buffer, sizeof(buffer), &nerr);
91 time(&nepoch);
92 localtime_r(&nepoch, &sttm);
94 return 0;
98 extern int h_errno;
100 int main(int argc, char *argv[])
102 char buffer[256];
103 struct tm *ptm;
104 time_t nepoch;
105 struct passwd *pwd, *pres1;
106 #ifndef NETBSD
107 struct spwd *spwd, *pres2;
108 #endif
109 struct hostent *phostent, *pres3;
110 char **p;
112 pthread_t tid1,tid2;
113 int res1,res2;
115 go = 1;
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));
124 sleep(3);
126 if (pres1) {
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);
131 #ifdef NETBSD
132 printf("Change: %s", ctime(&pwd->pw_change));
133 printf("Class: %s\n", pwd->pw_class);
134 #else
135 printf("Age: %s\n", pwd->pw_age);
136 printf("Comment: %s\n", pwd->pw_comment);
137 #endif
138 printf("Gecos: %s\n", pwd->pw_gecos);
139 printf("Dir: %s\n", pwd->pw_dir);
140 printf("Shell: %s\n", pwd->pw_shell);
142 else
143 printf("getpwnam_r() failed!\n");
145 free(pwd);
147 #ifndef NETBSD
148 spwd = (struct spwd*)malloc(sizeof(struct spwd));
150 pres2 = getspnam_r("hr", spwd, buffer, sizeof(buffer));
152 if (pres2) {
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);
159 else
160 printf("getspnam_r() failed!\n");
162 free(spwd);
163 #endif
165 ptm = (struct tm*)malloc(sizeof(struct tm));
167 time(&nepoch);
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);
179 #ifdef NETBSD
180 printf("Timezone: %s\n", ptm->tm_zone);
181 #else
182 printf("Timezone: %s\n", ptm->tm_name);
183 #endif
185 free(ptm);
187 phostent = (struct hostent*)malloc(sizeof(struct hostent));
189 pres3 = gethostbyname_r("blauwal", phostent, buffer, sizeof(buffer), h_errno);
191 if (pres3) {
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);
207 if (!pres3) {
208 perror("Expect ERANGE");
210 else
212 printf("ERROR: Check for buffersize went wrong\n");
215 #ifdef NETBSD
216 char exactbuf[35];
217 #else
218 char exactbuf[24]; /* should be exact the necessary size */
219 #endif
220 pres3 = gethostbyname_r("blauwal", phostent, exactbuf, sizeof(exactbuf), &h_errno);
221 if (!pres3) {
222 perror("Check with exact buffersize");
224 else
226 printf("Boundary check ok\n");
229 /* test error conditions */
230 pres3 = gethostbyname_r("nohost", phostent, buffer, sizeof(buffer), &h_errno);
231 if (!pres3) {
232 herror("Expect HOST_NOT_FOUND");
234 else
236 printf("failed to detect non existant host\n");
239 free(phostent);
240 go = 0; /* atomic enough for our purposes */
242 pthread_join(tid1, NULL);
243 pthread_join(tid2, NULL);
245 exit(0);