Rev 4874 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4874 | Rev 4921 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* Reentrant versions of read system call. */ |
1 | /* Reentrant versions of read system call. */ |
Line 2... | Line 2... | ||
2 | 2 | ||
3 | #include |
3 | #include |
4 | #include |
4 | #include |
5 | #include <_syslist.h> |
- | |
Line 6... | Line 5... | ||
6 | #include |
5 | #include <_syslist.h> |
7 | 6 | ||
Line 8... | Line 7... | ||
8 | /* Some targets provides their own versions of this functions. Those |
7 | /* Some targets provides their own versions of this functions. Those |
Line 15... | Line 14... | ||
15 | #endif |
14 | #endif |
Line 16... | Line 15... | ||
16 | 15 | ||
Line 17... | Line 16... | ||
17 | #ifndef REENTRANT_SYSCALLS_PROVIDED |
16 | #ifndef REENTRANT_SYSCALLS_PROVIDED |
- | 17 | ||
- | 18 | /* We use the errno variable used by the system dependent layer. */ |
|
Line 18... | Line 19... | ||
18 | 19 | #undef errno |
|
19 | /* We use the errno variable used by the system dependent layer. */ |
20 | extern int errno; |
20 | 21 | ||
Line 42... | Line 43... | ||
42 | This is a reentrant version of < |
43 | This is a reentrant version of < |
43 | takes a pointer to the global data block, which holds |
44 | takes a pointer to the global data block, which holds |
44 | < |
45 | < |
45 | */ |
46 | */ |
Line 46... | Line -... | ||
46 | - | ||
47 | - | ||
48 | extern unsigned __NFiles; |
- | |
49 | - | ||
50 | #define _READ 0x0001 /* file opened for reading */ |
- | |
51 | #define _BINARY 0x0040 /* file is binary, skip CRLF processing */ |
- | |
52 | #define _ISTTY 0x2000 /* is console device */ |
- | |
53 | - | ||
54 | #define __handle_check( __h, __r ) \ |
- | |
55 | if( (__h) < 0 || (__h) > __NFiles ) { \ |
- | |
56 | ptr->_errno = EBADF; \ |
- | |
57 | return( __r ); \ |
- | |
58 | } |
- | |
59 | - | ||
60 | _ssize_t |
- | |
61 | _DEFUN (_read, (fd, buf, cnt), |
- | |
62 | int fd _AND |
- | |
63 | _PTR buf _AND |
- | |
64 | size_t cnt) |
- | |
65 | { |
- | |
66 | - | ||
67 | return _read_r( _REENT, fd, buf, cnt); |
- | |
68 | } |
- | |
69 | 47 | ||
70 | _ssize_t |
48 | _ssize_t |
71 | _DEFUN (_read_r, (ptr, fd, buf, cnt), |
49 | _DEFUN (_read_r, (ptr, fd, buf, cnt), |
72 | struct _reent *ptr _AND |
50 | struct _reent *ptr _AND |
73 | int fd _AND |
51 | int fd _AND |
74 | _PTR buf _AND |
52 | _PTR buf _AND |
75 | size_t cnt) |
53 | size_t cnt) |
76 | { |
54 | { |
Line 77... | Line -... | ||
77 | _ssize_t ret; |
- | |
78 | - | ||
79 | _ssize_t read_len, total_len; |
- | |
80 | unsigned reduce_idx, finish_idx; |
- | |
81 | unsigned iomode_flags; |
- | |
82 | char *buffer = buf; |
- | |
83 | int rc; |
- | |
84 | int h; |
55 | _ssize_t ret; |
85 | unsigned amount_read; |
- | |
86 | int err; |
- | |
87 | - | ||
88 | __file_handle *fh; |
- | |
89 | - | ||
90 | __handle_check( fd, -1 ); |
56 | |
91 | __ChkTTYIOMode( fd ); |
- | |
92 | iomode_flags = __GetIOMode( fd ); |
- | |
93 | if( iomode_flags == 0 ) |
57 | errno = 0; |
94 | { |
- | |
95 | ptr->_errno = EBADF; |
- | |
96 | return( -1 ); |
- | |
97 | } |
- | |
98 | if( !(iomode_flags & _READ) ) |
- | |
99 | { |
58 | if ((ret = (_ssize_t)_read (fd, buf, cnt)) == -1 && errno != 0) |
100 | ptr->_errno = EACCES; /* changed from EBADF to EACCES 23-feb-89 */ |
- | |
101 | return( -1 ); |
- | |
102 | } |
- | |
103 | - | ||
104 | fh = (__file_handle*) __getOSHandle( fd ); |
- | |
105 | - | ||
106 | if( iomode_flags & _BINARY ) /* if binary mode */ |
- | |
107 | { |
- | |
108 | err = read_file(fh->name, buffer, fh->offset, cnt, &amount_read); |
- | |
109 | fh->offset+= amount_read; |
- | |
110 | total_len = amount_read; |
- | |
111 | - | ||
112 | if(err) |
- | |
113 | if ( amount_read == 0) |
59 | ptr->_errno = errno; |
114 | return (-1); |
- | |
115 | } |
- | |
116 | else |
- | |
117 | { |
- | |
118 | total_len = 0; |
- | |
119 | read_len = cnt; |
- | |
120 | do |
- | |
121 | { |
- | |
122 | err=read_file(fh->name,buffer, fh->offset, cnt, &amount_read); |
- | |
123 | fh->offset+=amount_read; |
- | |
124 | - | ||
125 | if( amount_read == 0 ) |
- | |
126 | break; /* EOF */ |
- | |
127 | - | ||
128 | reduce_idx = 0; |
- | |
129 | finish_idx = reduce_idx; |
- | |
130 | for( ; reduce_idx < amount_read; ++reduce_idx ) |
- | |
131 | { |
- | |
132 | if( buffer[ reduce_idx ] == 0x1a ) /* EOF */ |
- | |
133 | { |
- | |
134 | _lseek_r(ptr, fd, ((long)reduce_idx - (long)amount_read)+1L, |
- | |
135 | SEEK_CUR ); |
- | |
136 | total_len += finish_idx; |
- | |
137 | return( total_len ); |
- | |
138 | } |
- | |
139 | if( buffer[ reduce_idx ] != '\r' ) |
- | |
140 | { |
- | |
141 | buffer[ finish_idx++ ] = buffer[ reduce_idx ]; |
- | |
142 | }; |
- | |
143 | } |
- | |
144 | - | ||
145 | total_len += finish_idx; |
- | |
146 | buffer += finish_idx; |
- | |
147 | read_len -= finish_idx; |
- | |
148 | if( iomode_flags & _ISTTY ) |
- | |
149 | { |
- | |
150 | break; /* 04-feb-88, FWC */ |
- | |
151 | } |
- | |
152 | } while( read_len != 0 ); |
- | |
153 | } |
- | |
154 | return( total_len ); |
- | |
155 | } |
- | |
156 | - | ||
157 | - | ||
158 | _ssize_t |
- | |
159 | _DEFUN (read, (fd, buf, cnt), |
- | |
160 | int fd _AND |
- | |
161 | _PTR buf _AND |
- | |
162 | size_t cnt) |
- | |
163 | { |
- | |
164 | - | ||
Line 165... | Line 60... | ||
165 | return _read_r(_REENT, fd, buf, cnt); |
60 | return ret; |