From 49997ac30b46c42a0366423cb2048f8257774805 Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Fri, 20 Jun 2025 01:45:13 +0200 Subject: Store everything from lstat() --- src/list_management.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/list_management.c') diff --git a/src/list_management.c b/src/list_management.c index 3b02d7f..582eccd 100644 --- a/src/list_management.c +++ b/src/list_management.c @@ -11,12 +11,13 @@ #include "output.h" #include "list_management.h" +#include "options.h" /* === IMPLEMENTATION === */ -inline struct list_node *create_node(char *fname, off_t fsize, time_t ftime) { +inline struct list_node *create_node(char *fname, struct stat *ln_stat) { struct list_node *node = NULL; if (fname == NULL || fname[0] == '\0') { @@ -24,19 +25,23 @@ inline struct list_node *create_node(char *fname, off_t fsize, time_t ftime) { return NULL; } + if (ln_stat == NULL) { + LOGERR("ERROR: No valid file information given\n"); + return NULL; + } + if ((node = calloc(1, sizeof(struct list_node))) == NULL) { LOGERR("ERROR: Failed allocate memory for node: %s\n", strerror(errno)); return NULL; } - node->next = NULL; - node->fsize = fsize; - node->ftime = ftime; + node->next = NULL; /* With strncpy and strict compiler options it complained. A file name * can't be longer than 256 bytes, including the string terminating \0 * byte at the end. As the code no longer's in the same file...*/ memcpy(node->fname, fname, 255); node->fname[255] = '\0'; + memcpy(&(node->ln_stat), ln_stat, sizeof(struct stat)); return node; } @@ -61,12 +66,12 @@ inline void destroy_list(struct list_head *list) { /* dirty... */ #define INSERT_BY_NUMERIC_FIELD(list, node, field) \ { struct list_node *ptr = NULL; \ - if (node->field > list->first->field) { \ + if (node->ln_stat.field > list->first->ln_stat.field) { \ node->next = list->first; list->first = node; return 0; } \ ptr = list->first; \ while (ptr != NULL) { \ if (ptr->next == NULL) { ptr->next = node; return 0; } \ - if (node->field > ptr->next->field) { \ + if (node->ln_stat.field > ptr->next->ln_stat.field) { \ node->next = ptr->next; ptr->next = node; \ return 0; } \ ptr = ptr->next; \ @@ -74,7 +79,6 @@ inline void destroy_list(struct list_head *list) { } -/* TODO: later when, other parameters are actively used move the action to a template */ inline int insert_sorted_by_size(struct list_head *list, struct list_node *node) { if (list == NULL) { LOGERR("ERROR: No list given.\n"); return -1; } if (node == NULL) { LOGERR("ERROR: No node given.\n"); return -2; } @@ -83,7 +87,7 @@ inline int insert_sorted_by_size(struct list_head *list, struct list_node *node) list->first = node; return 0; } - INSERT_BY_NUMERIC_FIELD(list, node, fsize); + INSERT_BY_NUMERIC_FIELD(list, node, st_size); return -3; } @@ -95,7 +99,22 @@ inline int insert_sorted_by_time(struct list_head *list, struct list_node *node) list->first = node; return 0; } - INSERT_BY_NUMERIC_FIELD(list, node, ftime); + switch (option_time_field) { + case 'm': /* default setting first */ + INSERT_BY_NUMERIC_FIELD(list, node, st_mtim.tv_sec); + break; + case 'a': + INSERT_BY_NUMERIC_FIELD(list, node, st_atim.tv_sec); + break; + case 'c': + INSERT_BY_NUMERIC_FIELD(list, node, st_ctim.tv_sec); + break; + default: + LOGERR("WARNING: option_time_field has invalid value %c 0x%02X - going by default m\n", + option_time_field, option_time_field); + INSERT_BY_NUMERIC_FIELD(list, node, st_mtim.tv_sec); + break; + }; return -3; } @@ -116,7 +135,7 @@ struct list_head *create_list_sort_reversed(struct list_head *list) { ptr = list->first; while (ptr != NULL) { - cpynode = create_node(ptr->fname, ptr->fsize, ptr->ftime); + cpynode = create_node(ptr->fname, &(ptr->ln_stat)); if (cpynode == NULL) { destroy_list(lh); return NULL; -- cgit v1.2.3-70-g09d2