summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-11-02 13:54:12 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-11-02 13:54:12 -0600
commit1efe056f8bfcf3973636473711fc5d5ad3d238bf (patch)
tree3c9c1d56f68079a1849c7dbc33a4e64b7a851c0f /apps
parent9b3d10282739aa06216e96dc614433bb3e64b1f3 (diff)
downloadnuttx-1efe056f8bfcf3973636473711fc5d5ad3d238bf.tar.gz
nuttx-1efe056f8bfcf3973636473711fc5d5ad3d238bf.tar.bz2
nuttx-1efe056f8bfcf3973636473711fc5d5ad3d238bf.zip
BAS: Workaround for missing fstat
Diffstat (limited to 'apps')
-rw-r--r--apps/interpreters/bas/Kconfig7
-rw-r--r--apps/interpreters/bas/fs.c49
2 files changed, 54 insertions, 2 deletions
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 <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)