structure and group directories
This commit is contained in:
1 parent
f2e8055172
commit
a29805cad2
9 files changed
+294
-50
No files matched your search
+115
@@ -0,0 +1,115 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include "sorting.h"
|
||||
#include "type_definitions.h"
|
||||
#include "log.h"
|
||||
#include "stringing.h"
|
||||
|
||||
/*
|
||||
* compare entries case insensitive
|
||||
*/
|
||||
int compare_entries( const void* operand_a, const void* operand_b )
|
||||
{
|
||||
if ( !operand_a || !operand_b )
|
||||
{
|
||||
log_error( "invalid parameter to compare function" );
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
||||
|
||||
return compare_strings( a->name, b->name, NAME_MAX, false );
|
||||
}
|
||||
|
||||
/*
|
||||
* compare entries case sensitive
|
||||
*/
|
||||
int compare_entries_casesensitive(
|
||||
const void* operand_a,
|
||||
const void* operand_b
|
||||
)
|
||||
{
|
||||
if ( !operand_a || !operand_b )
|
||||
{
|
||||
log_error( "invalid parameter to compare function" );
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
||||
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
||||
|
||||
return compare_strings( a->name, b->name, NAME_MAX, true );
|
||||
}
|
||||
|
||||
int group_directories( directory_entry_t** entry_table, int num_entries )
|
||||
{
|
||||
if ( !entry_table ) return -EINVAL;
|
||||
if ( 0 >= num_entries ) return -EINVAL;
|
||||
|
||||
int directory_counter = 0;
|
||||
int non_dir_counter = 0;
|
||||
directory_entry_t** directory_table =
|
||||
( directory_entry_t** )calloc(
|
||||
( size_t )num_entries,
|
||||
sizeof( directory_entry_t* )
|
||||
);
|
||||
if ( !directory_table ) return -ENOMEM;
|
||||
|
||||
directory_entry_t** non_dir_table =
|
||||
( directory_entry_t** )calloc(
|
||||
( size_t )num_entries,
|
||||
sizeof( directory_entry_t* )
|
||||
);
|
||||
if ( !non_dir_table )
|
||||
{
|
||||
free( directory_table );
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
for ( int n = 0; n < num_entries; n++ )
|
||||
{
|
||||
if ( entry_table[ n ]->stats.st_mode & S_IFDIR )
|
||||
{
|
||||
directory_table[ directory_counter ] = entry_table[ n ];
|
||||
directory_counter++;
|
||||
}
|
||||
else
|
||||
{
|
||||
non_dir_table[ non_dir_counter ] = entry_table[ n ];
|
||||
non_dir_counter++;
|
||||
}
|
||||
}
|
||||
if ( num_entries != directory_counter + non_dir_counter )
|
||||
log_error( "`group_directories()`: counting is difficult" );
|
||||
|
||||
qsort(
|
||||
( void * )directory_table,
|
||||
( size_t )directory_counter,
|
||||
sizeof( *directory_table ),
|
||||
compare_entries
|
||||
);
|
||||
qsort(
|
||||
( void * )non_dir_table,
|
||||
( size_t )non_dir_counter,
|
||||
sizeof( *non_dir_table ),
|
||||
compare_entries
|
||||
);
|
||||
|
||||
for ( int n = 0; n < directory_counter; n++ )
|
||||
{
|
||||
entry_table[ n ] = directory_table[ n ];
|
||||
}
|
||||
for ( int n = 0; n < non_dir_counter; n++ )
|
||||
{
|
||||
entry_table[ directory_counter + n ] = non_dir_table[ n ];
|
||||
}
|
||||
|
||||
free( directory_table );
|
||||
free( non_dir_table );
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user