Пример #1
0
// 'ls' and 'dir' handler
static void shell_ls( char* args )
{
  const DM_DEVICE *pdev;
  unsigned dev, i;
  DM_DIR *d;
  struct dm_dirent *ent;
  u32 total;

  // Iterate through all devices, looking for the ones that can do "opendir"
  for( dev = 0; dev < dm_get_num_devices(); dev ++ )
  {  
    pdev = dm_get_device_at( dev );
    if( pdev->p_opendir_r == NULL || pdev->p_readdir_r == NULL || pdev->p_closedir_r == NULL )
      continue;
    d = dm_opendir( pdev->name );
    if( d )
    {
      total = 0;
      printf( "\n%s", pdev->name );
      while( ( ent = dm_readdir( d ) ) != NULL )
      {
        printf( "\n%s", ent->fname );
        for( i = strlen( ent->fname ); i <= DM_MAX_FNAME_LENGTH; i++ )
          printf( " " );
        printf( "%u bytes", ( unsigned )ent->fsize );
        total = total + ent->fsize;
      }
      printf( "\n\nTotal on %s: %u bytes\n", pdev->name, ( unsigned )total );
      dm_closedir( d );
    }
  }   
  printf( "\n" );
}
Пример #2
0
int _unlink_r( struct _reent *r, const char *fname )
{
  const DM_DEVICE* pdev;
  char* actname;
  int devid;
 
  if( !fname || *fname == '\0' )
  {
    r->_errno = ENOENT; 
    return -1;
  }
  // Look for device, return error if not found or if function not implemented
  if( ( devid = find_dm_entry( fname, &actname ) ) == -1 )
  {
    r->_errno = ENODEV;
    return -1; 
  }
  pdev = dm_get_device_at( devid );
  if( pdev->p_unlink_r == NULL )
  {
    r->_errno = ENOSYS;
    return -1;   
  }
 
  // And call the unlink function
  return pdev->p_unlink_r( r, actname );  
}
Пример #3
0
const char* dm_getaddr( int fd )
{
  const DM_DEVICE* pdev;
  
  // Find device, check write function
  pdev = dm_get_device_at( DM_GET_DEVID( fd ) );
  if( !pdev || pdev->p_getaddr_r == NULL )
  {
    _REENT->_errno = ENOSYS;
    return NULL;
  }
  
  return pdev->p_getaddr_r( _REENT, DM_GET_FD( fd ) );
}
Пример #4
0
// *****************************************************************************
// _close_r
int _close_r( struct _reent *r, int file )
{
  const DM_DEVICE* pdev;
  
  // Find device, check close function
  pdev = dm_get_device_at( DM_GET_DEVID( file ) );
  if( pdev->p_close_r == NULL )
  {
    r->_errno = ENOSYS;
    return -1; 
  }
  
  // And call the close function
  return pdev->p_close_r( r, DM_GET_FD( file ) );
}
Пример #5
0
// *****************************************************************************
// _write_r 
_ssize_t _write_r( struct _reent *r, int file, const void *ptr, size_t len )
{
  const DM_DEVICE* pdev;
  
  // Find device, check write function
  pdev = dm_get_device_at( DM_GET_DEVID( file ) );
  if( pdev->p_write_r == NULL )
  {
    r->_errno = ENOSYS;
    return -1; 
  }
  
  // And call the write function
  return pdev->p_write_r( r, DM_GET_FD( file ), ptr, len );  
}
Пример #6
0
// *****************************************************************************
// ioctl (actually our call, not newlib's)
static int _ioctl_r( struct _reent *r, int file, unsigned long request, void *ptr )
{
  const DM_DEVICE* pdev;
  
  // Find device, check ioctl function
  pdev = dm_get_device_at( DM_GET_DEVID( file ) );
  if( pdev->p_ioctl_r == NULL )
  {
    r->_errno = ENOSYS;
    return -1; 
  }
  
  // And call the ioctl function
  return pdev->p_ioctl_r( r, DM_GET_FD( file ), request, ptr );  
}
Пример #7
0
// Utility function: look in the device manager table and find the index
// for the given name. Returns an index into the device structure, -1 if error.
// Also returns a pointer to the actual file name (without the device part)
static int find_dm_entry( const char* name, char **pactname )
{
  int i;
  const DM_DEVICE* pdev;
  const char* preal;
  char tempname[ DM_MAX_DEV_NAME + 1 ];
  
  // Sanity check for name
  if( name == NULL || *name == '\0' || *name != '/' )
    return -1;
    
  // Find device name
  preal = strchr( name + 1, '/' );
  if( preal == NULL )
  {
    // This shortcut allows to register the "/" filesystem and use it like "/file.ext"
    strcpy( tempname, "/" );
    preal = name;
  }
  else
  {
    if( ( preal - name > DM_MAX_DEV_NAME ) || ( preal - name == 1 ) ) // name too short/too long
      return -1;
    memcpy( tempname, name, preal - name );
    tempname[ preal - name ] = '\0';
  }
    
  // Find device
  for( i = 0; i < dm_get_num_devices(); i ++ )
  {
    pdev = dm_get_device_at( i );
    if( !strcasecmp( tempname, pdev->name ) )
      break;
  }
  if( i == dm_get_num_devices() )
    return -1;
    
  // Find the actual first char of the name
  preal ++;
  if( *preal == '\0' )
    return -1;
  *pactname = ( char * )preal;
  return i;  
}
Пример #8
0
// *****************************************************************************
// _open_r
int _open_r( struct _reent *r, const char *name, int flags, int mode )
{
  char* actname;
  int res, devid;
  const DM_DEVICE* pdev;
 
  // Look for device, return error if not found or if function not implemented
  if( ( devid = find_dm_entry( name, &actname ) ) == -1 )
  {
    r->_errno = ENODEV;
    return -1; 
  }
  pdev = dm_get_device_at( devid );
  if( pdev->p_open_r == NULL )
  {
    r->_errno = ENOSYS;
    return -1;   
  }
  
  // Device found, call its function
  if( ( res = pdev->p_open_r( r, actname, flags, mode ) ) < 0 )
    return res;
  return DM_MAKE_DESC( devid, res );
}