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. */
23 #include <sys/types.h>
28 /* Read N bytes into BUF from COOKIE. */
30 __stdio_read (cookie, buf, n)
35 const int fd = (int) cookie;
36 #if defined (EINTR) && defined (EINTR_REPEAT)
42 nread = __read (fd, buf, (int) n);
53 return __read (fd, buf, n);
58 /* Write N bytes from BUF to COOKIE. */
60 __stdio_write (cookie, buf, n)
62 register const char *buf;
65 const int fd = (int) cookie;
66 register size_t written = 0;
70 int count = __write (fd, buf, (int) n);
78 #if defined (EINTR) && defined (EINTR_REPEAT)
90 /* Move COOKIE's file position *POS bytes, according to WHENCE.
91 The new file position is stored in *POS.
92 Returns zero if successful, nonzero if not. */
94 __stdio_seek (cookie, pos, whence)
100 new = __lseek ((int) cookie, (off_t) *pos, whence);
110 __stdio_close (cookie)
113 return __close ((int) cookie);
116 /* Return the POSIX.1 file descriptor associated with COOKIE,
117 or -1 for errors. If COOKIE does not relate to any POSIX.1 file
118 descriptor, this should return -1 with errno set to EOPNOTSUPP. */
120 __stdio_fileno (cookie)
127 /* Open the given file with the mode given in the __io_mode argument. */
129 __stdio_open (filename, m, cookieptr)
130 const char *filename;
137 if (m.__read && m.__write)
140 mode = m.__read ? O_RDONLY : O_WRONLY;
150 fd = __open (filename, mode | O_CREAT,
151 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
153 fd = __open (filename, mode);
158 *cookieptr = (PTR) fd;
163 /* Open FILENAME with the mode in M. Use the same magic cookie
164 already in *COOKIEPTR if possible, closing the old cookie with CLOSEFN. */
166 __stdio_reopen (filename, m, cookieptr, closefn)
167 const char *filename;
170 __io_close_fn closefn;
174 /* We leave the old descriptor open while we open the file.
175 That way ``freopen ("/dev/stdin", "r", stdin)'' works. */
177 if (__stdio_open (filename, m, &newcookie))
179 if (errno == ENFILE || errno == EMFILE)
181 /* We are out of file descriptors. Try closing the old one and
182 retrying the open. */
183 (void) (*closefn) (*cookieptr);
184 if (__stdio_open (filename, m, &newcookie))
191 if (newcookie != *cookieptr)
193 if (closefn != __stdio_close ||
194 /* Try to move the descriptor to the desired one. */
195 __dup2 ((int) newcookie, (int) *cookieptr) < 0)
196 /* Didn't work. Give the caller the new cookie. */
197 *cookieptr = newcookie;