2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
7 #include "dos_intern.h"
9 #include <aros/debug.h>
12 /*****************************************************************************
15 #include <proto/dos.h>
17 AROS_LH4(LONG
, FWrite
,
18 /* FWrite -- Writes a number of blocks to an output (buffered) */
21 AROS_LHA(BPTR
, fh
, D1
),
22 AROS_LHA(CONST_APTR
, block
, D2
),
23 AROS_LHA(ULONG
, blocklen
, D3
),
24 AROS_LHA(ULONG
, numblocks
, D4
),
27 struct DosLibrary
*, DOSBase
, 55, Dos
)
30 Write a number of blocks to a file.
33 fh - Write to this file
34 block - The data begins here
35 blocklen - number of bytes per block. Must be > 0.
36 numblocks - number of blocks to write. Must be > 0.
39 The number of blocks written to the file or EOF on error. IoErr()
40 gives additional information in case of an error.
43 Open(), FRead(), FPutc(), Close()
45 *****************************************************************************/
50 ASSERT_VALID_PTR(block
);
52 ASSERT(numblocks
> 0);
62 for(written
= 0; written
< numblocks
; written
++)
64 if (FWriteChars(fh
, ptr
, blocklen
, DOSBase
) != blocklen
)
81 FWriteChars(BPTR file
, CONST UBYTE
* buffer
, ULONG length
, struct DosLibrary
*DOSBase
)
83 ASSERT_VALID_PTR(BADDR(file
));
84 ASSERT_VALID_PTR(buffer
);
86 /* Get pointer to filehandle. */
87 struct FileHandle
*fh
= (struct FileHandle
*)BADDR(file
);
89 /* Check if file is in write mode */
90 if (!(fh
->fh_Flags
& FHF_WRITE
))
92 if (fh
->fh_Pos
< fh
->fh_End
)
94 /* Read mode. Try to seek back to the current position. */
95 if (Seek(file
, fh
->fh_Pos
- fh
->fh_End
, OFFSET_CURRENT
) < 0)
97 fh
->fh_Pos
= fh
->fh_End
= fh
->fh_Buf
;
103 /* Is there a buffer? */
104 if (fh
->fh_Buf
== NULL
)
106 if (vbuf_alloc(fh
, IOBUFSIZE
, DOSBase
) == NULL
)
113 fh
->fh_Flags
|= FHF_WRITE
;
115 fh
->fh_Pos
= fh
->fh_Buf
;
116 fh
->fh_End
= fh
->fh_Buf
+ fh
->fh_Size
;
122 if (fh
->fh_Flags
& FHF_NOBUF
)
127 if (fh
->fh_Pos
!= fh
->fh_Buf
)
134 written
= Write(file
, buffer
, length
);
139 for (written
= 0; written
< length
; ++written
)
141 /* Check if there is still some space in the buffer */
142 if (fh
->fh_Pos
>= fh
->fh_End
)
152 *fh
->fh_Pos
++ = buffer
[written
];
154 if (fh
->fh_Flags
& FHF_LINEBUF
155 && (buffer
[written
] == '\n' || buffer
[written
] == '\r'
156 || buffer
[written
] == '\0'))