// '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" ); }
// 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; }