added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / rom / dos / fwrite.c
blob4f4e99520e82b5f934beabc4ece2c8b73a4eb110
1 /*
2 Copyright © 1995-2007, 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(fh);
50 ASSERT_VALID_PTR(block);
51 ASSERT(blocklen > 0);
52 ASSERT(numblocks > 0);
54 ULONG len;
55 UBYTE *ptr;
57 ptr = block;
58 len = 0;
60 SetIoErr(0);
62 ULONG
63 written;
65 for(written = 0; written < numblocks; written++)
67 if (FWriteChars(fh, ptr, blocklen, DOSBase) != blocklen)
69 return(EOF);
71 else
73 ptr += blocklen;
77 return written;
79 AROS_LIBFUNC_EXIT
80 } /* FWrite */
83 LONG
84 FWriteChars(BPTR file, CONST UBYTE* buffer, ULONG length, struct DosLibrary *DOSBase)
86 ASSERT_VALID_PTR(BADDR(file));
87 ASSERT_VALID_PTR(buffer);
89 /* Get pointer to filehandle. */
90 struct FileHandle *fh = (struct FileHandle *)BADDR(file);
92 /* Check if file is in write mode */
93 if (!(fh->fh_Flags & FHF_WRITE))
95 if (fh->fh_Pos < fh->fh_End)
97 /* Read mode. Try to seek back to the current position. */
98 if (Seek(file, fh->fh_Pos - fh->fh_End, OFFSET_CURRENT) < 0)
100 fh->fh_Pos = fh->fh_End = fh->fh_Buf;
102 return EOF;
106 /* Is there a buffer? */
107 if (fh->fh_Buf == NULL)
109 if (NULL == vbuf_alloc(fh, IOBUFSIZE, DOSBase))
111 return(EOF);
115 /* Prepare buffer */
116 fh->fh_Flags |= FHF_WRITE;
118 fh->fh_Pos = fh->fh_Buf;
119 fh->fh_End = fh->fh_Buf + fh->fh_Size;
122 LONG
123 written = -1;
125 if (fh->fh_Flags & FHF_NOBUF)
127 LONG
128 goOn = TRUE;
130 if (fh->fh_Pos != fh->fh_Buf)
132 goOn = Flush(file);
135 if (FALSE != goOn)
137 written = Write(file, buffer, length);
140 else
142 for (written = 0; written < length; ++written)
144 /* Check if there is still some space in the buffer */
145 if (fh->fh_Pos >= fh->fh_End)
147 if (NULL == Flush(file))
149 written = -1;
150 break;
154 /* Write data */
155 *fh->fh_Pos++ = buffer[written];
157 if (fh->fh_Flags & FHF_LINEBUF
158 && (buffer[written] == '\n' || buffer[written] == '\r'
159 || buffer[written] == '\0'))
161 if (NULL == Flush(file))
163 written = -1;
164 break;
170 return(written);