1.1 --- a/templates/native.c Tue Dec 06 17:49:39 2016 +0100
1.2 +++ b/templates/native.c Tue Dec 06 17:55:55 2016 +0100
1.3 @@ -3,7 +3,7 @@
1.4 #include <limits.h> /* INT_MAX, INT_MIN */
1.5 #include <math.h> /* ceil, log10, pow */
1.6 #include <string.h> /* strcmp, strncpy, strlen */
1.7 -#include <stdio.h> /* snprintf */
1.8 +#include <stdio.h> /* fdopen, snprintf */
1.9 #include <errno.h> /* errno */
1.10 #include "types.h"
1.11 #include "exceptions.h"
1.12 @@ -644,13 +644,57 @@
1.13 return __builtins___boolean_False;
1.14 }
1.15
1.16 +/* Input/output. */
1.17 +
1.18 +__attr __fn_native__fdopen(__attr __args[])
1.19 +{
1.20 + __attr * const fd = &__args[1];
1.21 + __attr * const mode = &__args[2];
1.22 + /* fd.__data__ interpreted as int */
1.23 + int i = __load_via_object(fd->value, __pos___data__).intvalue;
1.24 + /* str.__data__ interpreted as string */
1.25 + char *s = __load_via_object(mode->value, __pos___data__).strvalue;
1.26 + FILE *f;
1.27 + __attr attr;
1.28 +
1.29 + errno = 0;
1.30 + f = fdopen(i, s);
1.31 +
1.32 + /* Produce an exception if the operation failed. */
1.33 +
1.34 + if (f == NULL)
1.35 + __raise_io_error(errno);
1.36 + else
1.37 + {
1.38 + attr.context = 0;
1.39 + attr.datavalue = (void *) f;
1.40 + return attr;
1.41 + }
1.42 +}
1.43 +
1.44 __attr __fn_native__read(__attr __args[])
1.45 {
1.46 __attr * const fd = &__args[1];
1.47 __attr * const n = &__args[2];
1.48 + /* fd.__data__ interpreted as int */
1.49 + int i = __load_via_object(fd->value, __pos___data__).intvalue;
1.50 + /* n.__data__ interpreted as int */
1.51 + int to_read = __load_via_object(n->value, __pos___data__).intvalue;
1.52 + void *buf[to_read + 1];
1.53 + ssize_t have_read;
1.54
1.55 - /* NOTE: To be written. */
1.56 - return __builtins___none_None;
1.57 + errno = 0;
1.58 + have_read = read(i, buf, to_read);
1.59 +
1.60 + if (have_read == -1)
1.61 + __raise_io_error(errno);
1.62 + else
1.63 + {
1.64 + /* Zero terminate the string. */
1.65 +
1.66 + buf[have_read] = 0;
1.67 + return __new_str((char *) buf);
1.68 + }
1.69 }
1.70
1.71 __attr __fn_native__write(__attr __args[])