Update.
[glibc/history.git] / sysdeps / generic / sysd-stdio.c
blobf5147bb3aabb9546981eaa9490ff13ff799ea495
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 /* Read N bytes into BUF from COOKIE. */
30 int
31 __stdio_read (cookie, buf, n)
32 void *cookie;
33 register char *buf;
34 register size_t n;
36 const int fd = (int) cookie;
37 #if defined (EINTR) && defined (EINTR_REPEAT)
38 int save = errno;
39 int nread;
41 try:;
42 __set_errno (0);
43 nread = __read (fd, buf, (int) n);
44 if (nread < 0)
46 if (errno == EINTR)
47 goto try;
48 return -1;
50 __set_errno (save);
51 return nread;
53 #else /* No EINTR. */
54 return __read (fd, buf, n);
55 #endif
59 /* Write N bytes from BUF to COOKIE. */
60 int
61 __stdio_write (cookie, buf, n)
62 void *cookie;
63 register const char *buf;
64 register 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 (cookie, pos, whence)
96 void *cookie;
97 fpos_t *pos;
98 int whence;
100 off_t new;
101 new = __lseek ((int) cookie, (off_t) *pos, whence);
102 if (new < 0)
103 return 1;
104 *pos = (fpos_t) new;
105 return 0;
109 /* Close COOKIE. */
111 __stdio_close (cookie)
112 void *cookie;
114 return __close ((int) cookie);
117 /* Return the POSIX.1 file descriptor associated with COOKIE,
118 or -1 for errors. If COOKIE does not relate to any POSIX.1 file
119 descriptor, this should return -1 with errno set to EOPNOTSUPP. */
121 __stdio_fileno (cookie)
122 void *cookie;
124 return (int) cookie;
128 /* Open the given file with the mode given in the __io_mode argument. */
130 __stdio_open (filename, m, cookieptr)
131 const char *filename;
132 __io_mode m;
133 void **cookieptr;
135 int fd;
136 int mode;
138 if (m.__read && m.__write)
139 mode = O_RDWR;
140 else
141 mode = m.__read ? O_RDONLY : O_WRONLY;
143 if (m.__append)
144 mode |= O_APPEND;
145 if (m.__exclusive)
146 mode |= O_EXCL;
147 if (m.__truncate)
148 mode |= O_TRUNC;
150 if (m.__create)
151 fd = __open (filename, mode | O_CREAT,
152 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
153 else
154 fd = __open (filename, mode);
156 if (fd < 0)
157 return -1;
159 *cookieptr = (void *) fd;
160 return 0;
164 /* Open FILENAME with the mode in M. Use the same magic cookie
165 already in *COOKIEPTR if possible, closing the old cookie with CLOSEFN. */
167 __stdio_reopen (filename, m, cookieptr, closefn)
168 const char *filename;
169 __io_mode m;
170 void **cookieptr;
171 __io_close_fn closefn;
173 void *newcookie;
175 /* We leave the old descriptor open while we open the file.
176 That way ``freopen ("/dev/stdin", "r", stdin)'' works. */
178 if (__stdio_open (filename, m, &newcookie))
180 if (errno == ENFILE || errno == EMFILE)
182 /* We are out of file descriptors. Try closing the old one and
183 retrying the open. */
184 (void) (*closefn) (*cookieptr);
185 if (__stdio_open (filename, m, &newcookie))
186 return -1;
188 else
189 return -1;
192 if (newcookie != *cookieptr)
194 if (closefn != __stdio_close ||
195 /* Try to move the descriptor to the desired one. */
196 __dup2 ((int) newcookie, (int) *cookieptr) < 0)
197 /* Didn't work. Give the caller the new cookie. */
198 *cookieptr = newcookie;
201 return 0;