Release 0.5
[wine/multimedia.git] / misc / file.c
blobadc43683313ca470c9ec783bd0267f992d8f4af9
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 <windows.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include "prototypes.h"
28 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
31 /***************************************************************************
32 _lopen
34 Emulate the _lopen windows call
35 ***************************************************************************/
36 WORD KERNEL__lopen (LPSTR lpPathName, WORD iReadWrite)
38 int handle;
39 char *UnixFileName;
41 #ifdef DEBUG_FILE
42 fprintf (stderr, "_lopen: open %s\n", lpPathName);
43 #endif
45 if ((UnixFileName = GetUnixFileName(lpPathName)) == NULL)
46 return HFILE_ERROR;
48 handle = open (UnixFileName, iReadWrite);
50 #ifdef DEBUG_FILE
51 fprintf (stderr, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
52 #endif
54 if (handle == -1)
55 return HFILE_ERROR;
56 else
57 return handle;
60 /***************************************************************************
61 _lread
62 ***************************************************************************/
63 WORD KERNEL__lread (WORD hFile, LPSTR lpBuffer, WORD wBytes)
65 int result;
67 #ifdef DEBUG_FILE
68 fprintf(stderr, "_lread: handle %d, buffer = %ld, length = %d\n",
69 hFile, lpBuffer, wBytes);
70 #endif
72 result = read (hFile, lpBuffer, wBytes);
74 if (result == -1)
75 return HFILE_ERROR;
76 else
77 return result;
80 /****************************************************************************
81 _lwrite
82 ****************************************************************************/
83 WORD KERNEL__lwrite (WORD hFile, LPSTR lpBuffer, WORD wBytes)
85 int result;
87 #ifdef DEBUG_FILE
88 fprintf(stderr, "_lwrite: handle %d, buffer = %ld, length = %d\n",
89 hFile, lpBuffer, wBytes);
90 #endif
91 result = write (hFile, lpBuffer, wBytes);
93 if (result == -1)
94 return HFILE_ERROR;
95 else
96 return result;
99 /***************************************************************************
100 _lclose
101 ***************************************************************************/
102 WORD KERNEL__lclose (WORD hFile)
104 #ifdef DEBUG_FILE
105 fprintf(stderr, "_lclose: handle %d\n", hFile);
106 #endif
108 close (hFile);
111 /**************************************************************************
112 OpenFile
114 Warning: This is nearly totally untested. It compiles, that's it...
115 -SL 9/13/93
116 **************************************************************************/
117 WORD KERNEL_OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
119 int base,flags;
121 #ifdef DEBUG_FILE
122 fprintf(stderr,"Openfile(%s,<struct>,%d) ",lpFileName,wStyle);
123 #endif
125 base=wStyle&0xF;
126 flags=wStyle&0xFFF0;
128 flags&=0xFF0F; /* strip SHARE bits for now */
129 flags&=0xD7FF; /* strip PROMPT & CANCEL bits for now */
130 flags&=0x7FFF; /* strip REOPEN bit for now */
131 flags&=0xFBFF; /* strib VERIFY bit for now */
133 if(flags&OF_CREATE) { base |=O_CREAT; flags &=0xEFFF; }
135 fprintf(stderr,"now %d,%d\n",base,flags);
137 if(flags&(OF_DELETE|OF_EXIST))
139 fprintf(stderr,"Unsupported OpenFile option\n");
140 return -1;
142 else
144 return KERNEL__lopen (lpFileName, wStyle);
148 /**************************************************************************
149 SetHandleCount
151 Changes the number of file handles available to the application. Since
152 Linux isn't limited to 20 files, this one's easy. - SL
153 **************************************************************************/
155 WORD SetHandleCount (WORD wNumber)
157 printf("SetHandleCount(%d)\n",wNumber);
158 return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
161 /***************************************************************************
162 _llseek
163 ***************************************************************************/
164 LONG KERNEL__llseek (WORD hFile, LONG lOffset, int nOrigin)
166 int origin;
168 #ifdef DEBUG_FILE
169 fprintf(stderr, "_llseek: handle %d, offset %ld, origin %d\n", hFile, lOffset, nOrigin);
170 #endif
172 switch (nOrigin) {
173 case 1: origin = SEEK_CUR;
174 break;
175 case 2: origin = SEEK_END;
176 break;
177 default: origin = SEEK_SET;
178 break;
181 return ( lseek(hFile, lOffset, origin) );
184 /***************************************************************************
185 _lcreate
186 ***************************************************************************/
187 LONG KERNEL__lcreate (LPSTR lpszFilename, int fnAttribute)
189 int handle;
190 char *UnixFileName;
192 #ifdef DEBUG_FILE
193 fprintf(stderr, "_lcreate: filename %s, attributes %d\n",lpszFilename,
194 fnAttribute);
195 #endif
197 if ((UnixFileName = GetUnixFileName(lpszFilename)) == NULL)
198 return HFILE_ERROR;
200 handle = open (UnixFileName, O_CREAT | O_TRUNC | O_WRONLY );
202 if (handle == -1)
203 return HFILE_ERROR;
204 else
205 return handle;
208 /***************************************************************************
209 GetDriveType
210 ***************************************************************************/
211 UINT GetDriveType(int drive)
214 #ifdef DEBUG_FILE
215 fprintf(stderr,"GetDriveType %c:\n",'A'+drive);
216 #endif
218 if (!DOS_ValidDrive(drive))
219 return 0;
221 if (drive == 0 || drive == 1)
222 return DRIVE_REMOVABLE;
224 return DRIVE_REMOTE;
227 /***************************************************************************
228 GetTempDrive
229 ***************************************************************************/
230 BYTE GetTempDrive(BYTE chDriveLetter)
232 #ifdef DEBUG_FILE
233 fprintf(stderr,"GetTempDrive (%d)\n",chDriveLetter);
234 #endif
235 return('C');
238 /***************************************************************************
239 GetWindowsDirectory
240 ***************************************************************************/
241 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
243 if (cbSysPath < strlen(WindowsDirectory))
244 *lpszSysPath = 0;
245 else
246 strcpy(lpszSysPath, WindowsDirectory);
248 #ifdef DEBUG_FILE
249 fprintf(stderr,"GetWindowsDirectory (%s)\n",lpszSysPath);
250 #endif
252 return(strlen(lpszSysPath));
254 /***************************************************************************
255 GetSystemDirectory
256 ***************************************************************************/
257 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
259 if (cbSysPath < strlen(SystemDirectory))
260 *lpszSysPath = 0;
261 else
262 strcpy(lpszSysPath, SystemDirectory);
264 #ifdef DEBUG_FILE
265 fprintf(stderr,"GetSystemDirectory (%s)\n",lpszSysPath);
266 #endif
268 return(strlen(lpszSysPath));
270 /***************************************************************************
271 GetTempFileName
272 ***************************************************************************/
273 int GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
275 int unique;
276 char tempname[256];
278 if (uUnique == 0)
279 unique = time(NULL)%99999L;
280 else
281 unique = uUnique%99999L;
283 strcpy(tempname,lpszPrefixString);
284 tempname[3]='\0';
286 sprintf(lpszTempFileName,"%s\%s%d.tmp",WindowsDirectory, tempname,
287 unique);
289 ToDos(lpszTempFileName);
291 #ifdef DEBUG_FILE
292 fprintf(stderr,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
293 lpszPrefixString,uUnique,lpszTempFileName);
294 #endif
296 return unique;
299 /***************************************************************************
300 SetErrorMode
301 ***************************************************************************/
302 WORD SetErrorMode(WORD x)
304 fprintf(stderr,"wine: SetErrorMode %4x (ignored)\n",x);