implemented entry table as linked list
This commit is contained in:
1 parent
b5811ecd39
commit
2c17c15472
4 files changed
+303
-60
No files matched your search
@@ -109,4 +109,8 @@ $(FLAGS_FILE):
|
|||||||
|
|
||||||
|
|
||||||
test: $(BIN)
|
test: $(BIN)
|
||||||
$@ .
|
$(BIN) \
|
||||||
|
--permissions=octal \
|
||||||
|
-A \
|
||||||
|
~/repositories/test-projects/zig/
|
||||||
|
|
||||||
@@ -10,6 +10,12 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <argp.h>
|
#include <argp.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
no_permissions = 0,
|
||||||
|
char_permissions = 1,
|
||||||
|
octal_permissions = 2
|
||||||
|
} permission_mode_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool show_all;
|
bool show_all;
|
||||||
bool show_almost_all;
|
bool show_almost_all;
|
||||||
@@ -18,6 +24,7 @@ typedef struct {
|
|||||||
bool human_readable;
|
bool human_readable;
|
||||||
bool long_view;
|
bool long_view;
|
||||||
bool tree_view;
|
bool tree_view;
|
||||||
|
permission_mode_t permission_mode;
|
||||||
char* path;
|
char* path;
|
||||||
} arguments_t;
|
} arguments_t;
|
||||||
|
|
||||||
|
|||||||
+52
-13
@@ -1,10 +1,18 @@
|
|||||||
#include <argp.h>
|
#include <argp.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
#include "arguments.h"
|
#include "arguments.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
// keys for arguments with no short version
|
// keys for arguments with no short version
|
||||||
#define KEY_NO_COLOUR ( ( int )0x100 )
|
#define KEY_SHOW_ALL ( ( int )'a' )
|
||||||
|
#define KEY_SHOW_ALMOST_ALL ( ( int )'A' )
|
||||||
|
#define KEY_NO_COLOUR ( ( int )0x100 )
|
||||||
|
#define KEY_GROUP_DIRECTORIES ( ( int )'g' )
|
||||||
|
#define KEY_HUMAN_READABLE ( ( int )'h' )
|
||||||
|
#define KEY_LONG_VIEW ( ( int )'l' )
|
||||||
|
#define KEY_PERMISSIONS ( ( int )'p' )
|
||||||
|
#define KEY_TREE_VIEW ( ( int )'t' )
|
||||||
|
|
||||||
// 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 );
|
||||||
@@ -20,7 +28,7 @@ static struct argp_option options[] =
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
.name = "all",
|
.name = "all",
|
||||||
.key = 'a',
|
.key = KEY_SHOW_ALL,
|
||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "show all items (including hidden ones)",
|
.doc = "show all items (including hidden ones)",
|
||||||
@@ -28,7 +36,7 @@ static struct argp_option options[] =
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "almost-all",
|
.name = "almost-all",
|
||||||
.key = 'A',
|
.key = KEY_SHOW_ALMOST_ALL,
|
||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "same as `all`, but hiding `.` and `..`",
|
.doc = "same as `all`, but hiding `.` and `..`",
|
||||||
@@ -44,7 +52,7 @@ static struct argp_option options[] =
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "group-directories",
|
.name = "group-directories",
|
||||||
.key = 'g',
|
.key = KEY_GROUP_DIRECTORIES,
|
||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "group directories at the top",
|
.doc = "group directories at the top",
|
||||||
@@ -52,7 +60,7 @@ static struct argp_option options[] =
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "human-readable",
|
.name = "human-readable",
|
||||||
.key = 'h',
|
.key = KEY_HUMAN_READABLE,
|
||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "show sizes in human readable format",
|
.doc = "show sizes in human readable format",
|
||||||
@@ -60,15 +68,23 @@ static struct argp_option options[] =
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "long",
|
.name = "long",
|
||||||
.key = 'l',
|
.key = KEY_LONG_VIEW,
|
||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "use long listing format",
|
.doc = "use long listing format",
|
||||||
.group = 0
|
.group = 0
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
.name = "permissions",
|
||||||
|
.key = KEY_PERMISSIONS,
|
||||||
|
.arg = "mode",
|
||||||
|
.flags = 0,
|
||||||
|
.doc = "use tree listing format",
|
||||||
|
.group = 0
|
||||||
|
},
|
||||||
{
|
{
|
||||||
.name = "tree",
|
.name = "tree",
|
||||||
.key = 'T',
|
.key = KEY_TREE_VIEW,
|
||||||
.arg = NULL,
|
.arg = NULL,
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
.doc = "use tree listing format",
|
.doc = "use tree listing format",
|
||||||
@@ -99,6 +115,7 @@ void initialise_args( arguments_t* arguments )
|
|||||||
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->permission_mode = no_permissions;
|
||||||
arguments->path = ".";
|
arguments->path = ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,27 +139,49 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
|
|||||||
|
|
||||||
switch ( key )
|
switch ( key )
|
||||||
{
|
{
|
||||||
case 'a':
|
case KEY_SHOW_ALL:
|
||||||
arguments->show_all = true;
|
arguments->show_all = true;
|
||||||
break;
|
break;
|
||||||
case 'A':
|
case KEY_SHOW_ALMOST_ALL:
|
||||||
arguments->show_almost_all = true;
|
arguments->show_almost_all = true;
|
||||||
break;
|
break;
|
||||||
case KEY_NO_COLOUR:
|
case KEY_NO_COLOUR:
|
||||||
arguments->no_colour = true;
|
arguments->no_colour = true;
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case KEY_GROUP_DIRECTORIES:
|
||||||
arguments->group_directories = true;
|
arguments->group_directories = true;
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case KEY_HUMAN_READABLE:
|
||||||
arguments->human_readable = true;
|
arguments->human_readable = true;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case KEY_LONG_VIEW:
|
||||||
arguments->long_view = true;
|
arguments->long_view = true;
|
||||||
break;
|
break;
|
||||||
case 't':
|
case KEY_TREE_VIEW:
|
||||||
arguments->tree_view = true;
|
arguments->tree_view = true;
|
||||||
break;
|
break;
|
||||||
|
case KEY_PERMISSIONS:
|
||||||
|
if ( !arg )
|
||||||
|
{
|
||||||
|
arguments->permission_mode = no_permissions;
|
||||||
|
}
|
||||||
|
else if ( !strncmp( "none", arg, 4 ) )
|
||||||
|
{
|
||||||
|
arguments->permission_mode = no_permissions;
|
||||||
|
}
|
||||||
|
else if ( !strncmp( "char", arg, 4 ) )
|
||||||
|
{
|
||||||
|
arguments->permission_mode = char_permissions;
|
||||||
|
}
|
||||||
|
else if ( !strncmp( "octal", arg, 5 ) )
|
||||||
|
{
|
||||||
|
arguments->permission_mode = octal_permissions;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ARGP_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
break;
|
||||||
case ARGP_KEY_ARG:
|
case ARGP_KEY_ARG:
|
||||||
if ( 0 == state->arg_num )
|
if ( 0 == state->arg_num )
|
||||||
{
|
{
|
||||||
|
|||||||
+239
-46
@@ -1,13 +1,14 @@
|
|||||||
#define _XOPEN_SOURCE 700
|
#define _XOPEN_SOURCE 700
|
||||||
|
|
||||||
// external dependencies
|
// external dependencies
|
||||||
#include <stdint.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <argp.h>
|
#include <argp.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
@@ -16,18 +17,37 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "arguments.h"
|
#include "arguments.h"
|
||||||
|
|
||||||
|
// type definitions
|
||||||
|
typedef struct directory_entry {
|
||||||
|
char name[ NAME_MAX ];
|
||||||
|
mode_t mode;
|
||||||
|
struct directory_entry* next;
|
||||||
|
} directory_entry_t;
|
||||||
|
|
||||||
|
// constant declarations
|
||||||
|
char permission_characters[3] = { 'x', 'w', 'r' };
|
||||||
|
|
||||||
|
// function prototypes
|
||||||
|
int permissions_to_string( mode_t permissions, char* string );
|
||||||
|
int mask_permissions( mode_t file_mode );
|
||||||
|
size_t count_entries( DIR* directory, arguments_t* args );
|
||||||
|
directory_entry_t* get_entries(
|
||||||
|
DIR* directory,
|
||||||
|
size_t num_entries,
|
||||||
|
arguments_t* args
|
||||||
|
);
|
||||||
|
|
||||||
// program logic
|
// program logic
|
||||||
int main( int argc, char** argv )
|
int main( int argc, char** argv )
|
||||||
{
|
{
|
||||||
arguments_t args;
|
arguments_t args;
|
||||||
|
int permissions;
|
||||||
|
char permissions_string[10];
|
||||||
|
|
||||||
initialise_args( &args );
|
initialise_args( &args );
|
||||||
|
|
||||||
parse_args( argc, argv, &args );
|
parse_args( argc, argv, &args );
|
||||||
|
|
||||||
DIR* directory = opendir( args.path );
|
DIR* directory = opendir( args.path );
|
||||||
struct dirent* entry;
|
|
||||||
|
|
||||||
if ( !directory )
|
if ( !directory )
|
||||||
{
|
{
|
||||||
@@ -39,53 +59,226 @@ int main( int argc, char** argv )
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ( NULL != ( entry = readdir( directory ) ) )
|
size_t num_entries = count_entries( directory, &args );
|
||||||
|
if ( !num_entries ) return 0;
|
||||||
|
|
||||||
|
directory_entry_t* entry_table =
|
||||||
|
get_entries( directory, num_entries, &args );
|
||||||
|
if ( !entry_table )
|
||||||
{
|
{
|
||||||
char full_path[ PATH_MAX ];
|
log_info( "`get_entries()` failed" );
|
||||||
if ( !args.show_all && ( !strncmp( entry->d_name, ".", 1) || !strncmp( entry->d_name, "..", 2 ) ) )
|
return -EIO;
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: implement size aware output.
|
||||||
|
//
|
||||||
|
// struct winsize w;
|
||||||
|
// ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
|
|
||||||
|
directory_entry_t* current_entry = entry_table;
|
||||||
|
|
||||||
|
while ( NULL != current_entry )
|
||||||
|
{
|
||||||
|
char* file_type;
|
||||||
|
switch ( current_entry->mode & S_IFMT )
|
||||||
|
{
|
||||||
|
case S_IFBLK:
|
||||||
|
file_type = "b";
|
||||||
|
break;
|
||||||
|
case S_IFCHR:
|
||||||
|
file_type = "c";
|
||||||
|
break;
|
||||||
|
case S_IFDIR:
|
||||||
|
file_type = "d";
|
||||||
|
break;
|
||||||
|
case S_IFIFO:
|
||||||
|
file_type = "f";
|
||||||
|
break;
|
||||||
|
case S_IFLNK:
|
||||||
|
file_type = "l";
|
||||||
|
break;
|
||||||
|
case S_IFREG:
|
||||||
|
file_type = " ";
|
||||||
|
break;
|
||||||
|
case S_IFSOCK:
|
||||||
|
file_type = "s";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
switch ( args.permission_mode )
|
||||||
|
{
|
||||||
|
case no_permissions:
|
||||||
|
log_info(
|
||||||
|
"%s %s",
|
||||||
|
file_type,
|
||||||
|
current_entry->name
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case char_permissions:
|
||||||
|
permissions_to_string(
|
||||||
|
current_entry->mode,
|
||||||
|
permissions_string
|
||||||
|
);
|
||||||
|
log_info(
|
||||||
|
"%s %s %s",
|
||||||
|
file_type,
|
||||||
|
permissions_string,
|
||||||
|
current_entry->name
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case octal_permissions:
|
||||||
|
permissions = mask_permissions( current_entry->mode );
|
||||||
|
log_info(
|
||||||
|
"%s %o %s",
|
||||||
|
file_type,
|
||||||
|
permissions,
|
||||||
|
current_entry->name
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_entry = current_entry->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// free all allocated elements/entries
|
||||||
|
directory_entry_t* next_entry;
|
||||||
|
while ( entry_table && num_entries )
|
||||||
|
{
|
||||||
|
next_entry = entry_table->next;
|
||||||
|
free( entry_table );
|
||||||
|
entry_table = next_entry;
|
||||||
|
num_entries--;
|
||||||
|
}
|
||||||
closedir( directory );
|
closedir( directory );
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t count_entries( DIR* directory, arguments_t* args )
|
||||||
|
{
|
||||||
|
if ( !directory ) return 0;
|
||||||
|
if ( !args ) return 0;
|
||||||
|
|
||||||
|
size_t entry_count = 0;
|
||||||
|
struct dirent* entry;
|
||||||
|
|
||||||
|
rewinddir( directory );
|
||||||
|
|
||||||
|
while ( NULL != ( entry = readdir( directory ) ) )
|
||||||
|
{
|
||||||
|
if ( !args->show_all && ( !strncmp( entry->d_name, ".", 1)
|
||||||
|
|| !strncmp( entry->d_name, "..", 2 ) ) )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !args->show_almost_all && '.' == entry->d_name[0] )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
entry_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
rewinddir( directory );
|
||||||
|
|
||||||
|
return entry_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
directory_entry_t* get_entries(
|
||||||
|
DIR* directory,
|
||||||
|
size_t num_entries,
|
||||||
|
arguments_t* args
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if ( !directory ) return NULL;
|
||||||
|
if ( !num_entries ) return NULL;
|
||||||
|
if ( !args ) return NULL;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* make sure to initialise the pointers to NULL, as random values
|
||||||
|
* will not be caught by `if ( !pointer )` and cause a
|
||||||
|
* segmentation fault on dereferencing.
|
||||||
|
*/
|
||||||
|
directory_entry_t* entry_table = NULL;
|
||||||
|
directory_entry_t* last_element = NULL;
|
||||||
|
directory_entry_t* new_element = NULL;
|
||||||
|
struct dirent* entry;
|
||||||
|
char full_path[PATH_MAX];
|
||||||
|
struct stat file_stats = { 0 };
|
||||||
|
|
||||||
|
rewinddir( directory );
|
||||||
|
|
||||||
|
while ( NULL != ( entry = readdir( directory ) ) && num_entries )
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* 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->d_name, ".", 2)
|
||||||
|
|| !strncmp( entry->d_name, "..", 3 ) ) )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !args->show_almost_all && ( '.' == entry->d_name[0] ) )
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_element = ( directory_entry_t* )malloc( sizeof(directory_entry_t) );
|
||||||
|
if ( !new_element ) return NULL;
|
||||||
|
if ( !entry_table ) entry_table = new_element;
|
||||||
|
|
||||||
|
snprintf(
|
||||||
|
full_path,
|
||||||
|
PATH_MAX,
|
||||||
|
"%s/%s",
|
||||||
|
args->path,
|
||||||
|
entry->d_name
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( lstat( full_path, &file_stats ) ) return NULL;
|
||||||
|
|
||||||
|
strncpy( new_element->name, entry->d_name, NAME_MAX );
|
||||||
|
new_element->mode = file_stats.st_mode;
|
||||||
|
new_element->next = NULL;
|
||||||
|
|
||||||
|
if ( NULL != last_element ) last_element->next = new_element;
|
||||||
|
last_element = new_element;
|
||||||
|
|
||||||
|
num_entries--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry_table;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mask_permissions( mode_t file_mode )
|
||||||
|
{
|
||||||
|
return file_mode & ( S_IRWXU | S_IRWXG | S_IRWXO );
|
||||||
|
}
|
||||||
|
|
||||||
|
int permissions_to_string( mode_t file_mode, char* string )
|
||||||
|
{
|
||||||
|
if ( !string ) return -EINVAL;
|
||||||
|
|
||||||
|
int n;
|
||||||
|
int permissions = mask_permissions( file_mode );
|
||||||
|
|
||||||
|
for ( n = 0; n < 9; n++ )
|
||||||
|
{
|
||||||
|
if ( permissions & ( 1 << n ) )
|
||||||
|
{
|
||||||
|
string[ 8 - n ] = permission_characters[ n % 3 ];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string[ 8 - n ] = '-';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
string[ 9 ] = '\0';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user