@@ -56,9 +56,9 @@ static int vfs_resolve_mount(const char* path, vfs_mount_res_t* out) {
5656
5757 if (!best ) {
5858 if (path )
59- printf ("no mount matches path: %s" , path );
59+ printf ("resolve_mount: no mount matches path: %s" , path );
6060 else
61- printf ("no mount matches the given path." );
61+ printf ("resolve_mount: no mount matches the given path." );
6262 return -2 ;
6363 }
6464
@@ -153,42 +153,53 @@ void vfs_close(vfs_file_t* file) {
153153 fat16_close (& file -> f );
154154}
155155
156- int vfs_ls (const char * path ) {
156+ int vfs_ls (const char * path )
157+ {
157158 if (!path ) {
158- printf ("ls: invalid path passed " );
159+ printf ("ls: invalid path" );
159160 return -1 ;
160161 }
161162
163+ char norm [256 ];
164+ vfs_normalize_path (path , norm );
165+
162166 vfs_mount_res_t res ;
163- if (vfs_resolve_mount (path , & res ) != 0 )
167+ if (vfs_resolve_mount (norm , & res ) != 0 )
164168 return -1 ;
165169
166- if (res .mnt -> type == FS_FAT16 ){
170+ int listed_anything = 0 ;
171+
172+ for (int i = 0 ; i < mounted_partition_count ; i ++ ) {
173+ mount_entry_t * m = & mounted_partitions [i ];
174+
175+ if (vfs_is_direct_child_mount (norm , m )) {
176+ const char * name = vfs_basename (m -> mount_point );
177+ printfnoln (yellow_color "%s " reset_color , name );
178+ }
179+ }
180+
181+ if (res .mnt -> type == FS_FAT16 ) {
167182 fat16_fs_t * fs = (fat16_fs_t * )res .mnt -> fs ;
168183
169184 if (* res .rel_path == '\0' ) {
170185 fat16_list_root (fs );
171- return 0 ;
172- }
186+ } else {
187+ fat16_dir_entry_t e ;
188+ if (fat16_find_path (fs , res .rel_path , & e ) != 0 ) {
189+ printf ("ls: path not found" );
190+ return -3 ;
191+ }
173192
174- fat16_dir_entry_t e ;
175- if (fat16_find_path (fs , res .rel_path , & e ) != 0 ) {
176- printf ("ls: path not found" );
177- return -3 ;
178- }
193+ if (!(e .attr & 0x10 )) {
194+ printf ("ls: not a directory" );
195+ return -4 ;
196+ }
179197
180- if (!(e .attr & 0x10 )) {
181- printf ("ls: not a directory" );
182- return -4 ;
198+ fat16_list_dir_cluster (fs , e .first_cluster );
183199 }
184-
185- fat16_list_dir_cluster (fs , e .first_cluster );
186-
187- return 0 ;
188200 }
189-
190- printf ("ls: unknown filesystem" );
191- return -2 ;
201+
202+ return 0 ;
192203}
193204
194205int vfs_open (const char * path , vfs_file_t * out ) {
@@ -440,4 +451,33 @@ int vfs_mv(const char* src, const char* dst)
440451
441452const char * vfs_getcwd (void ) {
442453 return vfs_cwd ;
454+ }
455+
456+ int vfs_is_direct_child_mount (const char * parent , mount_entry_t * m ) {
457+ if (!strcmp (parent , "/" )) {
458+ if (m -> mount_point [0 ] != '/' )
459+ return 0 ;
460+ /* Only one slash allowed */
461+ const char * rest = m -> mount_point + 1 ;
462+ return strchr (rest , '/' ) == NULL ;
463+ }
464+
465+ size_t plen = strlen (parent );
466+ if (strncmp (m -> mount_point , parent , plen ) != 0 )
467+ return 0 ;
468+
469+ if (m -> mount_point [plen ] != '/' )
470+ return 0 ;
471+
472+ return strchr (m -> mount_point + plen + 1 , '/' ) == NULL ;
473+ }
474+
475+ const char * vfs_basename (const char * path ) {
476+ const char * last = path ;
477+ while (* path ) {
478+ if (* path == '/' )
479+ last = path + 1 ;
480+ path ++ ;
481+ }
482+ return last ;
443483}
0 commit comments