Network: Don't require <sys/mbuf.h> for a single struct definitions
[AROS.git] / compiler / stdc / freopen.c
blob95b7fafcf592fd90bb3efa87adaf6aa7175c4378
1 /*
2 Copyright © 2010-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 C99 function freopen().
6 */
7 #include <proto/exec.h>
8 #include <proto/dos.h>
9 #include <stdlib.h>
11 #include "__stdio.h"
13 /*****************************************************************************
15 NAME */
16 #include <stdio.h>
18 FILE *freopen (
20 /* SYNOPSIS */
21 const char * restrict path,
22 const char * restrict mode,
23 FILE * restrict stream
26 /* FUNCTION
27 Opens the file whose name is the string pointed to by path and
28 associates the stream pointed to by stream with it.
30 INPUTS
31 path - the file to open, NULL to only change the mode of the stream.
32 mode - Mode to open file, see fopen for description of the string.
33 When path is NULL end-of-file and error indicator will be
34 cleared and indication if stream is read and/or write.
35 No change to position in file or no truncation will be
36 performed.
37 stream - the stream to wich the file will be associated.
39 RESULT
40 NULL on error or stream. When NULL is returned input stream is
41 not changed.
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
50 fopen(), fclose()
52 INTERNALS
54 ******************************************************************************/
56 if (!stream)
57 return NULL;
59 if (path != NULL)
61 if (!(stream->flags & __STDCIO_STDIO_DONTCLOSE))
62 Close(stream->fh);
63 FILE *stream2 = fopen(path, mode);
64 int dontfree = stream->flags & __STDCIO_STDIO_DONTFREE;
65 *stream = *stream2;
66 stream->flags |= dontfree;
67 Remove((struct Node *)stream2);
68 free(stream2);
70 else
72 char l2, hasplus;
74 l2 = mode[1];
75 if (l2 == 'b') l2 = mode[2];
76 hasplus = (l2 == '+');
78 /* Keep some flags; clear the rest */
79 stream->flags &= ~(__STDCIO_STDIO_TMP | __STDCIO_STDIO_DONTCLOSE | __STDCIO_STDIO_DONTFREE);
81 /* Set mode */
82 if (hasplus)
83 stream->flags |= __STDCIO_STDIO_RDWR;
84 else
85 switch(mode[0])
87 case 'r':
88 stream->flags |= __STDCIO_STDIO_READ;
89 break;
90 case 'w':
91 case 'a':
92 stream->flags |= __STDCIO_STDIO_WRITE;
93 break;
94 default:
95 return NULL;
99 return stream;