diff options
Diffstat (limited to 'apps/interpreters/bas/fs.c')
-rw-r--r-- | apps/interpreters/bas/fs.c | 49 |
1 files changed, 47 insertions, 2 deletions
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 <sys/time.h> #include <sys/types.h> -#include <sys/stat.h> + +#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT +# include <sys/stat.h> +#endif + #include <assert.h> #include <errno.h> #include <fcntl.h> + #ifdef HAVE_GETTEXT # include <libintl.h> # define _(String) gettext(String) #else # define _(String) String #endif + #include <math.h> #include <signal.h> #include <stdio.h> @@ -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) |