1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright 2025 Thorsten Töpper
*
* vim:ts=4:sw=4:expandtab
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "output.h"
#include "list_management.h"
/* === IMPLEMENTATION === */
inline struct list_node *create_node(char *fname, size_t fsize, time_t ftime) {
struct list_node *node = NULL;
if (fname == NULL || fname[0] == '\0') {
LOGERR("ERROR: No valid filename 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;
/* 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';
return node;
}
inline void destroy_list(struct list_head *list) {
struct list_node *ptr = NULL;
if (list == NULL)
return;
while (list->first != NULL) {
ptr = list->first->next;
if (ptr == NULL) {
free(list->first);
break;
}
list->first->next = ptr->next;
free(ptr);
}
free(list);
}
/* dirty... */
#define INSERT_BY_NUMERIC_FIELD(list, node, field) \
{ struct list_node *ptr = NULL; \
if (node->field > list->first->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) { \
node->next = ptr->next; ptr->next = node; \
return 0; } \
ptr = ptr->next; \
} \
}
/* 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; }
if (list->first == NULL) {
list->first = node;
return 0;
}
INSERT_BY_NUMERIC_FIELD(list, node, fsize);
return -3;
}
inline int insert_sorted_by_time(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; }
if (list->first == NULL) {
list->first = node;
return 0;
}
INSERT_BY_NUMERIC_FIELD(list, node, ftime);
return -3;
}
#undef INSERT_BY_NUMERIC_FIELD
struct list_head *create_list_sort_reversed(struct list_head *list) {
struct list_head *lh = NULL;
struct list_node *cpynode = NULL, *ptr = NULL;
if (list == NULL) {
LOGERR("ERROR: No list given\n");
return NULL;
}
if ((lh = calloc(1, sizeof(struct list_head))) == NULL) {
LOGERR("ERROR: Failed to allocate memory for a struct list_head\n");
return NULL;
}
lh->first = NULL;
ptr = list->first;
while (ptr != NULL) {
cpynode = create_node(ptr->fname, ptr->fsize, ptr->ftime);
if (cpynode == NULL) {
destroy_list(lh);
return NULL;
}
cpynode->next = lh->first;
lh->first = cpynode;
ptr = ptr->next;
}
return lh;
}
|