added positional argument, started experimenting with lstat()
This commit is contained in:
1 parent
c87dd241a0
commit
0455ebaa40
3 files changed
+36
-18
No files matched your search
@@ -13,10 +13,12 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
bool show_all;
|
bool show_all;
|
||||||
bool show_almost_all;
|
bool show_almost_all;
|
||||||
|
bool no_colour;
|
||||||
bool group_directories;
|
bool group_directories;
|
||||||
bool human_readable;
|
bool human_readable;
|
||||||
bool long_view;
|
bool long_view;
|
||||||
bool tree_view;
|
bool tree_view;
|
||||||
|
char* path;
|
||||||
} arguments_t;
|
} arguments_t;
|
||||||
|
|
||||||
void initialise_args( arguments_t* arguments );
|
void initialise_args( arguments_t* arguments );
|
||||||
|
|||||||
+26
-1
@@ -3,6 +3,9 @@
|
|||||||
#include "arguments.h"
|
#include "arguments.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
// keys for arguments with no short version
|
||||||
|
#define KEY_NO_COLOUR ( ( int )0x100 )
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
static error_t arg_parser( int key, char* arg, struct argp_state* state );
|
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 `..`",
|
.doc = "same as `all`, but hiding `.` and `..`",
|
||||||
.group = 0
|
.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",
|
.name = "group-directories",
|
||||||
.key = 'g',
|
.key = 'g',
|
||||||
@@ -83,10 +94,12 @@ void initialise_args( arguments_t* arguments )
|
|||||||
{
|
{
|
||||||
arguments->show_all = false;
|
arguments->show_all = false;
|
||||||
arguments->show_almost_all = false;
|
arguments->show_almost_all = false;
|
||||||
|
arguments->no_colour = false;
|
||||||
arguments->group_directories = false;
|
arguments->group_directories = false;
|
||||||
arguments->human_readable = false;
|
arguments->human_readable = false;
|
||||||
arguments->long_view = false;
|
arguments->long_view = false;
|
||||||
arguments->tree_view = false;
|
arguments->tree_view = false;
|
||||||
|
arguments->path = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
error_t parse_args( int argc, char** argv, arguments_t* arguments )
|
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 )
|
static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
||||||
{
|
{
|
||||||
( void )arg;
|
|
||||||
arguments_t* arguments = ( arguments_t* )( state->input );
|
arguments_t* arguments = ( arguments_t* )( state->input );
|
||||||
|
|
||||||
if ( !state ) return -EINVAL;
|
if ( !state ) return -EINVAL;
|
||||||
@@ -116,6 +128,9 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
|||||||
case 'A':
|
case 'A':
|
||||||
arguments->show_almost_all = true;
|
arguments->show_almost_all = true;
|
||||||
break;
|
break;
|
||||||
|
case KEY_NO_COLOUR:
|
||||||
|
arguments->no_colour = true;
|
||||||
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
arguments->group_directories = true;
|
arguments->group_directories = true;
|
||||||
break;
|
break;
|
||||||
@@ -128,6 +143,16 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
|||||||
case 't':
|
case 't':
|
||||||
arguments->tree_view = true;
|
arguments->tree_view = true;
|
||||||
break;
|
break;
|
||||||
|
case ARGP_KEY_ARG:
|
||||||
|
if ( 0 == state->arg_num )
|
||||||
|
{
|
||||||
|
arguments->path = arg;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ARGP_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return ARGP_ERR_UNKNOWN;
|
return ARGP_ERR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-17
@@ -6,6 +6,7 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
// internal dependencies
|
// internal dependencies
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@@ -20,42 +21,32 @@ int main( int argc, char** argv )
|
|||||||
|
|
||||||
parse_args( argc, argv, &args );
|
parse_args( argc, argv, &args );
|
||||||
|
|
||||||
char argument_directory[ PATH_MAX ] = ".";
|
DIR* directory = opendir( args.path );
|
||||||
DIR* directory = opendir( argument_directory );
|
|
||||||
struct dirent* entry;
|
struct dirent* entry;
|
||||||
|
|
||||||
if ( !directory )
|
if ( !directory )
|
||||||
{
|
{
|
||||||
if ( ENOENT == errno )
|
if ( ENOENT == errno )
|
||||||
log_error( "no directory `%s`", argument_directory );
|
log_error( "no directory `%s`", args.path );
|
||||||
else
|
else
|
||||||
log_error( "failed to open directory `%s`", argument_directory );
|
log_error( "failed to open directory `%s`", args.path );
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ( NULL != ( entry = readdir( directory ) ) )
|
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 ) ) )
|
if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) )
|
||||||
{
|
{
|
||||||
continue;
|
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 );
|
closedir( directory );
|
||||||
|
|
||||||
// char current_directory[ PATH_MAX ];
|
|
||||||
//
|
|
||||||
// if ( NULL != getcwd( ¤t_directory[0], PATH_MAX ) )
|
|
||||||
// {
|
|
||||||
// log_info( "current directory: %s", current_directory );
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// log_error( "failed to get the current directory" );
|
|
||||||
// return -1;
|
|
||||||
// }
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user