Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / _write.c
blobd60ca5f191fb5ca09ad8548f412f655db053be5b
1 /* $Id$
3 * _write.c - write() for both files and sockets (SAS/C)
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 */
10 #include <ios1.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <dos.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <dos/dos.h>
17 #include <proto/dos.h>
19 #include <bsdsocket.h>
21 extern int __io2errno(long);
23 int
24 __write(int fd, const void *buffer, unsigned int length)
26 struct UFB *ufb;
27 int count, totcount;
28 char *ptr;
31 * Check for the break signals
33 __chkabort();
35 * find the ufb *
37 if ((ufb = __chkufb(fd)) == NULL) {
38 errno = EINVAL;
39 return -1;
42 * Check if write is allowed
44 if (!(ufb->ufbflg & UFB_WA)) {
45 _OSERR = ERROR_WRITE_PROTECTED;
46 errno = EIO;
47 return -1;
51 * Seek to end of the file if necessary
53 if (ufb->ufbflg & UFB_APP)
54 __lseek(fd, 0, 2);
57 * Check if translation is not needed
59 if (!(ufb->ufbflg & UFB_XLAT) ||
60 (ptr = memchr(buffer, 0x0A, length)) == NULL) {
61 if (ufb->ufbflg & UFB_SOCK) {
62 if ((count = send(fd, (char *)buffer, length, 0)) < 0)
63 return -1;
65 else {
66 if ((count = Write(ufb->ufbfh, (void *)buffer, length)) == -1)
67 goto osfail;
69 return count;
72 totcount = length;
75 * Translate, ie., append CR before each LF
77 do {
78 count = ptr - (char *)buffer;
79 if (ufb->ufbflg & UFB_SOCK) {
80 if (send(fd, (char *)buffer, count, 0) < 0)
81 return -1;
82 if (send(fd, "\015"/* CR */, 1, 0) < 0)
83 return -1;
85 else {
86 if (Write(ufb->ufbfh, (void *)buffer, count) == -1)
87 goto osfail;
88 if (Write(ufb->ufbfh, "\015"/* CR */, 1) == -1)
89 goto osfail;
91 length -= count;
93 buffer = ptr;
94 } while ((ptr = memchr((char *)buffer + 1, 0x0A, length)) != NULL);
96 if (ufb->ufbflg & UFB_SOCK) {
97 if ((count = send(fd, (char *)buffer, length, 0)) < 0)
98 return -1;
100 else {
101 if (Write(ufb->ufbfh, (void *)buffer, length) == -1)
102 goto osfail;
105 return totcount;
107 osfail:
108 errno = __io2errno(_OSERR = IoErr());
109 return -1;