1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
12 /***********************************************************************/
21 #include "unixsupport.h"
27 #define EWOULDBLOCK (-1)
30 CAMLprim value
unix_write(value fd
, value buf
, value vofs
, value vlen
)
32 long ofs
, len
, written
;
34 char iobuf
[UNIX_BUFFER_SIZE
];
41 numbytes
= len
> UNIX_BUFFER_SIZE
? UNIX_BUFFER_SIZE
: len
;
42 memmove (iobuf
, &Byte(buf
, ofs
), numbytes
);
43 enter_blocking_section();
44 ret
= write(Int_val(fd
), iobuf
, numbytes
);
45 leave_blocking_section();
47 if ((errno
== EAGAIN
|| errno
== EWOULDBLOCK
) && written
> 0) break;
48 uerror("write", Nothing
);
55 return Val_long(written
);
58 /* When an error occurs after the first loop, unix_write reports the
59 error and discards the number of already written characters.
60 In this case, it would be better to discard the error and return the
61 number of bytes written, since most likely, unix_write will be call again,
62 and the error will be reproduced and this time will be reported.
63 This problem is avoided in unix_single_write, which is faithful to the
66 CAMLprim value
unix_single_write(value fd
, value buf
, value vofs
, value vlen
)
70 char iobuf
[UNIX_BUFFER_SIZE
];
77 numbytes
= len
> UNIX_BUFFER_SIZE
? UNIX_BUFFER_SIZE
: len
;
78 memmove (iobuf
, &Byte(buf
, ofs
), numbytes
);
79 enter_blocking_section();
80 ret
= write(Int_val(fd
), iobuf
, numbytes
);
81 leave_blocking_section();
82 if (ret
== -1) uerror("single_write", Nothing
);