3 // $Id: OS_NS_unistd.inl 81696 2008-05-14 18:15:31Z johnnyw $
5 #include "ace/OS_NS_sys_utsname.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_errno.h"
8 #include "ace/OS_NS_macros.h"
9 #include "ace/OS_NS_fcntl.h"
10 #include "ace/Default_Constants.h"
11 #include "ace/OS_Memory.h"
12 #include "ace/Truncate.h"
14 #if defined (ACE_HAS_CLOCK_GETTIME)
15 # include "ace/os_include/os_time.h"
16 #endif /* ACE_HAS_CLOCK_GETTIME */
18 #if defined (ACE_LACKS_ACCESS)
19 # include "ace/OS_NS_stdio.h"
20 #endif /* ACE_LACKS_ACCESS */
22 #if defined (ACE_VXWORKS) || defined (ACE_HAS_WINCE)
23 # include "ace/os_include/os_unistd.h"
24 # if defined (ACE_VXWORKS) && (((ACE_VXWORKS >= 0x620) && (ACE_VXWORKS <= 0x660)) || defined (ACE_HAS_VXWORKS551_MEDUSA))
25 # if defined (__RTP__)
26 # include "ace/os_include/os_strings.h"
28 # include "ace/os_include/os_string.h"
31 #endif /* VXWORKS || ACE_HAS_WINCE */
33 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
36 ACE_OS::access (const char *path, int amode)
38 ACE_OS_TRACE ("ACE_OS::access");
39 #if defined (ACE_LACKS_ACCESS)
40 # if defined (ACE_HAS_WINCE) || defined (ACE_VXWORKS)
41 // @@ WINCE: There should be a Win32 API that can do this.
42 // Hard coded read access here.
43 ACE_UNUSED_ARG (amode);
44 FILE* handle = ACE_OS::fopen (ACE_TEXT_CHAR_TO_TCHAR(path),
48 ACE_OS::fclose (handle);
53 ACE_UNUSED_ARG (path);
54 ACE_UNUSED_ARG (amode);
55 ACE_NOTSUP_RETURN (-1);
56 # endif // ACE_HAS_WINCE
57 #elif defined(ACE_WIN32)
58 // Windows doesn't support checking X_OK(6)
59 ACE_OSCALL_RETURN (::access (path, amode & 6), int, -1);
61 ACE_OSCALL_RETURN (::access (path, amode), int, -1);
62 #endif /* ACE_LACKS_ACCESS */
66 #if defined (ACE_HAS_WCHAR)
68 ACE_OS::access (const wchar_t *path, int amode)
70 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
71 ACE_OSCALL_RETURN (::_waccess (path, amode), int, -1);
72 #else /* ACE_WIN32 && !ACE_HAS_WINCE */
73 return ACE_OS::access (ACE_Wide_To_Ascii (path).char_rep (), amode);
74 #endif /* ACE_WIN32 && !ACE_HAS_WINCE */
76 #endif /* ACE_HAS_WCHAR */
79 ACE_OS::alarm (u_int secs)
81 ACE_OS_TRACE ("ACE_OS::alarm");
82 #if defined (ACE_LACKS_ALARM)
83 ACE_UNUSED_ARG (secs);
84 ACE_NOTSUP_RETURN (0);
86 return ::alarm (secs);
87 #endif /* ACE_LACKS_ALARM */
91 ACE_OS::getpagesize (void)
93 ACE_OS_TRACE ("ACE_OS::getpagesize");
94 #if defined (ACE_WIN32) && !defined (ACE_HAS_PHARLAP)
96 ::GetSystemInfo (&sys_info);
97 return (long) sys_info.dwPageSize;
98 #elif defined (_SC_PAGESIZE) && !defined (ACE_HAS_NOTSUP_SC_PAGESIZE)
99 return ::sysconf (_SC_PAGESIZE);
100 #elif defined (ACE_HAS_GETPAGESIZE)
101 return ::getpagesize ();
103 // Use the default set in config.h
104 return ACE_PAGE_SIZE;
105 #endif /* ACE_WIN32 */
109 ACE_OS::allocation_granularity (void)
111 #if defined (ACE_WIN32)
112 SYSTEM_INFO sys_info;
113 ::GetSystemInfo (&sys_info);
114 return sys_info.dwAllocationGranularity;
116 return ACE_OS::getpagesize ();
117 #endif /* ACE_WIN32 */
120 #if !defined (ACE_LACKS_CHDIR)
122 ACE_OS::chdir (const char *path)
124 ACE_OS_TRACE ("ACE_OS::chdir");
125 #if defined (ACE_HAS_NONCONST_CHDIR)
126 ACE_OSCALL_RETURN (::chdir (const_cast<char *> (path)), int, -1);
127 #elif defined (ACE_HAS_WINCE)
128 ACE_UNUSED_ARG (path);
129 ACE_NOTSUP_RETURN (-1);
131 ACE_OSCALL_RETURN (::chdir (path), int, -1);
132 #endif /* ACE_HAS_NONCONST_CHDIR */
135 #if defined (ACE_HAS_WCHAR)
137 ACE_OS::chdir (const wchar_t *path)
139 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
140 ACE_OSCALL_RETURN (::_wchdir (path), int, -1);
141 #else /* ACE_WIN32 */
142 return ACE_OS::chdir (ACE_Wide_To_Ascii (path).char_rep ());
143 #endif /* ACE_WIN32 */
145 #endif /* ACE_HAS_WCHAR */
146 #endif /* ACE_LACKS_CHDIR */
149 ACE_OS::rmdir (const char *path)
151 #if defined (ACE_HAS_WINCE)
152 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::RemoveDirectory (ACE_TEXT_CHAR_TO_TCHAR (path)),
156 ACE_OSCALL_RETURN (::rmdir (path), int, -1);
157 #endif /* ACE_WIN32 */
160 #if defined (ACE_HAS_WCHAR)
162 ACE_OS::rmdir (const wchar_t *path)
164 #if defined (ACE_HAS_WINCE)
165 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::RemoveDirectoryW (path),
168 #elif defined (ACE_WIN32)
169 ACE_OSCALL_RETURN (::_wrmdir (path), int, -1);
171 ACE_Wide_To_Ascii n_path (path);
172 return ACE_OS::rmdir (n_path.char_rep ());
173 #endif /* ACE_HAS_WINCE */
175 #endif /* ACE_HAS_WCHAR */
177 // @todo: which 4 and why??? dhinton
178 // NOTE: The following four function definitions must appear before
179 // ACE_OS::sema_init ().
182 ACE_OS::close (ACE_HANDLE handle)
184 ACE_OS_TRACE ("ACE_OS::close");
185 #if defined (ACE_WIN32)
186 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::CloseHandle (handle), ace_result_), int, -1);
188 ACE_OSCALL_RETURN (::close (handle), int, -1);
189 #endif /* ACE_WIN32 */
192 ACE_INLINE ACE_HANDLE
193 ACE_OS::dup (ACE_HANDLE handle)
195 ACE_OS_TRACE ("ACE_OS::dup");
196 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
198 if (::DuplicateHandle(::GetCurrentProcess (),
200 ::GetCurrentProcess(),
204 DUPLICATE_SAME_ACCESS))
207 ACE_FAIL_RETURN (ACE_INVALID_HANDLE);
209 #elif defined (ACE_LACKS_DUP)
210 ACE_UNUSED_ARG (handle);
211 ACE_NOTSUP_RETURN (-1);
212 #elif defined (ACE_HAS_WINCE)
213 ACE_UNUSED_ARG (handle);
214 ACE_NOTSUP_RETURN (0);
216 ACE_OSCALL_RETURN (::dup (handle), ACE_HANDLE, ACE_INVALID_HANDLE);
217 #endif /* ACE_WIN32 && !ACE_HAS_WINCE */
221 ACE_OS::dup2 (ACE_HANDLE oldhandle, ACE_HANDLE newhandle)
223 ACE_OS_TRACE ("ACE_OS::dup2");
224 #if defined (ACE_LACKS_DUP2)
225 // msvcrt has _dup2 ?!
226 ACE_UNUSED_ARG (oldhandle);
227 ACE_UNUSED_ARG (newhandle);
228 ACE_NOTSUP_RETURN (-1);
230 ACE_OSCALL_RETURN (::dup2 (oldhandle, newhandle), int, -1);
231 #endif /* ACE_LACKS_DUP2 */
235 ACE_OS::execv (const char *path,
238 ACE_OS_TRACE ("ACE_OS::execv");
239 #if defined (ACE_LACKS_EXEC)
240 ACE_UNUSED_ARG (path);
241 ACE_UNUSED_ARG (argv);
243 ACE_NOTSUP_RETURN (-1);
244 #elif defined (ACE_WIN32)
245 # if defined (__BORLANDC__) /* VSB */
246 return ::execv (path, argv);
247 # elif defined (__MINGW32__)
248 return ::_execv (path, (char *const *) argv);
250 // Why this odd-looking code? If execv() returns at all, it's an error.
251 // Windows defines this as returning an intptr_t rather than a simple int,
252 // and the conversion triggers compile warnings. So just return -1 if
254 ::_execv (path, (const char *const *) argv);
256 # endif /* __BORLANDC__ */
258 ACE_OSCALL_RETURN (::execv (path, argv), int, -1);
259 #endif /* ACE_LACKS_EXEC */
263 ACE_OS::execve (const char *path,
267 ACE_OS_TRACE ("ACE_OS::execve");
268 #if defined (ACE_LACKS_EXEC)
269 ACE_UNUSED_ARG (path);
270 ACE_UNUSED_ARG (argv);
271 ACE_UNUSED_ARG (envp);
273 ACE_NOTSUP_RETURN (-1);
274 #elif defined (ACE_WIN32)
275 # if defined (__BORLANDC__) /* VSB */
276 return ::execve (path, argv, envp);
277 # elif defined (__MINGW32__)
278 return ::_execve (path, (char *const *) argv, (char *const *) envp);
280 // Why this odd-looking code? If execv() returns at all, it's an error.
281 // Windows defines this as returning an intptr_t rather than a simple int,
282 // and the conversion triggers compile warnings. So just return -1 if
284 ::_execve (path, (const char *const *) argv, (const char *const *) envp);
286 # endif /* __BORLANDC__ */
288 ACE_OSCALL_RETURN (::execve (path, argv, envp), int, -1);
289 #endif /* ACE_LACKS_EXEC */
293 ACE_OS::execvp (const char *file,
296 ACE_OS_TRACE ("ACE_OS::execvp");
297 #if defined (ACE_LACKS_EXEC)
298 ACE_UNUSED_ARG (file);
299 ACE_UNUSED_ARG (argv);
301 ACE_NOTSUP_RETURN (-1);
302 #elif defined (ACE_WIN32)
303 # if defined (__BORLANDC__) /* VSB */
304 return ::execvp (file, argv);
305 # elif defined (__MINGW32__)
306 return ::_execvp (file, (char *const *) argv);
308 // Why this odd-looking code? If execv() returns at all, it's an error.
309 // Windows defines this as returning an intptr_t rather than a simple int,
310 // and the conversion triggers compile warnings. So just return -1 if
312 ::_execvp (file, (const char *const *) argv);
314 # endif /* __BORLANDC__ */
316 ACE_OSCALL_RETURN (::execvp (file, argv), int, -1);
317 #endif /* ACE_LACKS_EXEC */
323 ACE_OS_TRACE ("ACE_OS::fork");
324 #if defined (ACE_LACKS_FORK)
325 ACE_NOTSUP_RETURN (pid_t (-1));
327 ACE_OSCALL_RETURN (::fork (), pid_t, -1);
328 #endif /* ACE_LACKS_FORK */
332 ACE_OS::fsync (ACE_HANDLE handle)
334 ACE_OS_TRACE ("ACE_OS::fsync");
335 # if defined (ACE_LACKS_FSYNC)
336 ACE_UNUSED_ARG (handle);
337 ACE_NOTSUP_RETURN (-1);
338 # elif defined (ACE_WIN32)
339 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::FlushFileBuffers (handle), ace_result_), int, -1);
341 ACE_OSCALL_RETURN (::fsync (handle), int, -1);
342 # endif /* ACE_LACKS_FSYNC */
346 ACE_OS::ftruncate (ACE_HANDLE handle, ACE_OFF_T offset)
348 ACE_OS_TRACE ("ACE_OS::ftruncate");
349 #if defined (ACE_WIN32)
350 # if !defined (ACE_LACKS_WIN32_SETFILEPOINTEREX)
352 loff.QuadPart = offset;
353 if (::SetFilePointerEx (handle, loff, 0, FILE_BEGIN))
355 if (::SetFilePointer (handle,
358 FILE_BEGIN) != INVALID_SET_FILE_POINTER)
360 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::SetEndOfFile (handle), ace_result_), int, -1);
362 ACE_FAIL_RETURN (-1);
364 ACE_OSCALL_RETURN (::ftruncate (handle, offset), int, -1);
365 #endif /* ACE_WIN32 */
369 ACE_OS::getcwd (char *buf, size_t size)
371 ACE_OS_TRACE ("ACE_OS::getcwd");
372 #if defined (ACE_LACKS_GETCWD)
373 ACE_UNUSED_ARG (buf);
374 ACE_UNUSED_ARG (size);
375 ACE_NOTSUP_RETURN (0);
376 #elif defined (ACE_WIN32)
377 return ::getcwd (buf, static_cast<int> (size));
379 ACE_OSCALL_RETURN (::getcwd (buf, size), char *, 0);
380 #endif /* ACE_LACKS_GETCWD */
383 #if defined (ACE_HAS_WCHAR)
385 ACE_OS::getcwd (wchar_t *buf, size_t size)
387 # if defined (ACE_HAS_WINCE)
388 ACE_UNUSED_ARG (buf);
389 ACE_UNUSED_ARG (size);
390 ACE_NOTSUP_RETURN (0);
391 # elif defined (ACE_WIN32)
392 return ::_wgetcwd (buf, static_cast<int> (size));
394 char *narrow_buf = new char[size];
396 result = ACE_OS::getcwd (narrow_buf, size);
397 ACE_Ascii_To_Wide wide_buf (result);
398 delete [] narrow_buf;
400 ACE_OS::strsncpy (buf, wide_buf.wchar_rep (), size);
401 return result == 0 ? 0 : buf;
402 # endif /* ACE_WIN32 */
404 #endif /* ACE_HAS_WCHAR */
407 ACE_OS::getgid (void)
409 ACE_OS_TRACE ("ACE_OS::getgid");
410 #if defined (ACE_LACKS_GETGID)
411 ACE_NOTSUP_RETURN (static_cast<gid_t> (-1));
413 ACE_OSCALL_RETURN (::getgid (), gid_t, static_cast<gid_t> (-1));
414 # endif /* ACE_LACKS_GETGID */
418 ACE_OS::getegid (void)
420 ACE_OS_TRACE ("ACE_OS::getegid");
421 #if defined (ACE_LACKS_GETEGID)
422 ACE_NOTSUP_RETURN (static_cast<gid_t> (-1));
424 ACE_OSCALL_RETURN (::getegid (), gid_t, static_cast<gid_t> (-1));
425 # endif /* ACE_LACKS_GETEGID */
429 ACE_OS::getopt (int argc, char *const *argv, const char *optstring)
431 ACE_OS_TRACE ("ACE_OS::getopt");
432 #if defined (ACE_LACKS_GETOPT)
433 ACE_UNUSED_ARG (argc);
434 ACE_UNUSED_ARG (argv);
435 ACE_UNUSED_ARG (optstring);
436 ACE_NOTSUP_RETURN (-1);
438 ACE_OSCALL_RETURN (::getopt (argc, argv, optstring), int, -1);
439 # endif /* ACE_LACKS_GETOPT */
443 ACE_OS::getpgid (pid_t pid)
445 ACE_OS_TRACE ("ACE_OS::getpgid");
446 #if defined (ACE_LACKS_GETPGID)
447 ACE_UNUSED_ARG (pid);
448 ACE_NOTSUP_RETURN (-1);
449 #elif defined (linux) && __GLIBC__ > 1 && __GLIBC_MINOR__ >= 0
450 // getpgid() is from SVR4, which appears to be the reason why GLIBC
451 // doesn't enable its prototype by default.
452 // Rather than create our own extern prototype, just use the one
453 // that is visible (ugh).
454 ACE_OSCALL_RETURN (::__getpgid (pid), pid_t, -1);
456 ACE_OSCALL_RETURN (::getpgid (pid), pid_t, -1);
457 #endif /* ACE_LACKS_GETPGID */
461 ACE_OS::getpid (void)
463 // ACE_OS_TRACE ("ACE_OS::getpid");
464 #if defined (ACE_LACKS_GETPID)
465 ACE_NOTSUP_RETURN (-1);
466 #elif defined (ACE_WIN32)
467 return ::GetCurrentProcessId ();
469 ACE_OSCALL_RETURN (::getpid (), int, -1);
470 #endif /* ACE_LACKS_GETPID */
474 ACE_OS::getppid (void)
476 ACE_OS_TRACE ("ACE_OS::getppid");
477 #if defined (ACE_LACKS_GETPPID)
478 ACE_NOTSUP_RETURN (-1);
480 ACE_OSCALL_RETURN (::getppid (), pid_t, -1);
481 #endif /* ACE_LACKS_GETPPID */
485 ACE_OS::getuid (void)
487 ACE_OS_TRACE ("ACE_OS::getuid");
488 #if defined (ACE_LACKS_GETUID)
489 ACE_NOTSUP_RETURN (static_cast<uid_t> (-1));
491 ACE_OSCALL_RETURN (::getuid (), uid_t, static_cast<uid_t> (-1));
492 # endif /* ACE_LACKS_GETUID*/
496 ACE_OS::geteuid (void)
498 ACE_OS_TRACE ("ACE_OS::geteuid");
499 #if defined (ACE_LACKS_GETEUID)
500 ACE_NOTSUP_RETURN (static_cast<uid_t> (-1));
502 ACE_OSCALL_RETURN (::geteuid (), uid_t, (uid_t) -1);
503 # endif /* ACE_LACKS_GETEUID */
507 ACE_OS::hostname (char name[], size_t maxnamelen)
509 ACE_OS_TRACE ("ACE_OS::hostname");
510 #if defined (ACE_HAS_PHARLAP)
511 // PharLap only can do net stuff with the RT version.
512 # if defined (ACE_HAS_PHARLAP_RT)
513 // @@This is not at all reliable... requires ethernet and BOOTP to be used.
514 // A more reliable way is to go thru the devices w/ EtsTCPGetDeviceCfg until
515 // a legit IP address is found, then get its name w/ gethostbyaddr.
516 ACE_SOCKCALL_RETURN (gethostname (name, maxnamelen), int, SOCKET_ERROR);
518 ACE_UNUSED_ARG (name);
519 ACE_UNUSED_ARG (maxnamelen);
520 ACE_NOTSUP_RETURN (-1);
521 # endif /* ACE_HAS_PHARLAP_RT */
522 #elif defined (ACE_VXWORKS) || defined (ACE_HAS_WINCE)
523 ACE_OSCALL_RETURN (::gethostname (name, maxnamelen), int, -1);
524 #elif defined (ACE_WIN32)
525 if (::gethostname (name, ACE_Utils::truncate_cast<int> (maxnamelen)) == 0)
531 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::GetComputerNameA (name,
532 LPDWORD (&maxnamelen)),
533 ace_result_), int, -1);
535 #else /* ACE_HAS_PHARLAP */
536 ACE_utsname host_info;
538 if (ACE_OS::uname (&host_info) == -1)
542 ACE_OS::strsncpy (name, host_info.nodename, maxnamelen);
545 #endif /* ACE_HAS_PHARLAP */
548 #if defined (ACE_HAS_WCHAR)
550 ACE_OS::hostname (wchar_t name[], size_t maxnamelen)
552 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
553 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (GetComputerNameW (name,
554 LPDWORD (&maxnamelen)),
555 ace_result_), int, -1);
556 #else /* ACE_WIN32 && !ACE_HAS_WINCE */
557 // Emulate using the char version
561 ACE_NEW_RETURN (char_name, char[maxnamelen], -1);
563 result = ACE_OS::hostname(char_name, maxnamelen);
564 ACE_OS::strcpy (name, ACE_Ascii_To_Wide (char_name).wchar_rep ());
568 #endif /* ACE_WIN32 && !ACE_HAS_WINCE */
570 #endif /* ACE_HAS_WCHAR */
573 ACE_OS::isatty (int handle)
575 ACE_OS_TRACE ("ACE_OS::isatty");
576 #if defined (ACE_LACKS_ISATTY)
577 ACE_UNUSED_ARG (handle);
579 # elif defined (ACE_WIN32)
580 return ::_isatty (handle);
582 ACE_OSCALL_RETURN (::isatty (handle), int, -1);
583 # endif /* ACE_LACKS_ISATTY */
586 #if defined (ACE_WIN32)
588 ACE_OS::isatty (ACE_HANDLE handle)
590 #if defined (ACE_LACKS_ISATTY)
591 ACE_UNUSED_ARG (handle);
594 int fd = ::_open_osfhandle (intptr_t (handle), 0);
595 int status = ::_isatty (fd);
598 #endif /* ACE_LACKS_ISATTY */
601 #endif /* ACE_WIN32 */
604 ACE_OS::lseek (ACE_HANDLE handle, ACE_OFF_T offset, int whence)
606 ACE_OS_TRACE ("ACE_OS::lseek");
607 #if defined (ACE_WIN32)
608 # if SEEK_SET != FILE_BEGIN || SEEK_CUR != FILE_CURRENT || SEEK_END != FILE_END
609 //#error Windows NT is evil AND rude!
616 whence = FILE_CURRENT;
623 return static_cast<ACE_OFF_T> (-1); // rather safe than sorry
625 # endif /* SEEK_SET != FILE_BEGIN || SEEK_CUR != FILE_CURRENT || SEEK_END != FILE_END */
626 LONG low_offset = ACE_LOW_PART(offset);
627 LONG high_offset = ACE_HIGH_PART(offset);
629 ::SetFilePointer (handle, low_offset, &high_offset, whence);
630 if (result == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
631 ACE_FAIL_RETURN (static_cast<ACE_OFF_T> (-1));
635 ACE_OSCALL_RETURN (::lseek (handle, offset, whence), ACE_OFF_T, -1);
636 #endif /* ACE_WIN32 */
639 #if defined (ACE_HAS_LLSEEK) || defined (ACE_HAS_LSEEK64)
640 ACE_INLINE ACE_LOFF_T
641 ACE_OS::llseek (ACE_HANDLE handle, ACE_LOFF_T offset, int whence)
643 ACE_OS_TRACE ("ACE_OS::llseek");
645 #if ACE_SIZEOF_LONG == 8
646 /* The native lseek is 64 bit. Use it. */
647 return ACE_OS::lseek (handle, offset, whence);
648 #elif defined (ACE_HAS_LLSEEK) && defined (ACE_HAS_LSEEK64)
649 # error Either ACE_HAS_LSEEK64 and ACE_HAS_LLSEEK should be defined, not both!
650 #elif defined (ACE_HAS_LSEEK64)
651 ACE_OSCALL_RETURN (::lseek64 (handle, offset, whence), ACE_LOFF_T, -1);
652 #elif defined (ACE_HAS_LLSEEK)
653 # if defined (ACE_WIN32)
654 # ifndef ACE_LACKS_WIN32_SETFILEPOINTEREX
655 LARGE_INTEGER distance, new_file_pointer;
657 distance.QuadPart = offset;
660 (::SetFilePointerEx (handle, distance, &new_file_pointer, whence)
661 ? new_file_pointer.QuadPart
662 : static_cast<ACE_LOFF_T> (-1));
664 LARGE_INTEGER l_offset;
665 l_offset.QuadPart = offset;
666 LONG low_offset = l_offset.LowPart;
667 LONG high_offset = l_offset.HighPart;
669 l_offset.LowPart = ::SetFilePointer (handle,
673 if (l_offset.LowPart == INVALID_SET_FILE_POINTER &&
674 GetLastError () != NO_ERROR)
675 return static_cast<ACE_LOFF_T> (-1);
676 l_offset.HighPart = high_offset;
677 return l_offset.QuadPart;
678 # endif /* ACE_LACKS_WIN32_SETFILEPOINTEREX */
680 ACE_OSCALL_RETURN (::llseek (handle, offset, whence), ACE_LOFF_T, -1);
684 #endif /* ACE_HAS_LLSEEK || ACE_HAS_LSEEK64 */
687 ACE_OS::read (ACE_HANDLE handle, void *buf, size_t len)
689 ACE_OS_TRACE ("ACE_OS::read");
690 #if defined (ACE_WIN32)
692 if (::ReadFile (handle, buf, static_cast<DWORD> (len), &ok_len, 0))
693 return (ssize_t) ok_len;
695 ACE_FAIL_RETURN (-1);
700 # if defined (ACE_HAS_CHARPTR_SOCKOPT)
701 ACE_OSCALL (::read (handle, static_cast <char *> (buf), len), ssize_t, -1, result);
703 ACE_OSCALL (::read (handle, buf, len), ssize_t, -1, result);
704 # endif /* ACE_HAS_CHARPTR_SOCKOPT */
706 # if !(defined (EAGAIN) && defined (EWOULDBLOCK) && EAGAIN == EWOULDBLOCK)
707 // Optimize this code out if we can detect that EAGAIN ==
708 // EWOULDBLOCK at compile time. If we cannot detect equality at
709 // compile-time (e.g. if EAGAIN or EWOULDBLOCK are not preprocessor
710 // macros) perform the check at run-time. The goal is to avoid two
711 // TSS accesses in the _REENTRANT case when EAGAIN == EWOULDBLOCK.
713 # if !defined (EAGAIN) || !defined (EWOULDBLOCK)
714 && EAGAIN != EWOULDBLOCK
715 # endif /* !EAGAIN || !EWOULDBLOCK */
720 # endif /* EAGAIN != EWOULDBLOCK*/
723 #endif /* ACE_WIN32 */
727 ACE_OS::read (ACE_HANDLE handle, void *buf, size_t len,
728 ACE_OVERLAPPED *overlapped)
730 ACE_OS_TRACE ("ACE_OS::read");
731 overlapped = overlapped;
732 #if defined (ACE_WIN32)
734 DWORD short_len = static_cast<DWORD> (len);
735 if (::ReadFile (handle, buf, short_len, &ok_len, overlapped))
736 return (ssize_t) ok_len;
738 ACE_FAIL_RETURN (-1);
740 return ACE_OS::read (handle, buf, len);
741 #endif /* ACE_WIN32 */
745 ACE_OS::readlink (const char *path, char *buf, size_t bufsiz)
747 ACE_OS_TRACE ("ACE_OS::readlink");
748 # if defined (ACE_LACKS_READLINK)
749 ACE_UNUSED_ARG (path);
750 ACE_UNUSED_ARG (buf);
751 ACE_UNUSED_ARG (bufsiz);
752 ACE_NOTSUP_RETURN (-1);
754 # if !defined(ACE_HAS_NONCONST_READLINK)
755 ACE_OSCALL_RETURN (::readlink (path, buf, bufsiz), ssize_t, -1);
758 ::readlink (const_cast <char *>(path), buf, bufsiz), ssize_t, -1);
760 # endif /* ACE_LACKS_READLINK */
764 ACE_OS::pipe (ACE_HANDLE fds[])
766 ACE_OS_TRACE ("ACE_OS::pipe");
767 # if defined (ACE_LACKS_PIPE)
768 ACE_UNUSED_ARG (fds);
769 ACE_NOTSUP_RETURN (-1);
770 # elif defined (ACE_WIN32)
771 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL
772 (::CreatePipe (&fds[0], &fds[1], 0, 0),
773 ace_result_), int, -1);
775 ACE_OSCALL_RETURN (::pipe (fds), int, -1);
776 # endif /* ACE_LACKS_PIPE */
780 ACE_OS::sbrk (intptr_t brk)
782 #if defined (ACE_LACKS_SBRK)
783 ACE_UNUSED_ARG (brk);
784 ACE_NOTSUP_RETURN (0);
786 ACE_OSCALL_RETURN (::sbrk (brk), void *, 0);
787 #endif /* ACE_LACKS_SBRK */
791 ACE_OS::setgid (gid_t gid)
793 ACE_OS_TRACE ("ACE_OS::setgid");
794 #if defined (ACE_LACKS_SETGID)
795 ACE_UNUSED_ARG (gid);
796 ACE_NOTSUP_RETURN (-1);
798 ACE_OSCALL_RETURN (::setgid (gid), int, -1);
799 # endif /* ACE_LACKS_SETGID */
803 ACE_OS::setegid (gid_t gid)
805 ACE_OS_TRACE ("ACE_OS::setegid");
806 #if defined (ACE_LACKS_SETEGID)
807 ACE_UNUSED_ARG (gid);
808 ACE_NOTSUP_RETURN (-1);
810 ACE_OSCALL_RETURN (::setegid (gid), int, -1);
811 # endif /* ACE_LACKS_SETEGID */
815 ACE_OS::setpgid (pid_t pid, pid_t pgid)
817 ACE_OS_TRACE ("ACE_OS::setpgid");
818 #if defined (ACE_LACKS_SETPGID)
819 ACE_UNUSED_ARG (pid);
820 ACE_UNUSED_ARG (pgid);
821 ACE_NOTSUP_RETURN (-1);
823 ACE_OSCALL_RETURN (::setpgid (pid, pgid), int, -1);
824 #endif /* ACE_LACKS_SETPGID */
828 ACE_OS::setregid (gid_t rgid, gid_t egid)
830 ACE_OS_TRACE ("ACE_OS::setregid");
831 #if defined (ACE_LACKS_SETREGID)
832 ACE_UNUSED_ARG (rgid);
833 ACE_UNUSED_ARG (egid);
834 ACE_NOTSUP_RETURN (-1);
836 ACE_OSCALL_RETURN (::setregid (rgid, egid), int, -1);
837 #endif /* ACE_LACKS_SETREGID */
841 ACE_OS::setreuid (uid_t ruid, uid_t euid)
843 ACE_OS_TRACE ("ACE_OS::setreuid");
844 #if defined (ACE_LACKS_SETREUID)
845 ACE_UNUSED_ARG (ruid);
846 ACE_UNUSED_ARG (euid);
847 ACE_NOTSUP_RETURN (-1);
849 ACE_OSCALL_RETURN (::setreuid (ruid, euid), int, -1);
850 #endif /* ACE_LACKS_SETREUID */
854 ACE_OS::setsid (void)
856 ACE_OS_TRACE ("ACE_OS::setsid");
857 #if defined (ACE_LACKS_SETSID)
858 ACE_NOTSUP_RETURN (-1);
860 ACE_OSCALL_RETURN (::setsid (), int, -1);
861 # endif /* ACE_LACKS_SETSID */
865 ACE_OS::setuid (uid_t uid)
867 ACE_OS_TRACE ("ACE_OS::setuid");
868 #if defined (ACE_LACKS_SETUID)
869 ACE_UNUSED_ARG (uid);
870 ACE_NOTSUP_RETURN (-1);
872 ACE_OSCALL_RETURN (::setuid (uid), int, -1);
873 # endif /* ACE_LACKS_SETUID */
877 ACE_OS::seteuid (uid_t uid)
879 ACE_OS_TRACE ("ACE_OS::seteuid");
880 #if defined (ACE_LACKS_SETEUID)
881 ACE_UNUSED_ARG (uid);
882 ACE_NOTSUP_RETURN (-1);
884 ACE_OSCALL_RETURN (::seteuid (uid), int, -1);
885 # endif /* ACE_LACKS_SETEUID */
889 ACE_OS::sleep (u_int seconds)
891 ACE_OS_TRACE ("ACE_OS::sleep");
892 #if defined (ACE_WIN32)
893 ::Sleep (seconds * ACE_ONE_SECOND_IN_MSECS);
895 #elif defined (ACE_HAS_CLOCK_GETTIME)
896 struct timespec rqtp;
897 // Initializer doesn't work with Green Hills 1.8.7
898 rqtp.tv_sec = seconds;
900 //FUZZ: disable check_for_lack_ACE_OS
901 ACE_OSCALL_RETURN (::nanosleep (&rqtp, 0), int, -1);
902 //FUZZ: enable check_for_lack_ACE_OS
904 ACE_OSCALL_RETURN (::sleep (seconds), int, -1);
905 #endif /* ACE_WIN32 */
909 ACE_OS::sleep (const ACE_Time_Value &tv)
911 ACE_OS_TRACE ("ACE_OS::sleep");
912 #if defined (ACE_WIN32)
913 ::Sleep (tv.msec ());
915 #elif defined (ACE_HAS_CLOCK_GETTIME)
916 timespec_t rqtp = tv;
917 //FUZZ: disable check_for_lack_ACE_OS
918 ACE_OSCALL_RETURN (::nanosleep (&rqtp, 0), int, -1);
919 //FUZZ: enable check_for_lack_ACE_OS
921 # if defined (ACE_HAS_NONCONST_SELECT_TIMEVAL)
922 // Copy the timeval, because this platform doesn't declare the timeval
923 // as a pointer to const.
924 timeval tv_copy = tv;
925 # if defined(ACE_TANDEM_T1248_PTHREADS)
926 ACE_OSCALL_RETURN (::spt_select (0, 0, 0, 0, &tv_copy), int, -1);
928 //FUZZ: disable check_for_lack_ACE_OS
929 ACE_OSCALL_RETURN (::select (0, 0, 0, 0, &tv_copy), int, -1);
930 //FUZZ: enable check_for_lack_ACE_OS
932 # else /* ! ACE_HAS_NONCONST_SELECT_TIMEVAL */
933 const timeval *tvp = tv;
934 //FUZZ: disable check_for_lack_ACE_OS
935 ACE_OSCALL_RETURN (::select (0, 0, 0, 0, tvp), int, -1);
936 //FUZZ: enable check_for_lack_ACE_OS
937 # endif /* ACE_HAS_NONCONST_SELECT_TIMEVAL */
938 #endif /* ACE_WIN32 */
942 ACE_OS::swab (const void *src,
946 #if defined (ACE_LACKS_SWAB)
947 // ------------------------------------------------------------
948 // The following copyright notice applies to the swab()
949 // implementation within this "ACE_LACKS_SWAB" block of code.
950 // ------------------------------------------------------------
952 Copyright (c) 1994-2006 Red Hat, Inc. All rights reserved.
954 This copyrighted material is made available to anyone wishing to
955 use, modify, copy, or redistribute it subject to the terms and
956 conditions of the BSD License. This program is distributed in
957 the hope that it will be useful, but WITHOUT ANY WARRANTY
958 expressed or implied, including the implied warranties of
959 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. A copy of
960 this license is available at
961 http://www.opensource.org/licenses. Any Red Hat trademarks that
962 are incorporated in the source code or documentation are not
963 subject to the BSD License and may only be used or replicated with
964 the express permission of Red Hat, Inc.
967 const char *from = static_cast<const char*> (src);
968 char *to = static_cast<char *> (dest);
970 for (ptr = 1; ptr < length; ptr += 2)
973 char q = from[ptr-1];
977 if (ptr == length) /* I.e., if length is odd, */
978 to[ptr-1] = 0; /* then pad with a NUL. */
979 #elif defined (ACE_HAS_NONCONST_SWAB)
980 const char *tmp = static_cast<const char*> (src);
981 char *from = const_cast<char *> (tmp);
982 char *to = static_cast<char *> (dest);
983 ::swab (from, to, length);
984 #elif defined (ACE_HAS_CONST_CHAR_SWAB)
985 const char *from = static_cast<const char*> (src);
986 char *to = static_cast<char *> (dest);
987 ::swab (from, to, length);
989 ::swab (src, dest, length);
990 #endif /* ACE_LACKS_SWAB */
995 ACE_OS::sysconf (int name)
997 ACE_OS_TRACE ("ACE_OS::sysconf");
998 #if defined (ACE_LACKS_SYSCONF)
999 ACE_UNUSED_ARG (name);
1000 ACE_NOTSUP_RETURN (-1);
1002 ACE_OSCALL_RETURN (::sysconf (name), long, -1);
1003 #endif /* ACE_LACKS_SYSCONF */
1007 ACE_OS::sysinfo (int cmd, char *buf, long count)
1009 ACE_OS_TRACE ("ACE_OS::sysinfo");
1010 #if defined (ACE_HAS_SYSINFO)
1011 ACE_OSCALL_RETURN (::sysinfo (cmd, buf, count), long, -1);
1013 ACE_UNUSED_ARG (cmd);
1014 ACE_UNUSED_ARG (buf);
1015 ACE_UNUSED_ARG (count);
1017 ACE_NOTSUP_RETURN (0);
1018 #endif /* ACE_HAS_SYSINFO */
1022 ACE_OS::truncate (const ACE_TCHAR *filename,
1025 ACE_OS_TRACE ("ACE_OS::truncate");
1026 #if defined (ACE_WIN32)
1027 ACE_HANDLE handle = ACE_OS::open (filename,
1029 ACE_DEFAULT_FILE_PERMS);
1031 # if !defined (ACE_LACKS_WIN32_SETFILEPOINTEREX)
1032 LARGE_INTEGER loffset;
1033 loffset.QuadPart = offset;
1035 LONG low_offset = ACE_LOW_PART(offset);
1036 LONG high_offset = ACE_HIGH_PART(offset);
1039 if (handle == ACE_INVALID_HANDLE)
1040 ACE_FAIL_RETURN (-1);
1042 # if !defined (ACE_LACKS_WIN32_SETFILEPOINTEREX)
1043 else if (::SetFilePointerEx (handle,
1048 else if (::SetFilePointer (handle,
1051 FILE_BEGIN) != INVALID_SET_FILE_POINTER
1052 || GetLastError () == NO_ERROR)
1053 # endif /* ACE_LACKS_WIN32_SETFILEPOINTEREX */
1055 BOOL result = ::SetEndOfFile (handle);
1056 ::CloseHandle (handle);
1057 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (result, ace_result_), int, -1);
1061 ::CloseHandle (handle);
1062 ACE_FAIL_RETURN (-1);
1065 #elif !defined (ACE_LACKS_TRUNCATE)
1067 (::truncate (ACE_TEXT_ALWAYS_CHAR (filename), offset), int, -1);
1069 ACE_UNUSED_ARG (filename);
1070 ACE_UNUSED_ARG (offset);
1071 ACE_NOTSUP_RETURN (-1);
1072 #endif /* ACE_WIN32 */
1075 ACE_INLINE useconds_t
1076 ACE_OS::ualarm (useconds_t usecs, useconds_t interval)
1078 ACE_OS_TRACE ("ACE_OS::ualarm");
1080 #if defined (ACE_HAS_UALARM)
1081 return ::ualarm (usecs, interval);
1082 #elif !defined (ACE_LACKS_UNIX_SIGNALS)
1083 ACE_UNUSED_ARG (interval);
1084 return ::alarm (usecs * ACE_ONE_SECOND_IN_USECS);
1086 ACE_UNUSED_ARG (usecs);
1087 ACE_UNUSED_ARG (interval);
1088 ACE_NOTSUP_RETURN (0);
1089 #endif /* ACE_HAS_UALARM */
1092 ACE_INLINE useconds_t
1093 ACE_OS::ualarm (const ACE_Time_Value &tv,
1094 const ACE_Time_Value &tv_interval)
1096 ACE_OS_TRACE ("ACE_OS::ualarm");
1098 #if defined (ACE_HAS_UALARM)
1099 useconds_t usecs = (tv.sec () * ACE_ONE_SECOND_IN_USECS) + tv.usec ();
1100 useconds_t interval =
1101 (tv_interval.sec () * ACE_ONE_SECOND_IN_USECS) + tv_interval.usec ();
1102 return ::ualarm (usecs, interval);
1103 #elif !defined (ACE_LACKS_UNIX_SIGNALS)
1104 ACE_UNUSED_ARG (tv_interval);
1105 return ::alarm (tv.sec ());
1107 ACE_UNUSED_ARG (tv_interval);
1108 ACE_UNUSED_ARG (tv);
1109 ACE_NOTSUP_RETURN (0);
1110 #endif /* ACE_HAS_UALARM */
1114 ACE_OS::unlink (const char *path)
1116 ACE_OS_TRACE ("ACE_OS::unlink");
1117 # if defined (ACE_HAS_NONCONST_UNLINK)
1118 ACE_OSCALL_RETURN (::unlink (const_cast<char *> (path)), int, -1);
1119 # elif defined (ACE_HAS_WINCE)
1120 // @@ The problem is, DeleteFile is not actually equals to unlink. ;(
1121 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::DeleteFile (ACE_TEXT_CHAR_TO_TCHAR (path)), ace_result_),
1123 # elif defined (ACE_LACKS_UNLINK)
1124 ACE_UNUSED_ARG (path);
1125 ACE_NOTSUP_RETURN (-1);
1127 ACE_OSCALL_RETURN (::unlink (path), int, -1);
1128 # endif /* ACE_HAS_NONCONST_UNLINK */
1131 #if defined (ACE_HAS_WCHAR)
1133 ACE_OS::unlink (const wchar_t *path)
1135 ACE_OS_TRACE ("ACE_OS::unlink");
1136 # if defined (ACE_HAS_WINCE)
1137 // @@ The problem is, DeleteFile is not actually equals to unlink. ;(
1138 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::DeleteFileW (path), ace_result_),
1140 # elif defined (ACE_WIN32)
1141 ACE_OSCALL_RETURN (::_wunlink (path), int, -1);
1143 ACE_Wide_To_Ascii npath (path);
1144 return ACE_OS::unlink (npath.char_rep ());
1145 # endif /* ACE_HAS_WINCE */
1147 #endif /* ACE_HAS_WCHAR */
1150 ACE_OS::write (ACE_HANDLE handle, const void *buf, size_t nbyte)
1152 ACE_OS_TRACE ("ACE_OS::write");
1153 #if defined (ACE_WIN32)
1154 DWORD bytes_written; // This is set to 0 byte WriteFile.
1156 // Strictly correctly, we should loop writing all the data if more
1157 // than a DWORD length can hold.
1158 DWORD short_nbyte = static_cast<DWORD> (nbyte);
1159 if (::WriteFile (handle, buf, short_nbyte, &bytes_written, 0))
1160 return (ssize_t) bytes_written;
1162 ACE_FAIL_RETURN (-1);
1164 # if defined (ACE_HAS_CHARPTR_SOCKOPT)
1165 ACE_OSCALL_RETURN (::write (handle, static_cast <char *> (const_cast <void *> (buf)), nbyte), ssize_t, -1);
1167 ACE_OSCALL_RETURN (::write (handle, buf, nbyte), ssize_t, -1);
1168 # endif /* ACE_HAS_CHARPTR_SOCKOPT */
1169 #endif /* ACE_WIN32 */
1173 ACE_OS::write (ACE_HANDLE handle,
1176 ACE_OVERLAPPED *overlapped)
1178 ACE_OS_TRACE ("ACE_OS::write");
1179 overlapped = overlapped;
1180 #if defined (ACE_WIN32)
1181 DWORD bytes_written; // This is set to 0 byte WriteFile.
1183 DWORD short_nbyte = static_cast<DWORD> (nbyte);
1184 if (::WriteFile (handle, buf, short_nbyte, &bytes_written, overlapped))
1185 return (ssize_t) bytes_written;
1187 ACE_FAIL_RETURN (-1);
1189 return ACE_OS::write (handle, buf, nbyte);
1190 #endif /* ACE_WIN32 */
1193 ACE_END_VERSIONED_NAMESPACE_DECL