2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 POSIX function fdopen().
8 #include "__arosc_privdata.h"
14 #include <exec/lists.h>
15 #include <proto/exec.h>
20 /*****************************************************************************
33 function associates a stream with an existing file descriptor.
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 fildes, 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
47 NULL on error or the new stream assiciated with the descriptor.
49 The new descriptor returned by the call is the lowest numbered
50 descriptor currently not in use by the process.
53 This function must not be used in a shared library or
54 in a threaded application.
61 open(), fclose(), fileno()
65 ******************************************************************************/
67 int oflags
, wanted_accmode
, current_accmode
;
73 if (!(fdesc
= __getfdesc(filedes
)))
79 oflags
= fdesc
->flags
;
83 oflags
= __smode2oflags(mode
);
85 wanted_accmode
= oflags
& O_ACCMODE
;
86 current_accmode
= fdesc
->flags
& O_ACCMODE
;
89 Check if the requested access mode flags are a valid subset of the
90 flags of the already open file has. Thus, if the files access mode
91 is O_RDWR the requested mode can be anything (O_RDONLY, O_WRONLY or
92 O_RDWR), else they must match exactly.
95 if ((current_accmode
!= O_RDWR
) && (wanted_accmode
!= current_accmode
))
102 fn
= malloc(sizeof(FILENODE
));
103 if (!fn
) return NULL
;
105 AddTail ((struct List
*)&__stdio_files
, (struct Node
*)fn
);
107 fn
->File
.flags
= __oflags2sflags(oflags
);
108 fn
->File
.fd
= filedes
;
110 return FILENODE2FILE(fn
);