3 * _read.c - read() for both files and sockets (SAS/C)
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
16 #include <proto/dos.h>
18 #include <bsdsocket.h>
20 extern int __io2errno(long);
23 __read(int fd
, void *buffer
, unsigned int length
)
27 char ch
, *ptr
, *nextptr
, *endptr
;
30 * Check for the break signals
36 if ((ufb
= __chkufb(fd
)) == NULL
) {
41 * Check if read is allowed
43 if (!(ufb
->ufbflg
& UFB_RA
)) {
44 _OSERR
= ERROR_READ_PROTECTED
;
53 if (ufb
->ufbflg
& UFB_SOCK
) {
54 if ((count
= recv(fd
, (UBYTE
*)buffer
, length
, 0)) < 0) {
59 if ((count
= Read(ufb
->ufbfh
, buffer
, length
)) == -1) {
60 errno
= __io2errno(_OSERR
= IoErr());
65 * Check if translation is not needed
67 if (count
== 0 || !(ufb
->ufbflg
& UFB_XLAT
))
70 endptr
= (char *)buffer
+ count
; /* first point NOT in buffer */
72 if (endptr
[-1] == 0x0D) /* checks last char */
74 Read(ufb
->ufbfh
, buffer
, 1); /* overwrites first read byte ! */
81 * Remove 0x0D's (CR) (This doesn't remove a CR at the end of the buffer).
83 if ((ptr
= memchr(buffer
, 0x0D, count
)) != NULL
) {
85 while (nextptr
< endptr
) {
86 if ((ch
= *nextptr
) != 0x0D)
90 count
= ptr
- (char *)buffer
;
94 * Test for CTRL-Z (end of file)
96 if ((ptr
= memchr(buffer
, 0x1A, count
)) == NULL
)
99 return ptr
- (char *)buffer
;