added positional argument, started experimenting with lstat()

This commit is contained in:
root committed 2026-06-24 23:27:51 +02:00
1 parent c87dd241a0
commit 0455ebaa40
3 files changed
+36 -18

No files matched your search

+2
View File
@@ -13,10 +13,12 @@
typedef struct {
bool show_all;
bool show_almost_all;
bool no_colour;
bool group_directories;
bool human_readable;
bool long_view;
bool tree_view;
char* path;
} arguments_t;
void initialise_args( arguments_t* arguments );
+26 -1
View File
@@ -3,6 +3,9 @@
#include "arguments.h"
#include "log.h"
// keys for arguments with no short version
#define KEY_NO_COLOUR ( ( int )0x100 )
// function prototypes
static error_t arg_parser( int key, char* arg, struct argp_state* state );
@@ -31,6 +34,14 @@ static struct argp_option options[] =
.doc = "same as `all`, but hiding `.` and `..`",
.group = 0
},
{
.name = "no-color",
.key = KEY_NO_COLOUR,
.arg = NULL,
.flags = 0,
.doc = "do not colour the output (for scripting)",
.group = 0
},
{
.name = "group-directories",
.key = 'g',
@@ -83,10 +94,12 @@ void initialise_args( arguments_t* arguments )
{
arguments->show_all = false;
arguments->show_almost_all = false;
arguments->no_colour = false;
arguments->group_directories = false;
arguments->human_readable = false;
arguments->long_view = false;
arguments->tree_view = false;
arguments->path = ".";
}
error_t parse_args( int argc, char** argv, arguments_t* arguments )
@@ -103,7 +116,6 @@ error_t parse_args( int argc, char** argv, arguments_t* arguments )
static error_t arg_parser( int key, char* arg, struct argp_state* state )
{
( void )arg;
arguments_t* arguments = ( arguments_t* )( state->input );
if ( !state ) return -EINVAL;
@@ -116,6 +128,9 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
case 'A':
arguments->show_almost_all = true;
break;
case KEY_NO_COLOUR:
arguments->no_colour = true;
break;
case 'g':
arguments->group_directories = true;
break;
@@ -128,6 +143,16 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
case 't':
arguments->tree_view = true;
break;
case ARGP_KEY_ARG:
if ( 0 == state->arg_num )
{
arguments->path = arg;
}
else
{
return ARGP_ERR_UNKNOWN;
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
+8 -17
View File
@@ -6,6 +6,7 @@
#include <dirent.h>
#include <errno.h>
#include <linux/limits.h>
#include <sys/stat.h>
// internal dependencies
#include "log.h"
@@ -20,42 +21,32 @@ int main( int argc, char** argv )
parse_args( argc, argv, &args );
char argument_directory[ PATH_MAX ] = ".";
DIR* directory = opendir( argument_directory );
DIR* directory = opendir( args.path );
struct dirent* entry;
if ( !directory )
{
if ( ENOENT == errno )
log_error( "no directory `%s`", argument_directory );
log_error( "no directory `%s`", args.path );
else
log_error( "failed to open directory `%s`", argument_directory );
log_error( "failed to open directory `%s`", args.path );
return -1;
}
while ( NULL != ( entry = readdir( directory ) ) )
{
char full_path[ PATH_MAX ]; // TODO: assemble full path
struct stat status = { 0 };
if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) )
{
continue;
}
log_info( "%s", entry->d_name );
// if ( lstat( ) ) // TODO: get file stats
log_info( "%s: %c", entry->d_name, entry->d_type );
}
closedir( directory );
// char current_directory[ PATH_MAX ];
//
// if ( NULL != getcwd( &current_directory[0], PATH_MAX ) )
// {
// log_info( "current directory: %s", current_directory );
// }
// else
// {
// log_error( "failed to get the current directory" );
// return -1;
// }
return 0;
}