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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright 2025 Thorsten Töpper
*
* vim:ts=4:sw=4:expandtab
*/
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "output.h"
#include "data_management.h"
#include "options.h"
/* === DEFINITIONS === */
#define out_vsep fputc('|', stdout)
#define out_print_newline() fputc('\n', stdout)
#define out_print_fname(x) printf(" %s ", x->fname)
void out_print_size(struct list_node *ptr);
void out_print_time(time_t tv);
void out_print_time_by_option(struct list_node *ptr);
/* === IMPLEMENTATION === */
inline int fputc_all_cols(char c, FILE *fdout) {
struct winsize terminal;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminal) == -1) {
if (errno != 25) {
LOGERR("ERROR: ioctl(STDOUT_FILENO, TIOCGWINSZ,...) failed: %s (errno %d)\n",
strerror(errno), errno);
}
terminal.ws_col = 72;
}
DBGTRC("DEBUG: terminal.ws_col = %d / terminal.ws_row = %d\n", terminal.ws_col, terminal.ws_row);
/* Somehow not always ws_col == 0, with optimization compiler flags and executed by watch. */
if (terminal.ws_col == 0) {
terminal.ws_col = 72;
}
return fputc_width_x(c, terminal.ws_col, fdout);
}
/* Not the most performant way, but during output the time critical tasks are already done. */
inline int fputc_width_x(char c, size_t x, FILE *fdout) {
size_t i=0;
if (fdout == NULL) {
LOGERR("ERROR: No output stream given.\n");
return EOF;
}
if ( ! isprint(c) ) { c = '?'; }
for (i=0; i<x; i++) {
if (fputc(c, fdout) == EOF) {
LOGERR("ERROR: Failed to write char 0x%02X to stream %d\n", c, fdout->_fileno);
return EOF;
}
}
return 1;
}
/* === FORMATTING RELATED FUNCTIONS === */
inline void out_print_size(struct list_node *ptr) {
printf(" %8ld %2s ", ((ptr->ln_stat.st_size>=1024) ? (ptr->ln_stat.st_size/1024) : ptr->ln_stat.st_size),
((ptr->ln_stat.st_size >= 1024) ? "kB" : ""));
}
inline void out_print_time(time_t tv) {
struct tm *tm = NULL;
char timestamp[128];
tm = localtime(&tv);
if (option_timestamp_long) {
strftime(timestamp, sizeof(timestamp), "%Y%m%d %H:%M:%S %Z", tm);
printf(" %20s ", timestamp);
} else {
strftime(timestamp, sizeof(timestamp), "%H:%M:%S", tm);
printf(" %8s ", timestamp);
}
}
inline void out_print_time_by_option(struct list_node *ptr) {
time_t time_value = 0;
switch (option_time_field) {
case 'm':
time_value = ptr->ln_stat.st_mtim.tv_sec;
break;
case 'a':
time_value = ptr->ln_stat.st_atim.tv_sec;
break;
case 'c':
time_value = ptr->ln_stat.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);
option_time_field = 'm';
time_value = ptr->ln_stat.st_mtim.tv_sec;
break;
};
out_print_time(time_value);
}
/* === END OF FORMATTING FUNCTIONS === */
/* The most simple default output, formatted output is planned */
void print_list(struct list_head *list) {
struct list_head *lh = list;
struct list_node *ptr;
size_t total_size = 0;
if (list == NULL) return;
if (option_sort_reverse_order) {
lh = create_list_sort_reversed(list);
/* be tolerant */
if (lh == NULL) {
lh = list;
}
}
ptr = lh->first;
while (ptr != NULL) {
out_print_size(ptr);
out_print_time_by_option(ptr);
out_print_fname(ptr);
out_print_newline();
/* Linux: Neither man stat(2) nor stat(3type) declare why struct stat field st_size
* would get a negative value assigned. The type off_t masks __off_t or __off64_t
* which themselves mask bits/types.h __SLONGWORD_TYPE on x86_64 that's long int.
* Haven't checked libc/kernel code but regardless why, I assume that a negative
* value would imply a corrupt filesystem.
*/
total_size += (ptr->ln_stat.st_size>0) ? (unsigned long int)ptr->ln_stat.st_size : 0;
ptr = ptr->next;
}
fputc_all_cols('=', stdout);
printf("\nTotal size: %lu %s\n", ((total_size>1024) ? total_size/1024 : total_size),
((total_size >= 1024) ? "kB" : ""));
if (lh != list) destroy_list(lh);
}
/* For now with a large walk around common format strings and prevention of attack vectors.
* n - name
* s - file size
*
* Time related
* t - timestamp set by option_time_field
* A - timestamp from atim.tv_sec
* C - timestamp from ctim.tv_sec
* M - timestamp from mtim.tv_sec
*/
void print_list_formatted(const char *format, struct list_head *list) {
struct list_head *lh = list;
struct list_node *ptr;
size_t format_len = 0, i = 0;
if (format == NULL || format[0] == '\0') {
LOGERR("ERROR: No format string given");
return;
}
if (list == NULL || list->first == NULL) {
LOGERR("ERROR: No data to print.\n");
return;
}
if (option_sort_reverse_order) {
lh = create_list_sort_reversed(list);
/* be tolerant */
if (lh == NULL) {
lh = list;
}
}
/* Note: the maximum length needs to be adjusted if the strings
* shall contain more complicated settings */
format_len = strnlen(format, 80);
ptr = lh->first;
while (ptr != NULL) {
for (i=0; i < format_len; i++) {
switch (format[i]) {
case 'n':
out_print_fname(ptr);
break;
case 's':
out_print_size(ptr);
break;
case 't':
out_print_time_by_option(ptr);
break;
case 'A':
out_print_time(ptr->ln_stat.st_atim.tv_sec);
break;
case 'C':
out_print_time(ptr->ln_stat.st_ctim.tv_sec);
break;
case 'M':
out_print_time(ptr->ln_stat.st_mtim.tv_sec);
break;
case ' ': /* just ignore this without warning */
break;
default:
LOGERR("WARNING: Invalid character 0x%02X ignoring it\n", format[i]);
break;
};
}
out_print_newline();
ptr = ptr->next;
}
if (lh != list) destroy_list(lh);
}
|