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 ************************************************************************/
26 #include "prototypes.h"
28 /* #define DEBUG_FILE /* */
30 char WindowsDirectory
[256], SystemDirectory
[256], TempDirectory
[256];
31 extern char WindowsPath
[256];
33 /***************************************************************************
36 Emulate the _lopen windows call
37 ***************************************************************************/
38 INT
_lopen (LPSTR lpPathName
, INT iReadWrite
)
44 fprintf (stderr
, "_lopen: open('%s', %X);\n", lpPathName
, iReadWrite
);
47 if ((UnixFileName
= GetUnixFileName(lpPathName
)) == NULL
)
50 handle
= open (UnixFileName
, iReadWrite
);
53 fprintf (stderr
, "_lopen: open: %s (handle %d)\n", UnixFileName
, handle
);
62 /***************************************************************************
64 ***************************************************************************/
65 INT
_lread (INT hFile
, LPSTR lpBuffer
, WORD wBytes
)
70 fprintf(stderr
, "_lread: handle %d, buffer = %ld, length = %d\n",
71 hFile
, (int) lpBuffer
, wBytes
);
74 result
= read (hFile
, lpBuffer
, wBytes
);
82 /****************************************************************************
84 ****************************************************************************/
85 INT
_lwrite (INT hFile
, LPSTR lpBuffer
, WORD wBytes
)
91 fprintf(stderr
, "_lwrite: handle %d, buffer = %ld, length = %d\n",
92 hFile
, (int) lpBuffer
, wBytes
);
95 result
= write (hFile
, lpBuffer
, wBytes
);
103 /***************************************************************************
105 ***************************************************************************/
106 INT
_lclose (INT hFile
)
109 fprintf(stderr
, "_lclose: handle %d\n", hFile
);
117 /**************************************************************************
120 Warning: This is nearly totally untested. It compiles, that's it...
122 **************************************************************************/
123 INT
OpenFile (LPSTR lpFileName
, LPOFSTRUCT ofs
, WORD wStyle
)
130 fprintf(stderr
,"OpenFile(%s,<struct>,%04X)\n",lpFileName
,wStyle
);
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
)
148 fprintf(stderr
,"now %d,%d\n",base
,flags
);
151 if (flags
& OF_EXIST
)
153 printf("OpenFile // OF_EXIST '%s' !\n", lpFileName
);
154 handle
= _lopen (lpFileName
, wStyle
);
156 /* Try again with WindowsPath */
157 if (FindFile(buf
, sizeof(buf
), lpFileName
, NULL
, WindowsPath
) != NULL
) {
158 handle
= _lopen (buf
, wStyle
);
164 if (flags
& OF_DELETE
)
166 printf("OpenFile // OF_DELETE '%s' !\n", lpFileName
);
167 return unlink(lpFileName
);
173 if ((UnixFileName
= GetUnixFileName(lpFileName
)) == NULL
)
175 handle
= open(UnixFileName
, base
, 0666);
177 /* Try again with WindowsPath */
178 if (FindFile(buf
, sizeof(buf
), lpFileName
, NULL
, WindowsPath
) != NULL
) {
180 printf("OpenFile // file '%s' found !\n", buf
);
183 handle
= open(UnixFileName
, base
, 0666);
188 fprintf(stderr
, "OpenFile: returning %04.4x\n", handle
);
198 /**************************************************************************
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
209 WORD
SetHandleCount (WORD wNumber
)
211 printf("SetHandleCount(%d)\n",wNumber
);
212 return((wNumber
<OPEN_MAX
) ? wNumber
: OPEN_MAX
);
215 /***************************************************************************
217 ***************************************************************************/
218 LONG
_llseek (INT hFile
, LONG lOffset
, INT nOrigin
)
223 fprintf(stderr
, "_llseek: handle %d, offset %ld, origin %d\n", hFile
, lOffset
, nOrigin
);
227 case 1: origin
= SEEK_CUR
;
229 case 2: origin
= SEEK_END
;
231 default: origin
= SEEK_SET
;
235 return ( lseek(hFile
, lOffset
, origin
) );
238 /***************************************************************************
240 ***************************************************************************/
241 INT
_lcreat (LPSTR lpszFilename
, INT fnAttribute
)
247 fprintf(stderr
, "_lcreat: filename %s, attributes %d\n",lpszFilename
,
250 if ((UnixFileName
= GetUnixFileName(lpszFilename
)) == NULL
)
252 handle
= open (UnixFileName
, O_CREAT
| O_TRUNC
| O_WRONLY
, 426);
260 /***************************************************************************
262 ***************************************************************************/
263 UINT
GetDriveType(INT drive
)
267 fprintf(stderr
,"GetDriveType %c:\n",'A'+drive
);
270 if (!DOS_ValidDrive(drive
))
271 return DRIVE_DOESNOTEXIST
;
273 if (drive
== 0 || drive
== 1)
274 return DRIVE_REMOVABLE
;
279 /***************************************************************************
281 ***************************************************************************/
282 BYTE
GetTempDrive(BYTE chDriveLetter
)
285 fprintf(stderr
,"GetTempDrive (%d)\n",chDriveLetter
);
290 /***************************************************************************
292 ***************************************************************************/
293 UINT
GetWindowsDirectory(LPSTR lpszSysPath
, UINT cbSysPath
)
295 if (cbSysPath
< strlen(WindowsDirectory
))
298 strcpy(lpszSysPath
, WindowsDirectory
);
301 fprintf(stderr
,"GetWindowsDirectory (%s)\n",lpszSysPath
);
304 ChopOffSlash(lpszSysPath
);
305 return(strlen(lpszSysPath
));
307 /***************************************************************************
309 ***************************************************************************/
310 UINT
GetSystemDirectory(LPSTR lpszSysPath
, UINT cbSysPath
)
312 if (cbSysPath
< strlen(SystemDirectory
))
315 strcpy(lpszSysPath
, SystemDirectory
);
318 fprintf(stderr
,"GetSystemDirectory (%s)\n",lpszSysPath
);
321 ChopOffSlash(lpszSysPath
);
322 return(strlen(lpszSysPath
));
324 /***************************************************************************
326 ***************************************************************************/
327 INT
GetTempFileName(BYTE bDriveLetter
, LPCSTR lpszPrefixString
, UINT uUnique
, LPSTR lpszTempFileName
)
334 unique
= time(NULL
)%99999L;
336 unique
= uUnique
%99999L;
338 strcpy(tempname
,lpszPrefixString
);
341 sprintf(lpszTempFileName
,"%s\\%s%d.tmp", TempDirectory
, tempname
,
344 ToDos(lpszTempFileName
);
347 fprintf(stderr
,"GetTempFilename: %c %s %d => %s\n",bDriveLetter
,
348 lpszPrefixString
,uUnique
,lpszTempFileName
);
350 if ((handle
= _lcreat (lpszTempFileName
, 0x0000)) == -1) {
351 fprintf(stderr
,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName
);
359 /***************************************************************************
361 ***************************************************************************/
362 WORD
SetErrorMode(WORD x
)
364 fprintf(stderr
,"wine: SetErrorMode %4x (ignored)\n",x
);
367 /***************************************************************************
369 ***************************************************************************/
370 long _hread(int hf
, void FAR
*hpvBuffer
, long cbBuffer
)
372 return read(hf
, hpvBuffer
, cbBuffer
);
374 /***************************************************************************
376 ***************************************************************************/
377 long _hwrite(int hf
, const void FAR
*hpvBuffer
, long cbBuffer
)
379 return write(hf
, hpvBuffer
, cbBuffer
);