Release 950901
[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 "windows.h"
27 #include "msdos.h"
28 #include "options.h"
29 #include "stddebug.h"
30 #include "debug.h"
31
32 #define MAX_PATH 255
33
34 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
35
36 /***************************************************************************
37  _lopen 
38
39  Emulate the _lopen windows call
40  ***************************************************************************/
41 INT _lopen (LPSTR lpPathName, INT iReadWrite)
42 {
43   int  handle;
44   char *UnixFileName;
45   int mode = 0;
46
47   dprintf_file(stddeb, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
48   if ((UnixFileName = DOS_GetUnixFileName(lpPathName)) == NULL)
49         return HFILE_ERROR;
50   switch(iReadWrite & 3)
51   {
52   case OF_READ:      mode = O_RDONLY; break;
53   case OF_WRITE:     mode = O_WRONLY; break;
54   case OF_READWRITE: mode = O_RDWR; break;
55   }
56   handle = open( UnixFileName, mode );
57   if (( handle == -1 ) && Options.allowReadOnly)
58     handle = open( UnixFileName, O_RDONLY );
59
60   dprintf_file(stddeb, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
61
62   if (handle == -1)
63         return HFILE_ERROR;
64   else
65         return handle;
66 }
67
68 /***************************************************************************
69  _lread
70  ***************************************************************************/
71 INT _lread (INT hFile, LPSTR lpBuffer, WORD wBytes)
72 {
73   int result;
74
75   dprintf_file(stddeb, "_lread: handle %d, buffer = %p, length = %d\n",
76                         hFile, lpBuffer, wBytes);
77   
78   result = (wBytes == 0) ? 0 : read (hFile, lpBuffer, wBytes);
79
80   if (result == -1)
81         return HFILE_ERROR;
82   else
83         return result;
84 }
85
86 /****************************************************************************
87  _lwrite
88 ****************************************************************************/
89 INT _lwrite (INT hFile, LPCSTR lpBuffer, WORD wBytes)
90 {
91     int result;
92
93     dprintf_file(stddeb, "_lwrite: handle %d, buffer = %p, length = %d\n",
94                  hFile, lpBuffer, wBytes);
95
96     if(wBytes == 0) {  /* Expand the file size if necessary */
97         char toWrite = 0;
98         off_t prev, end;
99         
100         prev = lseek(hFile, 0, SEEK_CUR);
101         if(prev == -1) return HFILE_ERROR;
102         end = lseek(hFile, 0, SEEK_END);
103         if(end == -1) return HFILE_ERROR;
104         if(prev > end) {
105             lseek(hFile, prev-1, SEEK_SET);
106             result = write(hFile, &toWrite, 1) - 1;
107             if(result == -2) ++result;
108         }
109         else {
110             lseek(hFile, prev, SEEK_SET);
111             result = 0;
112         }
113     }
114     else result = write (hFile, lpBuffer, wBytes);
115
116     if (result == -1)
117         return HFILE_ERROR;
118     else
119         return result;
120 }
121
122 /***************************************************************************
123  _lclose
124  ***************************************************************************/
125 INT _lclose (INT hFile)
126 {
127     dprintf_file(stddeb, "_lclose: handle %d\n", hFile);
128     
129     if (hFile == 0 || hFile == 1 || hFile == 2) {
130         fprintf( stderr, "Program attempted to close stdin, stdout or stderr!\n" );
131         return 0;
132     }
133     if (close (hFile))
134         return HFILE_ERROR;
135     else
136         return 0;
137 }
138
139 /**************************************************************************
140  OpenFile
141  **************************************************************************/
142 INT OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
143 {
144     char         filename[MAX_PATH+1];
145     int          action;
146     struct stat  s;
147     struct tm   *now;
148     int          res, handle;
149     int          verify_time = 0;
150   
151     dprintf_file(stddeb,"Openfile(%s,<struct>,%d)\n",lpFileName,wStyle);
152   
153     action = wStyle & 0xff00;
154   
155   
156     /* OF_CREATE is completly different from all other options, so
157        handle it first */
158
159     if (action & OF_CREATE)
160     {
161         char *unixfilename;
162
163         if (!(action & OF_REOPEN)) strcpy(ofs->szPathName, lpFileName);
164         ofs->cBytes = sizeof(OFSTRUCT);
165         ofs->fFixedDisk = FALSE;
166         ofs->nErrCode = 0;
167         *((int*)ofs->reserved) = 0;
168
169         if ((unixfilename = DOS_GetUnixFileName (ofs->szPathName)) == NULL)
170         {
171             errno_to_doserr();
172             ofs->nErrCode = ExtendedError;
173             return -1;
174         }
175         handle = open (unixfilename, (wStyle & 0x0003) | O_CREAT, 0666);
176         if (handle == -1)
177         {
178             errno_to_doserr();
179             ofs->nErrCode = ExtendedError;
180         }   
181         return handle;
182     }
183
184
185     /* If path isn't given, try to find the file. */
186
187     if (!(action & OF_REOPEN))
188     {
189         char temp[MAX_PATH + 1];
190         
191         if(index(lpFileName,'\\') || index(lpFileName,'/') || 
192            index(lpFileName,':')) 
193         {
194             strcpy (filename,lpFileName);
195             goto found;
196         }
197         /* Try current directory */
198         if (DOS_FindFile(temp, MAX_PATH, lpFileName, NULL, ".")) {
199             strcpy(filename, DOS_GetDosFileName(temp));
200             goto found;
201         }
202
203         /* Try Windows directory */
204         GetWindowsDirectory(filename, MAX_PATH);
205         if (DOS_FindFile(temp, MAX_PATH, lpFileName, NULL, filename)) {
206             strcpy(filename, DOS_GetDosFileName(temp));
207             goto found;
208         }
209         
210         /* Try Windows system directory */
211         GetSystemDirectory(filename, MAX_PATH);
212         if (DOS_FindFile(temp, MAX_PATH, lpFileName, NULL, filename)) {
213             strcpy(filename, DOS_GetDosFileName(temp));
214             goto found;
215         }
216
217         /* Try the path of the current executable */
218         if (GetCurrentTask())
219         {
220             char *p;
221             GetModuleFileName( GetCurrentTask(), filename, MAX_PATH );
222             if ((p = strrchr( filename, '\\' )))
223             {
224                 p[1] = '\0';
225                 if (DOS_FindFile(temp, MAX_PATH, lpFileName, NULL, filename)) {
226                     strcpy(filename, DOS_GetDosFileName(temp));
227                     goto found;
228                 }
229             }
230         }
231
232         /* Try all directories in path */
233
234         if (DOS_FindFile(temp,MAX_PATH,lpFileName,NULL,WindowsPath))
235         {
236             strcpy(filename, DOS_GetDosFileName(temp));
237             goto found;
238         }
239         /* ??? shouldn't we give an error here? */
240         strcpy (filename, lpFileName);
241         
242         found:
243         
244         ofs->cBytes = sizeof(OFSTRUCT);
245         ofs->fFixedDisk = FALSE;
246         strcpy(ofs->szPathName, filename);
247         ofs->nErrCode = 0;
248         if (!(action & OF_VERIFY))
249           *((int*)ofs->reserved) = 0;
250     }
251     
252
253     if (action & OF_PARSE)
254       return 0;
255
256     if (action & OF_DELETE)
257       return unlink(ofs->szPathName);
258
259
260     /* Now on to getting some information about that file */
261
262     if ((res = stat(DOS_GetUnixFileName(ofs->szPathName), &s)))
263       {
264       errno_to_doserr();
265       ofs->nErrCode = ExtendedError;
266       return -1;
267     }
268     
269     now = localtime (&s.st_mtime);
270
271     if (action & OF_VERIFY)
272       verify_time = *((int*)ofs->reserved);
273     
274     *((WORD*)(&ofs->reserved[2]))=
275          ((now->tm_hour * 0x2000) + (now->tm_min * 0x20) + (now->tm_sec / 2));
276     *((WORD*)(&ofs->reserved[0]))=
277          ((now->tm_year * 0x200) + (now->tm_mon * 0x20) + now->tm_mday);
278
279
280     if (action & OF_VERIFY)
281       return (verify_time != *((int*)ofs->reserved));
282
283     if (action & OF_EXIST)
284       return 0;
285     
286     if ((handle = _lopen( ofs->szPathName, wStyle )) == -1)
287     {
288         ofs->nErrCode = 2;  /* not found */
289         return -1;
290     }
291     return handle;
292 }
293
294
295 /**************************************************************************
296  SetHandleCount
297
298  Changes the number of file handles available to the application.  Since
299  Linux isn't limited to 20 files, this one's easy. - SL
300  **************************************************************************/
301
302 #if !defined (OPEN_MAX)
303 /* This one is for the Sun */
304 #define OPEN_MAX _POSIX_OPEN_MAX
305 #endif
306 WORD SetHandleCount (WORD wNumber)
307 {
308   dprintf_file(stddeb,"SetHandleCount(%d)\n",wNumber);
309   return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
310 }
311
312 /***************************************************************************
313  _llseek
314  ***************************************************************************/
315 LONG _llseek (INT hFile, LONG lOffset, INT nOrigin)
316 {
317         int origin;
318         
319         dprintf_file(stddeb, "_llseek: handle %d, offset %ld, origin %d\n", 
320                 hFile, lOffset, nOrigin);
321
322         switch (nOrigin) {
323                 case 1: origin = SEEK_CUR;
324                         break;
325                 case 2: origin = SEEK_END;
326                         break;
327                 default: origin = SEEK_SET;
328                         break;
329         }
330
331         return lseek(hFile, lOffset, origin);
332 }
333
334 /***************************************************************************
335  _lcreat
336  ***************************************************************************/
337 INT _lcreat (LPSTR lpszFilename, INT fnAttribute)
338 {
339         int handle;
340         char *UnixFileName;
341
342         dprintf_file(stddeb, "_lcreat: filename %s, attributes %d\n",
343                 lpszFilename, fnAttribute);
344         if ((UnixFileName = DOS_GetUnixFileName(lpszFilename)) == NULL)
345                 return HFILE_ERROR;
346         handle =  open (UnixFileName, O_CREAT | O_TRUNC | O_RDWR, 0666);
347
348         if (handle == -1)
349                 return HFILE_ERROR;
350         else
351                 return handle;
352 }
353
354 /***************************************************************************
355  GetDriveType
356  ***************************************************************************/
357 UINT GetDriveType(INT drive)
358 {
359
360         dprintf_file(stddeb,"GetDriveType %c:\n",'A'+drive);
361
362         if (!DOS_ValidDrive(drive))
363                 return DRIVE_DOESNOTEXIST;
364
365         if (drive == 0 || drive == 1)
366                 return DRIVE_REMOVABLE;
367                  
368         return DRIVE_FIXED;
369 }
370
371 /***************************************************************************
372  GetTempDrive
373  ***************************************************************************/
374 BYTE GetTempDrive(BYTE chDriveLetter)
375 {
376     dprintf_file(stddeb,"GetTempDrive (%d)\n",chDriveLetter);
377     if (TempDirectory[1] == ':') return TempDirectory[0];
378     else return 'C';
379 }
380
381 /***************************************************************************
382  GetWindowsDirectory
383  ***************************************************************************/
384 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
385 {
386         if (cbSysPath < strlen(WindowsDirectory)) 
387                 *lpszSysPath = 0;
388         else
389                 strcpy(lpszSysPath, WindowsDirectory);
390         
391         dprintf_file(stddeb,"GetWindowsDirectory (%s)\n",lpszSysPath);
392
393         return strlen(lpszSysPath);
394 }
395 /***************************************************************************
396  GetSystemDirectory
397  ***************************************************************************/
398 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
399 {
400         if (cbSysPath < strlen(SystemDirectory))
401                 *lpszSysPath = 0;
402         else
403                 strcpy(lpszSysPath, SystemDirectory);
404
405         dprintf_file(stddeb,"GetSystemDirectory (%s)\n",lpszSysPath);
406
407         return strlen(lpszSysPath);
408 }
409 /***************************************************************************
410  GetTempFileName
411  ***************************************************************************/
412 INT GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
413 {
414         int unique;
415         int handle;
416         char tempname[256];
417         
418         if (uUnique == 0)
419                 unique = time(NULL)%99999L;
420         else
421                 unique = uUnique%99999L;
422
423         strcpy(tempname,lpszPrefixString);
424         tempname[3]='\0';
425
426         sprintf(lpszTempFileName,"%s\\%s%d.tmp", TempDirectory, tempname, 
427                 unique);
428
429         ToDos(lpszTempFileName);
430
431         dprintf_file(stddeb,"GetTempFilename: %c %s %d => %s\n",bDriveLetter,
432                 lpszPrefixString,uUnique,lpszTempFileName);
433         if ((handle = _lcreat (lpszTempFileName, 0x0000)) == -1) {
434                 fprintf(stderr,"GetTempFilename: can't create temp file '%s' !\n", lpszTempFileName);
435         }
436         else
437                 close(handle);
438
439         return unique;
440 }
441
442 /***************************************************************************
443  SetErrorMode
444  ***************************************************************************/
445 WORD SetErrorMode(WORD x)
446 {
447     dprintf_file(stdnimp,"wine: SetErrorMode %4x (ignored)\n",x);
448
449     return 1;
450 }
451
452 /***************************************************************************
453  _hread
454  ***************************************************************************/
455 LONG _hread(INT hf, LPSTR hpvBuffer, LONG cbBuffer)
456 {
457     return (cbBuffer == 0) ? 0 : read(hf, hpvBuffer, cbBuffer);
458 }
459 /***************************************************************************
460  _hwrite
461  ***************************************************************************/
462 LONG _hwrite(INT hf, LPCSTR hpvBuffer, LONG cbBuffer)
463 {
464     return (cbBuffer == 0) ? 0 : write(hf, hpvBuffer, cbBuffer);
465 }