Fixed out-by-one error in previous commit.
[AROS.git] / workbench / c / Unpack / file.c
blob675fbed6ff5abaf48e818e96709fe8255b7e6297
1 /*
2 Copyright © 2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <dos/dos.h>
9 #include "modes.h"
10 #include "file.h"
11 #include "gui.h"
13 LONG file_size = 0;
15 BPTR FILE_Open( CONST_STRPTR path, LONG mode )
17 LONG mode2dos[] = { MODE_OLDFILE, MODE_NEWFILE };
18 BPTR file;
20 if( mode != MODE_READ && mode != MODE_WRITE );
22 file = Open( path, mode2dos[mode] );
23 if( file == BNULL ) goto error;
25 Seek( file, 0, OFFSET_END );
26 file_size = Seek( file, 0, OFFSET_BEGINNING ) + 1;
28 return file;
30 error:
31 return BNULL;
34 LONG FILE_Read( BPTR file, APTR buffer, LONG length )
36 LONG read = 0,
37 left = length;
39 while( read < length )
41 LONG actual = Read( file, buffer, left );
42 if( actual == -1 ) return -1;
44 read += actual;
45 buffer += actual;
46 left -= actual;
48 GUI_Update( Seek( file, 0, OFFSET_CURRENT ), file_size );
50 if( actual == 0 ) break;
53 return read;
56 LONG FILE_Write( BPTR file, CONST_APTR buffer, LONG length )
58 LONG written = 0,
59 left = length;
61 while( written < length )
63 LONG actual = Write( file, buffer, left );
64 if( actual == -1 ) return -1;
66 written += actual;
67 buffer += actual;
68 left -= actual;
71 return written;