This commit is contained in:
root committed 2026-07-19 21:40:21 +02:00
1 parent a29805cad2
commit 4b1c8c0de4
5 files changed
+148 -45

No files matched your search

+32 -35
View File
@@ -1,4 +1,6 @@
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
// external dependencies
#include <stdlib.h>
@@ -30,6 +32,7 @@ int get_entries(
int num_entries,
arguments_t* args
);
int is_entry_visible( arguments_t* args, char* entry_name );
// program logic
int main( int argc, char** argv )
@@ -74,16 +77,7 @@ int main( int argc, char** argv )
return ret;
}
// __compar_fn_t comparator = args.case_sensitive_sorting
// ? compare_entries_casesensitive : compare_entries;
// qsort(
// ( void * )entry_table,
// ( size_t )num_entries,
// sizeof( *entry_table ),
// comparator
// );
ret = group_directories(entry_table, num_entries);
ret = sort_entries(&args, entry_table, num_entries);
if ( 0 != ret )
{
log_error( "grouping directories failed with error [%d]", ret );
@@ -176,16 +170,7 @@ int count_entries( DIR* directory, arguments_t* args )
while ( NULL != ( entry = readdir( directory ) ) )
{
if ( !args->show_all && ( !strncmp( entry->d_name, ".", 2)
|| !strncmp( entry->d_name, "..", 3 ) ) )
{
continue;
}
if ( !args->show_almost_all && '.' == entry->d_name[0] )
{
continue;
}
if ( is_entry_visible( args, entry->d_name ) ) continue;
entry_count++;
}
@@ -223,21 +208,7 @@ int get_entries(
while ( NULL != ( entry = readdir( directory ) )
&& entry_count < num_entries )
{
/*
* it is important to compare test string length + 1.
* otherwise the check will succeed, if the name under test
* starts with the test string.
*/
if ( !args->show_all && ( !strncmp( entry->d_name, ".", 2)
|| !strncmp( entry->d_name, "..", 3 ) ) )
{
continue;
}
if ( !args->show_almost_all && ( '.' == entry->d_name[0] ) )
{
continue;
}
if ( is_entry_visible( args, entry->d_name ) ) continue;
new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
if ( !new_entry ) return -ENOMEM;
@@ -291,3 +262,29 @@ int permissions_to_string( mode_t file_mode, char* string )
return 0;
}
/* check if entry should be displayed
*
* return 1 if entry should be ignored
* return 0 otherwise
*/
int is_entry_visible( arguments_t* args, char* entry_name )
{
/*
* it is important to compare test string length + 1.
* otherwise the check will succeed, if the name under test
* starts with the test string.
*/
if ( !args->show_all && ( !strncmp( entry_name, ".", 2)
|| !strncmp( entry_name, "..", 3 ) ) )
{
return 1;
}
if ( !args->show_all && !args->show_almost_all
&& ( '.' == entry_name[0] ) )
{
return 1;
}
return 0;
}