Services and Modeling for Embedded Software Development
Embecosm divider strip
Prev  Next

5.3.7.  Provide the Status of an Open File, fstat

For a namespace clean function, implement _fstat, otherwise implement fstat. The detailed implementation will depend on the file handling functionality available.

A minimal implementation should assume that all files are character special devices and populate the status data structure accordingly.

#include <sys/stat.h>

int
_fstat (int          file,
        struct stat *st)
{
  st->st_mode = S_IFCHR;
  return  0;

}       /* _fstat () */
	  

The OpenRISC 1000  implementation requires two versions of this, one for the BSP using the console for output and one for the BSP using a UART and supporting both standard input and standard output.

Without a UART, the implementation still checks that the file descriptor is one of the two that are supported, and otherwise returns an error.

#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>

#undef errno
extern int  errno;

int
_fstat (int          file,
        struct stat *st)
{
  if ((STDOUT_FILENO == file) || (STDERR_FILENO == file))
    {
      st->st_mode = S_IFCHR;
      return  0;
    }
  else
    {
      errno = EBADF;
      return  -1;
    }
}       /* _fstat () */
	  

The implementation when a UART is available is almost identical, except that STDIN_FILENO is also an acceptable file for which status can be provided.

Embecosm divider strip