system/src/newlib/_syscalls.c
/* fd, is a user file descriptor. */
fd, is a user file descriptor.
fd is a user file descriptor .
int
_swilseek (int fd, int ptr, int dir)
{
  int res;
  struct fdent *pfd;
  pfd = findslot (fd);
  if (pfd == NULL)
    {
      errno = EBADF;
      return -1;
    }
  if ((dir != SEEK_CUR) && (dir != SEEK_SET) && (dir != SEEK_END))
    {
      errno = EINVAL;
      return -1;
    }
  if (dir == SEEK_CUR)
    {
      ptr = pfd->pos + ptr;
      if (ptr < 0)
        {
          errno = EINVAL;
          if ((pfd->pos > 0) && (ptr > 0))
            {
              errno = EOVERFLOW;
            }
          return -1;
        }
      dir = SEEK_SET;
    }
  int block[2];
  if (dir == SEEK_END)
    {
      block[0] = pfd->handle;
      res = checkerror (call_host (SEMIHOSTING_SYS_FLEN, block));
      if (res == -1)
        {
          return -1;
        }
      ptr += res;
    }
  block[0] = pfd->handle;
  block[1] = ptr;
  res = checkerror (call_host (SEMIHOSTING_SYS_SEEK, block));
  if (res >= 0)
    {
      pfd->pos = ptr;
      return ptr;
    }
  else
    {
      return -1;
    }
}
int _swilseek ( int fd , int ptr , int dir ) { int res ; struct fdent * pfd ; pfd = findslot ( fd ) ; if ( pfd == NULL ) { errno = EBADF ; return -1 ; } if ( ( dir != SEEK_CUR ) && ( dir != SEEK_SET ) && ( dir != SEEK_END ) ) { errno = EINVAL ; return -1 ; } if ( dir == SEEK_CUR ) { ptr = pfd -> pos + ptr ; if ( ptr < 0 ) { errno = EINVAL ; if ( ( pfd -> pos > 0 ) && ( ptr > 0 ) ) { errno = EOVERFLOW ; } return -1 ; } dir = SEEK_SET ; } int block [ 2 ] ; if ( dir == SEEK_END ) { block [ 0 ] = pfd -> handle ; res = checkerror ( call_host ( SEMIHOSTING_SYS_FLEN , block ) ) ; if ( res == -1 ) { return -1 ; } ptr += res ; } block [ 0 ] = pfd -> handle ; block [ 1 ] = ptr ; res = checkerror ( call_host ( SEMIHOSTING_SYS_SEEK , block ) ) ; if ( res >= 0 ) { pfd -> pos = ptr ; return ptr ; } else { return -1 ; } }
fd, is a user file descriptor.
fd is a user file descriptor .
/* Valid file descriptor? */ /* Valid whence? */ /* Convert SEEK_CUR to SEEK_SET */ /* The resulting file offset would be negative. */ /* This code only does absolute seeks.  */ /* At this point ptr is the current file position. */