diff --git a/src/main.c b/src/main.c index 2da62b6..c5ea28a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,17 +1,22 @@ +#define _XOPEN_SOURCE 700 + // external dependencies #include +#include #include #include #include -#include #include #include #include +#include +#include // internal dependencies #include "log.h" #include "arguments.h" + // program logic int main( int argc, char** argv ) { @@ -36,14 +41,48 @@ int main( int argc, char** argv ) while ( NULL != ( entry = readdir( directory ) ) ) { - char full_path[ PATH_MAX ]; // TODO: assemble full path - struct stat status = { 0 }; + char full_path[ PATH_MAX ]; if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) ) { continue; } - // if ( lstat( ) ) // TODO: get file stats - log_info( "%s: %c", entry->d_name, entry->d_type ); + + size_t path_length = strlen( args.path ); + char* seperator = args.path[ path_length - 1 ] == '/' ? "" : "/"; + + snprintf( full_path, PATH_MAX, "%s%s%s", args.path, seperator, entry->d_name ); + + struct stat file_stat = { 0 }; + if ( lstat( full_path, &file_stat ) ) + { + log_error( "failed to get file stats for [ %s ]", full_path ); + return -EIO; + } + + switch ( file_stat.st_mode & S_IFMT ) + { + case S_IFBLK: + log_info( "%s is a [ block device ]", full_path ); + break; + case S_IFCHR: + log_info( "%s is a [ character device ]", full_path ); + break; + case S_IFDIR: + log_info( "%s is a [ directory ]", full_path ); + break; + case S_IFIFO: + log_info( "%s is a [ fifo device ]", full_path ); + break; + case S_IFLNK: + log_info( "%s is a [ link ]", full_path ); + break; + case S_IFREG: + log_info( "%s is a [ file ]", full_path ); + break; + case S_IFSOCK: + log_info( "%s is a [ socket ]", full_path ); + break; + } } closedir( directory );