2 #include "ace/config-all.h" /* Need ACE_TRACE */
3 #include "ace/Object_Manager_Base.h"
4 #include "ace/OS_NS_string.h"
5 #include "ace/Global_Macros.h"
6 #include "ace/os_include/os_errno.h"
7 #include "ace/os_include/os_search.h"
8 #if defined (ACE_ANDROID) && (__ANDROID_API__ < 19)
9 # include "ace/os_include/os_signal.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 // Doesn't need a macro since it *never* returns!
17 ACE_OS::_exit (int status)
19 ACE_OS_TRACE ("ACE_OS::_exit");
20 #if defined (ACE_VXWORKS)
22 #elif defined (ACE_MQX)
24 #elif !defined (ACE_LACKS__EXIT)
27 ACE_UNUSED_ARG (status);
28 #endif /* ACE_VXWORKS */
34 #if defined (ACE_ANDROID) && (__ANDROID_API__ < 19)
35 ACE_OS::_exit (128 + SIGABRT);
36 #elif !defined (ACE_LACKS_ABORT)
38 #elif !defined (ACE_LACKS_EXIT)
40 #endif /* !ACE_LACKS_ABORT */
44 ACE_OS::atexit (ACE_EXIT_HOOK func, const char* name)
46 return ACE_OS_Object_Manager::instance ()->at_exit (func, name);
50 ACE_OS::atoi (const char *s)
55 #if defined (ACE_HAS_WCHAR)
57 ACE_OS::atoi (const wchar_t *s)
59 #if defined (ACE_WIN32) && defined (ACE_HAS_WTOI)
62 return ACE_OS::atoi (ACE_Wide_To_Ascii (s).char_rep ());
63 #endif /* ACE_WIN32 */
65 #endif /* ACE_HAS_WCHAR */
68 ACE_OS::atol (const char *s)
73 #if defined (ACE_HAS_WCHAR)
75 ACE_OS::atol (const wchar_t *s)
77 #if defined (ACE_WIN32) && defined (ACE_HAS_WTOL)
80 return ACE_OS::atol (ACE_Wide_To_Ascii (s).char_rep ());
81 #endif /* ACE_WIN32 */
83 #endif /* ACE_HAS_WCHAR */
86 ACE_OS::atof (const char *s)
91 #if defined (ACE_HAS_WCHAR)
93 ACE_OS::atof (const wchar_t *s)
95 #if !defined (ACE_HAS_WTOF)
96 return ACE_OS::atof (ACE_Wide_To_Ascii (s).char_rep ());
97 #elif defined (ACE_WTOF_EQUIVALENT)
98 return ACE_WTOF_EQUIVALENT (s);
99 #else /* ACE_HAS__WTOF */
101 #endif /* ACE_HAS_WTOF */
103 #endif /* ACE_HAS_WCHAR */
106 ACE_OS::atop (const char *s)
108 ACE_TRACE ("ACE_OS::atop");
109 #if defined (ACE_WIN64)
110 intptr_t ip = ::_atoi64 (s);
112 intptr_t ip = ::atoi (s);
113 #endif /* ACE_WIN64 */
114 void * p = reinterpret_cast<void *> (ip);
118 #if defined (ACE_HAS_WCHAR)
120 ACE_OS::atop (const wchar_t *s)
122 # if defined (ACE_WIN64)
123 intptr_t ip = ::_wtoi64 (s);
125 intptr_t ip = ACE_OS::atoi (s);
126 # endif /* ACE_WIN64 */
127 void * p = reinterpret_cast<void *> (ip);
130 #endif /* ACE_HAS_WCHAR */
133 ACE_OS::bsearch (const void *key,
137 ACE_COMPARE_FUNC compar)
139 #if !defined (ACE_LACKS_BSEARCH)
140 return ::bsearch (key, base, nel, size, compar);
142 ACE_UNUSED_ARG (key);
143 ACE_UNUSED_ARG (base);
144 ACE_UNUSED_ARG (nel);
145 ACE_UNUSED_ARG (size);
146 ACE_UNUSED_ARG (compar);
147 ACE_NOTSUP_RETURN (0);
148 #endif /* ACE_LACKS_BSEARCH */
152 ACE_OS::getenv (const char *symbol)
154 ACE_OS_TRACE ("ACE_OS::getenv");
155 #if defined (ACE_LACKS_GETENV)
156 ACE_UNUSED_ARG (symbol);
157 ACE_NOTSUP_RETURN (0);
158 #else /* ACE_LACKS_GETENV */
159 return ::getenv (symbol);
160 #endif /* ACE_LACKS_GETENV */
163 #if defined (ACE_HAS_WCHAR) && defined (ACE_WIN32)
165 ACE_OS::getenv (const wchar_t *symbol)
167 #if defined (ACE_LACKS_GETENV)
168 ACE_UNUSED_ARG (symbol);
169 ACE_NOTSUP_RETURN (0);
171 return ::_wgetenv (symbol);
172 #endif /* ACE_LACKS_GETENV */
174 #endif /* ACE_HAS_WCHAR && ACE_WIN32 */
177 ACE_OS::itoa (int value, char *string, int radix)
179 #if !defined (ACE_HAS_ITOA)
180 return ACE_OS::itoa_emulation (value, string, radix);
181 #elif defined (ACE_ITOA_EQUIVALENT)
182 return ACE_ITOA_EQUIVALENT (value, string, radix);
183 #else /* !ACE_HAS_ITOA */
184 return ::itoa (value, string, radix);
185 #endif /* !ACE_HAS_ITOA */
188 #if defined (ACE_HAS_WCHAR)
190 ACE_OS::itoa (int value, wchar_t *string, int radix)
192 #if defined (ACE_LACKS_ITOW)
193 return ACE_OS::itow_emulation (value, string, radix);
194 #else /* ACE_LACKS_ITOW */
195 return ::_itow (value, string, radix);
196 #endif /* ACE_LACKS_ITOW */
198 #endif /* ACE_HAS_WCHAR */
200 ACE_INLINE ACE_HANDLE
201 ACE_OS::mkstemp (char *s)
203 #if !defined (ACE_LACKS_MKSTEMP)
204 return ::mkstemp (s);
205 #elif defined (ACE_USES_WCHAR)
206 // For wide-char filesystems, we must convert the narrow-char input to
207 // a wide-char string for mkstemp_emulation(), then convert the name
208 // back to narrow-char for the caller.
209 ACE_Ascii_To_Wide wide_s (s);
210 const ACE_HANDLE fh = ACE_OS::mkstemp_emulation (wide_s.wchar_rep ());
211 if (fh != ACE_INVALID_HANDLE)
213 ACE_Wide_To_Ascii narrow_s (wide_s.wchar_rep ());
214 ACE_OS::strcpy (s, narrow_s.char_rep ());
218 return ACE_OS::mkstemp_emulation (s);
219 #endif /* !ACE_LACKS_MKSTEMP */
222 #if defined (ACE_HAS_WCHAR)
223 ACE_INLINE ACE_HANDLE
224 ACE_OS::mkstemp (wchar_t *s)
226 # if !defined (ACE_LACKS_MKSTEMP)
227 // For wide-char filesystems, we must convert the wide-char input to
228 // a narrow-char string for mkstemp(), then convert the name
229 // back to wide-char for the caller.
230 ACE_Wide_To_Ascii narrow_s (s);
231 const ACE_HANDLE fh = ::mkstemp (narrow_s.char_rep ());
232 if (fh != ACE_INVALID_HANDLE)
234 ACE_Ascii_To_Wide wide_s (narrow_s.char_rep ());
235 ACE_OS::strcpy (s, wide_s.wchar_rep ());
238 # elif defined (ACE_USES_WCHAR)
239 return ACE_OS::mkstemp_emulation (s);
241 // For wide-char filesystems, we must convert the wide-char input to
242 // a narrow-char string for mkstemp_emulation(), then convert the name
243 // back to wide-char for the caller.
244 ACE_Wide_To_Ascii narrow_s (s);
245 const ACE_HANDLE fh = ACE_OS::mkstemp_emulation (narrow_s.char_rep ());
246 if (fh != ACE_INVALID_HANDLE)
248 ACE_Ascii_To_Wide wide_s (narrow_s.char_rep ());
249 ACE_OS::strcpy (s, wide_s.wchar_rep ());
252 # endif /* !ACE_LACKS_MKSTEMP */
254 #endif /* ACE_HAS_WCHAR */
256 #if !defined (ACE_DISABLE_MKTEMP)
258 # if !defined (ACE_LACKS_MKTEMP)
260 ACE_OS::mktemp (char *s)
262 # if defined (ACE_WIN32)
263 return ::_mktemp (s);
264 # else /* ACE_WIN32 */
266 # endif /* ACE_WIN32 */
269 # if defined (ACE_HAS_WCHAR)
271 ACE_OS::mktemp (wchar_t *s)
273 # if defined (ACE_WIN32)
274 return ::_wmktemp (s);
276 // For narrow-char filesystems, we must convert the wide-char input to
277 // a narrow-char string for mktemp (), then convert the name back to
278 // wide-char for the caller.
279 ACE_Wide_To_Ascii narrow_s (s);
280 if (::mktemp (narrow_s.char_rep ()) == 0)
282 ACE_Ascii_To_Wide wide_s (narrow_s.char_rep ());
283 ACE_OS::strcpy (s, wide_s.wchar_rep ());
287 # endif /* ACE_HAS_WCHAR */
289 # endif /* !ACE_LACKS_MKTEMP */
290 #endif /* !ACE_DISABLE_MKTEMP */
293 ACE_OS::putenv (const char *string)
295 ACE_OS_TRACE ("ACE_OS::putenv");
296 #if defined (ACE_LACKS_PUTENV) && defined (ACE_HAS_SETENV)
298 char *sp = ACE_OS::strchr (const_cast <char *> (string), '=');
301 char *stmp = ACE_OS::strdup (string);
304 stmp[sp - string] = '\0';
305 result = ACE_OS::setenv (stmp, sp+sizeof (char), 1);
316 result = ACE_OS::setenv (string, "", 1);
320 #elif defined (ACE_LACKS_PUTENV)
321 ACE_UNUSED_ARG (string);
322 ACE_NOTSUP_RETURN (0);
323 #elif defined (ACE_PUTENV_EQUIVALENT)
324 return ACE_PUTENV_EQUIVALENT (const_cast <char *> (string));
326 return ACE_STD_NAMESPACE::putenv (const_cast <char *> (string));
327 #endif /* ACE_LACKS_PUTENV && ACE_HAS_SETENV */
331 ACE_OS::setenv(const char *envname, const char *envval, int overwrite)
333 #if defined (ACE_LACKS_SETENV)
334 ACE_UNUSED_ARG (envname);
335 ACE_UNUSED_ARG (envval);
336 ACE_UNUSED_ARG (overwrite);
337 ACE_NOTSUP_RETURN (-1);
339 return ACE_STD_NAMESPACE::setenv (envname, envval, overwrite);
344 ACE_OS::unsetenv(const char *name)
346 #if defined (ACE_LACKS_UNSETENV)
347 ACE_UNUSED_ARG (name);
348 ACE_NOTSUP_RETURN (-1);
350 # if defined (ACE_HAS_VOID_UNSETENV)
354 return ACE_STD_NAMESPACE::unsetenv (name);
355 # endif /* ACE_HAS_VOID_UNSETENV */
356 #endif /* ACE_LACKS_UNSETENV */
359 #if defined (ACE_HAS_WCHAR) && defined (ACE_WIN32)
361 ACE_OS::putenv (const wchar_t *string)
363 ACE_OS_TRACE ("ACE_OS::putenv");
364 #if defined (ACE_LACKS_PUTENV)
365 ACE_UNUSED_ARG (string);
366 ACE_NOTSUP_RETURN (-1);
368 return ::_wputenv (string);
369 #endif /* ACE_LACKS_PUTENV */
371 #endif /* ACE_HAS_WCHAR && ACE_WIN32 */
374 ACE_OS::qsort (void *base,
377 ACE_COMPARE_FUNC compar)
379 #if !defined (ACE_LACKS_QSORT)
380 ::qsort (base, nel, width, compar);
382 ACE_UNUSED_ARG (base);
383 ACE_UNUSED_ARG (nel);
384 ACE_UNUSED_ARG (width);
385 ACE_UNUSED_ARG (compar);
386 #endif /* !ACE_LACKS_QSORT */
392 ACE_OS_TRACE ("ACE_OS::rand");
393 #if !defined (ACE_LACKS_RAND)
396 ACE_NOTSUP_RETURN (-1);
397 #endif /* ACE_LACKS_RAND */
401 ACE_OS::rand_r (unsigned int *seed)
403 ACE_OS_TRACE ("ACE_OS::rand_r");
404 #if defined (ACE_LACKS_RAND_R)
405 long new_seed = (long) *seed;
407 new_seed = 0x12345987;
408 long temp = new_seed / 127773;
409 new_seed = 16807 * (new_seed - temp * 127773) - 2836 * temp;
411 new_seed += 2147483647;
412 *seed = (unsigned int)new_seed;
413 return (int) (new_seed & RAND_MAX);
415 return ace_rand_r_helper (seed);
416 # endif /* ACE_LACKS_RAND_R */
419 #if !defined (ACE_LACKS_REALPATH)
421 ACE_OS::realpath (const char *file_name,
424 # if defined (ACE_WIN32)
425 return ::_fullpath (resolved_name, file_name, PATH_MAX);
426 # else /* ACE_WIN32 */
427 return ::realpath (file_name, resolved_name);
428 # endif /* ! ACE_WIN32 */
431 # if defined (ACE_HAS_WCHAR)
433 ACE_OS::realpath (const wchar_t *file_name,
434 wchar_t *resolved_name)
436 # if defined (ACE_WIN32)
437 return ::_wfullpath (resolved_name, file_name, PATH_MAX);
438 # else /* ACE_WIN32 */
439 ACE_Wide_To_Ascii n_file_name (file_name);
440 char n_resolved[PATH_MAX];
441 if (0 != ACE_OS::realpath (n_file_name.char_rep (), n_resolved))
443 ACE_Ascii_To_Wide w_resolved (n_resolved);
444 ACE_OS::strcpy (resolved_name, w_resolved.wchar_rep ());
445 return resolved_name;
448 # endif /* ! ACE_WIN32 */
450 # endif /* ACE_HAS_WCHAR */
451 #endif /* !ACE_LACKS_REALPATH */
453 ACE_INLINE ACE_EXIT_HOOK
454 ACE_OS::set_exit_hook (ACE_EXIT_HOOK exit_hook)
456 ACE_EXIT_HOOK old_hook = exit_hook_;
457 exit_hook_ = exit_hook;
462 ACE_OS::srand (u_int seed)
464 ACE_OS_TRACE ("ACE_OS::srand");
465 #ifdef ACE_LACKS_SRAND
466 ACE_UNUSED_ARG (seed);
473 ACE_OS::strtod (const char *s, char **endptr)
475 return ::strtod (s, endptr);
478 #if defined (ACE_HAS_WCHAR) && !defined (ACE_LACKS_WCSTOD)
480 ACE_OS::strtod (const wchar_t *s, wchar_t **endptr)
482 return std::wcstod (s, endptr);
484 #endif /* ACE_HAS_WCHAR && !ACE_LACKS_WCSTOD */
487 ACE_OS::strtol (const char *s, char **ptr, int base)
489 #if defined (ACE_LACKS_STRTOL)
490 return ACE_OS::strtol_emulation (s, ptr, base);
491 #else /* ACE_LACKS_STRTOL */
492 return ::strtol (s, ptr, base);
493 #endif /* ACE_LACKS_STRTOL */
496 #if defined (ACE_HAS_WCHAR)
498 ACE_OS::strtol (const wchar_t *s, wchar_t **ptr, int base)
500 #if defined (ACE_LACKS_WCSTOL)
501 return ACE_OS::wcstol_emulation (s, ptr, base);
503 return std::wcstol (s, ptr, base);
504 #endif /* ACE_LACKS_WCSTOL */
506 #endif /* ACE_HAS_WCHAR */
508 ACE_INLINE unsigned long
509 ACE_OS::strtoul (const char *s, char **ptr, int base)
511 #if defined (ACE_LACKS_STRTOUL)
512 return ACE_OS::strtoul_emulation (s, ptr, base);
513 #else /* ACE_LACKS_STRTOUL */
514 return ::strtoul (s, ptr, base);
515 #endif /* ACE_LACKS_STRTOUL */
518 #if defined (ACE_HAS_WCHAR)
519 ACE_INLINE unsigned long
520 ACE_OS::strtoul (const wchar_t *s, wchar_t **ptr, int base)
522 #if defined (ACE_LACKS_WCSTOUL)
523 return ACE_OS::wcstoul_emulation (s, ptr, base);
525 return std::wcstoul (s, ptr, base);
526 #endif /* ACE_LACKS_WCSTOUL */
528 #endif /* ACE_HAS_WCHAR */
531 ACE_OS::strtoll (const char *s, char **ptr, int base)
533 #if defined (ACE_LACKS_STRTOLL)
534 return ACE_OS::strtoll_emulation (s, ptr, base);
535 #elif defined (ACE_STRTOLL_EQUIVALENT)
536 return ACE_STRTOLL_EQUIVALENT (s, ptr, base);
538 return ace_strtoll_helper (s, ptr, base);
539 #endif /* ACE_LACKS_STRTOLL */
542 #if defined (ACE_HAS_WCHAR)
544 ACE_OS::strtoll (const wchar_t *s, wchar_t **ptr, int base)
546 #if defined (ACE_LACKS_WCSTOLL)
547 return ACE_OS::wcstoll_emulation (s, ptr, base);
548 #elif defined (ACE_WCSTOLL_EQUIVALENT)
549 return ACE_WCSTOLL_EQUIVALENT (s, ptr, base);
551 return std::wcstoll (s, ptr, base);
552 #endif /* ACE_LACKS_WCSTOLL */
554 #endif /* ACE_HAS_WCHAR */
556 ACE_INLINE ACE_UINT64
557 ACE_OS::strtoull (const char *s, char **ptr, int base)
559 #if defined (ACE_LACKS_STRTOULL)
560 return ACE_OS::strtoull_emulation (s, ptr, base);
561 #elif defined (ACE_STRTOULL_EQUIVALENT)
562 return ACE_STRTOULL_EQUIVALENT (s, ptr, base);
564 return ace_strtoull_helper (s, ptr, base);
565 #endif /* ACE_LACKS_STRTOULL */
568 #if defined (ACE_HAS_WCHAR)
569 ACE_INLINE ACE_UINT64
570 ACE_OS::strtoull (const wchar_t *s, wchar_t **ptr, int base)
572 #if defined (ACE_LACKS_WCSTOULL)
573 return ACE_OS::wcstoull_emulation (s, ptr, base);
574 #elif defined (ACE_WCSTOULL_EQUIVALENT)
575 return ACE_WCSTOULL_EQUIVALENT (s, ptr, base);
577 return std::wcstoull (s, ptr, base);
578 #endif /* ACE_LACKS_WCSTOULL */
580 #endif /* ACE_HAS_WCHAR */
583 ACE_OS::system (const ACE_TCHAR *s)
585 // ACE_OS_TRACE ("ACE_OS::system");
586 #if defined (ACE_LACKS_SYSTEM)
588 ACE_NOTSUP_RETURN (-1);
589 #elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
590 return ::_wsystem (s);
592 return ::system (ACE_TEXT_ALWAYS_CHAR (s));
593 #endif /* ACE_LACKS_SYSTEM */
596 ACE_INLINE const char*
597 ACE_OS::getprogname ()
599 #if defined (ACE_HAS_GETPROGNAME)
600 return ::getprogname ();
602 return ACE_OS::getprogname_emulation ();
603 #endif /* ACE_HAS_GETPROGNAME */
607 ACE_OS::setprogname (const char* name)
609 #if defined (ACE_HAS_SETPROGNAME)
610 ::setprogname (name);
612 ACE_OS::setprogname_emulation (name);
613 #endif /* ACE_HAS_SETPROGNAME */
616 ACE_END_VERSIONED_NAMESPACE_DECL