structure and group directories

This commit is contained in:
root committed 2026-07-01 20:50:09 +02:00
1 parent f2e8055172
commit a29805cad2
9 files changed
+294 -50

No files matched your search

+29 -45
View File
@@ -1,4 +1,3 @@
#include <asm-generic/errno-base.h>
#define _XOPEN_SOURCE 700
// external dependencies
@@ -7,9 +6,7 @@
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include <errno.h>
#include <linux/limits.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
@@ -17,12 +14,8 @@
// internal dependencies
#include "log.h"
#include "arguments.h"
// type definitions
typedef struct directory_entry {
char name[ NAME_MAX ];
mode_t mode;
} directory_entry_t;
#include "type_definitions.h"
#include "sorting.h"
// constant declarations
char permission_characters[3] = { 'x', 'w', 'r' };
@@ -37,7 +30,6 @@ int get_entries(
int num_entries,
arguments_t* args
);
int compare_entries( const void* operand_a, const void* operand_b );
// program logic
int main( int argc, char** argv )
@@ -47,6 +39,8 @@ int main( int argc, char** argv )
char permissions_string[10];
int ret;
// TODO: free memory before exiting on error!
initialise_args( &args );
parse_args( argc, argv, &args );
@@ -63,7 +57,8 @@ int main( int argc, char** argv )
}
int num_entries = count_entries( directory, &args );
if ( !num_entries ) return 0;
if ( 0 > num_entries ) return -EIO;
if ( 0 == num_entries ) return 0;
directory_entry_t** entry_table =
( directory_entry_t** )calloc(
@@ -79,22 +74,26 @@ int main( int argc, char** argv )
return ret;
}
qsort(
( void * )entry_table,
( size_t )num_entries,
sizeof( *entry_table ),
( __compar_fn_t )compare_entries
);
// __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
// );
// TODO: implement size aware output.
//
// struct winsize w;
// ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
ret = group_directories(entry_table, num_entries);
if ( 0 != ret )
{
log_error( "grouping directories failed with error [%d]", ret );
return ret;
}
for ( int n = 0; n < num_entries; n++ )
{
char* file_type;
switch ( entry_table[ n ]->mode & S_IFMT )
switch ( entry_table[ n ]->stats.st_mode & S_IFMT )
{
case S_IFBLK:
file_type = "b";
@@ -130,7 +129,7 @@ int main( int argc, char** argv )
case char_permissions:
permissions_to_string(
entry_table[ n ]->mode,
entry_table[ n ]->stats.st_mode,
permissions_string
);
log_info(
@@ -142,7 +141,8 @@ int main( int argc, char** argv )
break;
case octal_permissions:
permissions = mask_permissions( entry_table[ n ]->mode );
permissions =
mask_permissions( entry_table[ n ]->stats.st_mode );
log_info(
"%s %o %s",
file_type,
@@ -166,8 +166,8 @@ int main( int argc, char** argv )
int count_entries( DIR* directory, arguments_t* args )
{
if ( !directory ) return 0;
if ( !args ) return 0;
if ( !directory ) return -EINVAL;
if ( !args ) return -EINVAL;
int entry_count = 0;
struct dirent* entry;
@@ -203,7 +203,7 @@ int get_entries(
)
{
if ( !directory ) return -EINVAL;
if ( !num_entries ) return -EINVAL;
if ( 0 >= num_entries ) return -EINVAL;
if ( !args ) return -EINVAL;
int entry_count = 0;
@@ -212,7 +212,7 @@ int get_entries(
char full_path[PATH_MAX];
struct stat file_stats = { 0 };
if ( !entry_table )
if ( !entry_table )
{
log_error( "failed to allocate memory for the entry table" );
return -ENOMEM;
@@ -253,7 +253,7 @@ int get_entries(
if ( lstat( full_path, &file_stats ) ) return -EIO;
strncpy( new_entry->name, entry->d_name, NAME_MAX );
new_entry->mode = file_stats.st_mode;
new_entry->stats.st_mode = file_stats.st_mode;
entry_table[entry_count] = new_entry;
@@ -291,19 +291,3 @@ int permissions_to_string( mode_t file_mode, char* string )
return 0;
}
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;
log_info( "a = %s", a->name );
log_info( "b = %s", b->name );
return strncmp( a->name, b->name, NAME_MAX );
}