Detabbed
[AROS.git] / rom / dos / fwrite.c
blobe27120984b8ab0ee09fc4cac1ec97da2babe7f42
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Lang: english
6 */
7 #include "dos_intern.h"
9 #include <aros/debug.h>
12 /*****************************************************************************
14 NAME */
15 #include <proto/dos.h>
17 AROS_LH4(LONG, FWrite,
18 /* FWrite -- Writes a number of blocks to an output (buffered) */
20 /* SYNOPSIS */
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),
26 /* LOCATION */
27 struct DosLibrary *, DOSBase, 55, Dos)
29 /* FUNCTION
30 Write a number of blocks to a file.
32 INPUTS
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.
38 RESULT
39 The number of blocks written to the file or EOF on error. IoErr()
40 gives additional information in case of an error.
42 SEE ALSO
43 Open(), FRead(), FPutc(), Close()
45 *****************************************************************************/
47 AROS_LIBFUNC_INIT
49 ASSERT_VALID_PTR(BADDR(fh));
50 ASSERT_VALID_PTR(block);
51 ASSERT(blocklen > 0);
52 ASSERT(numblocks > 0);
54 ULONG written;
55 const UBYTE *ptr;
57 ptr = block;
59 SetIoErr(0);
61 for(written = 0; written < numblocks; written++)
63 if (FWriteChars(fh, ptr, blocklen, DOSBase) != blocklen)
65 return(EOF);
67 else
69 ptr += blocklen;
73 return written;
75 AROS_LIBFUNC_EXIT
76 } /* FWrite */
79 LONG
80 FWriteChars(BPTR file, CONST UBYTE* buffer, ULONG length, struct DosLibrary *DOSBase)
82 ASSERT_VALID_PTR(BADDR(file));
83 ASSERT_VALID_PTR(buffer);
85 /* Get pointer to filehandle. */
86 struct FileHandle *fh = (struct FileHandle *)BADDR(file);
88 if (fh == NULL)
89 return EOF;
91 /* Check if file is in write mode */
92 if (!(fh->fh_Flags & FHF_WRITE))
94 if (fh->fh_Pos < fh->fh_End)
96 /* Read mode. Try to seek back to the current position. */
97 if (Seek(file, fh->fh_Pos - fh->fh_End, OFFSET_CURRENT) < 0)
99 fh->fh_Pos = fh->fh_End = 0;
101 return EOF;
105 /* Is there a buffer? */
106 if (fh->fh_Buf == BNULL)
108 if (vbuf_alloc(fh, NULL, IOBUFSIZE) == NULL)
110 return(EOF);
114 /* Prepare buffer */
115 fh->fh_Flags |= FHF_WRITE;
117 fh->fh_Pos = 0;
118 fh->fh_End = fh->fh_BufSize;
121 LONG
122 written = -1;
124 if (fh->fh_Flags & FHF_NOBUF)
126 LONG
127 goOn = TRUE;
129 if (fh->fh_Pos != 0)
131 goOn = Flush(file);
134 if (goOn)
136 written = Write(file, buffer, length);
139 else
141 for (written = 0; written < length; ++written)
143 /* Check if there is still some space in the buffer */
144 if (fh->fh_Pos >= fh->fh_End)
146 if (!Flush(file))
148 written = -1;
149 break;
153 /* Write data */
154 ((UBYTE *)BADDR(fh->fh_Buf))[fh->fh_Pos++] = buffer[written];
156 if (fh->fh_Flags & FHF_LINEBUF
157 && (buffer[written] == '\n' || buffer[written] == '\r'
158 || buffer[written] == '\0'))
160 if (!Flush(file))
162 written = -1;
163 break;
169 return(written);