mescc: Mes C Library: Prepare for M2-Planet: fdgetc.

* lib/mes/fdgetc.c: Rewrite C-constructs not supported by M2-Planet.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2020-05-17 19:28:04 +02:00
parent 11d6a9679d
commit 189b9d9e8c
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273

View file

@ -19,12 +19,14 @@
*/ */
#include <mes/lib.h> #include <mes/lib.h>
#include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <unistd.h> #include <unistd.h>
int __ungetc_buf[__FILEDES_MAX + 1] = { 0 }; int *__ungetc_buf;
int int
__ungetc_p (int filedes) __ungetc_p (int filedes)
@ -35,8 +37,13 @@ __ungetc_p (int filedes)
void void
__ungetc_init () __ungetc_init ()
{ {
if (__ungetc_buf[__FILEDES_MAX] == 0) if (__ungetc_buf == 0)
memset (__ungetc_buf, -1, (__FILEDES_MAX + 1) * sizeof (int)); {
int save_errno = errno;
__ungetc_buf = malloc ((__FILEDES_MAX + 1) * sizeof (int));
errno = save_errno;
memset (__ungetc_buf, -1, (__FILEDES_MAX + 1) * sizeof (int));
}
} }
void void
@ -65,10 +72,10 @@ fdgetc (int fd)
int r = read (fd, &c, 1); int r = read (fd, &c, 1);
if (r < 1) if (r < 1)
return -1; return -1;
i = (int) c; i = c;
} }
if (i < 0) if (i < 0)
i += 256; i = i + 256;
return i; return i;
} }