From 1efe056f8bfcf3973636473711fc5d5ad3d238bf Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 2 Nov 2014 13:54:12 -0600 Subject: BAS: Workaround for missing fstat --- apps/interpreters/bas/Kconfig | 7 +++++++ apps/interpreters/bas/fs.c | 49 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) (limited to 'apps/interpreters') diff --git a/apps/interpreters/bas/Kconfig b/apps/interpreters/bas/Kconfig index 823d13ff7..275ac6350 100644 --- a/apps/interpreters/bas/Kconfig +++ b/apps/interpreters/bas/Kconfig @@ -42,6 +42,13 @@ config INTERPRETER_BAS_HAVE_ENVIRON ---help--- NuttX does not support the environ variable +config INTERPRETER_BAS_HAVE_FSTAT + bool + default n + depends on EXPERIMENTAL + ---help--- + NuttX does not support the fstat() function + config INTERPRETER_BAS_HAVE_SYSCFG bool default n diff --git a/apps/interpreters/bas/fs.c b/apps/interpreters/bas/fs.c index 56bee2dae..ad67982de 100644 --- a/apps/interpreters/bas/fs.c +++ b/apps/interpreters/bas/fs.c @@ -66,16 +66,22 @@ #include #include -#include + +#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT +# include +#endif + #include #include #include + #ifdef HAVE_GETTEXT # include # define _(String) gettext(String) #else # define _(String) String #endif + #include #include #include @@ -1910,7 +1916,12 @@ long int FS_loc(int chn) long int FS_lof(int chn) { +#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT struct stat buf; +#else + off_t curpos; + off_t endpos; +#endif int fd; if (opened(chn, -1) == -1) @@ -1936,13 +1947,47 @@ long int FS_lof(int chn) } assert(fd != -1); + + /* Get the size of the file */ + +#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT if (fstat(fd, &buf) == -1) { FS_errmsg = strerror(errno); return -1; } - return buf.st_size / file[chn]->recLength; + return (long int)(buf.st_size / file[chn]->recLength); +#else + /* Save the current file position */ + + curpos = lseek(fd, 0, SEEK_CUR); + if (curpos == (off_t)-1) + { + FS_errmsg = strerror(errno); + return -1; + } + + /* Get the position at the end of the file */ + + endpos = lseek(fd, 0, SEEK_END); + if (endpos == (off_t)-1) + { + FS_errmsg = strerror(errno); + return -1; + } + + /* Restore the file position */ + + curpos = lseek(fd, curpos, SEEK_SET); + if (curpos == (off_t)-1) + { + FS_errmsg = strerror(errno); + return -1; + } + + return (long int)(endpos / file[chn]->recLength); +#endif } long int FS_recLength(int chn) -- cgit v1.2.3