1.1 --- a/templates/native.c Thu Dec 08 23:36:43 2016 +0100
1.2 +++ b/templates/native.c Thu Dec 08 23:50:56 2016 +0100
1.3 @@ -680,6 +680,19 @@
1.4
1.5 /* Input/output. */
1.6
1.7 +__attr __fn_native__fclose(__attr __args[])
1.8 +{
1.9 + __attr * const fp = &__args[1];
1.10 + /* fp interpreted as FILE reference */
1.11 + FILE *f = (FILE *) fp->datavalue;
1.12 +
1.13 + errno = 0;
1.14 + if (fclose(f))
1.15 + __raise_io_error(__new_int(errno));
1.16 +
1.17 + return __builtins___none_None;
1.18 +}
1.19 +
1.20 __attr __fn_native__fopen(__attr __args[])
1.21 {
1.22 __attr * const filename = &__args[1];
1.23 @@ -791,6 +804,19 @@
1.24 return __builtins___none_None;
1.25 }
1.26
1.27 +__attr __fn_native__close(__attr __args[])
1.28 +{
1.29 + __attr * const fd = &__args[1];
1.30 + /* fd.__data__ interpreted as int */
1.31 + int i = __load_via_object(fd->value, __pos___data__).intvalue;
1.32 +
1.33 + errno = 0;
1.34 + if (close(i) == -1)
1.35 + __raise_io_error(__new_int(errno));
1.36 +
1.37 + return __builtins___none_None;
1.38 +}
1.39 +
1.40 __attr __fn_native__read(__attr __args[])
1.41 {
1.42 __attr * const fd = &__args[1];
1.43 @@ -808,14 +834,12 @@
1.44
1.45 if (have_read == -1)
1.46 __raise_io_error(__new_int(errno));
1.47 - else
1.48 - {
1.49 - /* Reserve space for a new string. */
1.50 +
1.51 + /* Reserve space for a new string. */
1.52
1.53 - s = __ALLOCATE(have_read + 1, 1);
1.54 - strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
1.55 - return __new_str(s);
1.56 - }
1.57 + s = __ALLOCATE(have_read + 1, 1);
1.58 + strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
1.59 + return __new_str(s);
1.60 }
1.61
1.62 __attr __fn_native__write(__attr __args[])
1.63 @@ -826,9 +850,15 @@
1.64 int i = __load_via_object(fd->value, __pos___data__).intvalue;
1.65 /* str.__data__ interpreted as string */
1.66 char *s = __load_via_object(str->value, __pos___data__).strvalue;
1.67 + ssize_t have_written;
1.68
1.69 - write(i, s, sizeof(char) * strlen(s));
1.70 - return __builtins___none_None;
1.71 + errno = 0;
1.72 + have_written = write(i, s, sizeof(char) * strlen(s));
1.73 +
1.74 + if (have_written == -1)
1.75 + __raise_io_error(__new_int(errno));
1.76 +
1.77 + return __new_int(have_written);
1.78 }
1.79
1.80 /* Module initialisation. */