1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
29 /* Read N bytes into BUF from COOKIE. */
31 __stdio_read (void *cookie
, char *buf
, size_t n
)
33 const int fd
= (int) cookie
;
34 #if defined EINTR && defined EINTR_REPEAT
40 nread
= __read (fd
, buf
, (int) n
);
51 return __read (fd
, buf
, n
);
56 /* Write N bytes from BUF to COOKIE. */
58 __stdio_write (void *cookie
, const char *buf
, size_t n
)
60 const int fd
= (int) cookie
;
61 register size_t written
= 0;
65 int count
= __write (fd
, buf
, (int) n
);
73 #if defined EINTR && defined EINTR_REPEAT
85 /* Move COOKIE's file position *POS bytes, according to WHENCE.
86 The new file position is stored in *POS.
87 Returns zero if successful, nonzero if not. */
89 __stdio_seek (void *cookie
, fpos_t *pos
, int whence
)
92 new = __lseek ((int) cookie
, (off_t
) *pos
, whence
);
102 __stdio_close (void *cookie
)
104 return __close ((int) cookie
);
107 /* Return the POSIX.1 file descriptor associated with COOKIE,
108 or -1 for errors. If COOKIE does not relate to any POSIX.1 file
109 descriptor, this should return -1 with errno set to EOPNOTSUPP. */
111 __stdio_fileno (void *cookie
)
117 /* Open the given file with the mode given in the __io_mode argument. */
119 __stdio_open (const char *filename
, __io_mode m
, void **cookieptr
)
124 if (m
.__read
&& m
.__write
)
127 mode
= m
.__read
? O_RDONLY
: O_WRONLY
;
137 fd
= __open (filename
, mode
| O_CREAT
,
138 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
);
140 fd
= __open (filename
, mode
);
145 *cookieptr
= (void *) fd
;
150 /* Open FILENAME with the mode in M. Use the same magic cookie
151 already in *COOKIEPTR if possible, closing the old cookie with CLOSEFN. */
153 __stdio_reopen (const char *filename
, __io_mode m
, void **cookieptr
,
154 __io_close_fn closefn
)
158 /* We leave the old descriptor open while we open the file.
159 That way ``freopen ("/dev/stdin", "r", stdin)'' works. */
161 if (__stdio_open (filename
, m
, &newcookie
))
163 if (errno
== ENFILE
|| errno
== EMFILE
)
165 /* We are out of file descriptors. Try closing the old one and
166 retrying the open. */
167 (void) (*closefn
) (*cookieptr
);
168 if (__stdio_open (filename
, m
, &newcookie
))
175 if (newcookie
!= *cookieptr
)
177 if (closefn
!= __stdio_close
||
178 /* Try to move the descriptor to the desired one. */
179 __dup2 ((int) newcookie
, (int) *cookieptr
) < 0)
180 /* Didn't work. Give the caller the new cookie. */
181 *cookieptr
= newcookie
;