Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / fdopen.c
blob3481154d15a8a41097325304f7829e30872d67bc
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 FUNCTION
20 <<fdopen>>---turn open file into a stream
22 INDEX
23 fdopen
24 INDEX
25 _fdopen_r
27 SYNOPSIS
28 #include <stdio.h>
29 FILE *fdopen(int <[fd]>, const char *<[mode]>);
30 FILE *_fdopen_r(struct _reent *<[reent]>,
31 int <[fd]>, const char *<[mode]>);
33 DESCRIPTION
34 <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
35 descriptor for an already-open file (returned, for example, by the
36 system subroutine <<open>> rather than by <<fopen>>).
37 The <[mode]> argument has the same meanings as in <<fopen>>.
39 RETURNS
40 File pointer or <<NULL>>, as for <<fopen>>.
42 PORTABILITY
43 <<fdopen>> is ANSI.
46 #include <_ansi.h>
47 #include <reent.h>
48 #include <sys/types.h>
49 #include <sys/fcntl.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include "local.h"
53 #include <_syslist.h>
55 FILE *
56 _fdopen_r (struct _reent *ptr,
57 int fd,
58 const char *mode)
60 register FILE *fp;
61 int flags, oflags;
62 #ifdef HAVE_FCNTL
63 int fdflags, fdmode;
64 #endif
66 if ((flags = __sflags (ptr, mode, &oflags)) == 0)
67 return 0;
69 /* make sure the mode the user wants is a subset of the actual mode */
70 #ifdef HAVE_FCNTL
71 if ((fdflags = _fcntl_r (ptr, fd, F_GETFL, 0)) < 0)
72 return 0;
73 fdmode = fdflags & O_ACCMODE;
74 if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
76 _REENT_ERRNO(ptr) = EBADF;
77 return 0;
79 #endif
81 if ((fp = __sfp (ptr)) == 0)
82 return 0;
84 _newlib_flockfile_start (fp);
86 fp->_flags = flags;
87 /* POSIX recommends setting the O_APPEND bit on fd to match append
88 streams. Someone may later clear O_APPEND on fileno(fp), but the
89 stream must still remain in append mode. Rely on __sflags
90 setting __SAPP properly. */
91 #ifdef HAVE_FCNTL
92 if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
93 _fcntl_r (ptr, fd, F_SETFL, fdflags | O_APPEND);
94 #endif
95 fp->_file = fd;
96 fp->_cookie = (void *) fp;
98 #undef _read
99 #undef _write
100 #undef _seek
101 #undef _close
103 fp->_read = __sread;
104 fp->_write = __swrite;
105 fp->_seek = __sseek;
106 fp->_close = __sclose;
108 #ifdef __SCLE
109 /* Explicit given mode results in explicit setting mode on fd */
110 if (oflags & O_BINARY)
111 setmode (fp->_file, O_BINARY);
112 else if (oflags & O_TEXT)
113 setmode (fp->_file, O_TEXT);
114 if (__stextmode (fp->_file))
115 fp->_flags |= __SCLE;
116 #endif
118 _newlib_flockfile_end (fp);
119 return fp;
122 #ifndef _REENT_ONLY
124 FILE *
125 fdopen (int fd,
126 const char *mode)
128 return _fdopen_r (_REENT, fd, mode);
131 #endif