220 lines
4.9 KiB
C
220 lines
4.9 KiB
C
#ifndef _XOPEN_SOURCE
|
|
#define _XOPEN_SOURCE 700
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <linux/limits.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <string.h>
|
|
#include "sorting.h"
|
|
#include "type_definitions.h"
|
|
#include "arguments.h"
|
|
#include "log.h"
|
|
#include "stringing.h"
|
|
|
|
/* function prototypes */
|
|
int compare_string_case_insensitive(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
);
|
|
int compare_string_case_sensitive(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
);
|
|
int compare_directory(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
);
|
|
int compare_extension(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
);
|
|
int compare_timestamp(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
);
|
|
int compare_size(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
);
|
|
int sort_directories_first(
|
|
directory_entry_t** const entry_table,
|
|
int num_entries
|
|
);
|
|
|
|
/* sort entries taking flags into account */
|
|
int sort_entries(
|
|
const arguments_t* const args,
|
|
directory_entry_t **const entry_table,
|
|
int num_entries
|
|
)
|
|
{
|
|
if ( !args ) return -EINVAL;
|
|
if ( !entry_table ) return -EINVAL;
|
|
if ( 0 > num_entries ) return -EINVAL;
|
|
|
|
__compar_fn_t compare_function;
|
|
|
|
if ( true == args->case_sensitive_sorting )
|
|
{
|
|
compare_function = compare_string_case_sensitive;
|
|
}
|
|
else
|
|
{
|
|
compare_function = compare_string_case_insensitive;
|
|
}
|
|
|
|
if ( args->sorting_mode != sort_by_name )
|
|
{
|
|
qsort(
|
|
( void* )entry_table,
|
|
( size_t )num_entries,
|
|
sizeof( *entry_table ),
|
|
compare_function
|
|
);
|
|
}
|
|
|
|
switch ( args->sorting_mode )
|
|
{
|
|
case sort_by_name:
|
|
break;
|
|
case sort_by_extension:
|
|
compare_function = compare_extension;
|
|
break;
|
|
case sort_by_time:
|
|
compare_function = compare_timestamp;
|
|
break;
|
|
case sort_by_size:
|
|
compare_function = compare_size;
|
|
break;
|
|
}
|
|
|
|
qsort(
|
|
( void* )entry_table,
|
|
( size_t )num_entries,
|
|
sizeof( *entry_table ),
|
|
compare_function
|
|
);
|
|
|
|
if ( true == args->group_directories )
|
|
{
|
|
qsort(
|
|
( void* )entry_table,
|
|
( size_t )num_entries,
|
|
sizeof( *entry_table ),
|
|
compare_directory
|
|
);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* compare entries case insensitive
|
|
*/
|
|
int compare_string_case_insensitive( 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_string_case_sensitive(
|
|
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 );
|
|
}
|
|
|
|
/* compare directory entries
|
|
*
|
|
*
|
|
*/
|
|
int compare_directory(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
)
|
|
{
|
|
if ( !operand_a || !operand_b )
|
|
{
|
|
log_error( "invalid parameter to compare function" );
|
|
exit( -EINVAL );
|
|
}
|
|
|
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
|
|
|
return ( b->stats.st_mode & S_IFDIR ) - ( a->stats.st_mode & S_IFDIR );
|
|
}
|
|
|
|
int compare_extension(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
)
|
|
{
|
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
|
|
|
if ( !a->extension && !b->extension ) return 0;
|
|
if ( !a->extension && b->extension ) return -1;
|
|
if ( a->extension && !b->extension ) return 1;
|
|
|
|
return compare_strings( a->extension, b->extension, NAME_MAX, false );
|
|
}
|
|
|
|
int compare_timestamp(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
)
|
|
{
|
|
if ( !operand_a || !operand_b )
|
|
{
|
|
log_error( "invalid parameter to compare function" );
|
|
exit( -EINVAL );
|
|
}
|
|
|
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
|
|
|
return ( int )( b->stats.st_mtim.tv_sec - a->stats.st_mtim.tv_sec );
|
|
}
|
|
|
|
int compare_size(
|
|
const void* operand_a,
|
|
const void* operand_b
|
|
)
|
|
{
|
|
if ( !operand_a || !operand_b )
|
|
{
|
|
log_error( "invalid parameter to compare function" );
|
|
exit( -EINVAL );
|
|
}
|
|
|
|
directory_entry_t* a = *( directory_entry_t** )operand_a;
|
|
directory_entry_t* b = *( directory_entry_t** )operand_b;
|
|
|
|
return ( int )( b->stats.st_size - a->stats.st_size );
|
|
}
|