Release 950216
[wine] / misc / file.c
1 /************************************************************************
2  * FILE.C     Copyright (C) 1993 John Burton
3  *
4  * File I/O routines for the Linux Wine Project.
5  *
6  * WARNING : Many options of OpenFile are not yet implemeted.
7  *
8  * NOV 93 Erik Bos (erik@xs4all.nl)
9  *              - removed ParseDosFileName, and DosDrive structures.
10  *              - structures dynamically configured at runtime.
11  *              - _lopen modified to use DOS_GetUnixFileName.
12  *              - Existing functions modified to use dosfs functions.
13  *              - Added _llseek, _lcreat, GetDriveType, GetTempDrive, 
14  *                GetWindowsDirectory, GetSystemDirectory, GetTempFileName.
15  *
16  ************************************************************************/
17
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <unistd.h>
22 #include <time.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include "dos_fs.h"
26 #include "regfunc.h"
27 #include "windows.h"
28 #include "wine.h"
29 #include "msdos.h"
30 #include "registers.h"
31 #include "options.h"
32 #include "stddebug.h"
33 #include "debug.h"
34
35 #define MAX_PATH 255
36
37 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
38
39 /***************************************************************************
40  _lopen 
41
42  Emulate the _lopen windows call
43  ***************************************************************************/
44 INT _lopen (LPSTR lpPathName, INT iReadWrite)
45 {
46   int  handle;
47   char *UnixFileName;
48
49   dprintf_file(stddeb, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
50   if ((UnixFileName = DOS_GetUnixFileName(lpPathName)) == NULL)
51         return HFILE_ERROR;
52   iReadWrite &= 0x000F;
53   handle =  open (UnixFileName, iReadWrite);
54
55   dprintf_file(stddeb, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
56
57   if (handle == -1)
58         return HFILE_ERROR;
59   else
60         return handle;
61 }
62
63 /***************************************************************************
64  _lread
65  ***************************************************************************/
66 INT _lread (INT hFile, LPSTR lpBuffer, WORD wBytes)
67 {
68   int result;
69
70   dprintf_file(stddeb, "_lread: handle %d, buffer = %ld, length = %d\n",
71                         hFile, (long) lpBuffer, wBytes);
72   
73   result = read (hFile, lpBuffer, wBytes);
74
75   if (result == -1)
76         return HFILE_ERROR;
77   else
78         return result;
79 }
80
81 /****************************************************************************
82  _lwrite
83 ****************************************************************************/
84 INT _lwrite (INT hFile, LPSTR lpBuffer, WORD wBytes)
85 {
86         int result;
87
88   dprintf_file(stddeb, "_lwrite: handle %d, buffer = %ld, length = %d\n",
89                         hFile, (long) lpBuffer, wBytes);
90
91     result = write (hFile, lpBuffer, wBytes);
92
93         if (result == -1)
94                 return HFILE_ERROR;
95         else
96                 return result;
97 }
98
99 /***************************************************************************
100  _lclose
101  ***************************************************************************/
102 INT _lclose (INT hFile)
103 {
104         dprintf_file(stddeb, "_lclose: handle %d\n", hFile);
105         if (close (hFile))
106                 return HFILE_ERROR;
107         else
108                 return 0;
109 }
110
111 /**************************************************************************
112  OpenFile
113  **************************************************************************/
114 INT OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
115 {
116 #ifdef WINELIB
117     dprintf_file(stdnimp, "OpenFile: not implemented\n");
118 #else
119 #ifndef PROCEMU
120     struct sigcontext_struct  ccontext;
121                               /* To make macros like EAX happy */
122     struct sigcontext_struct *context=&ccontext; 
123 #endif
124     char                      filename[MAX_PATH+1];
125     int                       action;
126     struct stat               s;
127     struct tm                 *now;
128     int                       res;
129     int                       verify_time;
130   
131     dprintf_file(stddeb,"Openfile(%s,<struct>,%d) ",lpFileName,wStyle);
132   
133     action = wStyle & 0xff00;
134   
135   
136     /* OF_CREATE is completly different from all other options, so
137        handle it first */
138
139     if (action & OF_CREATE)
140       {
141       int handle;
142       char *unixfilename;
143
144       if (!(action & OF_REOPEN))
145         strcpy(ofs->szPathName, lpFileName);
146       ofs->cBytes = sizeof(OFSTRUCT);
147       ofs->fFixedDisk = FALSE;
148       ofs->nErrCode = 0;
149       *((int*)ofs->reserved) = 0;
150
151       if ((unixfilename = DOS_GetUnixFileName (ofs->szPathName)) == NULL)
152       {
153         errno_to_doserr();
154         ofs->nErrCode = ExtendedError;
155         return -1;
156       }
157       handle = open (unixfilename, (wStyle & 0x0003) | O_CREAT, 0x666);
158       if (handle == -1)
159       {
160         errno_to_doserr();
161         ofs->nErrCode = ExtendedError;
162       }   
163       return handle;
164       }
165
166
167     /* If path isn't given, try to find the file. */
168
169     if (!(action & OF_REOPEN))
170       {
171         if( !( index(lpFileName,'\\') || index(lpFileName,'/') || 
172               index(lpFileName,':')))
173         while(1)
174         {
175           char temp[MAX_PATH+1];
176           strcpy (filename, lpFileName);
177           if ( (!stat(DOS_GetUnixFileName(filename), &s)) && (S_ISREG(s.st_mode)) )
178             break;
179           GetWindowsDirectory (filename,MAX_PATH);
180           if ((!filename[0])||(filename[strlen(filename)-1]!='\\'))
181             strcat(filename, "\\");
182           strcat (filename, lpFileName);
183           if ( (!stat(DOS_GetUnixFileName(filename), &s)) && (S_ISREG(s.st_mode)) )
184             break;
185           GetSystemDirectory (filename,MAX_PATH);
186           if ((!filename[0])||(filename[strlen(filename)-1]!='\\'))
187             strcat(filename, "\\");
188           strcat (filename, lpFileName);
189           if ( (!stat(DOS_GetUnixFileName(filename), &s)) && (S_ISREG(s.st_mode)) )
190             break;
191           if (!DOS_FindFile(temp,MAX_PATH,lpFileName,NULL,WindowsPath))
192             {
193               strcpy(filename, DOS_GetDosFileName(temp));
194               break;
195             }
196           strcpy (filename, lpFileName);
197           break;
198         }
199         else
200           strcpy (filename,lpFileName);
201
202         ofs->cBytes = sizeof(OFSTRUCT);
203         ofs->fFixedDisk = FALSE;
204         strcpy(ofs->szPathName, filename);
205         ofs->nErrCode = 0;
206         if (!(action & OF_VERIFY))
207           *((int*)ofs->reserved) = 0;
208     }
209     
210
211     if (action & OF_PARSE)
212       return 0;
213
214     if (action & OF_DELETE)
215       return unlink(ofs->szPathName);
216
217
218     /* Now on to getting some information about that file */
219
220     if ((res = stat(DOS_GetUnixFileName(ofs->szPathName), &s)))
221       {
222       errno_to_doserr();
223       ofs->nErrCode = ExtendedError;
224       return -1;
225     }
226     
227     now = localtime (&s.st_mtime);
228
229     if (action & OF_VERIFY)
230       verify_time = *((int*)ofs->reserved);
231     
232     *((WORD*)(&ofs->reserved[2]))=
233          ((now->tm_hour * 0x2000) + (now->tm_min * 0x20) + (now->tm_sec / 2));
234     *((WORD*)(&ofs->reserved[0]))=
235          ((now->tm_year * 0x200) + (now->tm_mon * 0x20) + now->tm_mday);
236
237
238     if (action & OF_VERIFY)
239       return (verify_time != *((int*)ofs->reserved));
240
241     if (action & OF_EXIST)
242       return 0;
243     
244    /* Now we are actually going to open the file. According to Microsoft's
245        Knowledge Basis, this is done by calling int 21h, ax=3dh. */    
246
247     AX = 0x3d00;
248     AL = (AL & 0x0f) | (wStyle & 0x70); /* Handle OF_SHARE_xxx etc. */
249     AL = (AL & 0xf0) | (wStyle & 0x03); /* Handle OF_READ etc. */
250     DS = segment (ofs->szPathName);
251     DX = offset (ofs->szPathName);
252   
253     OpenExistingFile (context);
254
255     if (EFL & 0x00000001)     /* Cflag */
256     {
257       ofs->nErrCode = AL;
258       return -1;
259       }
260
261     return AX;
262 #endif /*WINELIB*/
263 }
264
265 /**************************************************************************
266  SetHandleCount
267
268  Changes the number of file handles available to the application.  Since
269  Linux isn't limited to 20 files, this one's easy. - SL
270  **************************************************************************/
271
272 #if !defined (OPEN_MAX)
273 /* This one is for the Sun */
274 #define OPEN_MAX _POSIX_OPEN_MAX
275 #endif
276 WORD SetHandleCount (WORD wNumber)
277 {
278   dprintf_file(stddeb,"SetHandleCount(%d)\n",wNumber);
279   return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
280 }
281
282 /***************************************************************************
283  _llseek
284  ***************************************************************************/
285 LONG _llseek (INT hFile, LONG lOffset, INT nOrigin)
286 {
287         int origin;
288         
289         dprintf_file(stddeb, "_llseek: handle %d, offset %ld, origin %d\n", 
290                 hFile, lOffset, nOrigin);
291
292         switch (nOrigin) {
293                 case 1: origin = SEEK_CUR;
294                         break;
295                 case 2: origin = SEEK_END;
296                         break;
297                 default: origin = SEEK_SET;
298                         break;
299                 }
300
301         return ( lseek(hFile, lOffset, origin) );
302 }
303
304 /***************************************************************************
305  _lcreat
306  ***************************************************************************/
307 INT _lcreat (LPSTR lpszFilename, INT fnAttribute)
308 {
309         int handle;
310         char *UnixFileName;
311
312         dprintf_file(stddeb, "_lcreat: filename %s, attributes %d\n",
313                 lpszFilename, fnAttribute);
314         if ((UnixFileName = DOS_GetUnixFileName(lpszFilename)) == NULL)
315                 return HFILE_ERROR;
316         handle =  open (UnixFileName, O_CREAT | O_TRUNC | O_WRONLY, 426);
317
318         if (handle == -1)
319                 return HFILE_ERROR;
320         else
321                 return handle;
322 }
323
324 /***************************************************************************
325  GetDriveType
326  ***************************************************************************/
327 UINT GetDriveType(INT drive)
328 {
329
330         dprintf_file(stddeb,"GetDriveType %c:\n",'A'+drive);
331
332         if (!DOS_ValidDrive(drive))
333                 return DRIVE_DOESNOTEXIST;
334
335         if (drive == 0 || drive == 1)
336                 return DRIVE_REMOVABLE;
337                  
338         return DRIVE_FIXED;
339 }
340
341 /***************************************************************************
342  GetTempDrive
343  ***************************************************************************/
344 BYTE GetTempDrive(BYTE chDriveLetter)
345 {
346     dprintf_file(stddeb,"GetTempDrive (%d)\n",chDriveLetter);
347         return('C');
348 }
349
350 /***************************************************************************
351  GetWindowsDirectory
352  ***************************************************************************/
353 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
354 {
355         if (cbSysPath < strlen(WindowsDirectory)) 
356                 *lpszSysPath = 0;
357         else
358                 strcpy(lpszSysPath, WindowsDirectory);
359         
360         dprintf_file(stddeb,"GetWindowsDirectory (%s)\n",lpszSysPath);
361
362         ChopOffSlash(lpszSysPath);
363         return(strlen(lpszSysPath));
364 }
365 /***************************************************************************
366  GetSystemDirectory
367  ***************************************************************************/
368 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
369 {
370         if (cbSysPath < strlen(SystemDirectory))
371                 *lpszSysPath = 0;
372         else
373                 strcpy(lpszSysPath, SystemDirectory);
374
375         dprintf_file(stddeb,"GetSystemDirectory (%s)\n",lpszSysPath);
376
377         ChopOffSlash(lpszSysPath);
378         return(strlen(lpszSysPath));
379 }
380 /***************************************************************************
381  GetTempFileName
382  ***************************************************************************/
383 INT GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
384 {
385         int unique;
386         int handle;
387         char tempname[256];
388         
389         if (uUnique == 0)
390                 unique = time(NULL)%99999L;
391         else
392                 unique = uUnique%99999L;
393
394         strcpy(tempname,lpszPrefixString);
395         tempname[3]='\0';
396
397         sprintf(lpszTempFileName,"%s\\%s%d.tmp", TempDirectory, tempname, 
398                 unique);
399
400         ToDos(lpszTempFileName);
401
402         dprintf_file(stddeb,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
403                 lpszPrefixString,uUnique,lpszTempFileName);
404         if ((handle = _lcreat (lpszTempFileName, 0x0000)) == -1) {
405                 fprintf(stderr,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName);
406                 }
407         else
408                 close(handle);
409
410         return unique;
411 }
412
413 /***************************************************************************
414  SetErrorMode
415  ***************************************************************************/
416 WORD SetErrorMode(WORD x)
417 {
418     dprintf_file(stdnimp,"wine: SetErrorMode %4x (ignored)\n",x);
419
420     return 1;
421 }
422
423 /***************************************************************************
424  _hread
425  ***************************************************************************/
426 long _hread(int hf, void FAR *hpvBuffer, long cbBuffer)
427 {
428         return read(hf, hpvBuffer, cbBuffer);
429 }
430 /***************************************************************************
431  _hwrite
432  ***************************************************************************/
433 long _hwrite(int hf, const void FAR *hpvBuffer, long cbBuffer)
434 {
435         return write(hf, hpvBuffer, cbBuffer);
436 }