fixed tree view
This commit is contained in:
1 parent
6fb01cfe29
commit
9166ccdd84
1 file changed
+33
-47
+33
-47
@@ -33,7 +33,6 @@ static int permissions_to_string( mode_t permissions, char* string );
|
|||||||
static int mask_permissions( mode_t file_mode );
|
static int mask_permissions( mode_t file_mode );
|
||||||
static int count_entries( DIR* directory, const arguments_t* const args );
|
static int count_entries( DIR* directory, const arguments_t* const args );
|
||||||
static int get_entries(
|
static int get_entries(
|
||||||
char* const path,
|
|
||||||
DIR* directory,
|
DIR* directory,
|
||||||
directory_entry_t** entry_table,
|
directory_entry_t** entry_table,
|
||||||
int num_entries,
|
int num_entries,
|
||||||
@@ -42,10 +41,9 @@ static int get_entries(
|
|||||||
static int is_entry_visible( const arguments_t* const args, char* entry_name );
|
static int is_entry_visible( const arguments_t* const args, char* entry_name );
|
||||||
static int display_directory_contents(
|
static int display_directory_contents(
|
||||||
const arguments_t* const args,
|
const arguments_t* const args,
|
||||||
char* const path,
|
|
||||||
DIR* directory,
|
DIR* directory,
|
||||||
size_t recursion_level,
|
size_t recursion_level,
|
||||||
bool is_last
|
size_t is_last
|
||||||
);
|
);
|
||||||
|
|
||||||
// program logic
|
// program logic
|
||||||
@@ -68,14 +66,21 @@ int main( int argc, char** argv )
|
|||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( args.tree_view )
|
||||||
|
log_info( "%s", args.path );
|
||||||
|
|
||||||
|
if ( !chdir( args.path ) )
|
||||||
|
{
|
||||||
// TODO: handle return code
|
// TODO: handle return code
|
||||||
display_directory_contents(
|
display_directory_contents(
|
||||||
&args,
|
&args,
|
||||||
args.path,
|
|
||||||
directory,
|
directory,
|
||||||
0,
|
0,
|
||||||
false
|
0
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir( directory );
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -102,8 +107,8 @@ int count_entries( DIR* directory, const arguments_t* const args )
|
|||||||
return entry_count;
|
return entry_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get entries of **current** directory */
|
||||||
int get_entries(
|
int get_entries(
|
||||||
char* const path,
|
|
||||||
DIR* directory,
|
DIR* directory,
|
||||||
directory_entry_t** entry_table,
|
directory_entry_t** entry_table,
|
||||||
int num_entries,
|
int num_entries,
|
||||||
@@ -118,7 +123,6 @@ int get_entries(
|
|||||||
int entry_count = 0;
|
int entry_count = 0;
|
||||||
directory_entry_t* new_entry;
|
directory_entry_t* new_entry;
|
||||||
struct dirent* entry;
|
struct dirent* entry;
|
||||||
char full_path[PATH_MAX];
|
|
||||||
struct stat file_stats = { 0 };
|
struct stat file_stats = { 0 };
|
||||||
|
|
||||||
|
|
||||||
@@ -132,15 +136,7 @@ int get_entries(
|
|||||||
new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
|
new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
|
||||||
if ( !new_entry ) return -ENOMEM;
|
if ( !new_entry ) return -ENOMEM;
|
||||||
|
|
||||||
snprintf(
|
if ( lstat( entry->d_name, &file_stats ) ) return -EIO;
|
||||||
full_path,
|
|
||||||
PATH_MAX,
|
|
||||||
"%s/%s",
|
|
||||||
path,
|
|
||||||
entry->d_name
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( lstat( full_path, &file_stats ) ) return -EIO;
|
|
||||||
|
|
||||||
strncpy( new_entry->name, entry->d_name, NAME_MAX );
|
strncpy( new_entry->name, entry->d_name, NAME_MAX );
|
||||||
new_entry->stats = file_stats;
|
new_entry->stats = file_stats;
|
||||||
@@ -234,14 +230,14 @@ int is_entry_visible( const arguments_t* const args, char* entry_name )
|
|||||||
|
|
||||||
int display_directory_contents(
|
int display_directory_contents(
|
||||||
const arguments_t* const args,
|
const arguments_t* const args,
|
||||||
char* const path,
|
|
||||||
DIR* directory,
|
DIR* directory,
|
||||||
size_t recursion_level,
|
size_t recursion_level,
|
||||||
bool is_last
|
size_t is_last
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if ( !args ) return -EINVAL;
|
if ( !args ) return -EINVAL;
|
||||||
if ( !directory ) return -EINVAL;
|
if ( !directory ) return -EINVAL;
|
||||||
|
if ( recursion_level > args->depth ) return -EINVAL;
|
||||||
|
|
||||||
char file_type;
|
char file_type;
|
||||||
int permissions;
|
int permissions;
|
||||||
@@ -260,7 +256,7 @@ int display_directory_contents(
|
|||||||
);
|
);
|
||||||
if ( !entry_table ) return -ENOMEM;
|
if ( !entry_table ) return -ENOMEM;
|
||||||
|
|
||||||
ret = get_entries( path, directory, entry_table, num_entries, args );
|
ret = get_entries( directory, entry_table, num_entries, args );
|
||||||
if ( 0 > ret )
|
if ( 0 > ret )
|
||||||
{
|
{
|
||||||
log_error(
|
log_error(
|
||||||
@@ -392,12 +388,18 @@ int display_directory_contents(
|
|||||||
char tree[ NAME_MAX ] = "";
|
char tree[ NAME_MAX ] = "";
|
||||||
for ( size_t k = 0; k < recursion_level; k++ )
|
for ( size_t k = 0; k < recursion_level; k++ )
|
||||||
{
|
{
|
||||||
tree[ k ] = ' ';
|
if ( !( is_last & 1 << k ) )
|
||||||
tree[ k + 1 ] = ' ';
|
{
|
||||||
tree[ k + 2 ] = ' ';
|
tree[ 4 * k ] = '|';
|
||||||
tree[ k + 3 ] = ' ';
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tree[ 4 * k ] = ' ';
|
||||||
|
}
|
||||||
|
tree[ 4 * k + 1 ] = ' ';
|
||||||
|
tree[ 4 * k + 2 ] = ' ';
|
||||||
|
tree[ 4 * k + 3 ] = ' ';
|
||||||
}
|
}
|
||||||
if ( 0 < recursion_level && !is_last ) tree[ 0 ] = '|';
|
|
||||||
if ( num_entries != n + 1 )
|
if ( num_entries != n + 1 )
|
||||||
{
|
{
|
||||||
tree[ 4 * recursion_level ] = '+';
|
tree[ 4 * recursion_level ] = '+';
|
||||||
@@ -408,6 +410,7 @@ int display_directory_contents(
|
|||||||
}
|
}
|
||||||
tree[ 4 * recursion_level + 1 ] = '-';
|
tree[ 4 * recursion_level + 1 ] = '-';
|
||||||
tree[ 4 * recursion_level + 2 ] = '-';
|
tree[ 4 * recursion_level + 2 ] = '-';
|
||||||
|
tree[ 4 * recursion_level + 3 ] = ' ';
|
||||||
|
|
||||||
log_info(
|
log_info(
|
||||||
"%s%s",
|
"%s%s",
|
||||||
@@ -415,10 +418,8 @@ int display_directory_contents(
|
|||||||
entry->name
|
entry->name
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( args->tree_view
|
if ( recursion_level < args->depth
|
||||||
&& recursion_level < args->depth
|
&& entry->type == type_directory )
|
||||||
&& entry->type == type_directory
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
DIR* subdirectory = opendir(entry->name);
|
DIR* subdirectory = opendir(entry->name);
|
||||||
if ( !subdirectory )
|
if ( !subdirectory )
|
||||||
@@ -427,30 +428,16 @@ int display_directory_contents(
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char* entry_path = ( char* )calloc(
|
chdir( entry->name );
|
||||||
PATH_MAX,
|
|
||||||
sizeof(char)
|
|
||||||
);
|
|
||||||
if ( !entry_path ) continue;
|
|
||||||
|
|
||||||
snprintf(
|
|
||||||
entry_path,
|
|
||||||
PATH_MAX,
|
|
||||||
"%s/%s",
|
|
||||||
path,
|
|
||||||
entry->name
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO: handle return code
|
// TODO: handle return code
|
||||||
display_directory_contents(
|
display_directory_contents(
|
||||||
args,
|
args,
|
||||||
entry_path,
|
|
||||||
subdirectory,
|
subdirectory,
|
||||||
recursion_level + 1,
|
recursion_level + 1,
|
||||||
num_entries == n + 1
|
is_last | (size_t)( num_entries == n + 1 ) << recursion_level
|
||||||
);
|
);
|
||||||
|
closedir( subdirectory );
|
||||||
free( entry_path );
|
chdir( ".." );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -462,7 +449,6 @@ int display_directory_contents(
|
|||||||
free( entry_table[ n ] );
|
free( entry_table[ n ] );
|
||||||
}
|
}
|
||||||
free( entry_table );
|
free( entry_table );
|
||||||
closedir( directory );
|
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user