From 3abd3e0bccc0e1718822348081058bcb7b356885 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Tue, 17 Dec 2019 22:44:24 +0100 Subject: [PATCH] mescc: Mes C Library: lseek: Be careful clearing read buffer. * lib/linux/lseek.c (_lseek): New function. * lib/linux/lseek.c (lseek): Use it to check if we should reset read buffer. * lib/mes/__buffered_read.c (__read_buffer_max): New variable. (__buffered_read_init): Add environment override: MES_READ_BUFFER. (__buffered_read): Use it. --- lib/linux/lseek.c | 12 +++++++++++ lib/mes/__buffered_read.c | 42 +++++++++++++++++++++++++++++++++------ lib/posix/open.c | 2 +- lib/stdio/fopen.c | 4 +--- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/lib/linux/lseek.c b/lib/linux/lseek.c index 94f2f9f7..f71af59f 100644 --- a/lib/linux/lseek.c +++ b/lib/linux/lseek.c @@ -24,9 +24,21 @@ #include #include +#if !__MESC__ /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */ +off_t +_lseek (int filedes, off_t offset, int whence) +{ + return _sys_call3 (SYS_lseek, (int) filedes, (long) offset, (int) whence); +} +#endif + off_t lseek (int filedes, off_t offset, int whence) { +#if !__MESC__ /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */ + if (_lseek (filedes, 0, SEEK_CUR) == -1) + return -1; +#endif size_t skip = __buffered_read_clear (filedes); if (whence == SEEK_CUR) offset -= skip; diff --git a/lib/mes/__buffered_read.c b/lib/mes/__buffered_read.c index e47cca53..bac1f309 100644 --- a/lib/mes/__buffered_read.c +++ b/lib/mes/__buffered_read.c @@ -22,7 +22,13 @@ #include #include +#if !__MESC__ +#define __READ_BUFFER_MAX 128 +int __read_buffer_max; +#else /* FIXME: We want bin/mes-mescc's x86-linux sha256sum to stay the same. */ #define __READ_BUFFER_MAX 100 +#define __read_buffer_max 100 +#endif struct __read_buffer { @@ -36,7 +42,21 @@ void __buffered_read_init (int filedes) { if (!__read_cache) - __read_cache = (struct __read_buffer *) malloc (sizeof (struct __read_buffer) * __FILEDES_MAX); + { + __read_cache = (struct __read_buffer *) malloc (sizeof (struct __read_buffer) * __FILEDES_MAX); +#if !__MESC__ + __read_buffer_max = __READ_BUFFER_MAX; + char *p = getenv ("MES_READ_BUFFER"); + if (p) + { + __read_buffer_max = atoi (p); + if (__read_buffer_max < 0) + __read_buffer_max = 0; + if (__read_buffer_max > __READ_BUFFER_MAX) + __read_buffer_max = __READ_BUFFER_MAX; + } +#endif + } } size_t @@ -55,23 +75,33 @@ __buffered_read (int filedes, void *buffer, size_t size) __buffered_read_init (filedes); struct __read_buffer *cache = &__read_cache[filedes]; char *p = buffer; - if (!cache->size && size > __READ_BUFFER_MAX) + if (!cache->size && size > __read_buffer_max) return _read (filedes, buffer, size); while (cache->size > 0 && todo) { todo--; - *p++ = cache->string[__READ_BUFFER_MAX - cache->size--]; + *p++ = cache->string[__read_buffer_max - cache->size--]; } if (todo) { - ssize_t bytes = _read (filedes, cache->string, __READ_BUFFER_MAX); +#if !__MESC__ + if (todo > __read_buffer_max) + return size - todo + _read (filedes, p, todo); + if (__mes_debug () > 4) + { + eputs ("__buffered_read: "); + eputs (itoa (__read_buffer_max)); + eputs ("\n"); + } +#endif + ssize_t bytes = _read (filedes, cache->string, __read_buffer_max); if (bytes < 0) return -1; if (bytes) { cache->size = bytes; - if (bytes < __READ_BUFFER_MAX) - memmove (cache->string + __READ_BUFFER_MAX - bytes, cache->string, bytes); + if (bytes < __read_buffer_max) + memmove (cache->string + __read_buffer_max - bytes, cache->string, bytes); return size - todo + __buffered_read (filedes, p, todo); } } diff --git a/lib/posix/open.c b/lib/posix/open.c index ad76297d..490c3ec9 100644 --- a/lib/posix/open.c +++ b/lib/posix/open.c @@ -42,5 +42,5 @@ open (char const *file_name, int flags, ...) return r; } else - return _open2(file_name, flags); + return _open2 (file_name, flags); } diff --git a/lib/stdio/fopen.c b/lib/stdio/fopen.c index 6342e0a2..3164c11e 100644 --- a/lib/stdio/fopen.c +++ b/lib/stdio/fopen.c @@ -45,7 +45,7 @@ fopen (char const *file_name, char const *opentype) int flags = O_RDWR; if (opentype[0] == 'a') flags |= O_APPEND; - fd = open (file_name, flags, mode); + fd = _open3 (file_name, flags, mode); } else if (opentype[0] == 'w' || opentype[0] == 'a' || !strcmp (opentype, "r+")) { @@ -72,5 +72,3 @@ fopen (char const *file_name, char const *opentype) fd = 0; return (FILE *) (long) fd; } - -#undef open