* sysdeps/m68k/dl-machine.h (_dl_start_user): Pass correct
[glibc.git] / sysdeps / generic / sysd-stdio.c
blob479f15208ce844762f4c805514b5d10bd0f232bd
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. */
19 #include <errno.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
29 extern __io_read_fn __stdio_read;
30 extern __io_write_fn __stdio_write;
31 extern __io_seek_fn __stdio_seek;
32 extern __io_close_fn __stdio_close;
33 extern __io_fileno_fn __stdio_fileno;
35 /* Read N bytes into BUF from COOKIE. */
36 int
37 __stdio_read (void *cookie, char *buf, size_t n)
39 const int fd = (int) cookie;
40 #if defined EINTR && defined EINTR_REPEAT
41 int save = errno;
42 int nread;
44 try:;
45 __set_errno (0);
46 nread = __read (fd, buf, (int) n);
47 if (nread < 0)
49 if (errno == EINTR)
50 goto try;
51 return -1;
53 __set_errno (save);
54 return nread;
56 #else /* No EINTR. */
57 return __read (fd, buf, n);
58 #endif
62 /* Write N bytes from BUF to COOKIE. */
63 int
64 __stdio_write (void *cookie, const char *buf, size_t n)
66 const int fd = (int) cookie;
67 register size_t written = 0;
69 while (n > 0)
71 int count = __write (fd, buf, (int) n);
72 if (count > 0)
74 buf += count;
75 written += count;
76 n -= count;
78 else if (count < 0
79 #if defined EINTR && defined EINTR_REPEAT
80 && errno != EINTR
81 #endif
83 /* Write error. */
84 return -1;
87 return (int) written;
91 /* Move COOKIE's file position *POS bytes, according to WHENCE.
92 The new file position is stored in *POS.
93 Returns zero if successful, nonzero if not. */
94 int
95 __stdio_seek (void *cookie, fpos_t *pos, int whence)
97 off_t new;
98 new = __lseek ((int) cookie, (off_t) *pos, whence);
99 if (new < 0)
100 return 1;
101 *pos = (fpos_t) new;
102 return 0;
106 /* Close COOKIE. */
108 __stdio_close (void *cookie)
110 return __close ((int) cookie);
113 /* Return the POSIX.1 file descriptor associated with COOKIE,
114 or -1 for errors. If COOKIE does not relate to any POSIX.1 file
115 descriptor, this should return -1 with errno set to EOPNOTSUPP. */
117 __stdio_fileno (void *cookie)
119 return (int) cookie;
123 /* Open the given file with the mode given in the __io_mode argument. */
125 __stdio_open (const char *filename, __io_mode m, void **cookieptr)
127 int fd;
128 int mode;
130 if (m.__read && m.__write)
131 mode = O_RDWR;
132 else
133 mode = m.__read ? O_RDONLY : O_WRONLY;
135 if (m.__append)
136 mode |= O_APPEND;
137 if (m.__exclusive)
138 mode |= O_EXCL;
139 if (m.__truncate)
140 mode |= O_TRUNC;
142 if (m.__create)
143 fd = __open (filename, mode | O_CREAT,
144 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
145 else
146 fd = __open (filename, mode);
148 if (fd < 0)
149 return -1;
151 *cookieptr = (void *) fd;
152 return 0;
156 /* Open FILENAME with the mode in M. Use the same magic cookie
157 already in *COOKIEPTR if possible, closing the old cookie with CLOSEFN. */
159 __stdio_reopen (const char *filename, __io_mode m, void **cookieptr,
160 __io_close_fn closefn)
162 void *newcookie;
164 /* We leave the old descriptor open while we open the file.
165 That way ``freopen ("/dev/stdin", "r", stdin)'' works. */
167 if (__stdio_open (filename, m, &newcookie))
169 if (errno == ENFILE || errno == EMFILE)
171 /* We are out of file descriptors. Try closing the old one and
172 retrying the open. */
173 (void) (*closefn) (*cookieptr);
174 if (__stdio_open (filename, m, &newcookie))
175 return -1;
177 else
178 return -1;
181 if (newcookie != *cookieptr)
183 if (closefn != __stdio_close ||
184 /* Try to move the descriptor to the desired one. */
185 __dup2 ((int) newcookie, (int) *cookieptr) < 0)
186 /* Didn't work. Give the caller the new cookie. */
187 *cookieptr = newcookie;
190 return 0;