Fixed primary path for mpc sources (and removed some extra whitespace)
[AROS.git] / compiler / posixc / fgets.c
blobcc24297de078a72fb73e68a14bcea5dabe63dfe1
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function fgets().
6 */
8 #include <errno.h>
9 #include <string.h>
10 #include <dos/dos.h>
11 #include <dos/dosextens.h>
12 #include <proto/exec.h>
13 #include <proto/dos.h>
14 #include "__fdesc.h"
15 #include "__stdio.h"
17 /*****************************************************************************
19 NAME */
20 #include <stdio.h>
22 char * fgets (
24 /* SYNOPSIS */
25 char * buffer,
26 int size,
27 FILE * stream)
29 /* FUNCTION
30 Read one line of characters from the stream into the buffer.
31 Reading will stop, when a newline ('\n') is encountered, EOF
32 or when the buffer is full. If a newline is read, then it is
33 put into the buffer. The last character in the buffer is always
34 '\0' (Therefore at most size-1 characters can be read in one go).
36 INPUTS
37 buffer - Write characters into this buffer
38 size - This is the size of the buffer in characters.
39 stream - Read from this stream
41 RESULT
42 buffer or NULL in case of an error or EOF.
44 NOTES
46 EXAMPLE
47 // Read a file line by line
48 char line[256];
50 // Read until EOF
51 while (fgets (line, sizeof (line), fh))
53 // Evaluate the line
56 BUGS
58 SEE ALSO
59 fopen(), gets(), fputs()
61 INTERNALS
63 ******************************************************************************/
65 fdesc *fdesc = __getfdesc(stream->fd);
67 if (!fdesc)
69 errno = EBADF;
70 stream->flags |= __POSIXC_STDIO_ERROR;
72 return NULL;
75 FLUSHONREADCHECK
77 buffer = FGets (fdesc->fcb->handle, buffer, size);
79 if (!buffer)
81 if (IoErr ())
83 errno = __stdc_ioerr2errno(IoErr());
84 stream->flags |= __POSIXC_STDIO_ERROR;
86 else
88 stream->flags |= __POSIXC_STDIO_EOF;
91 else
93 int bsize = strlen(buffer);
94 if ((bsize + 1 < size) && (buffer[bsize - 1] != '\n'))
95 stream->flags |= __POSIXC_STDIO_EOF;
98 return buffer;
99 } /* fgets */