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, _lcreate, GetDriveType, GetTempDrive,
16 * GetWindowsDirectory, GetSystemDirectory, GetTempFileName.
18 ************************************************************************/
20 /* #define DEBUG_FILE */
28 #include "prototypes.h"
32 char WindowsDirectory
[256], SystemDirectory
[256], TempDirectory
[256];
34 /***************************************************************************
37 Emulate the _lopen windows call
38 ***************************************************************************/
39 INT
_lopen (LPSTR lpPathName
, INT iReadWrite
)
45 fprintf (stderr
, "_lopen: open('%s', %X);\n", lpPathName
, iReadWrite
);
48 if ((UnixFileName
= GetUnixFileName(lpPathName
)) == NULL
)
51 handle
= open (UnixFileName
, iReadWrite
);
54 fprintf (stderr
, "_lopen: open: %s (handle %d)\n", UnixFileName
, handle
);
63 /***************************************************************************
65 ***************************************************************************/
66 WORD
_lread (INT hFile
, LPSTR lpBuffer
, INT wBytes
)
71 fprintf(stderr
, "_lread: handle %d, buffer = %ld, length = %d\n",
72 hFile
, (int) lpBuffer
, wBytes
);
75 result
= read (hFile
, lpBuffer
, wBytes
);
83 /****************************************************************************
85 ****************************************************************************/
86 WORD
_lwrite (INT hFile
, LPSTR lpBuffer
, INT wBytes
)
91 fprintf(stderr
, "_lwrite: handle %d, buffer = %ld, length = %d\n",
92 hFile
, (int) lpBuffer
, wBytes
);
94 result
= write (hFile
, lpBuffer
, wBytes
);
102 /***************************************************************************
104 ***************************************************************************/
105 INT
_lclose (INT hFile
)
108 fprintf(stderr
, "_lclose: handle %d\n", hFile
);
116 /**************************************************************************
119 Warning: This is nearly totally untested. It compiles, that's it...
121 **************************************************************************/
122 INT
OpenFile (LPSTR lpFileName
, LPOFSTRUCT ofs
, WORD wStyle
)
127 fprintf(stderr
,"Openfile(%s,<struct>,%d) ",lpFileName
,wStyle
);
132 flags
&=0xFF0F; /* strip SHARE bits for now */
133 flags
&=0xD7FF; /* strip PROMPT & CANCEL bits for now */
134 flags
&=0x7FFF; /* strip REOPEN bit for now */
135 flags
&=0xFBFF; /* strib VERIFY bit for now */
137 if(flags
&OF_CREATE
) { base
|=O_CREAT
; flags
&=0xEFFF; }
140 fprintf(stderr
,"now %d,%d\n",base
,flags
);
143 if (flags
& OF_EXIST
) {
144 printf("OpenFile // OF_EXIST '%s' !\n", lpFileName
);
145 handle
= _lopen (lpFileName
, wStyle
);
149 if (flags
& OF_DELETE
) {
150 printf("OpenFile // OF_DELETE '%s' !\n", lpFileName
);
151 return unlink(lpFileName
);
154 return _lopen (lpFileName
, wStyle
);
158 /**************************************************************************
161 Changes the number of file handles available to the application. Since
162 Linux isn't limited to 20 files, this one's easy. - SL
163 **************************************************************************/
165 #if !defined (OPEN_MAX)
166 /* This one is for the Sun */
167 #define OPEN_MAX _POSIX_OPEN_MAX
169 WORD
SetHandleCount (WORD wNumber
)
171 printf("SetHandleCount(%d)\n",wNumber
);
172 return((wNumber
<OPEN_MAX
) ? wNumber
: OPEN_MAX
);
175 /***************************************************************************
177 ***************************************************************************/
178 LONG
_llseek (INT hFile
, LONG lOffset
, INT nOrigin
)
183 fprintf(stderr
, "_llseek: handle %d, offset %ld, origin %d\n", hFile
, lOffset
, nOrigin
);
187 case 1: origin
= SEEK_CUR
;
189 case 2: origin
= SEEK_END
;
191 default: origin
= SEEK_SET
;
195 return ( lseek(hFile
, lOffset
, origin
) );
198 /***************************************************************************
200 ***************************************************************************/
201 INT
_lcreate (LPSTR lpszFilename
, INT fnAttribute
)
207 fprintf(stderr
, "_lcreate: filename %s, attributes %d\n",lpszFilename
,
210 if ((UnixFileName
= GetUnixFileName(lpszFilename
)) == NULL
)
212 handle
= open (UnixFileName
, O_CREAT
| O_TRUNC
| O_WRONLY
, 426);
220 /***************************************************************************
222 ***************************************************************************/
223 UINT
GetDriveType(INT drive
)
227 fprintf(stderr
,"GetDriveType %c:\n",'A'+drive
);
230 if (!DOS_ValidDrive(drive
))
231 return DRIVE_DOESNOTEXIST
;
233 if (drive
== 0 || drive
== 1)
234 return DRIVE_REMOVABLE
;
239 /***************************************************************************
241 ***************************************************************************/
242 BYTE
GetTempDrive(BYTE chDriveLetter
)
245 fprintf(stderr
,"GetTempDrive (%d)\n",chDriveLetter
);
250 /***************************************************************************
252 ***************************************************************************/
253 UINT
GetWindowsDirectory(LPSTR lpszSysPath
, UINT cbSysPath
)
255 if (cbSysPath
< strlen(WindowsDirectory
))
258 strcpy(lpszSysPath
, WindowsDirectory
);
261 fprintf(stderr
,"GetWindowsDirectory (%s)\n",lpszSysPath
);
264 ChopOffSlash(lpszSysPath
);
265 return(strlen(lpszSysPath
));
267 /***************************************************************************
269 ***************************************************************************/
270 UINT
GetSystemDirectory(LPSTR lpszSysPath
, UINT cbSysPath
)
272 if (cbSysPath
< strlen(SystemDirectory
))
275 strcpy(lpszSysPath
, SystemDirectory
);
278 fprintf(stderr
,"GetSystemDirectory (%s)\n",lpszSysPath
);
281 ChopOffSlash(lpszSysPath
);
282 return(strlen(lpszSysPath
));
284 /***************************************************************************
286 ***************************************************************************/
287 INT
GetTempFileName(BYTE bDriveLetter
, LPCSTR lpszPrefixString
, UINT uUnique
, LPSTR lpszTempFileName
)
294 unique
= time(NULL
)%99999L;
296 unique
= uUnique
%99999L;
298 strcpy(tempname
,lpszPrefixString
);
301 sprintf(lpszTempFileName
,"%s\\%s%d.tmp", TempDirectory
, tempname
,
304 ToDos(lpszTempFileName
);
307 fprintf(stderr
,"GetTempFilename: %c %s %d => %s\n",bDriveLetter
,
308 lpszPrefixString
,uUnique
,lpszTempFileName
);
310 if ((handle
= _lcreate (lpszTempFileName
, 0x0000)) == -1) {
311 fprintf(stderr
,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName
);
319 /***************************************************************************
321 ***************************************************************************/
322 WORD
SetErrorMode(WORD x
)
324 fprintf(stderr
,"wine: SetErrorMode %4x (ignored)\n",x
);
327 /***************************************************************************
329 ***************************************************************************/
330 long _hread(int hf
, void FAR
*hpvBuffer
, long cbBuffer
)
332 return read(hf
, hpvBuffer
, cbBuffer
);
334 /***************************************************************************
336 ***************************************************************************/
337 long _hwrite(int hf
, const void FAR
*hpvBuffer
, long cbBuffer
)
339 return write(hf
, hpvBuffer
, cbBuffer
);