Release 950522
[wine.git] / misc / file.c
blob31c2cfc34f90d44ddedb66979fc7e07efc8e86eb
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@xs4all.nl)
9 * - removed ParseDosFileName, and DosDrive structures.
10 * - structures dynamically configured at runtime.
11 * - _lopen modified to use DOS_GetUnixFileName.
12 * - Existing functions modified to use dosfs functions.
13 * - Added _llseek, _lcreat, GetDriveType, GetTempDrive,
14 * GetWindowsDirectory, GetSystemDirectory, GetTempFileName.
16 ************************************************************************/
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <unistd.h>
22 #include <time.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include "dos_fs.h"
26 #include "windows.h"
27 #include "msdos.h"
28 #include "options.h"
29 #include "stddebug.h"
30 #include "debug.h"
32 #define MAX_PATH 255
34 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
36 /***************************************************************************
37 _lopen
39 Emulate the _lopen windows call
40 ***************************************************************************/
41 INT _lopen (LPSTR lpPathName, INT iReadWrite)
43 int handle;
44 char *UnixFileName;
45 int mode = 0;
47 dprintf_file(stddeb, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
48 if ((UnixFileName = DOS_GetUnixFileName(lpPathName)) == NULL)
49 return HFILE_ERROR;
50 switch(iReadWrite & 3)
52 case OF_READ: mode = O_RDONLY; break;
53 case OF_WRITE: mode = O_WRONLY; break;
54 case OF_READWRITE: mode = O_RDWR; break;
56 handle = open( UnixFileName, mode );
57 if (( handle == -1 ) && Options.allowReadOnly)
58 handle = open( UnixFileName, O_RDONLY );
60 dprintf_file(stddeb, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
62 if (handle == -1)
63 return HFILE_ERROR;
64 else
65 return handle;
68 /***************************************************************************
69 _lread
70 ***************************************************************************/
71 INT _lread (INT hFile, LPSTR lpBuffer, WORD wBytes)
73 int result;
75 dprintf_file(stddeb, "_lread: handle %d, buffer = %ld, length = %d\n",
76 hFile, (long) lpBuffer, wBytes);
78 result = read (hFile, lpBuffer, wBytes);
80 if (result == -1)
81 return HFILE_ERROR;
82 else
83 return result;
86 /****************************************************************************
87 _lwrite
88 ****************************************************************************/
89 INT _lwrite (INT hFile, LPSTR lpBuffer, WORD wBytes)
91 int result;
93 dprintf_file(stddeb, "_lwrite: handle %d, buffer = %ld, length = %d\n",
94 hFile, (long) lpBuffer, wBytes);
96 result = write (hFile, lpBuffer, wBytes);
98 if (result == -1)
99 return HFILE_ERROR;
100 else
101 return result;
104 /***************************************************************************
105 _lclose
106 ***************************************************************************/
107 INT _lclose (INT hFile)
109 dprintf_file(stddeb, "_lclose: handle %d\n", hFile);
110 if (close (hFile))
111 return HFILE_ERROR;
112 else
113 return 0;
116 /**************************************************************************
117 OpenFile
118 **************************************************************************/
119 INT OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
121 char filename[MAX_PATH+1];
122 int action;
123 struct stat s;
124 struct tm *now;
125 int res, handle;
126 int verify_time;
128 dprintf_file(stddeb,"Openfile(%s,<struct>,%d)\n",lpFileName,wStyle);
130 action = wStyle & 0xff00;
133 /* OF_CREATE is completly different from all other options, so
134 handle it first */
136 if (action & OF_CREATE)
138 char *unixfilename;
140 if (!(action & OF_REOPEN)) strcpy(ofs->szPathName, lpFileName);
141 ofs->cBytes = sizeof(OFSTRUCT);
142 ofs->fFixedDisk = FALSE;
143 ofs->nErrCode = 0;
144 *((int*)ofs->reserved) = 0;
146 if ((unixfilename = DOS_GetUnixFileName (ofs->szPathName)) == NULL)
148 errno_to_doserr();
149 ofs->nErrCode = ExtendedError;
150 return -1;
152 handle = open (unixfilename, (wStyle & 0x0003) | O_CREAT, 0x666);
153 if (handle == -1)
155 errno_to_doserr();
156 ofs->nErrCode = ExtendedError;
158 return handle;
162 /* If path isn't given, try to find the file. */
164 if (!(action & OF_REOPEN))
166 if( !( index(lpFileName,'\\') || index(lpFileName,'/') ||
167 index(lpFileName,':')))
168 while(1)
170 char temp[MAX_PATH+1];
171 /* Try current directory */
172 strcpy (filename, lpFileName);
173 if ( (!stat(DOS_GetUnixFileName(filename), &s)) && (S_ISREG(s.st_mode)) )
174 break;
176 /* Try Windows directory */
178 GetWindowsDirectory (filename,MAX_PATH);
179 if ((!filename[0])||(filename[strlen(filename)-1]!='\\'))
180 strcat(filename, "\\");
181 strcat (filename, lpFileName);
182 if ( (!stat(DOS_GetUnixFileName(filename), &s)) && (S_ISREG(s.st_mode)) )
183 break;
185 /* Try Windows system directory */
187 GetSystemDirectory (filename,MAX_PATH);
188 if ((!filename[0])||(filename[strlen(filename)-1]!='\\'))
189 strcat(filename, "\\");
190 strcat (filename, lpFileName);
191 if ( (!stat(DOS_GetUnixFileName(filename), &s)) && (S_ISREG(s.st_mode)) )
192 break;
194 /* Try the path of the current executable */
196 if (GetCurrentTask())
198 char *p;
199 GetModuleFileName( GetCurrentTask(), filename, MAX_PATH );
200 if ((p = strrchr( filename, '\\' )))
202 p[1] = '\0';
203 strcat( filename, lpFileName );
204 if ((!stat(DOS_GetUnixFileName(filename), &s)) &&
205 (S_ISREG(s.st_mode)) )
206 break;
210 /* Try all directories in path */
212 if (DOS_FindFile(temp,MAX_PATH,lpFileName,NULL,WindowsPath))
214 strcpy(filename, DOS_GetDosFileName(temp));
215 break;
217 strcpy (filename, lpFileName);
218 break;
220 else
221 strcpy (filename,lpFileName);
223 ofs->cBytes = sizeof(OFSTRUCT);
224 ofs->fFixedDisk = FALSE;
225 strcpy(ofs->szPathName, filename);
226 ofs->nErrCode = 0;
227 if (!(action & OF_VERIFY))
228 *((int*)ofs->reserved) = 0;
232 if (action & OF_PARSE)
233 return 0;
235 if (action & OF_DELETE)
236 return unlink(ofs->szPathName);
239 /* Now on to getting some information about that file */
241 if ((res = stat(DOS_GetUnixFileName(ofs->szPathName), &s)))
243 errno_to_doserr();
244 ofs->nErrCode = ExtendedError;
245 return -1;
248 now = localtime (&s.st_mtime);
250 if (action & OF_VERIFY)
251 verify_time = *((int*)ofs->reserved);
253 *((WORD*)(&ofs->reserved[2]))=
254 ((now->tm_hour * 0x2000) + (now->tm_min * 0x20) + (now->tm_sec / 2));
255 *((WORD*)(&ofs->reserved[0]))=
256 ((now->tm_year * 0x200) + (now->tm_mon * 0x20) + now->tm_mday);
259 if (action & OF_VERIFY)
260 return (verify_time != *((int*)ofs->reserved));
262 if (action & OF_EXIST)
263 return 0;
265 if ((handle = _lopen( ofs->szPathName, wStyle )) == -1)
267 ofs->nErrCode = 2; /* not found */
268 return -1;
270 return handle;
274 /**************************************************************************
275 SetHandleCount
277 Changes the number of file handles available to the application. Since
278 Linux isn't limited to 20 files, this one's easy. - SL
279 **************************************************************************/
281 #if !defined (OPEN_MAX)
282 /* This one is for the Sun */
283 #define OPEN_MAX _POSIX_OPEN_MAX
284 #endif
285 WORD SetHandleCount (WORD wNumber)
287 dprintf_file(stddeb,"SetHandleCount(%d)\n",wNumber);
288 return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
291 /***************************************************************************
292 _llseek
293 ***************************************************************************/
294 LONG _llseek (INT hFile, LONG lOffset, INT nOrigin)
296 int origin;
298 dprintf_file(stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
299 hFile, lOffset, nOrigin);
301 switch (nOrigin) {
302 case 1: origin = SEEK_CUR;
303 break;
304 case 2: origin = SEEK_END;
305 break;
306 default: origin = SEEK_SET;
307 break;
310 return ( lseek(hFile, lOffset, origin) );
313 /***************************************************************************
314 _lcreat
315 ***************************************************************************/
316 INT _lcreat (LPSTR lpszFilename, INT fnAttribute)
318 int handle;
319 char *UnixFileName;
321 dprintf_file(stddeb, "_lcreat: filename %s, attributes %d\n",
322 lpszFilename, fnAttribute);
323 if ((UnixFileName = DOS_GetUnixFileName(lpszFilename)) == NULL)
324 return HFILE_ERROR;
325 handle = open (UnixFileName, O_CREAT | O_TRUNC | O_WRONLY, 426);
327 if (handle == -1)
328 return HFILE_ERROR;
329 else
330 return handle;
333 /***************************************************************************
334 GetDriveType
335 ***************************************************************************/
336 UINT GetDriveType(INT drive)
339 dprintf_file(stddeb,"GetDriveType %c:\n",'A'+drive);
341 if (!DOS_ValidDrive(drive))
342 return DRIVE_DOESNOTEXIST;
344 if (drive == 0 || drive == 1)
345 return DRIVE_REMOVABLE;
347 return DRIVE_FIXED;
350 /***************************************************************************
351 GetTempDrive
352 ***************************************************************************/
353 BYTE GetTempDrive(BYTE chDriveLetter)
355 dprintf_file(stddeb,"GetTempDrive (%d)\n",chDriveLetter);
356 if (TempDirectory[1] == ':') return TempDirectory[0];
357 else return 'C';
360 /***************************************************************************
361 GetWindowsDirectory
362 ***************************************************************************/
363 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
365 if (cbSysPath < strlen(WindowsDirectory))
366 *lpszSysPath = 0;
367 else
368 strcpy(lpszSysPath, WindowsDirectory);
370 dprintf_file(stddeb,"GetWindowsDirectory (%s)\n",lpszSysPath);
372 ChopOffSlash(lpszSysPath);
373 return(strlen(lpszSysPath));
375 /***************************************************************************
376 GetSystemDirectory
377 ***************************************************************************/
378 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
380 if (cbSysPath < strlen(SystemDirectory))
381 *lpszSysPath = 0;
382 else
383 strcpy(lpszSysPath, SystemDirectory);
385 dprintf_file(stddeb,"GetSystemDirectory (%s)\n",lpszSysPath);
387 ChopOffSlash(lpszSysPath);
388 return(strlen(lpszSysPath));
390 /***************************************************************************
391 GetTempFileName
392 ***************************************************************************/
393 INT GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
395 int unique;
396 int handle;
397 char tempname[256];
399 if (uUnique == 0)
400 unique = time(NULL)%99999L;
401 else
402 unique = uUnique%99999L;
404 strcpy(tempname,lpszPrefixString);
405 tempname[3]='\0';
407 sprintf(lpszTempFileName,"%s\\%s%d.tmp", TempDirectory, tempname,
408 unique);
410 ToDos(lpszTempFileName);
412 dprintf_file(stddeb,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
413 lpszPrefixString,uUnique,lpszTempFileName);
414 if ((handle = _lcreat (lpszTempFileName, 0x0000)) == -1) {
415 fprintf(stderr,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName);
417 else
418 close(handle);
420 return unique;
423 /***************************************************************************
424 SetErrorMode
425 ***************************************************************************/
426 WORD SetErrorMode(WORD x)
428 dprintf_file(stdnimp,"wine: SetErrorMode %4x (ignored)\n",x);
430 return 1;
433 /***************************************************************************
434 _hread
435 ***************************************************************************/
436 LONG _hread(INT hf, LPSTR hpvBuffer, LONG cbBuffer)
438 return read(hf, hpvBuffer, cbBuffer);
440 /***************************************************************************
441 _hwrite
442 ***************************************************************************/
443 LONG _hwrite(INT hf, const LPSTR hpvBuffer, LONG cbBuffer)
445 return write(hf, hpvBuffer, cbBuffer);