1 | /* |
---|
2 | * Copyright (c) 1990 The Regents of the University of California. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
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 | * advertising materials, and 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. |
---|
16 | */ |
---|
17 | /* No user fns here. Pesch 15apr92. */ |
---|
18 | |
---|
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" |
---|
26 | |
---|
27 | /* |
---|
28 | * Small standard I/O/seek/close functions. |
---|
29 | * These maintain the `known seek offset' for seek optimisation. |
---|
30 | */ |
---|
31 | |
---|
32 | _READ_WRITE_RETURN_TYPE |
---|
33 | __sread (struct _reent *ptr, |
---|
34 | void *cookie, |
---|
35 | char *buf, |
---|
36 | _READ_WRITE_BUFSIZE_TYPE n) |
---|
37 | { |
---|
38 | register FILE *fp = (FILE *) cookie; |
---|
39 | register ssize_t ret; |
---|
40 | |
---|
41 | #ifdef __SCLE |
---|
42 | int oldmode = 0; |
---|
43 | if (fp->_flags & __SCLE) |
---|
44 | oldmode = setmode (fp->_file, O_BINARY); |
---|
45 | #endif |
---|
46 | |
---|
47 | ret = _read_r (ptr, fp->_file, buf, n); |
---|
48 | |
---|
49 | #ifdef __SCLE |
---|
50 | if (oldmode) |
---|
51 | setmode (fp->_file, oldmode); |
---|
52 | #endif |
---|
53 | |
---|
54 | /* If the read succeeded, update the current offset. */ |
---|
55 | |
---|
56 | if (ret >= 0) |
---|
57 | fp->_offset += ret; |
---|
58 | else |
---|
59 | fp->_flags &= ~__SOFF; /* paranoia */ |
---|
60 | return ret; |
---|
61 | } |
---|
62 | |
---|
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) |
---|
69 | { |
---|
70 | return 0; |
---|
71 | } |
---|
72 | |
---|
73 | _READ_WRITE_RETURN_TYPE |
---|
74 | __swrite (struct _reent *ptr, |
---|
75 | void *cookie, |
---|
76 | char const *buf, |
---|
77 | _READ_WRITE_BUFSIZE_TYPE n) |
---|
78 | { |
---|
79 | register FILE *fp = (FILE *) cookie; |
---|
80 | ssize_t w; |
---|
81 | #ifdef __SCLE |
---|
82 | int oldmode=0; |
---|
83 | #endif |
---|
84 | |
---|
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 */ |
---|
88 | |
---|
89 | #ifdef __SCLE |
---|
90 | if (fp->_flags & __SCLE) |
---|
91 | oldmode = setmode (fp->_file, O_BINARY); |
---|
92 | #endif |
---|
93 | |
---|
94 | w = _write_r (ptr, fp->_file, buf, n); |
---|
95 | |
---|
96 | #ifdef __SCLE |
---|
97 | if (oldmode) |
---|
98 | setmode (fp->_file, oldmode); |
---|
99 | #endif |
---|
100 | |
---|
101 | return w; |
---|
102 | } |
---|
103 | |
---|
104 | _fpos_t |
---|
105 | __sseek (struct _reent *ptr, |
---|
106 | void *cookie, |
---|
107 | _fpos_t offset, |
---|
108 | int whence) |
---|
109 | { |
---|
110 | register FILE *fp = (FILE *) cookie; |
---|
111 | register _off_t ret; |
---|
112 | |
---|
113 | ret = _lseek_r (ptr, fp->_file, (_off_t) offset, whence); |
---|
114 | if (ret == -1L) |
---|
115 | fp->_flags &= ~__SOFF; |
---|
116 | else |
---|
117 | { |
---|
118 | fp->_flags |= __SOFF; |
---|
119 | fp->_offset = ret; |
---|
120 | } |
---|
121 | return ret; |
---|
122 | } |
---|
123 | |
---|
124 | int |
---|
125 | __sclose (struct _reent *ptr, |
---|
126 | void *cookie) |
---|
127 | { |
---|
128 | FILE *fp = (FILE *) cookie; |
---|
129 | |
---|
130 | return _close_r (ptr, fp->_file); |
---|
131 | } |
---|
132 | |
---|
133 | #ifdef __SCLE |
---|
134 | int |
---|
135 | __stextmode (int fd) |
---|
136 | { |
---|
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 |
---|
143 | } |
---|
144 | #endif |
---|