Release 940405
[wine.git] / misc / file.c
blob5d41f1cb9294a068748de2eded32a9f0c954ede1
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 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
32 /***************************************************************************
33 _lopen
35 Emulate the _lopen windows call
36 ***************************************************************************/
37 int _lopen (LPSTR lpPathName, int iReadWrite)
39 int handle;
40 char *UnixFileName;
42 #ifdef DEBUG_FILE
43 fprintf (stderr, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
44 #endif
46 if ((UnixFileName = GetUnixFileName(lpPathName)) == NULL)
47 return HFILE_ERROR;
48 iReadWrite &= 0x000F;
49 handle = open (UnixFileName, iReadWrite);
51 #ifdef DEBUG_FILE
52 fprintf (stderr, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
53 #endif
55 if (handle == -1)
56 return HFILE_ERROR;
57 else
58 return handle;
61 /***************************************************************************
62 _lread
63 ***************************************************************************/
64 WORD _lread (int hFile, LPSTR lpBuffer, int wBytes)
66 int result;
68 #ifdef DEBUG_FILE
69 fprintf(stderr, "_lread: handle %d, buffer = %ld, length = %d\n",
70 hFile, (int) lpBuffer, wBytes);
71 #endif
73 result = read (hFile, lpBuffer, wBytes);
75 if (result == -1)
76 return HFILE_ERROR;
77 else
78 return result;
81 /****************************************************************************
82 _lwrite
83 ****************************************************************************/
84 WORD _lwrite (int hFile, LPSTR lpBuffer, int wBytes)
86 int result;
88 #ifdef DEBUG_FILE
89 fprintf(stderr, "_lwrite: handle %d, buffer = %ld, length = %d\n",
90 hFile, (int) lpBuffer, wBytes);
91 #endif
92 result = write (hFile, lpBuffer, wBytes);
94 if (result == -1)
95 return HFILE_ERROR;
96 else
97 return result;
100 /***************************************************************************
101 _lclose
102 ***************************************************************************/
103 int _lclose (int hFile)
105 #ifdef DEBUG_FILE
106 fprintf(stderr, "_lclose: handle %d\n", hFile);
107 #endif
108 if (close (hFile))
109 return HFILE_ERROR;
110 else
111 return 0;
114 /**************************************************************************
115 OpenFile
117 Warning: This is nearly totally untested. It compiles, that's it...
118 -SL 9/13/93
119 **************************************************************************/
120 int OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
122 int base,flags;
124 #ifdef DEBUG_FILE
125 fprintf(stderr,"Openfile(%s,<struct>,%d) ",lpFileName,wStyle);
126 #endif
128 base=wStyle&0xF;
129 flags=wStyle&0xFFF0;
131 flags&=0xFF0F; /* strip SHARE bits for now */
132 flags&=0xD7FF; /* strip PROMPT & CANCEL bits for now */
133 flags&=0x7FFF; /* strip REOPEN bit for now */
134 flags&=0xFBFF; /* strib VERIFY bit for now */
136 if(flags&OF_CREATE) { base |=O_CREAT; flags &=0xEFFF; }
138 fprintf(stderr,"now %d,%d\n",base,flags);
140 if(flags&(OF_DELETE|OF_EXIST))
142 fprintf(stderr,"Unsupported OpenFile option\n");
143 return -1;
145 else
147 return _lopen (lpFileName, wStyle);
151 /**************************************************************************
152 SetHandleCount
154 Changes the number of file handles available to the application. Since
155 Linux isn't limited to 20 files, this one's easy. - SL
156 **************************************************************************/
158 #if !defined (OPEN_MAX)
159 /* This one is for the Sun */
160 #define OPEN_MAX _POSIX_OPEN_MAX
161 #endif
162 WORD SetHandleCount (WORD wNumber)
164 printf("SetHandleCount(%d)\n",wNumber);
165 return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
168 /***************************************************************************
169 _llseek
170 ***************************************************************************/
171 LONG _llseek (int hFile, LONG lOffset, int nOrigin)
173 int origin;
175 #ifdef DEBUG_FILE
176 fprintf(stderr, "_llseek: handle %d, offset %ld, origin %d\n", hFile, lOffset, nOrigin);
177 #endif
179 switch (nOrigin) {
180 case 1: origin = SEEK_CUR;
181 break;
182 case 2: origin = SEEK_END;
183 break;
184 default: origin = SEEK_SET;
185 break;
188 return ( lseek(hFile, lOffset, origin) );
191 /***************************************************************************
192 _lcreate
193 ***************************************************************************/
194 LONG _lcreate (LPSTR lpszFilename, int fnAttribute)
196 int handle;
197 char *UnixFileName;
199 #ifdef DEBUG_FILE
200 fprintf(stderr, "_lcreate: filename %s, attributes %d\n",lpszFilename,
201 fnAttribute);
202 #endif
204 if ((UnixFileName = GetUnixFileName(lpszFilename)) == NULL)
205 return HFILE_ERROR;
207 handle = open (UnixFileName, O_CREAT | O_TRUNC | O_WRONLY );
209 if (handle == -1)
210 return HFILE_ERROR;
211 else
212 return handle;
215 /***************************************************************************
216 GetDriveType
217 ***************************************************************************/
218 UINT GetDriveType(int drive)
221 #ifdef DEBUG_FILE
222 fprintf(stderr,"GetDriveType %c:\n",'A'+drive);
223 #endif
225 if (!DOS_ValidDrive(drive))
226 return DRIVE_DOESNOTEXIST;
228 if (drive == 0 || drive == 1)
229 return DRIVE_REMOVABLE;
231 return DRIVE_REMOVABLE;
234 /***************************************************************************
235 GetTempDrive
236 ***************************************************************************/
237 BYTE GetTempDrive(BYTE chDriveLetter)
239 #ifdef DEBUG_FILE
240 fprintf(stderr,"GetTempDrive (%d)\n",chDriveLetter);
241 #endif
242 return('C');
245 /***************************************************************************
246 GetWindowsDirectory
247 ***************************************************************************/
248 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
250 if (cbSysPath < strlen(WindowsDirectory))
251 *lpszSysPath = 0;
252 else
253 strcpy(lpszSysPath, WindowsDirectory);
255 #ifdef DEBUG_FILE
256 fprintf(stderr,"GetWindowsDirectory (%s)\n",lpszSysPath);
257 #endif
259 ChopOffSlash(lpszSysPath);
260 return(strlen(lpszSysPath));
262 /***************************************************************************
263 GetSystemDirectory
264 ***************************************************************************/
265 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
267 if (cbSysPath < strlen(SystemDirectory))
268 *lpszSysPath = 0;
269 else
270 strcpy(lpszSysPath, SystemDirectory);
272 #ifdef DEBUG_FILE
273 fprintf(stderr,"GetSystemDirectory (%s)\n",lpszSysPath);
274 #endif
276 ChopOffSlash(lpszSysPath);
277 return(strlen(lpszSysPath));
279 /***************************************************************************
280 GetTempFileName
281 ***************************************************************************/
282 int GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
284 int unique;
285 char tempname[256];
287 if (uUnique == 0)
288 unique = time(NULL)%99999L;
289 else
290 unique = uUnique%99999L;
292 strcpy(tempname,lpszPrefixString);
293 tempname[3]='\0';
295 sprintf(lpszTempFileName,"%s\\%s%d.tmp",WindowsDirectory, tempname,
296 unique);
298 ToDos(lpszTempFileName);
300 #ifdef DEBUG_FILE
301 fprintf(stderr,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
302 lpszPrefixString,uUnique,lpszTempFileName);
303 #endif
305 return unique;
308 /***************************************************************************
309 SetErrorMode
310 ***************************************************************************/
311 WORD SetErrorMode(WORD x)
313 fprintf(stderr,"wine: SetErrorMode %4x (ignored)\n",x);
316 /***************************************************************************
317 _hread
318 ***************************************************************************/
319 long _hread(int hf, void FAR *hpvBuffer, long cbBuffer)
321 long dataread = 0;
322 size_t status, size;
324 while (cbBuffer)
326 size = cbBuffer < 30000 ? cbBuffer : 30000;
328 status = read(hf, hpvBuffer, size);
329 if (status == -1)
330 return HFILE_ERROR;
331 if (status == 0)
332 return dataread;
334 dataread += status;
335 hpvBuffer += status;
336 cbBuffer -= status;
338 return dataread;
340 /***************************************************************************
341 _hwrite
342 ***************************************************************************/
343 long _hwrite(int hf, const void FAR *hpvBuffer, long cbBuffer)
345 long datawritten = 0;
346 size_t status, size;
348 while (cbBuffer)
350 size = cbBuffer < 30000 ? cbBuffer : 30000;
352 status = write(hf, hpvBuffer, size);
353 if (status == -1)
354 return HFILE_ERROR;
355 if (status == 0)
356 return datawritten;
358 datawritten += status;
359 hpvBuffer += status;
360 cbBuffer -= status;
362 return datawritten;