#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 700 #endif // external dependencies #include #include #include #include #include #include #include #include #include // internal dependencies #include "log.h" #include "arguments.h" #include "type_definitions.h" #include "sorting.h" #include "format.h" #include "config.h" // macro definitions #define SUCCESS 0 #define LENGTH_PERMISSIONS ( ( size_t )11 ) #define LENGTH_SIZE ( ( size_t )9 ) // constant declarations static char PERMISSION_CHARS[] = { 'x', 'w', 'r' }; static char SIZE_CHARS[] = { 'B', 'k', 'M', 'G', 'T', 'P' }; // function prototypes static int permissions_to_string( const arguments_t* const args, mode_t permissions, char* string ); static void size_to_string( directory_entry_t* entry, char* file_size, size_t length ); static int count_entries( DIR* directory, const arguments_t* const args ); static int get_entries( DIR* directory, directory_entry_t** entry_table, int num_entries, const arguments_t* const args ); static int is_entry_visible( const arguments_t* const args, char* entry_name ); static int display_directory_contents( const arguments_t* const args, DIR* directory, size_t recursion_level, size_t is_last ); // program logic int main( int argc, char** argv ) { arguments_t args; initialise_args( &args ); parse_args( argc, argv, &args ); if ( args.display_help ) return SUCCESS; DIR* directory = opendir( args.path ); if ( !directory ) { if ( ENOENT == errno ) log_error( "no directory `%s`", args.path ); else log_error( "failed to open directory `%s`", args.path ); return -EIO; } if ( args.tree_view ) log_info( "%s", args.path ); if ( !chdir( args.path ) ) { // TODO: handle return code display_directory_contents( &args, directory, 0, 0 ); } closedir( directory ); return SUCCESS; } int count_entries( DIR* directory, const arguments_t* const args ) { if ( !directory ) return -EINVAL; if ( !args ) return -EINVAL; int entry_count = 0; struct dirent* entry; rewinddir( directory ); while ( NULL != ( entry = readdir( directory ) ) ) { if ( is_entry_visible( args, entry->d_name ) ) continue; entry_count++; } rewinddir( directory ); return entry_count; } /* get entries of **current** directory */ int get_entries( DIR* directory, directory_entry_t** entry_table, int num_entries, const arguments_t* const args ) { if ( !directory ) return -EINVAL; if ( !entry_table ) return -EINVAL; if ( 0 >= num_entries ) return -EINVAL; if ( !args ) return -EINVAL; int entry_count = 0; directory_entry_t* new_entry; struct dirent* entry; struct stat file_stats = { 0 }; rewinddir( directory ); while ( NULL != ( entry = readdir( directory ) ) && entry_count < num_entries ) { if ( is_entry_visible( args, entry->d_name ) ) continue; new_entry = ( directory_entry_t* )malloc( sizeof(directory_entry_t) ); if ( !new_entry ) return -ENOMEM; if ( lstat( entry->d_name, &file_stats ) ) return -EIO; strncpy( new_entry->name, entry->d_name, NAME_MAX ); new_entry->stats = file_stats; switch ( file_stats.st_mode & S_IFMT ) { case S_IFDIR: new_entry->type = type_directory; break; case S_IFCHR: new_entry->type = type_character; break; case S_IFBLK: new_entry->type = type_block; break; case S_IFREG: new_entry->type = type_regular; break; case S_IFIFO: new_entry->type = type_fifo; break; case S_IFLNK: new_entry->type = type_link; break; case S_IFSOCK: new_entry->type = type_socket; break; } entry_table[entry_count] = new_entry; entry_count++; } return ( int )entry_count; } int permissions_to_string( const arguments_t* const args, mode_t file_mode, char* string ) { if ( !string ) return -EINVAL; if ( !string ) return -EINVAL; mode_t permissions = file_mode & ( S_IRWXU | S_IRWXG | S_IRWXO ); switch ( args->permission_mode ) { case no_permissions: string[0] = '\0'; break; case char_permissions: string[ 0 ] = ' '; for ( int n = 0; n < 9; n++ ) { if ( permissions & ( 1 << n ) ) { string[ 9 - n ] = PERMISSION_CHARS[ n % 3 ]; } else { string[ 9 - n ] = '-'; } } string[ 10 ] = '\0'; break; case octal_permissions: snprintf( &string[ 0 ], 10, " %3o", permissions ); break; } return SUCCESS; } /* check if entry should be displayed * * return 1 if entry should be ignored * return 0 otherwise */ int is_entry_visible( const arguments_t* const 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 SUCCESS; } int display_directory_contents( const arguments_t* const args, DIR* directory, size_t recursion_level, size_t is_last ) { if ( !args ) return -EINVAL; if ( !directory ) return -EINVAL; if ( recursion_level > args->depth ) return -EINVAL; char file_type; char file_permissions[LENGTH_PERMISSIONS]; char file_size[LENGTH_SIZE]; int ret; int num_entries = count_entries( directory, args ); if ( 0 > num_entries ) return -EIO; if ( 0 == num_entries ) return SUCCESS; directory_entry_t** entry_table = ( directory_entry_t** )calloc( ( size_t )num_entries, sizeof( directory_entry_t* ) ); if ( !entry_table ) return -ENOMEM; ret = get_entries( directory, entry_table, num_entries, args ); if ( 0 > ret ) { log_error( "failed to retrieve directory entries (error code = %d)", ret ); return ret; } ret = sort_entries( args, 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++ ) { directory_entry_t* entry = entry_table[ n ]; switch ( entry->type ) { case type_block: file_type = 'b'; break; case type_character: file_type = 'c'; break; case type_directory: file_type = 'd'; break; case type_fifo: file_type = 'f'; break; case type_link: file_type = 'l'; break; case type_regular: file_type = ' '; break; case type_socket: file_type = 's'; break; } ( void )permissions_to_string( args, entry->stats.st_mode, file_permissions ); size_to_string( entry, &file_size[ 0 ], LENGTH_SIZE ); if ( !args->tree_view ) { log_info( "%c%s%s %s", file_type, file_permissions, file_size, entry->name ); } else { char tree[ NAME_MAX ] = ""; for ( size_t k = 0; k < recursion_level; k++ ) { if ( !( is_last & 1 << k ) ) { tree[ 4 * k ] = '|'; } else { tree[ 4 * k ] = ' '; } tree[ 4 * k + 1 ] = ' '; tree[ 4 * k + 2 ] = ' '; tree[ 4 * k + 3 ] = ' '; } if ( num_entries != n + 1 ) { tree[ 4 * recursion_level ] = '+'; } else { tree[ 4 * recursion_level ] = '`'; } tree[ 4 * recursion_level + 1 ] = '-'; tree[ 4 * recursion_level + 2 ] = '-'; tree[ 4 * recursion_level + 3 ] = ' '; fprintf( stdout, "%s", tree ); if ( entry->type == type_directory ) { format_set_style( directory_style, DIR_STYLE_NUM ); format_set_fg_colour( directory_colour ); } fprintf( stdout, "%s", entry->name ); if ( entry->type == type_directory ) { format_reset_style( directory_style, DIR_STYLE_NUM ); format_clear_fg_colour(); } fprintf( stdout, "\n" ); if ( recursion_level < args->depth && entry->type == type_directory ) { DIR* subdirectory = opendir(entry->name); if ( !subdirectory ) { log_error( "failed to open directory `%s`", entry->name ); } else { chdir( entry->name ); // TODO: handle return code display_directory_contents( args, subdirectory, recursion_level + 1, is_last | (size_t)( num_entries == n + 1 ) << recursion_level ); closedir( subdirectory ); chdir( ".." ); } } } } // free all allocated elements/entries for ( int n = 0; n < num_entries; n++ ) { free( entry_table[ n ] ); } free( entry_table ); return SUCCESS; } void size_to_string( directory_entry_t* entry, char* file_size, size_t length ) { char size_quantifier = '\0'; double size_format = ( float )entry->stats.st_size; if ( entry->stats.st_mode & S_IFREG || entry->stats.st_mode & S_IFCHR ) { for ( int k = 0; k < ( int )sizeof( SIZE_CHARS ); k++ ) { if ( 1000.0f > size_format ) { size_quantifier = SIZE_CHARS[ k ]; break; } size_format /= 1000.0f; } if ( 10.0 > size_format ) { snprintf( &file_size[ 0 ], length, " %1.1f%c", size_format, size_quantifier ); } else { snprintf( &file_size[ 0 ], length, " %3.0f%c", size_format, size_quantifier ); } } else { snprintf( &file_size[ 0 ], length, " " ); } }