2 #include "ace/OS_NS_unistd.h"
3 #include "ace/OS_NS_stdlib.h"
4 #include "ace/OS_NS_fcntl.h"
5 #include "ace/OS_NS_errno.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_pwd.h"
8 #include "ace/OS_NS_macros.h"
9 #include "ace/OS_NS_sys_stat.h"
10 #include "ace/OS_Memory.h"
12 #if defined (ACE_HAS_TRIO)
14 #endif /* ACE_HAS_TRIO */
16 #if defined (ACE_HAS_ALLOC_HOOKS)
17 # include "ace/Malloc_Base.h"
18 #endif /* ACE_HAS_ALLOC_HOOKS */
21 # include "ace/MQX_Filesystem.h"
24 /*****************************************************************************/
26 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
28 #if defined (ACE_WIN32)
30 ACE_OS::flock_adjust_params (ACE_OS::ace_flock_t *lock,
42 # if !defined (ACE_LACKS_WIN32_SETFILEPOINTEREX)
43 LARGE_INTEGER distance;
44 distance.QuadPart = 0;
45 if (!::SetFilePointerEx (lock->handle_,
50 ACE_OS::set_errno_to_last_error ();
54 offset.LowPart = ::SetFilePointer (lock->handle_,
58 if (offset.LowPart == INVALID_SET_FILE_POINTER &&
59 ::GetLastError() != NO_ERROR)
61 ACE_OS::set_errno_to_last_error ();
64 # endif /* ACE_LACKS_WIN32_SETFILEPOINTEREX */
66 # if defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
67 start += offset.QuadPart;
69 start += offset.LowPart;
70 # endif /* _FILE_OFFSET_BITS == 64 */
75 ACE_OFF_T const size = ACE_OS::filesize (lock->handle_);
83 lock->overlapped_.Offset = ACE_LOW_PART (start);
84 lock->overlapped_.OffsetHigh = ACE_HIGH_PART (start);
87 ACE_OFF_T const tlen = ACE_OS::filesize (lock->handle_);
92 #endif /* ACE_WIN32 */
95 ACE_OS::flock_init (ACE_OS::ace_flock_t *lock,
97 const ACE_TCHAR *name,
100 ACE_OS_TRACE ("ACE_OS::flock_init");
101 #if defined (ACE_WIN32)
102 // Once initialized, these values are never changed.
103 lock->overlapped_.Internal = 0;
104 lock->overlapped_.InternalHigh = 0;
105 lock->overlapped_.OffsetHigh = 0;
106 lock->overlapped_.hEvent = 0;
107 #endif /* ACE_WIN32 */
108 lock->handle_ = ACE_INVALID_HANDLE;
113 ACE_OSCALL (ACE_OS::open (name, flags, perms),
117 if (lock->handle_ != ACE_INVALID_HANDLE)
118 lock->lockname_ = ACE_OS::strdup (name);
119 return lock->handle_ == ACE_INVALID_HANDLE ? -1 : 0;
126 ACE_OS::flock_unlock (ACE_OS::ace_flock_t *lock,
131 ACE_OS_TRACE ("ACE_OS::flock_unlock");
132 #if defined (ACE_LACKS_FILELOCKS)
133 ACE_UNUSED_ARG (lock);
134 ACE_UNUSED_ARG (whence);
135 ACE_UNUSED_ARG (start);
136 ACE_UNUSED_ARG (len);
137 ACE_NOTSUP_RETURN (-1);
138 #elif defined (ACE_WIN32)
139 ACE_OS::flock_adjust_params (lock, whence, start, len);
140 DWORD low_len = ACE_LOW_PART (len);
141 DWORD high_len = ACE_HIGH_PART (len);
142 ACE_WIN32CALL_RETURN (
143 ACE_ADAPT_RETVAL (::UnlockFileEx (lock->handle_,
148 ace_result_), int, -1);
150 lock->lock_.l_whence = whence;
151 lock->lock_.l_start = start;
152 lock->lock_.l_len = len;
153 lock->lock_.l_type = F_UNLCK; // Unlock file.
156 ACE_OSCALL_RETURN (ACE_OS::fcntl (lock->handle_, F_SETLK,
157 reinterpret_cast<long> (&lock->lock_)),
159 #endif /* ACE_WIN32 */
163 ACE_OS::flock_destroy (ACE_OS::ace_flock_t *lock,
166 ACE_OS_TRACE ("ACE_OS::flock_destroy");
167 if (lock->handle_ != ACE_INVALID_HANDLE)
169 ACE_OS::flock_unlock (lock);
171 ACE_OS::close (lock->handle_);
172 lock->handle_ = ACE_INVALID_HANDLE;
173 if (lock->lockname_ != 0)
176 ACE_OS::unlink (lock->lockname_);
177 #if defined (ACE_HAS_ALLOC_HOOKS)
178 ACE_Allocator::instance()->free (
179 static_cast<void *> (const_cast<ACE_TCHAR *> (lock->lockname_)));
182 static_cast<void *> (const_cast<ACE_TCHAR *> (lock->lockname_)));
183 #endif /* ACE_HAS_ALLOC_HOOKS */
191 ACE_OS::flock_rdlock (ACE_OS::ace_flock_t *lock,
196 ACE_OS_TRACE ("ACE_OS::flock_rdlock");
197 #if defined (ACE_LACKS_FILELOCKS)
198 ACE_UNUSED_ARG (lock);
199 ACE_UNUSED_ARG (whence);
200 ACE_UNUSED_ARG (start);
201 ACE_UNUSED_ARG (len);
202 ACE_NOTSUP_RETURN (-1);
203 #elif defined (ACE_WIN32)
204 ACE_OS::flock_adjust_params (lock, whence, start, len);
205 DWORD low_len = ACE_LOW_PART (len);
206 DWORD high_len = ACE_HIGH_PART (len);
207 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::LockFileEx (lock->handle_,
213 ace_result_), int, -1);
215 lock->lock_.l_whence = whence;
216 lock->lock_.l_start = start;
217 lock->lock_.l_len = len;
218 lock->lock_.l_type = F_RDLCK; // set read lock
219 // block, if no access
220 ACE_OSCALL_RETURN (ACE_OS::fcntl (lock->handle_, F_SETLKW,
221 reinterpret_cast<long> (&lock->lock_)),
223 #endif /* ACE_WIN32 */
227 ACE_OS::flock_tryrdlock (ACE_OS::ace_flock_t *lock,
232 ACE_OS_TRACE ("ACE_OS::ace_flock_tryrdlock");
233 #if defined (ACE_LACKS_FILELOCKS)
234 ACE_UNUSED_ARG (lock);
235 ACE_UNUSED_ARG (whence);
236 ACE_UNUSED_ARG (start);
237 ACE_UNUSED_ARG (len);
238 ACE_NOTSUP_RETURN (-1);
239 #elif defined (ACE_WIN32)
240 ACE_OS::flock_adjust_params (lock, whence, start, len);
241 DWORD low_len = ACE_LOW_PART (len);
242 DWORD high_len = ACE_HIGH_PART (len);
243 ACE_WIN32CALL_RETURN (
244 ACE_ADAPT_RETVAL (::LockFileEx (lock->handle_,
245 LOCKFILE_FAIL_IMMEDIATELY,
250 ace_result_), int, -1);
252 lock->lock_.l_whence = whence;
253 lock->lock_.l_start = start;
254 lock->lock_.l_len = len;
255 lock->lock_.l_type = F_RDLCK; // set read lock
258 // Does not block, if no access, returns -1 and set errno = EBUSY;
259 ACE_OSCALL (ACE_OS::fcntl (lock->handle_, F_SETLK,
260 reinterpret_cast<long> (&lock->lock_)),
263 if (result == -1 && (errno == EACCES || errno == EAGAIN))
267 #endif /* ACE_WIN32 */
271 ACE_OS::flock_trywrlock (ACE_OS::ace_flock_t *lock,
276 ACE_OS_TRACE ("ACE_OS::ace_flock_trywrlock");
277 #if defined (ACE_LACKS_FILELOCKS)
278 ACE_UNUSED_ARG (lock);
279 ACE_UNUSED_ARG (whence);
280 ACE_UNUSED_ARG (start);
281 ACE_UNUSED_ARG (len);
282 ACE_NOTSUP_RETURN (-1);
283 #elif defined (ACE_WIN32)
284 ACE_OS::flock_adjust_params (lock, whence, start, len);
285 DWORD low_len = ACE_LOW_PART (len);
286 DWORD high_len = ACE_HIGH_PART (len);
287 ACE_WIN32CALL_RETURN (
288 ACE_ADAPT_RETVAL (::LockFileEx (lock->handle_,
289 LOCKFILE_FAIL_IMMEDIATELY | LOCKFILE_EXCLUSIVE_LOCK,
294 ace_result_), int, -1);
296 lock->lock_.l_whence = whence;
297 lock->lock_.l_start = start;
298 lock->lock_.l_len = len;
299 lock->lock_.l_type = F_WRLCK; // set write lock
302 // Does not block, if no access, returns -1 and set errno = EBUSY;
303 ACE_OSCALL (ACE_OS::fcntl (lock->handle_,
305 reinterpret_cast<long> (&lock->lock_)),
308 if (result == -1 && (errno == EACCES || errno == EAGAIN))
312 #endif /* ACE_WIN32 */
316 ACE_OS::flock_wrlock (ACE_OS::ace_flock_t *lock,
321 ACE_OS_TRACE ("ACE_OS::flock_wrlock");
322 #if defined (ACE_LACKS_FILELOCKS)
323 ACE_UNUSED_ARG (lock);
324 ACE_UNUSED_ARG (whence);
325 ACE_UNUSED_ARG (start);
326 ACE_UNUSED_ARG (len);
327 ACE_NOTSUP_RETURN (-1);
328 #elif defined (ACE_WIN32)
329 ACE_OS::flock_adjust_params (lock, whence, start, len);
330 DWORD low_len = ACE_LOW_PART (len);
331 DWORD high_len = ACE_HIGH_PART (len);
332 ACE_WIN32CALL_RETURN (
333 ACE_ADAPT_RETVAL (::LockFileEx (lock->handle_,
334 LOCKFILE_EXCLUSIVE_LOCK,
339 ace_result_), int, -1);
341 lock->lock_.l_whence = whence;
342 lock->lock_.l_start = start;
343 lock->lock_.l_len = len;
344 lock->lock_.l_type = F_WRLCK; // set write lock
345 // block, if no access
346 ACE_OSCALL_RETURN (ACE_OS::fcntl (lock->handle_, F_SETLKW,
347 reinterpret_cast<long> (&lock->lock_)),
349 #endif /* ACE_WIN32 */
353 ACE_OS::clearerr (FILE* fp)
355 ace_clearerr_helper (fp);
358 #if !defined (ACE_LACKS_CUSERID)
360 ACE_OS::cuserid (char *user, size_t maxlen)
362 ACE_OS_TRACE ("ACE_OS::cuserid");
363 #if defined (ACE_VXWORKS)
364 ACE_UNUSED_ARG (maxlen);
367 // Require that the user field be non-null, i.e., don't
368 // allocate or use static storage.
369 ACE_NOTSUP_RETURN (0);
373 ::remCurIdGet (user, 0);
376 #elif defined (ACE_HAS_PHARLAP) || defined (ACE_HAS_WINCE)
377 ACE_UNUSED_ARG (user);
378 ACE_UNUSED_ARG (maxlen);
379 ACE_NOTSUP_RETURN (0);
380 #elif defined (ACE_WIN32)
381 BOOL const result = GetUserNameA (user, (u_long *) &maxlen);
386 #elif defined (ACE_HAS_ALT_CUSERID)
387 # if defined (ACE_LACKS_PWD_FUNCTIONS)
388 ACE_UNUSED_ARG (user);
389 ACE_UNUSED_ARG (maxlen);
390 ACE_NOTSUP_RETURN (0);
391 //# error Cannot use alternate cuserid() without POSIX password functions!
392 # endif /* ACE_LACKS_PWD_FUNCTIONS */
394 // POSIX.1 dropped the cuserid() function.
395 // GNU GLIBC and other platforms correctly deprecate the cuserid()
400 // It doesn't make sense to have a zero length user ID.
405 struct passwd *pw = 0;
407 // Make sure the file pointer is at the beginning of the password file
409 // Should use ACE_OS::setpwent() but I didn't want to move this
412 // Use the effective user ID to determine the user name.
413 pw = ::getpwuid (ACE_OS::geteuid ());
415 // Make sure the password file is closed.
424 size_t max_length = 0;
429 // Not reentrant/thread-safe, but nothing else can be done if a
430 // zero pointer was passed in as the destination.
432 #if defined (_POSIX_SOURCE) && defined (L_cuserid)
433 const size_t ACE_L_cuserid = L_cuserid;
435 const size_t ACE_L_cuserid = 9; // 8 character user ID + NULL
436 #endif /* _POSIX_SOURCE */
438 static char tmp[ACE_L_cuserid] = { '\0' };
439 max_length = ACE_L_cuserid - 1; // Do not include NULL in length
449 // Extract the user name from the passwd structure.
450 if (ACE_OS::strlen (pw->pw_name) <= max_length)
452 return ACE_OS::strcpy (userid, pw->pw_name);
456 errno = ENOSPC; // Buffer is not large enough.
460 // Hackish because of missing buffer size!
461 ACE_UNUSED_ARG (maxlen);
462 ACE_OSCALL_RETURN (::ace_cuserid(user), char*, 0);
463 #endif /* ACE_VXWORKS */
466 #if defined (ACE_HAS_WCHAR)
468 ACE_OS::cuserid (wchar_t *user, size_t maxlen)
470 # if defined (ACE_HAS_WINCE)
471 ACE_UNUSED_ARG (user);
472 ACE_UNUSED_ARG (maxlen);
473 ACE_NOTSUP_RETURN (0);
474 # elif defined (ACE_WIN32)
475 BOOL const result = GetUserNameW (user, (u_long *) &maxlen);
480 # else /* ACE_WIN32 */
484 ACE_NEW_RETURN (char_user, char[maxlen + 1], 0);
486 if (ACE_OS::cuserid (char_user, maxlen))
488 ACE_OS::strcpy (user, ACE_Ascii_To_Wide (char_user).wchar_rep ());
495 # endif /* ACE_WIN32 */
497 #endif /* ACE_HAS_WCHAR */
498 #endif /* ACE_LACKS_CUSERID */
501 ACE_OS::fclose (FILE *fp)
503 ACE_OS_TRACE ("ACE_OS::fclose");
504 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fclose (fp), int, -1);
508 ACE_OS::fdopen (ACE_HANDLE handle, const ACE_TCHAR *mode)
510 ACE_OS_TRACE ("ACE_OS::fdopen");
511 #if defined (ACE_HAS_WINCE)
512 # if defined (ACE_HAS_NONCONST_WFDOPEN)
513 ACE_OSCALL_RETURN (::_wfdopen ((int)handle, const_cast <ACE_TCHAR*> (ACE_TEXT_ALWAYS_WCHAR (mode))),
517 ACE_OSCALL_RETURN (::_wfdopen (handle, ACE_TEXT_ALWAYS_WCHAR (mode)),
521 #elif defined (ACE_WIN32)
522 // kernel file handle -> FILE* conversion...
523 // Options: _O_APPEND, _O_RDONLY and _O_TEXT are lost
527 int const crt_handle = ::_open_osfhandle (intptr_t (handle), 0);
529 if (crt_handle != -1)
531 # if defined (ACE_USES_WCHAR)
532 file = ::_wfdopen (crt_handle, mode);
534 file = ::_fdopen (crt_handle, mode);
535 # endif /* ACE_USES_WCHAR */
539 ::_close (crt_handle);
544 #elif defined (ACE_LACKS_FDOPEN)
545 ACE_UNUSED_ARG (handle);
546 ACE_UNUSED_ARG (mode);
547 ACE_NOTSUP_RETURN (0);
550 (::fdopen (handle, ACE_TEXT_ALWAYS_CHAR (mode)), FILE *, 0);
551 #endif /* ACE_HAS_WINCE */
555 ACE_OS::fflush (FILE *fp)
557 ACE_OS_TRACE ("ACE_OS::fflush");
558 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fflush (fp), int, -1);
562 ACE_OS::fgetc (FILE *fp)
564 return ace_fgetc_helper (fp);
568 ACE_OS::getc (FILE *fp)
570 #ifdef ACE_LACKS_GETC
572 ACE_NOTSUP_RETURN (-1);
574 return ace_getc_helper (fp);
579 ACE_OS::fgetpos (FILE *fp, fpos_t *pos)
581 #ifdef ACE_LACKS_FGETPOS
583 ACE_UNUSED_ARG (pos);
584 ACE_NOTSUP_RETURN (-1);
586 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fgetpos (fp, pos), int, -1);
591 ACE_OS::fgets (char *buf, int size, FILE *fp)
593 ACE_OS_TRACE ("ACE_OS::fgets");
594 #if defined (ACE_LACKS_FGETS)
597 for (int i = 0; i < size - 1 && c != '\n'; ++i)
599 c = ACE_STD_NAMESPACE::fgetc (fp);
601 *iter++ = static_cast<char> (c);
604 return c == EOF ? 0 : buf;
606 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fgets (buf, size, fp), char *, 0);
607 #endif /* ACE_LACKS_FGETS */
610 #if defined (ACE_HAS_WCHAR) && !defined(ACE_LACKS_FGETWS)
612 ACE_OS::fgets (wchar_t *buf, int size, FILE *fp)
614 ACE_OS_TRACE ("ACE_OS::fgets");
615 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fgetws (buf, size, fp), wchar_t *, 0);
617 #endif /* ACE_HAS_WCHAR && !ACE_LACKS_FGETWS */
619 ACE_INLINE ACE_HANDLE
620 ACE_OS::fileno (FILE *stream)
622 #if defined ACE_FILENO_EQUIVALENT
623 return (ACE_HANDLE)((intptr_t)ACE_FILENO_EQUIVALENT (stream));
625 return ace_fileno_helper (stream);
629 #if !(defined (ACE_WIN32) && !defined (ACE_HAS_WINCE))
630 // Win32 PC implementation of fopen () is in OS_NS_stdio.cpp.
632 ACE_OS::fopen (const char *filename, const char *mode)
634 ACE_OS_TRACE ("ACE_OS::fopen");
635 ACE_OSCALL_RETURN (::fopen (filename, mode), FILE *, 0);
638 #if defined (ACE_HAS_WCHAR)
639 // Win32 PC implementation of fopen () is in OS_NS_stdio.cpp.
641 ACE_OS::fopen (const char *filename, const wchar_t *mode)
643 ACE_OS_TRACE ("ACE_OS::fopen");
644 ACE_Wide_To_Ascii n_mode (mode);
645 ACE_OSCALL_RETURN (::fopen (filename, n_mode.char_rep ()), FILE *, 0);
648 // Win32 PC implementation of fopen () is in OS_NS_stdio.cpp.
650 ACE_OS::fopen (const wchar_t *filename, const wchar_t *mode)
652 ACE_OS_TRACE ("ACE_OS::fopen");
653 #if defined (ACE_HAS_WINCE)
654 ACE_OSCALL_RETURN (::_wfopen (filename, mode), FILE *, 0);
656 // Non-Windows doesn't use wchar_t file systems.
657 ACE_Wide_To_Ascii n_filename (filename);
658 ACE_Wide_To_Ascii n_mode (mode);
660 (::fopen (n_filename.char_rep (), n_mode.char_rep ()), FILE*, 0);
661 #endif /* ACE_HAS_WINCE */
663 // Win32 PC implementation of fopen () is in OS_NS_stdio.cpp.
665 ACE_OS::fopen (const wchar_t *filename, const char *mode)
667 ACE_OS_TRACE ("ACE_OS::fopen");
668 #if defined (ACE_HAS_WINCE)
669 ACE_Ascii_To_Wide n_mode (mode);
671 (::_wfopen (filename, n_mode.wchar_rep ()), FILE *, 0);
673 // Non-Windows doesn't use wchar_t file systems.
674 ACE_Wide_To_Ascii n_filename (filename);
676 (::fopen (n_filename.char_rep (), mode), FILE*, 0);
677 #endif /* ACE_HAS_WINCE */
679 #endif /* ACE_HAS_WCHAR */
681 #endif /* ACE_WIN32 */
684 ACE_OS::ungetc (int c, FILE *fp)
686 #ifdef ACE_LACKS_UNGETC
689 ACE_NOTSUP_RETURN (-1);
691 return ace_ungetc_helper (c, fp);
696 ACE_OS::fputc (int c, FILE *fp)
698 #ifdef ACE_LACKS_FPUTC
701 ACE_NOTSUP_RETURN (-1);
703 return ace_fputc_helper (c, fp);
708 ACE_OS::putc (int c, FILE *fp)
710 #ifdef ACE_LACKS_PUTC
713 ACE_NOTSUP_RETURN (-1);
715 return ace_putc_helper (c, fp);
720 ACE_OS::fputs (const char *s, FILE *stream)
722 ACE_OS_TRACE ("ACE_OS::fputs");
723 #ifdef ACE_LACKS_FPUTS
725 ACE_UNUSED_ARG (stream);
726 ACE_NOTSUP_RETURN (-1);
728 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fputs (s, stream), int, -1);
732 #if defined (ACE_HAS_WCHAR) && !defined(ACE_LACKS_FPUTWS)
734 ACE_OS::fputs (const wchar_t *s, FILE *stream)
736 ACE_OS_TRACE ("ACE_OS::fputs");
737 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fputws (s, stream), int, -1);
739 #endif /* ACE_HAS_WCHAR && !ACE_LACKS_FPUTWS */
742 ACE_OS::fread (void *ptr, size_t size, size_t nelems, FILE *fp)
744 ACE_OS_TRACE ("ACE_OS::fread");
745 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fread (ptr, size, nelems, fp),
751 ACE_OS::freopen (const ACE_TCHAR *filename, const ACE_TCHAR *mode, FILE* stream)
753 ACE_OS_TRACE ("ACE_OS::freopen");
754 #if defined (ACE_WIN32) && defined(ACE_USES_WCHAR)
755 ACE_OSCALL_RETURN (::_wfreopen (ACE_TEXT_ALWAYS_WCHAR (filename),
756 ACE_TEXT_ALWAYS_WCHAR (mode),
759 #elif defined (ACE_LACKS_FREOPEN)
760 ACE_UNUSED_ARG (filename);
761 ACE_UNUSED_ARG (mode);
762 ACE_UNUSED_ARG (stream);
763 ACE_NOTSUP_RETURN (0);
766 (ACE_STD_NAMESPACE::freopen (ACE_TEXT_ALWAYS_CHAR (filename),
767 ACE_TEXT_ALWAYS_CHAR (mode),
770 #endif /* ACE_WIN32 && ACE_USES_WCHAR */
774 ACE_OS::fseek (FILE *fp, long offset, int whence)
776 #if defined (ACE_WIN32)
777 # if SEEK_SET != FILE_BEGIN || SEEK_CUR != FILE_CURRENT || SEEK_END != FILE_END
778 //#error Windows NT is evil AND rude!
785 whence = FILE_CURRENT;
792 return -1; // rather safe than sorry
794 # endif /* SEEK_SET != FILE_BEGIN || SEEK_CUR != FILE_CURRENT || SEEK_END != FILE_END */
795 #endif /* ACE_WIN32 */
796 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fseek (fp, offset, whence), int, -1);
800 ACE_OS::fsetpos (FILE* fp, fpos_t* pos)
802 #if defined (ACE_LACKS_FSETPOS)
804 ACE_UNUSED_ARG (pos);
805 ACE_NOTSUP_RETURN (-1);
807 ACE_OSCALL_RETURN (::fsetpos (fp, pos), int, -1);
808 #endif /* ACE_LACKS_FSETPOS */
812 ACE_OS::ftell (FILE* fp)
814 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::ftell (fp), long, -1);
818 ACE_OS::fwrite (const void *ptr, size_t size, size_t nitems, FILE *fp)
820 ACE_OS_TRACE ("ACE_OS::fwrite");
821 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::fwrite (ptr, size, nitems, fp),
827 ACE_OS::perror (const char *s)
829 ACE_OS_TRACE ("ACE_OS::perror");
830 #if defined (ACE_LACKS_PERROR)
834 #endif /* ACE_HAS_WINCE */
837 #if defined (ACE_HAS_WCHAR)
839 ACE_OS::perror (const wchar_t *s)
841 ACE_OS_TRACE ("ACE_OS::perror");
842 #if defined (ACE_LACKS_PERROR)
844 #elif defined (ACE_WIN32)
847 ACE_Wide_To_Ascii n_s (s);
848 ::perror (n_s.char_rep ());
849 #endif /* ACE_LACKS_PERROR */
851 #endif /* ACE_HAS_WCHAR */
854 ACE_OS::puts (const char *s)
856 ACE_OS_TRACE ("ACE_OS::puts");
857 #if defined (ACE_LACKS_PUTS)
859 ACE_NOTSUP_RETURN (-1);
861 ACE_OSCALL_RETURN (::puts (s), int, -1);
862 #endif /* ACE_LACKS_PUTS */
865 #if defined (ACE_HAS_WCHAR)
867 ACE_OS::puts (const wchar_t *s)
869 ACE_OS_TRACE ("ACE_OS::puts");
870 #if defined (ACE_WIN32)
871 ACE_OSCALL_RETURN (::_putws (s), int, -1);
872 #else /* ACE_WIN32 */
873 // There's no putws()...
874 ACE_Wide_To_Ascii n_s (s);
875 ACE_OSCALL_RETURN (::puts (n_s.char_rep ()), int, -1);
876 #endif /* ACE_WIN32 */
878 #endif /* ACE_HAS_WCHAR */
881 ACE_OS::rename (const char *old_name,
882 const char *new_name,
885 # if defined (ACE_LACKS_RENAME)
886 ACE_UNUSED_ARG (old_name);
887 ACE_UNUSED_ARG (new_name);
888 ACE_UNUSED_ARG (flags);
889 ACE_NOTSUP_RETURN (-1);
890 # elif defined (ACE_HAS_WINCE)
891 // Win CE is always wide-char.
892 ACE_UNUSED_ARG (flags);
893 if (0 == ::MoveFile (ACE_TEXT_CHAR_TO_TCHAR (old_name),
894 ACE_TEXT_CHAR_TO_TCHAR (new_name)))
895 ACE_FAIL_RETURN (-1);
897 # elif defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_MOVEFILEEX)
898 // NT4 (and up) provides a way to rename/move a file with similar semantics
899 // to what's usually done on UNIX - if there's an existing file with
900 // <new_name> it is removed before the file is renamed/moved. The
901 // MOVEFILE_COPY_ALLOWED is specified to allow such a rename across drives.
903 flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
904 if (::MoveFileExA (old_name, new_name, flags) == 0)
905 ACE_FAIL_RETURN (-1);
907 #elif defined (ACE_RENAME_EQUIVALENT)
908 ACE_OSCALL_RETURN (ACE_RENAME_EQUIVALENT (old_name, new_name), int, -1);
910 ACE_UNUSED_ARG (flags);
911 ACE_OSCALL_RETURN (::rename (old_name, new_name), int, -1);
912 # endif /* ACE_HAS_WINCE */
915 #if defined (ACE_HAS_WCHAR)
917 ACE_OS::rename (const wchar_t *old_name,
918 const wchar_t *new_name,
921 # if defined (ACE_LACKS_RENAME)
922 ACE_UNUSED_ARG (old_name);
923 ACE_UNUSED_ARG (new_name);
924 ACE_UNUSED_ARG (flags);
925 ACE_NOTSUP_RETURN (-1);
926 # elif defined (ACE_HAS_WINCE)
927 ACE_UNUSED_ARG (flags);
928 if (::MoveFileW (old_name, new_name) == 0)
929 ACE_FAIL_RETURN (-1);
931 # elif defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_MOVEFILEEX)
932 // NT4 (and up) provides a way to rename/move a file with similar semantics
933 // to what's usually done on UNIX - if there's an existing file with
934 // <new_name> it is removed before the file is renamed/moved. The
935 // MOVEFILE_COPY_ALLOWED is specified to allow such a rename across drives.
937 flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
938 if (::MoveFileExW (old_name, new_name, flags) == 0)
939 ACE_FAIL_RETURN (-1);
941 # elif defined (ACE_WIN32)
942 ACE_UNUSED_ARG (flags);
943 ACE_OSCALL_RETURN (::_wrename (old_name, new_name), int, -1);
945 ACE_Wide_To_Ascii nold_name (old_name);
946 ACE_Wide_To_Ascii nnew_name (new_name);
947 return ACE_OS::rename (nold_name.char_rep (), nnew_name.char_rep (), flags);
948 # endif /* ACE_HAS_WINCE */
950 #endif /* ACE_HAS_WCHAR */
953 ACE_OS::rewind (FILE *fp)
955 #if !defined (ACE_HAS_WINCE) && !defined (ACE_MQX)
956 ACE_OS_TRACE ("ACE_OS::rewind");
957 # if defined (ACE_LACKS_REWIND)
961 # endif /* ACE_LACKS_REWIND */
963 // This isn't perfect since it doesn't reset EOF, but it's probably
964 // the closest we can get on WINCE.
965 (void) ::fseek (fp, 0L, SEEK_SET);
966 #endif /* ACE_HAS_WINCE */
969 #if !defined (ACE_DISABLE_TEMPNAM)
971 ACE_OS::tempnam (const char *dir, const char *pfx)
973 ACE_OS_TRACE ("ACE_OS::tempnam");
974 #if defined (ACE_LACKS_TEMPNAM)
975 ACE_UNUSED_ARG (dir);
976 ACE_UNUSED_ARG (pfx);
977 ACE_NOTSUP_RETURN (0);
978 #elif defined (ACE_HAS_NONCONST_TEMPNAM)
979 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::tempnam (const_cast <char *> (dir), const_cast<char *> (pfx)), char *, 0);
980 #elif defined (ACE_TEMPNAM_EQUIVALENT)
981 ACE_OSCALL_RETURN (ACE_TEMPNAM_EQUIVALENT (dir, pfx), char *, 0);
982 #else /* ACE_LACKS_TEMPNAM */
983 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::tempnam (dir, pfx), char *, 0);
984 #endif /* ACE_LACKS_TEMPNAM */
987 #if defined (ACE_HAS_WCHAR)
989 ACE_OS::tempnam (const wchar_t *dir, const wchar_t *pfx)
991 ACE_OS_TRACE ("ACE_OS::tempnam");
992 #if defined (ACE_LACKS_TEMPNAM)
993 ACE_UNUSED_ARG (dir);
994 ACE_UNUSED_ARG (pfx);
995 ACE_NOTSUP_RETURN (0);
996 #elif defined(ACE_WIN32)
997 # if defined (ACE_HAS_NONCONST_TEMPNAM)
998 ACE_OSCALL_RETURN (::_wtempnam (const_cast <wchar_t*> (dir), const_cast <wchar_t*> (pfx)), wchar_t *, 0);
1000 ACE_OSCALL_RETURN (::_wtempnam (dir, pfx), wchar_t *, 0);
1001 # endif /* ACE_HAS_NONCONST_TEMPNAM */
1002 #else /* ACE_LACKS_TEMPNAM */
1003 // No native wide-char support; convert to narrow and call the char* variant.
1004 char *ndir = ACE_Wide_To_Ascii (dir).char_rep ();
1005 char *npfx = ACE_Wide_To_Ascii (pfx).char_rep ();
1006 char *name = ACE_OS::tempnam (ndir, npfx);
1007 // ACE_OS::tempnam returns a pointer to a malloc()-allocated space.
1008 // Convert that string to wide-char and free() the original.
1012 size_t namelen = ACE_OS::strlen (name) + 1;
1013 wname = reinterpret_cast<wchar_t *>
1014 (ACE_OS::malloc (namelen * sizeof (wchar_t)));
1016 ACE_OS::strcpy (wname, ACE_Ascii_To_Wide (name).wchar_rep ());
1017 ACE_OS::free (name);
1020 #endif /* ACE_LACKS_TEMPNAM */
1022 #endif /* ACE_HAS_WCHAR */
1023 #endif /* !ACE_DISABLE_TEMPNAM */
1026 ACE_OS::vasprintf (char **bufp, const char* format, va_list argptr)
1028 #if defined (ACE_HAS_VASPRINTF)
1029 return ::vasprintf (bufp, format, argptr);
1030 #elif defined (ACE_LACKS_VA_COPY)
1031 ACE_UNUSED_ARG (bufp);
1032 ACE_UNUSED_ARG (format);
1033 ACE_UNUSED_ARG (argptr);
1034 ACE_NOTSUP_RETURN (-1);
1036 return ACE_OS::vasprintf_emulation (bufp, format, argptr);
1037 #endif /* ACE_HAS_VASPRINTF */
1040 #if defined (ACE_HAS_WCHAR)
1042 ACE_OS::vasprintf (wchar_t **bufp, const wchar_t* format, va_list argptr)
1044 #if defined (ACE_HAS_VASWPRINTF)
1045 return ::vaswprintf (bufp, format, argptr);
1046 #elif defined (ACE_LACKS_VA_COPY)
1047 ACE_UNUSED_ARG (bufp);
1048 ACE_UNUSED_ARG (format);
1049 ACE_UNUSED_ARG (argptr);
1050 ACE_NOTSUP_RETURN (-1);
1052 return ACE_OS::vaswprintf_emulation (bufp, format, argptr);
1053 #endif /* ACE_HAS_VASWPRINTF */
1055 #endif /* ACE_HAS_WCHAR */
1058 ACE_OS::vprintf (const char *format, va_list argptr)
1060 #if defined (ACE_LACKS_VPRINTF)
1061 ACE_UNUSED_ARG (format);
1062 ACE_UNUSED_ARG (argptr);
1063 ACE_NOTSUP_RETURN (-1);
1065 return ::vprintf (format, argptr);
1066 #endif /* ACE_LACKS_VPRINTF */
1069 #if defined (ACE_HAS_WCHAR)
1071 ACE_OS::vprintf (const wchar_t *format, va_list argptr)
1073 #if defined (ACE_HAS_VWPRINTF)
1074 return ::vwprintf (format, argptr);
1076 ACE_UNUSED_ARG (format);
1077 ACE_UNUSED_ARG (argptr);
1078 ACE_NOTSUP_RETURN (-1);
1079 #endif /* ACE_HAS_VWPRINTF */
1081 #endif /* ACE_HAS_WCHAR */
1084 ACE_OS::vfprintf (FILE *fp, const char *format, va_list argptr)
1086 #ifdef ACE_LACKS_VFPRINTF
1087 ACE_UNUSED_ARG (fp);
1088 ACE_UNUSED_ARG (format);
1089 ACE_UNUSED_ARG (argptr);
1090 ACE_NOTSUP_RETURN (-1);
1092 return ACE_STD_NAMESPACE::vfprintf (fp, format, argptr);
1096 #if defined (ACE_HAS_WCHAR)
1098 ACE_OS::vfprintf (FILE *fp, const wchar_t *format, va_list argptr)
1100 #if defined (ACE_HAS_VFWPRINTF)
1101 return ::vfwprintf (fp, format, argptr);
1103 ACE_UNUSED_ARG (fp);
1104 ACE_UNUSED_ARG (format);
1105 ACE_UNUSED_ARG (argptr);
1106 ACE_NOTSUP_RETURN (-1);
1107 #endif /* ACE_HAS_VFWPRINTF */
1109 #endif /* ACE_HAS_WCHAR */
1112 ACE_OS::vsprintf (char *buffer, const char *format, va_list argptr)
1114 #ifdef ACE_LACKS_VSPRINTF
1115 ACE_UNUSED_ARG (buffer);
1116 ACE_UNUSED_ARG (format);
1117 ACE_UNUSED_ARG (argptr);
1118 ACE_NOTSUP_RETURN (-1);
1120 return ::vsprintf (buffer, format, argptr);
1121 #endif /* ACE_LACKS_VSPRINTF */
1124 #if defined (ACE_HAS_WCHAR)
1126 ACE_OS::vsprintf (wchar_t *buffer, const wchar_t *format, va_list argptr)
1128 # if (defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= 500) || \
1129 (defined (sun) && !(defined(_XOPEN_SOURCE) && (_XOPEN_VERSION-0==4))) || \
1130 defined (ACE_HAS_DINKUM_STL) || defined (__DMC__) || \
1131 defined (ACE_HAS_VSWPRINTF) || \
1132 (defined (ACE_WIN32_VC10) && !defined (ACE_HAS_WINCE)) || \
1133 (defined (ACE_WIN32_VC9) && !defined (ACE_HAS_WINCE)) || \
1134 (defined (ACE_WIN32_VC8) && !defined (ACE_HAS_WINCE) && \
1135 _MSC_FULL_VER > 140050000)
1137 // The XPG4/UNIX98/C99 signature of the wide-char sprintf has a
1138 // maxlen argument. Since this method doesn't supply one, pass in
1139 // a length that works (ULONG_MAX doesn't on all platform since some check
1140 // to see if the operation will remain in bounds). If this isn't ok, use
1141 // ACE_OS::snprintf().
1142 return vswprintf (buffer, 4096, format, argptr);
1144 # elif defined (__MINGW64_VERSION_MAJOR) && !defined (WIN64)
1145 // the MingW64 32bit version causes link errors when using the
1146 // 'standard' vswprint(). Luckily they have a mingw special.
1148 return __mingw_vswprintf (buffer, format, argptr);
1150 # elif defined (ACE_WIN32)
1151 // Windows has vswprintf, but the pre-VC8 signature is from the older
1152 // ISO C standard. Also see ACE_OS::snprintf() for more info on this.
1154 return vswprintf (buffer, format, argptr);
1157 ACE_UNUSED_ARG (buffer);
1158 ACE_UNUSED_ARG (format);
1159 ACE_UNUSED_ARG (argptr);
1160 ACE_NOTSUP_RETURN (-1);
1162 # endif /* XPG5 || ACE_HAS_DINKUM_STL */
1164 #endif /* ACE_HAS_WCHAR */
1167 ACE_OS::vsnprintf (char *buffer, size_t maxlen, const char *format, va_list ap)
1169 #if !defined (ACE_LACKS_VSNPRINTF)
1171 # if defined (ACE_WIN32) && !defined (ACE_HAS_C99_VSNPRINTF)
1172 result = ::_vsnprintf (buffer, maxlen, format, ap);
1174 // Win32 doesn't regard a full buffer with no 0-terminate as an overrun.
1175 if (result == static_cast<int> (maxlen) && maxlen > 0)
1176 buffer[maxlen-1] = '\0';
1178 // Win32 doesn't 0-terminate the string if it overruns maxlen.
1179 if (result == -1 && maxlen > 0)
1180 buffer[maxlen-1] = '\0';
1182 result = ::vsnprintf (buffer, maxlen, format, ap);
1184 // In out-of-range conditions, C99 defines vsnprintf() to return the number
1185 // of characters that would have been written if enough space was available.
1186 // Earlier variants of the vsnprintf() (e.g. UNIX98) defined it to return
1187 // -1. This method follows the C99 standard, but needs to guess at the
1188 // value; uses maxlen + 1.
1191 result = static_cast <int> (maxlen + 1);
1195 #elif defined (ACE_HAS_TRIO)
1196 return trio_vsnprintf (buffer, maxlen, format, ap);
1197 #elif defined (ACE_HAS_VSNPRINTF_EMULATION)
1198 return vsnprintf_emulation (buffer, maxlen, format, ap);
1200 ACE_UNUSED_ARG (buffer);
1201 ACE_UNUSED_ARG (maxlen);
1202 ACE_UNUSED_ARG (format);
1203 ACE_UNUSED_ARG (ap);
1204 ACE_NOTSUP_RETURN (-1);
1205 #endif /* ACE_LACKS_VSNPRINTF */
1208 #if defined (ACE_HAS_WCHAR)
1210 ACE_OS::vsnprintf (wchar_t *buffer, size_t maxlen, const wchar_t *format, va_list ap)
1212 # if (defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= 500) || \
1213 (defined (sun) && !(defined(_XOPEN_SOURCE) && (_XOPEN_VERSION-0==4))) || \
1214 (defined (ACE_HAS_DINKUM_STL) || defined (__DMC__)) || \
1215 defined (ACE_HAS_VSWPRINTF) || \
1220 # if defined (ACE_WIN32) && !defined (ACE_HAS_C99_VSNWPRINTF)
1221 // Microsoft's vswprintf() doesn't have the maxlen argument that
1222 // XPG4/UNIX98 define. They do, however, recommend use of _vsnwprintf()
1223 // as a substitute, which does have the same signature as the UNIX98 one.
1224 result = ::_vsnwprintf (buffer, maxlen, format, ap);
1226 // Win32 doesn't regard a full buffer with no 0-terminate as an overrun.
1227 if (result == static_cast<int> (maxlen) && maxlen > 0)
1228 buffer[maxlen-1] = '\0';
1230 // Win32 doesn't 0-terminate the string if it overruns maxlen.
1231 if (result == -1 && maxlen > 0)
1232 buffer[maxlen-1] = '\0';
1234 result = vswprintf (buffer, maxlen, format, ap);
1237 // In out-of-range conditions, C99 defines vsnprintf() to return the
1238 // number of characters that would have been written if enough space
1239 // was available. Earlier variants of the vsnprintf() (e.g. UNIX98)
1240 // defined it to return -1. This method follows the C99 standard,
1241 // but needs to guess at the value; uses maxlen + 1.
1242 // Note that a formatting failure also returns -1. On RHEL this is
1243 // errno EINVAL. Don't indicate a simple memory shortage for that.
1244 if (result == -1 && errno != EINVAL)
1245 result = static_cast <int> (maxlen + 1);
1249 ACE_UNUSED_ARG (buffer);
1250 ACE_UNUSED_ARG (maxlen);
1251 ACE_UNUSED_ARG (format);
1252 ACE_UNUSED_ARG (ap);
1253 ACE_NOTSUP_RETURN (-1);
1254 # endif /* platforms with a variant of vswprintf */
1256 #endif /* ACE_HAS_WCHAR */
1258 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
1259 #if defined (ACE_WIN32)
1260 ACE_INLINE const ACE_TEXT_OSVERSIONINFO &
1261 ACE_OS::get_win32_versioninfo ()
1263 return ACE_OS::win32_versioninfo_;
1266 ACE_INLINE HINSTANCE
1267 ACE_OS::get_win32_resource_module ()
1269 return ACE_OS::win32_resource_module_;
1273 ACE_OS::set_win32_resource_module (HINSTANCE instance)
1275 ACE_OS::win32_resource_module_ = instance;
1278 ACE_INLINE LPSECURITY_ATTRIBUTES
1279 ACE_OS::default_win32_security_attributes (LPSECURITY_ATTRIBUTES sa)
1281 #if defined (ACE_DEFINES_DEFAULT_WIN32_SECURITY_ATTRIBUTES)
1284 // @@ This is a good place to use pthread_once.
1285 static SECURITY_ATTRIBUTES default_sa;
1286 static SECURITY_DESCRIPTOR sd;
1287 InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
1288 SetSecurityDescriptorDacl(&sd, TRUE, 0, FALSE);
1289 default_sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1290 default_sa.lpSecurityDescriptor = &sd;
1291 default_sa.bInheritHandle = TRUE;
1295 #else /* !ACE_DEFINES_DEFAULT_WIN32_SECURITY_ATTRIBUTES */
1297 #endif /* ACE_DEFINES_DEFAULT_WIN32_SECURITY_ATTRIBUTES */
1300 ACE_INLINE LPSECURITY_ATTRIBUTES
1301 ACE_OS::default_win32_security_attributes_r (LPSECURITY_ATTRIBUTES sa,
1302 LPSECURITY_ATTRIBUTES sa_buffer,
1303 SECURITY_DESCRIPTOR* sd_buffer)
1305 #if defined (ACE_DEFINES_DEFAULT_WIN32_SECURITY_ATTRIBUTES)
1308 if (sa_buffer != 0 && sd_buffer != 0)
1310 InitializeSecurityDescriptor
1311 (sd_buffer, SECURITY_DESCRIPTOR_REVISION);
1312 SetSecurityDescriptorDacl (sd_buffer, TRUE, 0, FALSE);
1313 sa_buffer->nLength = sizeof(SECURITY_ATTRIBUTES);
1314 sa_buffer->lpSecurityDescriptor = sd_buffer;
1315 sa_buffer->bInheritHandle = TRUE;
1320 #else /* !ACE_DEFINES_DEFAULT_WIN32_SECURITY_ATTRIBUTES */
1321 ACE_UNUSED_ARG(sa_buffer);
1322 ACE_UNUSED_ARG(sd_buffer);
1324 #endif /* ACE_DEFINES_DEFAULT_WIN32_SECURITY_ATTRIBUTES */
1327 #endif /* ACE_WIN32 */
1330 ACE_END_VERSIONED_NAMESPACE_DECL