Release 940510
[wine/multimedia.git] / misc / file.c
blobff4bcbae4b3b29f2a4ae156265691ff753f2e049
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 */
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <windows.h>
28 #include "prototypes.h"
30 #define DEBUG_FILE
32 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
34 /***************************************************************************
35 _lopen
37 Emulate the _lopen windows call
38 ***************************************************************************/
39 INT _lopen (LPSTR lpPathName, INT iReadWrite)
41 int handle;
42 char *UnixFileName;
44 #ifdef DEBUG_FILE
45 fprintf (stderr, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
46 #endif
48 if ((UnixFileName = GetUnixFileName(lpPathName)) == NULL)
49 return HFILE_ERROR;
50 iReadWrite &= 0x000F;
51 handle = open (UnixFileName, iReadWrite);
53 #ifdef DEBUG_FILE
54 fprintf (stderr, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
55 #endif
57 if (handle == -1)
58 return HFILE_ERROR;
59 else
60 return handle;
63 /***************************************************************************
64 _lread
65 ***************************************************************************/
66 WORD _lread (INT hFile, LPSTR lpBuffer, INT wBytes)
68 int result;
70 #ifdef DEBUG_FILE
71 fprintf(stderr, "_lread: handle %d, buffer = %ld, length = %d\n",
72 hFile, (int) lpBuffer, wBytes);
73 #endif
75 result = read (hFile, lpBuffer, wBytes);
77 if (result == -1)
78 return HFILE_ERROR;
79 else
80 return result;
83 /****************************************************************************
84 _lwrite
85 ****************************************************************************/
86 WORD _lwrite (INT hFile, LPSTR lpBuffer, INT wBytes)
88 int result;
90 #ifdef DEBUG_FILE
91 fprintf(stderr, "_lwrite: handle %d, buffer = %ld, length = %d\n",
92 hFile, (int) lpBuffer, wBytes);
93 #endif
94 result = write (hFile, lpBuffer, wBytes);
96 if (result == -1)
97 return HFILE_ERROR;
98 else
99 return result;
102 /***************************************************************************
103 _lclose
104 ***************************************************************************/
105 INT _lclose (INT hFile)
107 #ifdef DEBUG_FILE
108 fprintf(stderr, "_lclose: handle %d\n", hFile);
109 #endif
110 if (close (hFile))
111 return HFILE_ERROR;
112 else
113 return 0;
116 /**************************************************************************
117 OpenFile
119 Warning: This is nearly totally untested. It compiles, that's it...
120 -SL 9/13/93
121 **************************************************************************/
122 INT OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
124 int base,flags;
125 int handle;
126 #ifdef DEBUG_FILE
127 fprintf(stderr,"Openfile(%s,<struct>,%d) ",lpFileName,wStyle);
128 #endif
129 base=wStyle&0xF;
130 flags=wStyle&0xFFF0;
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; }
139 #ifdef DEBUG_FILE
140 fprintf(stderr,"now %d,%d\n",base,flags);
141 #endif
143 if (flags & OF_EXIST) {
144 printf("OpenFile // OF_EXIST '%s' !\n", lpFileName);
145 handle = _lopen (lpFileName, wStyle);
146 close(handle);
147 return handle;
149 if (flags & OF_DELETE) {
150 printf("OpenFile // OF_DELETE '%s' !\n", lpFileName);
151 return unlink(lpFileName);
153 else {
154 return _lopen (lpFileName, wStyle);
158 /**************************************************************************
159 SetHandleCount
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
168 #endif
169 WORD SetHandleCount (WORD wNumber)
171 printf("SetHandleCount(%d)\n",wNumber);
172 return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
175 /***************************************************************************
176 _llseek
177 ***************************************************************************/
178 LONG _llseek (INT hFile, LONG lOffset, INT nOrigin)
180 int origin;
182 #ifdef DEBUG_FILE
183 fprintf(stderr, "_llseek: handle %d, offset %ld, origin %d\n", hFile, lOffset, nOrigin);
184 #endif
186 switch (nOrigin) {
187 case 1: origin = SEEK_CUR;
188 break;
189 case 2: origin = SEEK_END;
190 break;
191 default: origin = SEEK_SET;
192 break;
195 return ( lseek(hFile, lOffset, origin) );
198 /***************************************************************************
199 _lcreate
200 ***************************************************************************/
201 INT _lcreate (LPSTR lpszFilename, INT fnAttribute)
203 int handle;
204 char *UnixFileName;
206 #ifdef DEBUG_FILE
207 fprintf(stderr, "_lcreate: filename %s, attributes %d\n",lpszFilename,
208 fnAttribute);
209 #endif
210 if ((UnixFileName = GetUnixFileName(lpszFilename)) == NULL)
211 return HFILE_ERROR;
212 handle = open (UnixFileName, O_CREAT | O_TRUNC | O_WRONLY, 426);
214 if (handle == -1)
215 return HFILE_ERROR;
216 else
217 return handle;
220 /***************************************************************************
221 GetDriveType
222 ***************************************************************************/
223 UINT GetDriveType(INT drive)
226 #ifdef DEBUG_FILE
227 fprintf(stderr,"GetDriveType %c:\n",'A'+drive);
228 #endif
230 if (!DOS_ValidDrive(drive))
231 return DRIVE_DOESNOTEXIST;
233 if (drive == 0 || drive == 1)
234 return DRIVE_REMOVABLE;
236 return DRIVE_FIXED;
239 /***************************************************************************
240 GetTempDrive
241 ***************************************************************************/
242 BYTE GetTempDrive(BYTE chDriveLetter)
244 #ifdef DEBUG_FILE
245 fprintf(stderr,"GetTempDrive (%d)\n",chDriveLetter);
246 #endif
247 return('C');
250 /***************************************************************************
251 GetWindowsDirectory
252 ***************************************************************************/
253 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
255 if (cbSysPath < strlen(WindowsDirectory))
256 *lpszSysPath = 0;
257 else
258 strcpy(lpszSysPath, WindowsDirectory);
260 #ifdef DEBUG_FILE
261 fprintf(stderr,"GetWindowsDirectory (%s)\n",lpszSysPath);
262 #endif
264 ChopOffSlash(lpszSysPath);
265 return(strlen(lpszSysPath));
267 /***************************************************************************
268 GetSystemDirectory
269 ***************************************************************************/
270 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
272 if (cbSysPath < strlen(SystemDirectory))
273 *lpszSysPath = 0;
274 else
275 strcpy(lpszSysPath, SystemDirectory);
277 #ifdef DEBUG_FILE
278 fprintf(stderr,"GetSystemDirectory (%s)\n",lpszSysPath);
279 #endif
281 ChopOffSlash(lpszSysPath);
282 return(strlen(lpszSysPath));
284 /***************************************************************************
285 GetTempFileName
286 ***************************************************************************/
287 INT GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
289 int unique;
290 int handle;
291 char tempname[256];
293 if (uUnique == 0)
294 unique = time(NULL)%99999L;
295 else
296 unique = uUnique%99999L;
298 strcpy(tempname,lpszPrefixString);
299 tempname[3]='\0';
301 sprintf(lpszTempFileName,"%s\\%s%d.tmp", TempDirectory, tempname,
302 unique);
304 ToDos(lpszTempFileName);
306 #ifdef DEBUG_FILE
307 fprintf(stderr,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
308 lpszPrefixString,uUnique,lpszTempFileName);
309 #endif
310 if ((handle = _lcreate (lpszTempFileName, 0x0000)) == -1) {
311 fprintf(stderr,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName);
313 else
314 close(handle);
316 return unique;
319 /***************************************************************************
320 SetErrorMode
321 ***************************************************************************/
322 WORD SetErrorMode(WORD x)
324 fprintf(stderr,"wine: SetErrorMode %4x (ignored)\n",x);
327 /***************************************************************************
328 _hread
329 ***************************************************************************/
330 long _hread(int hf, void FAR *hpvBuffer, long cbBuffer)
332 return read(hf, hpvBuffer, cbBuffer);
334 /***************************************************************************
335 _hwrite
336 ***************************************************************************/
337 long _hwrite(int hf, const void FAR *hpvBuffer, long cbBuffer)
339 return write(hf, hpvBuffer, cbBuffer);