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 ************************************************************************/
34 char WindowsDirectory
[256], SystemDirectory
[256], TempDirectory
[256];
36 /***************************************************************************
39 Emulate the _lopen windows call
40 ***************************************************************************/
41 INT
_lopen (LPSTR lpPathName
, INT iReadWrite
)
47 dprintf_file(stddeb
, "_lopen: open('%s', %X);\n", lpPathName
, iReadWrite
);
48 if ((UnixFileName
= DOS_GetUnixFileName(lpPathName
)) == NULL
)
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
);
68 /***************************************************************************
70 ***************************************************************************/
71 INT
_lread (INT hFile
, LPSTR lpBuffer
, WORD wBytes
)
75 dprintf_file(stddeb
, "_lread: handle %d, buffer = %ld, length = %d\n",
76 hFile
, (long) lpBuffer
, wBytes
);
78 result
= read (hFile
, lpBuffer
, wBytes
);
86 /****************************************************************************
88 ****************************************************************************/
89 INT
_lwrite (INT hFile
, LPSTR lpBuffer
, WORD wBytes
)
93 dprintf_file(stddeb
, "_lwrite: handle %d, buffer = %ld, length = %d\n",
94 hFile
, (long) lpBuffer
, wBytes
);
96 result
= write (hFile
, lpBuffer
, wBytes
);
104 /***************************************************************************
106 ***************************************************************************/
107 INT
_lclose (INT hFile
)
109 dprintf_file(stddeb
, "_lclose: handle %d\n", hFile
);
116 /**************************************************************************
118 **************************************************************************/
119 INT
OpenFile (LPSTR lpFileName
, LPOFSTRUCT ofs
, WORD wStyle
)
121 char filename
[MAX_PATH
+1];
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
136 if (action
& OF_CREATE
)
140 if (!(action
& OF_REOPEN
)) strcpy(ofs
->szPathName
, lpFileName
);
141 ofs
->cBytes
= sizeof(OFSTRUCT
);
142 ofs
->fFixedDisk
= FALSE
;
144 *((int*)ofs
->reserved
) = 0;
146 if ((unixfilename
= DOS_GetUnixFileName (ofs
->szPathName
)) == NULL
)
149 ofs
->nErrCode
= ExtendedError
;
152 handle
= open (unixfilename
, (wStyle
& 0x0003) | O_CREAT
, 0x666);
156 ofs
->nErrCode
= ExtendedError
;
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
,':')))
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
)) )
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
)) )
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
)) )
194 /* Try the path of the current executable */
196 if (GetCurrentTask())
199 GetModuleFileName( GetCurrentTask(), filename
, MAX_PATH
);
200 if ((p
= strrchr( filename
, '\\' )))
203 strcat( filename
, lpFileName
);
204 if ((!stat(DOS_GetUnixFileName(filename
), &s
)) &&
205 (S_ISREG(s
.st_mode
)) )
210 /* Try all directories in path */
212 if (DOS_FindFile(temp
,MAX_PATH
,lpFileName
,NULL
,WindowsPath
))
214 strcpy(filename
, DOS_GetDosFileName(temp
));
217 strcpy (filename
, lpFileName
);
221 strcpy (filename
,lpFileName
);
223 ofs
->cBytes
= sizeof(OFSTRUCT
);
224 ofs
->fFixedDisk
= FALSE
;
225 strcpy(ofs
->szPathName
, filename
);
227 if (!(action
& OF_VERIFY
))
228 *((int*)ofs
->reserved
) = 0;
232 if (action
& OF_PARSE
)
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
)))
244 ofs
->nErrCode
= ExtendedError
;
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
)
265 if ((handle
= _lopen( ofs
->szPathName
, wStyle
)) == -1)
267 ofs
->nErrCode
= 2; /* not found */
274 /**************************************************************************
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
285 WORD
SetHandleCount (WORD wNumber
)
287 dprintf_file(stddeb
,"SetHandleCount(%d)\n",wNumber
);
288 return((wNumber
<OPEN_MAX
) ? wNumber
: OPEN_MAX
);
291 /***************************************************************************
293 ***************************************************************************/
294 LONG
_llseek (INT hFile
, LONG lOffset
, INT nOrigin
)
298 dprintf_file(stddeb
, "_llseek: handle %d, offset %ld, origin %d\n",
299 hFile
, lOffset
, nOrigin
);
302 case 1: origin
= SEEK_CUR
;
304 case 2: origin
= SEEK_END
;
306 default: origin
= SEEK_SET
;
310 return ( lseek(hFile
, lOffset
, origin
) );
313 /***************************************************************************
315 ***************************************************************************/
316 INT
_lcreat (LPSTR lpszFilename
, INT fnAttribute
)
321 dprintf_file(stddeb
, "_lcreat: filename %s, attributes %d\n",
322 lpszFilename
, fnAttribute
);
323 if ((UnixFileName
= DOS_GetUnixFileName(lpszFilename
)) == NULL
)
325 handle
= open (UnixFileName
, O_CREAT
| O_TRUNC
| O_WRONLY
, 426);
333 /***************************************************************************
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
;
350 /***************************************************************************
352 ***************************************************************************/
353 BYTE
GetTempDrive(BYTE chDriveLetter
)
355 dprintf_file(stddeb
,"GetTempDrive (%d)\n",chDriveLetter
);
356 if (TempDirectory
[1] == ':') return TempDirectory
[0];
360 /***************************************************************************
362 ***************************************************************************/
363 UINT
GetWindowsDirectory(LPSTR lpszSysPath
, UINT cbSysPath
)
365 if (cbSysPath
< strlen(WindowsDirectory
))
368 strcpy(lpszSysPath
, WindowsDirectory
);
370 dprintf_file(stddeb
,"GetWindowsDirectory (%s)\n",lpszSysPath
);
372 ChopOffSlash(lpszSysPath
);
373 return(strlen(lpszSysPath
));
375 /***************************************************************************
377 ***************************************************************************/
378 UINT
GetSystemDirectory(LPSTR lpszSysPath
, UINT cbSysPath
)
380 if (cbSysPath
< strlen(SystemDirectory
))
383 strcpy(lpszSysPath
, SystemDirectory
);
385 dprintf_file(stddeb
,"GetSystemDirectory (%s)\n",lpszSysPath
);
387 ChopOffSlash(lpszSysPath
);
388 return(strlen(lpszSysPath
));
390 /***************************************************************************
392 ***************************************************************************/
393 INT
GetTempFileName(BYTE bDriveLetter
, LPCSTR lpszPrefixString
, UINT uUnique
, LPSTR lpszTempFileName
)
400 unique
= time(NULL
)%99999L;
402 unique
= uUnique
%99999L;
404 strcpy(tempname
,lpszPrefixString
);
407 sprintf(lpszTempFileName
,"%s\\%s%d.tmp", TempDirectory
, tempname
,
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
);
423 /***************************************************************************
425 ***************************************************************************/
426 WORD
SetErrorMode(WORD x
)
428 dprintf_file(stdnimp
,"wine: SetErrorMode %4x (ignored)\n",x
);
433 /***************************************************************************
435 ***************************************************************************/
436 LONG
_hread(INT hf
, LPSTR hpvBuffer
, LONG cbBuffer
)
438 return read(hf
, hpvBuffer
, cbBuffer
);
440 /***************************************************************************
442 ***************************************************************************/
443 LONG
_hwrite(INT hf
, const LPSTR hpvBuffer
, LONG cbBuffer
)
445 return write(hf
, hpvBuffer
, cbBuffer
);