Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libc/stdio/stdio.c

  • Property svn:executable set to *
1/*-
2 * Copyright (c) 1990, 1993
3 *The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)stdio.c8.1 (Berkeley) 6/4/93";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: src/lib/libc/stdio/stdio.c,v 1.28 2008/05/05 16:14:02 jhb Exp $");
38
39#include "namespace.h"
40#include <errno.h>
41#include <fcntl.h>
42#include <limits.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include "un-namespace.h"
47#include "local.h"
48#include <sys/stat.h>
49
50
51/*
52 * Small standard I/O/seek/close functions.
53 */
54int
55__sread(cookie, buf, n)
56void *cookie;
57char *buf;
58int n;
59{
60FILE *fp = cookie;
61
62return(read(fp->_file, buf, (size_t)n));
63}
64
65int
66__swrite(cookie, buf, n)
67void *cookie;
68char const *buf;
69int n;
70{
71FILE *fp = cookie;
72
73return (write(fp->_file, buf, (size_t)n));
74}
75
76fpos_t
77__sseek(cookie, offset, whence)
78void *cookie;
79fpos_t offset;
80int whence;
81{
82FILE *fp = cookie;
83
84return (lseek(fp->_file, (off_t)offset, whence));
85}
86
87int
88__sclose(cookie)
89void *cookie;
90{
91
92return (close(((FILE *)cookie)->_file));
93}
94
95/*
96 * Higher level wrappers.
97 */
98int
99_sread(fp, buf, n)
100FILE *fp;
101char *buf;
102int n;
103{
104int ret;
105
106ret = (*fp->_read)(fp->_cookie, buf, n);
107if (ret > 0) {
108if (fp->_flags & __SOFF) {
109if (fp->_offset <= OFF_MAX - ret)
110fp->_offset += ret;
111else
112fp->_flags &= ~__SOFF;
113}
114} else if (ret < 0)
115fp->_flags &= ~__SOFF;
116return (ret);
117}
118
119int
120_swrite(fp, buf, n)
121FILE *fp;
122char const *buf;
123int n;
124{
125int ret;
126 int serrno;
127
128if (fp->_flags & __SAPP) {
129 serrno = errno;
130if (_sseek(fp, (fpos_t)0, SEEK_END) == -1 &&
131 (fp->_flags & __SOPT))
132return (-1);
133 errno = serrno;
134
135}
136ret = (*fp->_write)(fp->_cookie, buf, n);
137/* __SOFF removed even on success in case O_APPEND mode is set. */
138if (ret >= 0) {
139if ((fp->_flags & (__SAPP|__SOFF)) == (__SAPP|__SOFF) &&
140 fp->_offset <= OFF_MAX - ret)
141fp->_offset += ret;
142else
143fp->_flags &= ~__SOFF;
144
145} else if (ret < 0)
146fp->_flags &= ~__SOFF;
147return (ret);
148}
149
150fpos_t
151_sseek(fp, offset, whence)
152FILE *fp;
153fpos_t offset;
154int whence;
155{
156fpos_t ret;
157 int serrno, errret;
158
159 serrno = errno;
160errno = 0;
161ret = (*fp->_seek)(fp->_cookie, offset, whence);
162 errret = errno;
163 if (errno == 0)
164errno = serrno ;
165/*
166 * Disallow negative seeks per POSIX.
167 * It is needed here to help upper level caller
168 * in the cases it can't detect.
169 */
170if (ret < 0) {
171if (errret == 0) {
172if (offset != 0 || whence != SEEK_CUR) {
173if (HASUB(fp))
174FREEUB(fp);
175fp->_p = fp->_bf._base;
176fp->_r = 0;
177fp->_flags &= ~__SEOF;
178}
179fp->_flags |= __SERR;
180 errno = EINVAL;
181} else if (errret == ESPIPE)
182fp->_flags &= ~__SAPP;
183fp->_flags &= ~__SOFF;
184ret = -1;
185} else if (fp->_flags & __SOPT) {
186fp->_flags |= __SOFF;
187fp->_offset = ret;
188}
189return (ret);
190}
191
192/* WARNING : This implementation of fstat(2) , currently only give the file size */
193int
194fstat(int fildes, struct stat *buf)
195{
196extern int file_size(int fdesc);
197
198 struct stat *st =(struct stat *)buf;
199
200 st->st_size = file_size(fildes);
201
202 return 0;
203}

Archive Download this file

Revision: 2182