Release 940804
[wine.git] / misc / file.c
blob92359fd6487bf48bab62e4cb26f777eb9b8a4a4b
1 /************************************************************************
2 * FILE.C Copyright (C) 1993 John Burton
4 * File I/O routines for the Linux Wine Project.
6 * WARNING : Many options of OpenFile are not yet implemeted.
8 * NOV 93 Erik Bos (erik@(trashcan.)hacktic.nl
9 * - removed ParseDosFileName, and DosDrive structures.
10 * - structures dynamically configured at runtime.
11 * - _lopen modified to use GetUnixFileName.
13 * DEC 93 Erik Bos (erik@(trashcan.)hacktic.nl)
14 * - Existing functions modified to use dosfs functions.
15 * - Added _llseek, _lcreat, GetDriveType, GetTempDrive,
16 * GetWindowsDirectory, GetSystemDirectory, GetTempFileName.
18 ************************************************************************/
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include <windows.h>
26 #include "prototypes.h"
28 /* #define DEBUG_FILE /* */
30 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
31 extern char WindowsPath[256];
33 /***************************************************************************
34 _lopen
36 Emulate the _lopen windows call
37 ***************************************************************************/
38 INT _lopen (LPSTR lpPathName, INT iReadWrite)
40 int handle;
41 char *UnixFileName;
43 #ifdef DEBUG_FILE
44 fprintf (stderr, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
45 #endif
47 if ((UnixFileName = GetUnixFileName(lpPathName)) == NULL)
48 return HFILE_ERROR;
49 iReadWrite &= 0x000F;
50 handle = open (UnixFileName, iReadWrite);
52 #ifdef DEBUG_FILE
53 fprintf (stderr, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
54 #endif
56 if (handle == -1)
57 return HFILE_ERROR;
58 else
59 return handle;
62 /***************************************************************************
63 _lread
64 ***************************************************************************/
65 INT _lread (INT hFile, LPSTR lpBuffer, WORD wBytes)
67 int result;
69 #ifdef DEBUG_FILE
70 fprintf(stderr, "_lread: handle %d, buffer = %ld, length = %d\n",
71 hFile, (int) lpBuffer, wBytes);
72 #endif
74 result = read (hFile, lpBuffer, wBytes);
76 if (result == -1)
77 return HFILE_ERROR;
78 else
79 return result;
82 /****************************************************************************
83 _lwrite
84 ****************************************************************************/
85 INT _lwrite (INT hFile, LPSTR lpBuffer, WORD wBytes)
87 int result;
89 #if 0
90 #ifdef DEBUG_FILE
91 fprintf(stderr, "_lwrite: handle %d, buffer = %ld, length = %d\n",
92 hFile, (int) lpBuffer, wBytes);
93 #endif
94 #endif
95 result = write (hFile, lpBuffer, wBytes);
97 if (result == -1)
98 return HFILE_ERROR;
99 else
100 return result;
103 /***************************************************************************
104 _lclose
105 ***************************************************************************/
106 INT _lclose (INT hFile)
108 #ifdef DEBUG_FILE
109 fprintf(stderr, "_lclose: handle %d\n", hFile);
110 #endif
111 if (close (hFile))
112 return HFILE_ERROR;
113 else
114 return 0;
117 /**************************************************************************
118 OpenFile
120 Warning: This is nearly totally untested. It compiles, that's it...
121 -SL 9/13/93
122 **************************************************************************/
123 INT OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
125 int base, flags;
126 int handle;
127 char buf[256];
129 #ifdef DEBUG_FILE
130 fprintf(stderr,"OpenFile(%s,<struct>,%04X)\n",lpFileName,wStyle);
131 #endif
133 base = wStyle & 0xF;
134 flags = wStyle & 0xFFF0;
136 flags &= 0xFF0F; /* strip SHARE bits for now */
137 flags &= 0xD7FF; /* strip PROMPT & CANCEL bits for now */
138 flags &= 0x7FFF; /* strip REOPEN bit for now */
139 flags &= 0xFBFF; /* strib VERIFY bit for now */
141 if (flags & OF_CREATE)
143 base |= O_CREAT;
144 flags &= 0xEFFF;
147 #ifdef DEBUG_FILE
148 fprintf(stderr,"now %d,%d\n",base,flags);
149 #endif
151 if (flags & OF_EXIST)
153 printf("OpenFile // OF_EXIST '%s' !\n", lpFileName);
154 handle = _lopen (lpFileName, wStyle);
155 if (handle == -1) {
156 /* Try again with WindowsPath */
157 if (FindFile(buf, sizeof(buf), lpFileName, NULL, WindowsPath) != NULL) {
158 handle = _lopen (buf, wStyle);
161 close(handle);
162 return handle;
164 if (flags & OF_DELETE)
166 printf("OpenFile // OF_DELETE '%s' !\n", lpFileName);
167 return unlink(lpFileName);
169 else
171 int handle;
172 char *UnixFileName;
173 if ((UnixFileName = GetUnixFileName(lpFileName)) == NULL)
174 return HFILE_ERROR;
175 handle = open(UnixFileName, base, 0666);
176 if (handle == -1) {
177 /* Try again with WindowsPath */
178 if (FindFile(buf, sizeof(buf), lpFileName, NULL, WindowsPath) != NULL) {
179 #ifdef DEBUG_FILE
180 printf("OpenFile // file '%s' found !\n", buf);
181 #endif
182 UnixFileName = buf;
183 handle = open(UnixFileName, base, 0666);
187 #ifdef DEBUG_FILE
188 fprintf(stderr, "OpenFile: returning %04.4x\n", handle);
189 #endif
191 if (handle == -1)
192 return HFILE_ERROR;
193 else
194 return handle;
198 /**************************************************************************
199 SetHandleCount
201 Changes the number of file handles available to the application. Since
202 Linux isn't limited to 20 files, this one's easy. - SL
203 **************************************************************************/
205 #if !defined (OPEN_MAX)
206 /* This one is for the Sun */
207 #define OPEN_MAX _POSIX_OPEN_MAX
208 #endif
209 WORD SetHandleCount (WORD wNumber)
211 printf("SetHandleCount(%d)\n",wNumber);
212 return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
215 /***************************************************************************
216 _llseek
217 ***************************************************************************/
218 LONG _llseek (INT hFile, LONG lOffset, INT nOrigin)
220 int origin;
222 #ifdef DEBUG_FILE
223 fprintf(stderr, "_llseek: handle %d, offset %ld, origin %d\n", hFile, lOffset, nOrigin);
224 #endif
226 switch (nOrigin) {
227 case 1: origin = SEEK_CUR;
228 break;
229 case 2: origin = SEEK_END;
230 break;
231 default: origin = SEEK_SET;
232 break;
235 return ( lseek(hFile, lOffset, origin) );
238 /***************************************************************************
239 _lcreat
240 ***************************************************************************/
241 INT _lcreat (LPSTR lpszFilename, INT fnAttribute)
243 int handle;
244 char *UnixFileName;
246 #ifdef DEBUG_FILE
247 fprintf(stderr, "_lcreat: filename %s, attributes %d\n",lpszFilename,
248 fnAttribute);
249 #endif
250 if ((UnixFileName = GetUnixFileName(lpszFilename)) == NULL)
251 return HFILE_ERROR;
252 handle = open (UnixFileName, O_CREAT | O_TRUNC | O_WRONLY, 426);
254 if (handle == -1)
255 return HFILE_ERROR;
256 else
257 return handle;
260 /***************************************************************************
261 GetDriveType
262 ***************************************************************************/
263 UINT GetDriveType(INT drive)
266 #ifdef DEBUG_FILE
267 fprintf(stderr,"GetDriveType %c:\n",'A'+drive);
268 #endif
270 if (!DOS_ValidDrive(drive))
271 return DRIVE_DOESNOTEXIST;
273 if (drive == 0 || drive == 1)
274 return DRIVE_REMOVABLE;
276 return DRIVE_FIXED;
279 /***************************************************************************
280 GetTempDrive
281 ***************************************************************************/
282 BYTE GetTempDrive(BYTE chDriveLetter)
284 #ifdef DEBUG_FILE
285 fprintf(stderr,"GetTempDrive (%d)\n",chDriveLetter);
286 #endif
287 return('C');
290 /***************************************************************************
291 GetWindowsDirectory
292 ***************************************************************************/
293 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
295 if (cbSysPath < strlen(WindowsDirectory))
296 *lpszSysPath = 0;
297 else
298 strcpy(lpszSysPath, WindowsDirectory);
300 #ifdef DEBUG_FILE
301 fprintf(stderr,"GetWindowsDirectory (%s)\n",lpszSysPath);
302 #endif
304 ChopOffSlash(lpszSysPath);
305 return(strlen(lpszSysPath));
307 /***************************************************************************
308 GetSystemDirectory
309 ***************************************************************************/
310 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
312 if (cbSysPath < strlen(SystemDirectory))
313 *lpszSysPath = 0;
314 else
315 strcpy(lpszSysPath, SystemDirectory);
317 #ifdef DEBUG_FILE
318 fprintf(stderr,"GetSystemDirectory (%s)\n",lpszSysPath);
319 #endif
321 ChopOffSlash(lpszSysPath);
322 return(strlen(lpszSysPath));
324 /***************************************************************************
325 GetTempFileName
326 ***************************************************************************/
327 INT GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
329 int unique;
330 int handle;
331 char tempname[256];
333 if (uUnique == 0)
334 unique = time(NULL)%99999L;
335 else
336 unique = uUnique%99999L;
338 strcpy(tempname,lpszPrefixString);
339 tempname[3]='\0';
341 sprintf(lpszTempFileName,"%s\\%s%d.tmp", TempDirectory, tempname,
342 unique);
344 ToDos(lpszTempFileName);
346 #ifdef DEBUG_FILE
347 fprintf(stderr,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
348 lpszPrefixString,uUnique,lpszTempFileName);
349 #endif
350 if ((handle = _lcreat (lpszTempFileName, 0x0000)) == -1) {
351 fprintf(stderr,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName);
353 else
354 close(handle);
356 return unique;
359 /***************************************************************************
360 SetErrorMode
361 ***************************************************************************/
362 WORD SetErrorMode(WORD x)
364 fprintf(stderr,"wine: SetErrorMode %4x (ignored)\n",x);
367 /***************************************************************************
368 _hread
369 ***************************************************************************/
370 long _hread(int hf, void FAR *hpvBuffer, long cbBuffer)
372 return read(hf, hpvBuffer, cbBuffer);
374 /***************************************************************************
375 _hwrite
376 ***************************************************************************/
377 long _hwrite(int hf, const void FAR *hpvBuffer, long cbBuffer)
379 return write(hf, hpvBuffer, cbBuffer);