Write section access also assumes read access.
[wine] / dlls / kernel / file16.c
CommitLineData
f6a70969
EP
1/*
2 * File handling functions
3 *
4 * Copyright 1993 John Burton
5 * Copyright 1996 Alexandre Julliard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * TODO:
22 * Fix the CopyFileEx methods to implement the "extended" functionality.
23 * Right now, they simply call the CopyFile method.
24 */
25
26#include "config.h"
27#include "wine/port.h"
28
e37c6e18 29#include <stdarg.h>
f6a70969
EP
30#include <stdio.h>
31#include <assert.h>
32
33#define NONAMELESSUNION
34#define NONAMELESSSTRUCT
35#include "winerror.h"
36#include "windef.h"
37#include "winbase.h"
e37c6e18 38#include "winreg.h"
f6a70969
EP
39#include "winternl.h"
40#include "wine/winbase16.h"
41#include "wine/server.h"
f6a70969 42#include "kernel_private.h"
f6a70969
EP
43#include "wine/unicode.h"
44#include "wine/debug.h"
45
46WINE_DEFAULT_DEBUG_CHANNEL(file);
47
48/***********************************************************************
49 * _hread16 (KERNEL.349)
50 */
51LONG WINAPI _hread16( HFILE16 hFile, LPVOID buffer, LONG count)
52{
53 return _lread( (HFILE)DosFileHandleToWin32Handle(hFile), buffer, count );
54}
55
56
57/***********************************************************************
58 * _hwrite (KERNEL.350)
59 */
60LONG WINAPI _hwrite16( HFILE16 hFile, LPCSTR buffer, LONG count )
61{
62 return _hwrite( (HFILE)DosFileHandleToWin32Handle(hFile), buffer, count );
63}
64
768008fa
EP
65/***********************************************************************
66 * _lclose (KERNEL.81)
67 */
68HFILE16 WINAPI _lclose16( HFILE16 hFile )
69{
70 if ((hFile >= DOS_TABLE_SIZE) || !dos_handles[hFile])
71 {
72 SetLastError( ERROR_INVALID_HANDLE );
73 return HFILE_ERROR16;
74 }
75 TRACE("%d (handle32=%p)\n", hFile, dos_handles[hFile] );
76 CloseHandle( dos_handles[hFile] );
77 dos_handles[hFile] = 0;
78 return 0;
79}
f6a70969
EP
80
81/***********************************************************************
82 * _lcreat (KERNEL.83)
83 */
84HFILE16 WINAPI _lcreat16( LPCSTR path, INT16 attr )
85{
86 return Win32HandleToDosFileHandle( (HANDLE)_lcreat( path, attr ) );
87}
88
89/***********************************************************************
90 * _llseek (KERNEL.84)
91 *
92 * FIXME:
93 * Seeking before the start of the file should be allowed for _llseek16,
94 * but cause subsequent I/O operations to fail (cf. interrupt list)
95 *
96 */
97LONG WINAPI _llseek16( HFILE16 hFile, LONG lOffset, INT16 nOrigin )
98{
99 return SetFilePointer( DosFileHandleToWin32Handle(hFile), lOffset, NULL, nOrigin );
100}
101
102
103/***********************************************************************
104 * _lopen (KERNEL.85)
105 */
106HFILE16 WINAPI _lopen16( LPCSTR path, INT16 mode )
107{
108 return Win32HandleToDosFileHandle( (HANDLE)_lopen( path, mode ) );
109}
110
111
112/***********************************************************************
113 * _lread16 (KERNEL.82)
114 */
115UINT16 WINAPI _lread16( HFILE16 hFile, LPVOID buffer, UINT16 count )
116{
117 return (UINT16)_lread((HFILE)DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
118}
119
120
121/***********************************************************************
122 * _lwrite (KERNEL.86)
123 */
124UINT16 WINAPI _lwrite16( HFILE16 hFile, LPCSTR buffer, UINT16 count )
125{
126 return (UINT16)_hwrite( (HFILE)DosFileHandleToWin32Handle(hFile), buffer, (LONG)count );
127}
128
129/***********************************************************************
130 * _hread (KERNEL.349)
131 */
132LONG WINAPI WIN16_hread( HFILE16 hFile, SEGPTR buffer, LONG count )
133{
134 LONG maxlen;
135
136 TRACE("%d %08lx %ld\n", hFile, (DWORD)buffer, count );
137
138 /* Some programs pass a count larger than the allocated buffer */
139 maxlen = GetSelectorLimit16( SELECTOROF(buffer) ) - OFFSETOF(buffer) + 1;
140 if (count > maxlen) count = maxlen;
141 return _lread((HFILE)DosFileHandleToWin32Handle(hFile), MapSL(buffer), count );
142}
143
144
145/***********************************************************************
146 * _lread (KERNEL.82)
147 */
148UINT16 WINAPI WIN16_lread( HFILE16 hFile, SEGPTR buffer, UINT16 count )
149{
150 return (UINT16)WIN16_hread( hFile, buffer, (LONG)count );
151}
152
153
154/***********************************************************************
155 * DeleteFile (KERNEL.146)
156 */
157BOOL16 WINAPI DeleteFile16( LPCSTR path )
158{
159 return DeleteFileA( path );
160}
161
162/**************************************************************************
163 * GetFileAttributes (KERNEL.420)
164 */
165DWORD WINAPI GetFileAttributes16( LPCSTR name )
166{
167 return GetFileAttributesA( name );
168}
169
170
171/***********************************************************************
172 * GetTempFileName (KERNEL.97)
173 */
174UINT16 WINAPI GetTempFileName16( BYTE drive, LPCSTR prefix, UINT16 unique,
175 LPSTR buffer )
176{
177 char temppath[MAX_PATH];
178 char *prefix16 = NULL;
179 UINT16 ret;
180
181 if (!(drive & ~TF_FORCEDRIVE)) /* drive 0 means current default drive */
182 {
183 GetCurrentDirectoryA(sizeof(temppath), temppath);
184 drive |= temppath[0];
185 }
186
187 if (drive & TF_FORCEDRIVE)
188 {
189 char d[3];
190
191 d[0] = drive & ~TF_FORCEDRIVE;
192 d[1] = ':';
193 d[2] = '\0';
194 if (GetDriveTypeA(d) == DRIVE_NO_ROOT_DIR)
195 {
196 drive &= ~TF_FORCEDRIVE;
197 WARN("invalid drive %d specified\n", drive );
198 }
199 }
200
201 if (drive & TF_FORCEDRIVE)
202 sprintf(temppath,"%c:", drive & ~TF_FORCEDRIVE );
203 else
204 GetTempPathA( MAX_PATH, temppath );
205
206 if (prefix)
207 {
208 prefix16 = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + 2);
209 *prefix16 = '~';
210 strcpy(prefix16 + 1, prefix);
211 }
212
213 ret = GetTempFileNameA( temppath, prefix16, unique, buffer );
214
215 if (prefix16) HeapFree(GetProcessHeap(), 0, prefix16);
216 return ret;
217}
218
219/**************************************************************************
220 * SetFileAttributes (KERNEL.421)
221 */
222BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
223{
224 return SetFileAttributesA( lpFileName, attributes );
225}
226
227
228/***********************************************************************
229 * SetHandleCount (KERNEL.199)
230 */
231UINT16 WINAPI SetHandleCount16( UINT16 count )
232{
233 return SetHandleCount( count );
234}
2ac34461
AJ
235
236
237/*************************************************************************
238 * FindFirstFile (KERNEL.413)
239 */
240HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
241{
242 HGLOBAL16 h16;
243 HANDLE handle, *ptr;
244
245 if (!(h16 = GlobalAlloc16( GMEM_MOVEABLE, sizeof(handle) ))) return INVALID_HANDLE_VALUE16;
246 ptr = GlobalLock16( h16 );
247 *ptr = handle = FindFirstFileA( path, data );
248 GlobalUnlock16( h16 );
249
250 if (handle == INVALID_HANDLE_VALUE)
251 {
252 GlobalFree16( h16 );
253 h16 = INVALID_HANDLE_VALUE16;
254 }
255 return h16;
256}
257
258
259/*************************************************************************
260 * FindNextFile (KERNEL.414)
261 */
262BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
263{
264 HANDLE *ptr;
265 BOOL ret = FALSE;
266
267 if ((handle == INVALID_HANDLE_VALUE16) || !(ptr = GlobalLock16( handle )))
268 {
269 SetLastError( ERROR_INVALID_HANDLE );
270 return ret;
271 }
272 ret = FindNextFileA( *ptr, data );
273 GlobalUnlock16( handle );
274 return ret;
275}
276
277
278/*************************************************************************
279 * FindClose (KERNEL.415)
280 */
281BOOL16 WINAPI FindClose16( HANDLE16 handle )
282{
283 HANDLE *ptr;
284
285 if ((handle == INVALID_HANDLE_VALUE16) || !(ptr = GlobalLock16( handle )))
286 {
287 SetLastError( ERROR_INVALID_HANDLE );
288 return FALSE;
289 }
290 FindClose( *ptr );
291 GlobalUnlock16( handle );
292 GlobalFree16( handle );
293 return TRUE;
294}