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@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.
16 ************************************************************************/
34 char WindowsDirectory[256], SystemDirectory[256], TempDirectory[256];
36 /***************************************************************************
39 Emulate the _lopen windows call
40 ***************************************************************************/
41 INT _lopen (LPSTR lpPathName, INT iReadWrite)
47 dprintf_file(stddeb, "_lopen: open('%s', %X);\n", lpPathName, iReadWrite);
48 if ((UnixFileName = DOS_GetUnixFileName(lpPathName)) == NULL)
50 switch(iReadWrite & 3)
52 case OF_READ: mode = O_RDONLY; break;
53 case OF_WRITE: mode = O_WRONLY; break;
54 case OF_READWRITE: mode = O_RDWR; break;
56 handle = open( UnixFileName, mode );
57 if (( handle == -1 ) && Options.allowReadOnly)
58 handle = open( UnixFileName, O_RDONLY );
60 dprintf_file(stddeb, "_lopen: open: %s (handle %d)\n", UnixFileName, handle);
68 /***************************************************************************
70 ***************************************************************************/
71 INT _lread (INT hFile, LPSTR lpBuffer, WORD wBytes)
75 dprintf_file(stddeb, "_lread: handle %d, buffer = %p, length = %d\n",
76 hFile, lpBuffer, wBytes);
78 result = (wBytes == 0) ? 0 : read (hFile, lpBuffer, wBytes);
86 /****************************************************************************
88 ****************************************************************************/
89 INT _lwrite (INT hFile, LPCSTR lpBuffer, WORD wBytes)
93 dprintf_file(stddeb, "_lwrite: handle %d, buffer = %p, length = %d\n",
94 hFile, lpBuffer, wBytes);
96 if(wBytes == 0) { /* Expand the file size if necessary */
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;
105 lseek(hFile, prev-1, SEEK_SET);
106 result = write(hFile, &toWrite, 1) - 1;
107 if(result == -2) ++result;
110 lseek(hFile, prev, SEEK_SET);
114 else result = write (hFile, lpBuffer, wBytes);
122 /***************************************************************************
124 ***************************************************************************/
125 INT _lclose (INT hFile)
127 dprintf_file(stddeb, "_lclose: handle %d\n", hFile);
129 if (hFile == 0 || hFile == 1 || hFile == 2) {
130 fprintf( stderr, "Program attempted to close stdin, stdout or stderr!\n" );
139 /**************************************************************************
141 **************************************************************************/
142 INT OpenFile (LPSTR lpFileName, LPOFSTRUCT ofs, WORD wStyle)
144 char filename[MAX_PATH+1];
151 dprintf_file(stddeb,"Openfile(%s,<struct>,%d)\n",lpFileName,wStyle);
153 action = wStyle & 0xff00;
156 /* OF_CREATE is completly different from all other options, so
159 if (action & OF_CREATE)
163 if (!(action & OF_REOPEN)) strcpy(ofs->szPathName, lpFileName);
164 ofs->cBytes = sizeof(OFSTRUCT);
165 ofs->fFixedDisk = FALSE;
167 *((int*)ofs->reserved) = 0;
169 if ((unixfilename = DOS_GetUnixFileName (ofs->szPathName)) == NULL)
172 ofs->nErrCode = ExtendedError;
175 handle = open (unixfilename, (wStyle & 0x0003) | O_CREAT, 0666);
179 ofs->nErrCode = ExtendedError;
185 /* If path isn't given, try to find the file. */
187 if (!(action & OF_REOPEN))
189 char temp[MAX_PATH + 1];
191 if(index(lpFileName,'\\') || index(lpFileName,'/') ||
192 index(lpFileName,':'))
194 strcpy (filename,lpFileName);
197 /* Try current directory */
198 if (DOS_FindFile(temp, MAX_PATH, lpFileName, NULL, ".")) {
199 strcpy(filename, DOS_GetDosFileName(temp));
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));
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));
217 /* Try the path of the current executable */
218 if (GetCurrentTask())
221 GetModuleFileName( GetCurrentTask(), filename, MAX_PATH );
222 if ((p = strrchr( filename, '\\' )))
225 if (DOS_FindFile(temp, MAX_PATH, lpFileName, NULL, filename)) {
226 strcpy(filename, DOS_GetDosFileName(temp));
232 /* Try all directories in path */
234 if (DOS_FindFile(temp,MAX_PATH,lpFileName,NULL,WindowsPath))
236 strcpy(filename, DOS_GetDosFileName(temp));
239 /* ??? shouldn't we give an error here? */
240 strcpy (filename, lpFileName);
244 ofs->cBytes = sizeof(OFSTRUCT);
245 ofs->fFixedDisk = FALSE;
246 strcpy(ofs->szPathName, filename);
248 if (!(action & OF_VERIFY))
249 *((int*)ofs->reserved) = 0;
253 if (action & OF_PARSE)
256 if (action & OF_DELETE)
257 return unlink(ofs->szPathName);
260 /* Now on to getting some information about that file */
262 if ((res = stat(DOS_GetUnixFileName(ofs->szPathName), &s)))
265 ofs->nErrCode = ExtendedError;
269 now = localtime (&s.st_mtime);
271 if (action & OF_VERIFY)
272 verify_time = *((int*)ofs->reserved);
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);
280 if (action & OF_VERIFY)
281 return (verify_time != *((int*)ofs->reserved));
283 if (action & OF_EXIST)
286 if ((handle = _lopen( ofs->szPathName, wStyle )) == -1)
288 ofs->nErrCode = 2; /* not found */
295 /**************************************************************************
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 **************************************************************************/
302 #if !defined (OPEN_MAX)
303 /* This one is for the Sun */
304 #define OPEN_MAX _POSIX_OPEN_MAX
306 WORD SetHandleCount (WORD wNumber)
308 dprintf_file(stddeb,"SetHandleCount(%d)\n",wNumber);
309 return((wNumber<OPEN_MAX) ? wNumber : OPEN_MAX);
312 /***************************************************************************
314 ***************************************************************************/
315 LONG _llseek (INT hFile, LONG lOffset, INT nOrigin)
319 dprintf_file(stddeb, "_llseek: handle %d, offset %ld, origin %d\n",
320 hFile, lOffset, nOrigin);
323 case 1: origin = SEEK_CUR;
325 case 2: origin = SEEK_END;
327 default: origin = SEEK_SET;
331 return lseek(hFile, lOffset, origin);
334 /***************************************************************************
336 ***************************************************************************/
337 INT _lcreat (LPSTR lpszFilename, INT fnAttribute)
342 dprintf_file(stddeb, "_lcreat: filename %s, attributes %d\n",
343 lpszFilename, fnAttribute);
344 if ((UnixFileName = DOS_GetUnixFileName(lpszFilename)) == NULL)
346 handle = open (UnixFileName, O_CREAT | O_TRUNC | O_RDWR, 0666);
354 /***************************************************************************
356 ***************************************************************************/
357 UINT GetDriveType(INT drive)
360 dprintf_file(stddeb,"GetDriveType %c:\n",'A'+drive);
362 if (!DOS_ValidDrive(drive))
363 return DRIVE_DOESNOTEXIST;
365 if (drive == 0 || drive == 1)
366 return DRIVE_REMOVABLE;
371 /***************************************************************************
373 ***************************************************************************/
374 BYTE GetTempDrive(BYTE chDriveLetter)
376 dprintf_file(stddeb,"GetTempDrive (%d)\n",chDriveLetter);
377 if (TempDirectory[1] == ':') return TempDirectory[0];
381 /***************************************************************************
383 ***************************************************************************/
384 UINT GetWindowsDirectory(LPSTR lpszSysPath, UINT cbSysPath)
386 if (cbSysPath < strlen(WindowsDirectory))
389 strcpy(lpszSysPath, WindowsDirectory);
391 dprintf_file(stddeb,"GetWindowsDirectory (%s)\n",lpszSysPath);
393 return strlen(lpszSysPath);
395 /***************************************************************************
397 ***************************************************************************/
398 UINT GetSystemDirectory(LPSTR lpszSysPath, UINT cbSysPath)
400 if (cbSysPath < strlen(SystemDirectory))
403 strcpy(lpszSysPath, SystemDirectory);
405 dprintf_file(stddeb,"GetSystemDirectory (%s)\n",lpszSysPath);
407 return strlen(lpszSysPath);
409 /***************************************************************************
411 ***************************************************************************/
412 INT GetTempFileName(BYTE bDriveLetter, LPCSTR lpszPrefixString, UINT uUnique, LPSTR lpszTempFileName)
419 unique = time(NULL)%99999L;
421 unique = uUnique%99999L;
423 strcpy(tempname,lpszPrefixString);
426 sprintf(lpszTempFileName,"%s\\%s%d.tmp", TempDirectory, tempname,
429 ToDos(lpszTempFileName);
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);
442 /***************************************************************************
444 ***************************************************************************/
445 WORD SetErrorMode(WORD x)
447 dprintf_file(stdnimp,"wine: SetErrorMode %4x (ignored)\n",x);
452 /***************************************************************************
454 ***************************************************************************/
455 LONG _hread(INT hf, LPSTR hpvBuffer, LONG cbBuffer)
457 return (cbBuffer == 0) ? 0 : read(hf, hpvBuffer, cbBuffer);
459 /***************************************************************************
461 ***************************************************************************/
462 LONG _hwrite(INT hf, LPCSTR hpvBuffer, LONG cbBuffer)
464 return (cbBuffer == 0) ? 0 : write(hf, hpvBuffer, cbBuffer);