Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / Unload_libACE.cpp
blob17088486a3273540ad58205173beab714c3e90e0
2 //=============================================================================
3 /**
4 * @file Unload_libACE.cpp
6 * This is a simple test of library unloading that uses
7 * an application which has _not_ been linked with libACE
8 * but uses dlopen() to dynamically load libACE
9 * and then uses dlclose() to unload it.
11 * @author David Smith <dts@prismtech.com> and Don Sharp <Donald.Sharp@prismtech.com>
13 //=============================================================================
16 //FUZZ: disable check_for_lack_ACE_OS
17 //FUZZ: disable check_for_improper_main_declaration
19 #include <stdio.h>
21 #undef UNLOAD_LIBACE_TEST
23 #if defined (__GNUC__)
24 #if !defined (ACE_VXWORKS) && !defined (__MINGW32__) && !defined (__CYGWIN32__)
25 #define UNLOAD_LIBACE_TEST 1
26 #endif /* !ACE_VXWORKS && !__MINGW32__ && !CYGWIN32 */
27 #endif /* __GNUC__ */
29 #if defined (ACE_AS_STATIC_LIBS)
30 #undef UNLOAD_LIBACE_TEST
31 #endif /* ACE_AS_STATIC_LIBS */
33 #ifdef UNLOAD_LIBACE_TEST
35 #include <errno.h>
36 #include <dlfcn.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
42 #define TIME_STAMP_FIELD_WIDTH 32
44 static char *
45 time_stamp (char date_and_time[], int date_and_timelen, int format)
47 static char const *const month_name[] =
49 "Jan",
50 "Feb",
51 "Mar",
52 "Apr",
53 "May",
54 "Jun",
55 "Jul",
56 "Aug",
57 "Sep",
58 "Oct",
59 "Nov",
60 "Dec"
63 static char const *const day_of_week_name[] =
65 "Sun",
66 "Mon",
67 "Tue",
68 "Wed",
69 "Thu",
70 "Fri",
71 "Sat"
74 char *ts = 0;
76 if (date_and_timelen >= TIME_STAMP_FIELD_WIDTH)
78 time_t timeval;
79 struct tm *now;
81 time (&timeval);
82 now = localtime (&timeval); /* Get current local time. */
84 if (format == 'Y')
86 sprintf (date_and_time,
87 "%3s %3s %2d %04d %02d:%02d:%02d.%06d",
88 day_of_week_name[now->tm_wday],
89 month_name[now->tm_mon],
90 (int) now->tm_mday,
91 (int) now->tm_year + 1900,
92 (int) now->tm_hour,
93 (int) now->tm_min, (int) now->tm_sec, (int) 0);
95 else /* 'T' */
97 sprintf (date_and_time,
98 "%3s %2d %02d:%02d:%02d.%03d %04d",
99 month_name[now->tm_mon],
100 (int) now->tm_mday,
101 (int) now->tm_hour,
102 (int) now->tm_min,
103 (int) now->tm_sec, (int) 0,
104 (int) now->tm_year + 1900);
107 ts = date_and_time;
109 return ts;
113 main (int, char **)
115 char const *const program = "UnloadLibACE";
117 int status = 0;
118 void *handle = 0;
119 char *ace_root = 0;
120 char tbuf[BUFSIZ];
121 char ybuf[BUFSIZ];
122 FILE *logfp = 0;
124 if ((logfp = fopen ("log/UnloadLibACE.log", "w")) != 0)
126 setvbuf (logfp, 0, _IONBF, 0);
127 // reassign stdout/stderr to log file
128 int fdno = fileno (logfp);
130 dup2 (fdno, fileno (stdout));
131 dup2 (fdno, fileno (stderr));
132 setvbuf (stdout, 0, _IONBF, 0);
133 setvbuf (stderr, 0, _IONBF, 0);
134 fflush (stdout);
135 fflush (stderr);
137 printf ("%s@LM_DEBUG@ Starting %s test at %s\n",
138 time_stamp (tbuf, BUFSIZ, 'T'),
139 program, time_stamp (ybuf, BUFSIZ, 'Y'));
141 if ((ace_root = getenv ("ACE_ROOT")) != 0)
143 char buf[BUFSIZ];
145 strcpy (buf, ace_root);
146 strcat (buf, "/lib/");
147 const char *subdir_env = getenv ("ACE_EXE_SUB_DIR");
148 if (subdir_env)
150 strcat (buf, subdir_env);
151 strcat (buf, "/");
153 strcat (buf, "lib");
154 #if defined (ACE_LIB_NAME)
155 strcat (buf, ACE_LIB_NAME);
156 #else
157 strcat (buf, "ACE");
158 #endif /* ACE_LIB_NAME */
159 #if defined (__APPLE__)
160 strcat (buf, ".dylib");
161 #else
162 strcat (buf, ".so");
163 #endif /* (__hpux) */
165 handle = dlopen (buf, RTLD_LAZY);
166 if (handle == 0)
168 // is it because of "No such file or directory" ?
169 if (errno != ENOENT)
171 fprintf (stderr,
172 "%s@LM_ERROR@ dlopen() returned NULL\n",
173 time_stamp (tbuf, BUFSIZ, 'T'));
174 fprintf (stderr,
175 "%s@LM_ERROR@ dlerror() says: %s\n",
176 time_stamp (tbuf, BUFSIZ, 'T'), dlerror ());
177 status = 1;
179 else
181 printf ("%s@LM_DEBUG@ dlopen() did not find %s\n",
182 time_stamp (tbuf, BUFSIZ, 'T'), buf);
183 status = 0;
186 else if (dlclose (handle) != 0)
188 fprintf (stderr,
189 "%s@LM_ERROR@ dlclose() failed : %s\n",
190 time_stamp (tbuf, BUFSIZ, 'T'), strerror (errno));
191 status = 1;
194 else
196 fprintf (stderr,
197 "%s@LM_ERROR@ ACE_ROOT environment variable not set\n",
198 time_stamp (tbuf, BUFSIZ, 'T'));
199 status = 1;
202 fflush (stdout);
203 fflush (stderr);
204 fflush (logfp);
206 fclose (logfp);
208 else
210 // Couldn't go into the log file !!!
211 printf ("%s@LM_DEBUG@ Starting %s test at %s\n",
212 time_stamp (tbuf, BUFSIZ, 'T'),
213 program, time_stamp (ybuf, BUFSIZ, 'Y'));
215 fprintf (stderr,
216 "%s@LM_ERROR@ Could not open log/UnloadLibACE.log : %s\n",
217 time_stamp (tbuf, BUFSIZ, 'T'), strerror (errno));
218 status = 1;
221 printf ("%s@LM_DEBUG@ Ending %s test at %s\n",
222 time_stamp (tbuf, BUFSIZ, 'T'),
223 program, time_stamp (ybuf, BUFSIZ, 'Y'));
225 fflush (stderr);
226 fflush (stdout);
227 fclose (stdout);
228 fclose (stderr);
230 // Don't change this since we do NOT want to use ACE for this test!
231 exit (status);
232 return 0;
234 #else
235 # if defined (WIN32) && defined (ACE_USES_WCHAR)
236 // Borrow include list from ace_wchar.h
237 # if !defined (__BORLANDC__)
238 # include /**/ <wchar.h>
239 # endif /* __BORLANDC__ */
242 wmain (int, wchar_t **)
243 #else
245 main (int, char **)
246 #endif /* (WIN32) && (ACE_USES_WCHAR) */
248 char const *const program = "UnloadLibACE";
250 FILE *logfp = 0;
252 if ((logfp = fopen ("log/UnloadLibACE.log", "w")) != 0)
254 fprintf (logfp, "@LM_DEBUG@ Starting %s test\n", program);
255 fprintf (logfp, "@LM_DEBUG@ %s test not implemented for this platform\n",
256 program);
257 fprintf (logfp, "@LM_DEBUG@ Ending %s test\n", program);
259 fflush (logfp);
260 fclose (logfp);
262 return 0;
264 #endif /* UNLOAD_LIBACE_TEST */