BAS: Workaround for missing fstat

This commit is contained in:
Gregory Nutt 2014-11-02 13:54:12 -06:00
parent d18e4bb3ff
commit 1f28e8c4ba
2 changed files with 54 additions and 2 deletions

View File

@ -42,6 +42,13 @@ config INTERPRETER_BAS_HAVE_ENVIRON
---help--- ---help---
NuttX does not support the environ variable 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 config INTERPRETER_BAS_HAVE_SYSCFG
bool bool
default n default n

View File

@ -66,16 +66,22 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT
# include <sys/stat.h>
#endif
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#ifdef HAVE_GETTEXT #ifdef HAVE_GETTEXT
# include <libintl.h> # include <libintl.h>
# define _(String) gettext(String) # define _(String) gettext(String)
#else #else
# define _(String) String # define _(String) String
#endif #endif
#include <math.h> #include <math.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
@ -1910,7 +1916,12 @@ long int FS_loc(int chn)
long int FS_lof(int chn) long int FS_lof(int chn)
{ {
#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT
struct stat buf; struct stat buf;
#else
off_t curpos;
off_t endpos;
#endif
int fd; int fd;
if (opened(chn, -1) == -1) if (opened(chn, -1) == -1)
@ -1936,13 +1947,47 @@ long int FS_lof(int chn)
} }
assert(fd != -1); assert(fd != -1);
/* Get the size of the file */
#ifdef CONFIG_INTERPRETER_BAS_HAVE_FSTAT
if (fstat(fd, &buf) == -1) if (fstat(fd, &buf) == -1)
{ {
FS_errmsg = strerror(errno); FS_errmsg = strerror(errno);
return -1; 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) long int FS_recLength(int chn)