author | Claus Gittinger <cg@exept.de> |
Thu, 17 Jan 2013 23:37:55 +0100 | |
changeset 14667 | f6a830b971f3 |
parent 7595 | 89c4c7dba914 |
child 15990 | 358873f8b450 |
child 18011 | deb0c3355881 |
permissions | -rw-r--r-- |
7595 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
3 |
All Rights Reserved |
7595 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5409 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
4762 | 15 |
OSFileHandle subclass:#UnixFileDescriptorHandle |
16 |
instanceVariableNames:'' |
|
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'OS-Unix' |
|
20 |
! |
|
21 |
||
22 |
!UnixFileDescriptorHandle primitiveDefinitions! |
|
23 |
%{ |
|
24 |
#include <stdio.h> |
|
5416 | 25 |
#include <errno.h> |
4762 | 26 |
%} |
27 |
||
28 |
! ! |
|
29 |
||
7595 | 30 |
!UnixFileDescriptorHandle class methodsFor:'documentation'! |
31 |
||
32 |
copyright |
|
33 |
" |
|
34 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
35 |
All Rights Reserved |
7595 | 36 |
|
37 |
This software is furnished under a license and may be used |
|
38 |
only in accordance with the terms of that license and with the |
|
39 |
inclusion of the above copyright notice. This software may not |
|
40 |
be provided or otherwise made available to, or used by, any |
|
41 |
other person. No title to or ownership of the software is |
|
42 |
hereby transferred. |
|
43 |
" |
|
44 |
! ! |
|
4762 | 45 |
|
5415 | 46 |
!UnixFileDescriptorHandle methodsFor:'input/output'! |
47 |
||
48 |
readBytes:count into:aByteBuffer startingAt:firstIndex |
|
49 |
"read count bytes into a byte-buffer; |
|
50 |
Return the number of bytes read (negative on error)" |
|
51 |
||
52 |
%{ |
|
53 |
unsigned char *extPtr; |
|
54 |
int nRead = -1; |
|
55 |
INT fd = (INT)(__externalAddressVal(self)); |
|
56 |
INT cnt, offs; |
|
57 |
int nInstBytes, objSize; |
|
5416 | 58 |
|
5415 | 59 |
if (! __bothSmallInteger(count, firstIndex)) { |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
60 |
goto bad; |
5415 | 61 |
} |
62 |
cnt = __smallIntegerVal(count); |
|
63 |
offs = __smallIntegerVal(firstIndex) - 1; |
|
64 |
||
65 |
if (fd < 0) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
66 |
goto bad; |
5415 | 67 |
} |
7198
62450598d208
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
5416
diff
changeset
|
68 |
if (__isExternalBytesLike(aByteBuffer)) { |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
69 |
OBJ sz; |
5415 | 70 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
71 |
nInstBytes = 0; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
72 |
extPtr = (char *)(__externalBytesAddress(aByteBuffer)); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
73 |
if (extPtr == NULL) goto bad; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
74 |
sz = __externalBytesSize(aByteBuffer); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
75 |
if (__isSmallInteger(sz)) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
76 |
objSize = __smallIntegerVal(sz); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
77 |
} else { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
78 |
objSize = -1; /* unknown */ |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
79 |
} |
5415 | 80 |
} else { |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
81 |
OBJ oClass; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
82 |
int nInstVars; |
5415 | 83 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
84 |
oClass = __Class(aByteBuffer); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
85 |
switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
86 |
case BYTEARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
87 |
case WORDARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
88 |
case LONGARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
89 |
case SWORDARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
90 |
case SLONGARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
91 |
case FLOATARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
92 |
case DOUBLEARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
93 |
break; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
94 |
default: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
95 |
goto bad; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
96 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
97 |
extPtr = (char *)0; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
98 |
nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
99 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
100 |
objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes; |
5415 | 101 |
} |
102 |
if ((offs >= 0) |
|
103 |
&& (cnt >= 0) |
|
104 |
&& ((objSize == -1) || (objSize >= (cnt + offs)))) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
105 |
nRead = 0; |
5415 | 106 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
107 |
do { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
108 |
int n; |
5415 | 109 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
110 |
if (extPtr) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
111 |
n = read(fd, extPtr+offs, cnt); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
112 |
} else { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
113 |
char *bp; |
5415 | 114 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
115 |
/* |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
116 |
* on interrupt, anObject may be moved to another location. |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
117 |
* So we recompute the byte-address here. |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
118 |
*/ |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
119 |
bp = __ByteArrayInstPtr(aByteBuffer)->ba_element + nInstBytes; |
5415 | 120 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
121 |
n = read(fd, bp + offs, cnt); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
122 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
123 |
if (n > 0) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
124 |
cnt -= n; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
125 |
offs += n; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
126 |
nRead += n; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
127 |
} else { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
128 |
if (n < 0) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
129 |
if (errno == EINTR) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
130 |
continue; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
131 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
132 |
break; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
133 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
134 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
135 |
} while (cnt > 0); |
5415 | 136 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
137 |
RETURN (__mkSmallInteger(nRead)); |
5415 | 138 |
} |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
139 |
bad: ; |
5415 | 140 |
%}. |
141 |
^ self primitiveFailed |
|
142 |
||
143 |
" |
|
144 |
|h buff n| |
|
145 |
||
146 |
h := self basicNew. |
|
147 |
h setFileDescriptor:0. |
|
148 |
buff := ByteArray new:10. buff inspect. |
|
149 |
n := h readBytes:10 into:buff startingAt:1. |
|
150 |
Transcript show:n; space; showCR:buff. |
|
151 |
" |
|
152 |
! |
|
153 |
||
154 |
writeBytes:count from:aByteBuffer startingAt:firstIndex |
|
155 |
"write count bytes from a byte-buffer; |
|
156 |
Return the number of bytes written (negative on error)" |
|
157 |
||
158 |
%{ |
|
159 |
unsigned char *extPtr; |
|
160 |
int nWritten = -1; |
|
161 |
INT fd = (INT)(__externalAddressVal(self)); |
|
162 |
INT cnt, offs; |
|
163 |
int nInstBytes, objSize; |
|
5416 | 164 |
|
5415 | 165 |
if (! __bothSmallInteger(count, firstIndex)) { |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
166 |
goto bad; |
5415 | 167 |
} |
168 |
cnt = __smallIntegerVal(count); |
|
169 |
offs = __smallIntegerVal(firstIndex) - 1; |
|
170 |
||
171 |
if (fd < 0) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
172 |
goto bad; |
5415 | 173 |
} |
7198
62450598d208
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
5416
diff
changeset
|
174 |
if (__isExternalBytesLike(aByteBuffer)) { |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
175 |
OBJ sz; |
5415 | 176 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
177 |
nInstBytes = 0; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
178 |
extPtr = (char *)(__externalBytesAddress(aByteBuffer)); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
179 |
if (extPtr == NULL) goto bad; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
180 |
sz = __externalBytesSize(aByteBuffer); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
181 |
if (__isSmallInteger(sz)) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
182 |
objSize = __smallIntegerVal(sz); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
183 |
} else { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
184 |
objSize = -1; /* unknown */ |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
185 |
} |
5415 | 186 |
} else { |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
187 |
OBJ oClass; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
188 |
int nInstVars; |
5415 | 189 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
190 |
oClass = __Class(aByteBuffer); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
191 |
switch (__smallIntegerVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
192 |
case BYTEARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
193 |
case WORDARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
194 |
case LONGARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
195 |
case SWORDARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
196 |
case SLONGARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
197 |
case FLOATARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
198 |
case DOUBLEARRAY: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
199 |
break; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
200 |
default: |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
201 |
goto bad; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
202 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
203 |
extPtr = (char *)0; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
204 |
nInstVars = __smallIntegerVal(__ClassInstPtr(oClass)->c_ninstvars); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
205 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
206 |
objSize = __Size(aByteBuffer) - OHDR_SIZE - nInstBytes; |
5415 | 207 |
} |
208 |
if ((offs >= 0) |
|
209 |
&& (cnt >= 0) |
|
210 |
&& ((objSize == -1) || (objSize >= (cnt + offs)))) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
211 |
nWritten = 0; |
5415 | 212 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
213 |
do { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
214 |
int n; |
5415 | 215 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
216 |
if (extPtr) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
217 |
n = write(fd, extPtr+offs, cnt); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
218 |
} else { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
219 |
char *bp; |
5415 | 220 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
221 |
/* |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
222 |
* on interrupt, anObject may be moved to another location. |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
223 |
* So we recompute the byte-address here. |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
224 |
*/ |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
225 |
bp = __ByteArrayInstPtr(aByteBuffer)->ba_element + nInstBytes; |
5415 | 226 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
227 |
n = write(fd, bp + offs, cnt); |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
228 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
229 |
if (n > 0) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
230 |
cnt -= n; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
231 |
offs += n; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
232 |
nWritten += n; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
233 |
} else { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
234 |
if (n < 0) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
235 |
if (errno == EINTR) { |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
236 |
continue; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
237 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
238 |
break; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
239 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
240 |
} |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
241 |
} while (cnt > 0); |
5415 | 242 |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
243 |
RETURN (__mkSmallInteger(nWritten)); |
5415 | 244 |
} |
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
245 |
bad: ; |
5415 | 246 |
%}. |
247 |
^ self primitiveFailed |
|
248 |
||
249 |
" |
|
250 |
|h buff n| |
|
251 |
||
252 |
h := self basicNew. |
|
253 |
h setFileDescriptor:1. |
|
254 |
buff := '12345678901234567890'. |
|
255 |
n := h writeBytes:10 from:buff startingAt:1. |
|
256 |
" |
|
257 |
! ! |
|
258 |
||
7259 | 259 |
!UnixFileDescriptorHandle methodsFor:'private-accessing'! |
5415 | 260 |
|
261 |
setFileDescriptor:anInteger |
|
262 |
||
263 |
%{ |
|
264 |
if (__isSmallInteger(anInteger)) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
265 |
__externalAddressVal(self) = (OBJ)(__smallIntegerVal(anInteger)); |
5415 | 266 |
} |
267 |
%} |
|
268 |
||
269 |
||
270 |
! ! |
|
271 |
||
5413 | 272 |
!UnixFileDescriptorHandle methodsFor:'release'! |
4762 | 273 |
|
5409 | 274 |
closeFile |
275 |
"close the underlying file" |
|
4762 | 276 |
|
277 |
%{ |
|
278 |
INT fd = (INT)(__externalAddressVal(self)); |
|
279 |
||
280 |
if (fd > 0) { |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
281 |
__externalAddressVal(self) = (OBJ)-1; |
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
282 |
close(fd); |
4762 | 283 |
} |
284 |
%}. |
|
285 |
||
286 |
! ! |
|
287 |
||
288 |
!UnixFileDescriptorHandle class methodsFor:'documentation'! |
|
289 |
||
290 |
version |
|
14667
f6a830b971f3
care for externalAddress being NULL before accessing an external buffer's contents
Claus Gittinger <cg@exept.de>
parents:
7595
diff
changeset
|
291 |
^ '$Header: /cvs/stx/stx/libbasic/UnixFileDescriptorHandle.st,v 1.9 2013-01-17 22:37:55 cg Exp $' |
4762 | 292 |
! ! |