Fixed typos
[ACE_TAO.git] / ACE / ace / OS_NS_fcntl.cpp
blobd530a27ecc89c18c6da6e97a3088331955e45a73
1 #include "ace/OS_NS_fcntl.h"
3 #if !defined (ACE_HAS_INLINED_OSCALLS)
4 # include "ace/OS_NS_fcntl.inl"
5 #endif /* ACE_HAS_INLINED_OSCALLS */
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_Thread.h"
9 #include "ace/OS_NS_macros.h"
10 #include "ace/Object_Manager_Base.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 ACE_HANDLE
15 ACE_OS::open (const char *filename,
16 int mode,
17 mode_t perms,
18 LPSECURITY_ATTRIBUTES sa)
20 ACE_OS_TRACE ("ACE_OS::open");
22 #if defined (ACE_WIN32)
23 DWORD access = GENERIC_READ;
24 if (ACE_BIT_ENABLED (mode, O_WRONLY))
25 access = GENERIC_WRITE;
26 else if (ACE_BIT_ENABLED (mode, O_RDWR))
27 access = GENERIC_READ | GENERIC_WRITE;
29 DWORD creation = OPEN_EXISTING;
31 if ((mode & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
32 creation = CREATE_NEW;
33 else if ((mode & (_O_CREAT | _O_TRUNC)) == (_O_CREAT | _O_TRUNC))
34 creation = CREATE_ALWAYS;
35 else if (ACE_BIT_ENABLED (mode, _O_CREAT))
36 creation = OPEN_ALWAYS;
37 else if (ACE_BIT_ENABLED (mode, _O_TRUNC))
38 creation = TRUNCATE_EXISTING;
40 DWORD flags = 0;
42 if (ACE_BIT_ENABLED (mode, _O_TEMPORARY))
43 flags |= FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_TEMPORARY;
45 if (ACE_BIT_ENABLED (mode, FILE_FLAG_WRITE_THROUGH))
46 flags |= FILE_FLAG_WRITE_THROUGH;
47 if (ACE_BIT_ENABLED (mode, FILE_FLAG_OVERLAPPED))
48 flags |= FILE_FLAG_OVERLAPPED;
49 if (ACE_BIT_ENABLED (mode, FILE_FLAG_NO_BUFFERING))
50 flags |= FILE_FLAG_NO_BUFFERING;
51 if (ACE_BIT_ENABLED (mode, FILE_FLAG_RANDOM_ACCESS))
52 flags |= FILE_FLAG_RANDOM_ACCESS;
53 if (ACE_BIT_ENABLED (mode, FILE_FLAG_SEQUENTIAL_SCAN))
54 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
55 if (ACE_BIT_ENABLED (mode, FILE_FLAG_DELETE_ON_CLOSE))
56 flags |= FILE_FLAG_DELETE_ON_CLOSE;
57 if (ACE_BIT_ENABLED (mode, FILE_FLAG_BACKUP_SEMANTICS))
58 flags |= FILE_FLAG_BACKUP_SEMANTICS;
59 if (ACE_BIT_ENABLED (mode, FILE_FLAG_POSIX_SEMANTICS))
60 flags |= FILE_FLAG_POSIX_SEMANTICS;
62 ACE_MT (ACE_thread_mutex_t *ace_os_monitor_lock = 0;)
64 if (ACE_BIT_ENABLED (mode, _O_APPEND))
66 ACE_MT
68 ace_os_monitor_lock = static_cast <ACE_thread_mutex_t *> (
69 ACE_OS_Object_Manager::preallocated_object[
70 ACE_OS_Object_Manager::ACE_OS_MONITOR_LOCK]);
71 ACE_OS::thread_mutex_lock (ace_os_monitor_lock);
75 DWORD shared_mode = perms;
76 SECURITY_ATTRIBUTES sa_buffer;
77 SECURITY_DESCRIPTOR sd_buffer;
79 #if defined (ACE_HAS_WINCE)
80 ACE_HANDLE h = ::CreateFileW (ACE_Ascii_To_Wide (filename).wchar_rep (),
81 access,
82 shared_mode,
83 ACE_OS::default_win32_security_attributes_r
84 (sa, &sa_buffer, &sd_buffer),
85 creation,
86 flags,
87 0);
88 #else /* ACE_HAS_WINCE */
89 ACE_HANDLE h = ::CreateFileA (filename,
90 access,
91 shared_mode,
92 ACE_OS::default_win32_security_attributes_r
93 (sa, &sa_buffer, &sd_buffer),
94 creation,
95 flags,
96 0);
97 #endif /* ACE_HAS_WINCE */
99 if (ACE_BIT_ENABLED (mode, _O_APPEND))
101 LONG high_size = 0;
102 if (h != ACE_INVALID_HANDLE
103 && ::SetFilePointer (h,
105 &high_size,
106 FILE_END) == INVALID_SET_FILE_POINTER
107 && GetLastError () != NO_ERROR)
109 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock);)
110 ACE_FAIL_RETURN (ACE_INVALID_HANDLE);
113 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock);)
116 if (h == ACE_INVALID_HANDLE)
117 ACE_FAIL_RETURN (h);
118 else
119 return h;
120 #elif defined (INTEGRITY)
121 ACE_UNUSED_ARG (sa);
122 if(!strcmp(filename,ACE_DEV_NULL)) {
123 ACE_OSCALL_RETURN (::AllocateNullConsoleDescriptor(), ACE_HANDLE, -1);
125 else {
126 ACE_OSCALL_RETURN (::open (filename, mode, perms), ACE_HANDLE, -1);
128 #elif defined (ACE_MQX)
129 ACE_UNUSED_ARG (perms);
130 ACE_UNUSED_ARG (sa);
131 return MQX_Filesystem::inst ().open (filename, mode);
132 #else
133 ACE_UNUSED_ARG (sa);
134 ACE_OSCALL_RETURN (::open (filename, mode, perms), ACE_HANDLE, ACE_INVALID_HANDLE);
135 #endif /* ACE_WIN32 */
138 #if defined (ACE_HAS_WCHAR)
139 ACE_HANDLE
140 ACE_OS::open (const wchar_t *filename,
141 int mode,
142 mode_t perms,
143 LPSECURITY_ATTRIBUTES sa)
145 #if defined (ACE_WIN32)
146 // @@ (brunsch) Yuck, maybe there is a way to combine the code
147 // here with the char version
149 DWORD access = GENERIC_READ;
150 if (ACE_BIT_ENABLED (mode, O_WRONLY))
151 access = GENERIC_WRITE;
152 else if (ACE_BIT_ENABLED (mode, O_RDWR))
153 access = GENERIC_READ | GENERIC_WRITE;
155 DWORD creation = OPEN_EXISTING;
157 if ((mode & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
158 creation = CREATE_NEW;
159 else if ((mode & (_O_CREAT | _O_TRUNC)) == (_O_CREAT | _O_TRUNC))
160 creation = CREATE_ALWAYS;
161 else if (ACE_BIT_ENABLED (mode, _O_CREAT))
162 creation = OPEN_ALWAYS;
163 else if (ACE_BIT_ENABLED (mode, _O_TRUNC))
164 creation = TRUNCATE_EXISTING;
166 DWORD flags = 0;
168 if (ACE_BIT_ENABLED (mode, _O_TEMPORARY))
169 flags |= FILE_FLAG_DELETE_ON_CLOSE | FILE_ATTRIBUTE_TEMPORARY;
171 if (ACE_BIT_ENABLED (mode, FILE_FLAG_WRITE_THROUGH))
172 flags |= FILE_FLAG_WRITE_THROUGH;
173 if (ACE_BIT_ENABLED (mode, FILE_FLAG_OVERLAPPED))
174 flags |= FILE_FLAG_OVERLAPPED;
175 if (ACE_BIT_ENABLED (mode, FILE_FLAG_NO_BUFFERING))
176 flags |= FILE_FLAG_NO_BUFFERING;
177 if (ACE_BIT_ENABLED (mode, FILE_FLAG_RANDOM_ACCESS))
178 flags |= FILE_FLAG_RANDOM_ACCESS;
179 if (ACE_BIT_ENABLED (mode, FILE_FLAG_SEQUENTIAL_SCAN))
180 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
181 if (ACE_BIT_ENABLED (mode, FILE_FLAG_DELETE_ON_CLOSE))
182 flags |= FILE_FLAG_DELETE_ON_CLOSE;
183 if (ACE_BIT_ENABLED (mode, FILE_FLAG_BACKUP_SEMANTICS))
184 flags |= FILE_FLAG_BACKUP_SEMANTICS;
185 if (ACE_BIT_ENABLED (mode, FILE_FLAG_POSIX_SEMANTICS))
186 flags |= FILE_FLAG_POSIX_SEMANTICS;
188 ACE_MT (ACE_thread_mutex_t *ace_os_monitor_lock = 0;)
190 if (ACE_BIT_ENABLED (mode, _O_APPEND))
192 ACE_MT
194 ace_os_monitor_lock = static_cast <ACE_thread_mutex_t *> (
195 ACE_OS_Object_Manager::preallocated_object[
196 ACE_OS_Object_Manager::ACE_OS_MONITOR_LOCK]);
197 ACE_OS::thread_mutex_lock (ace_os_monitor_lock);
201 DWORD shared_mode = perms;
202 SECURITY_ATTRIBUTES sa_buffer;
203 SECURITY_DESCRIPTOR sd_buffer;
205 ACE_HANDLE h = ::CreateFileW (filename,
206 access,
207 shared_mode,
208 ACE_OS::default_win32_security_attributes_r
209 (sa, &sa_buffer, &sd_buffer),
210 creation,
211 flags,
214 if (ACE_BIT_ENABLED (mode, _O_APPEND))
216 LONG high_size = 0;
217 if (h != ACE_INVALID_HANDLE
218 && ::SetFilePointer (h,
220 &high_size,
221 FILE_END) == INVALID_SET_FILE_POINTER
222 && GetLastError () != NO_ERROR)
224 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock);)
225 ACE_FAIL_RETURN (ACE_INVALID_HANDLE);
228 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock);)
231 if (h == ACE_INVALID_HANDLE)
232 ACE_FAIL_RETURN (h);
233 else
234 return h;
235 #else /* ACE_WIN32 */
236 // Just emulate with ascii version
237 return ACE_OS::open (ACE_Wide_To_Ascii (filename).char_rep (),
238 mode,
239 perms,
240 sa);
241 #endif /* ACE_WIN32 */
243 #endif /* ACE_HAS_WCHAR */
245 ACE_END_VERSIONED_NAMESPACE_DECL