Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / stdio.c
blob6347949db2481cf4058af38c0864effa27310b3e
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 /* No user fns here. Pesch 15apr92. */
19 #include <_ansi.h>
20 #include <reent.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <sys/unistd.h>
25 #include "local.h"
28 * Small standard I/O/seek/close functions.
29 * These maintain the `known seek offset' for seek optimisation.
32 _READ_WRITE_RETURN_TYPE
33 __sread (struct _reent *ptr,
34 void *cookie,
35 char *buf,
36 _READ_WRITE_BUFSIZE_TYPE n)
38 register FILE *fp = (FILE *) cookie;
39 register ssize_t ret;
41 #ifdef __SCLE
42 int oldmode = 0;
43 if (fp->_flags & __SCLE)
44 oldmode = setmode (fp->_file, O_BINARY);
45 #endif
47 ret = _read_r (ptr, fp->_file, buf, n);
49 #ifdef __SCLE
50 if (oldmode)
51 setmode (fp->_file, oldmode);
52 #endif
54 /* If the read succeeded, update the current offset. */
56 if (ret >= 0)
57 fp->_offset += ret;
58 else
59 fp->_flags &= ~__SOFF; /* paranoia */
60 return ret;
63 /* Dummy function used in sscanf/swscanf. */
64 _READ_WRITE_RETURN_TYPE
65 __seofread (struct _reent *_ptr,
66 void *cookie,
67 char *buf,
68 _READ_WRITE_BUFSIZE_TYPE len)
70 return 0;
73 _READ_WRITE_RETURN_TYPE
74 __swrite (struct _reent *ptr,
75 void *cookie,
76 char const *buf,
77 _READ_WRITE_BUFSIZE_TYPE n)
79 register FILE *fp = (FILE *) cookie;
80 ssize_t w;
81 #ifdef __SCLE
82 int oldmode=0;
83 #endif
85 if (fp->_flags & __SAPP)
86 _lseek_r (ptr, fp->_file, (_off_t) 0, SEEK_END);
87 fp->_flags &= ~__SOFF; /* in case O_APPEND mode is set */
89 #ifdef __SCLE
90 if (fp->_flags & __SCLE)
91 oldmode = setmode (fp->_file, O_BINARY);
92 #endif
94 w = _write_r (ptr, fp->_file, buf, n);
96 #ifdef __SCLE
97 if (oldmode)
98 setmode (fp->_file, oldmode);
99 #endif
101 return w;
104 _fpos_t
105 __sseek (struct _reent *ptr,
106 void *cookie,
107 _fpos_t offset,
108 int whence)
110 register FILE *fp = (FILE *) cookie;
111 register _off_t ret;
113 ret = _lseek_r (ptr, fp->_file, (_off_t) offset, whence);
114 if (ret == -1L)
115 fp->_flags &= ~__SOFF;
116 else
118 fp->_flags |= __SOFF;
119 fp->_offset = ret;
121 return ret;
125 __sclose (struct _reent *ptr,
126 void *cookie)
128 FILE *fp = (FILE *) cookie;
130 return _close_r (ptr, fp->_file);
133 #ifdef __SCLE
135 __stextmode (int fd)
137 #ifdef __CYGWIN__
138 extern int _cygwin_istext_for_stdio (int);
139 return _cygwin_istext_for_stdio (fd);
140 #else
141 return 0;
142 #endif
144 #endif