Added very useful and convenient macro for parsing attribute IDs
[AROS.git] / compiler / clib / fdopen.c
blob84b6d4d47d959c93b5655a9bea4e3fc9ae779898
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function fdopen().
6 */
8 #include "__arosc_privdata.h"
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <errno.h>
14 #include <exec/lists.h>
15 #include <proto/exec.h>
17 #include "__stdio.h"
18 #include "__fdesc.h"
20 /*****************************************************************************
22 NAME */
23 #include <stdio.h>
25 FILE *fdopen (
27 /* SYNOPSIS */
28 int filedes,
29 const char *mode
32 /* FUNCTION
33 function associates a stream with an existing file descriptor.
35 INPUTS
36 filedes - The descriptor the stream has to be associated with
37 mode - The mode of the stream (same as with fopen()) must be com­
38 patible with the mode of the file descriptor. The file
39 position indicator of the new stream is set to that
40 belonging to filedes, and the error and end-of-file indica­
41 tors are cleared. Modes "w" or "w+" do not cause trunca­
42 tion of the file. The file descriptor is not dup'ed, and
43 will be closed when the stream created by fdopen is
44 closed.
46 RESULT
47 NULL on error or the new stream associated with the descriptor.
49 The new descriptor returned by the call is the lowest numbered
50 descriptor currently not in use by the process.
52 NOTES
53 This function must not be used in a shared library or
54 in a threaded application.
56 EXAMPLE
58 BUGS
60 SEE ALSO
61 open(), fclose(), fileno()
63 INTERNALS
65 ******************************************************************************/
67 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
68 int oflags, wanted_accmode, current_accmode;
69 fdesc *fdesc;
70 FILENODE *fn;
72 if (!(fdesc = __getfdesc(filedes)))
74 errno = EBADF;
75 return NULL;
78 oflags = fdesc->fcb->flags;
80 if (mode)
82 oflags = __smode2oflags(mode);
84 wanted_accmode = oflags & O_ACCMODE;
85 current_accmode = fdesc->fcb->flags & O_ACCMODE;
87 /*
88 Check if the requested access mode flags are a valid subset of the
89 flags the already open file has. Thus, if the file's access mode
90 is O_RDWR the requested mode can be anything (O_RDONLY, O_WRONLY or
91 O_RDWR), else they must match exactly.
94 if ((current_accmode != O_RDWR) && (wanted_accmode != current_accmode))
96 errno = EINVAL;
97 return NULL;
101 fn = AllocPooled(aroscbase->acb_internalpool, sizeof(FILENODE));
102 if (!fn) return NULL;
104 AddTail ((struct List *)&aroscbase->acb_stdio_files, (struct Node *)fn);
106 fn->File.flags = __oflags2sflags(oflags);
107 fn->File.fd = filedes;
109 return FILENODE2FILE(fn);