aboutsummaryrefslogtreecommitdiff
path: root/src/output.c
blob: 4c660d69c5b13ce0a3e9a56f2e3265ca159a0968 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* SPDX-License-Identifier: Apache-2.0 */

/* Copyright 2025 Thorsten Töpper
 *
 * vim:ts=4:sw=4:expandtab
 */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <stdint.h>
#include <errno.h>

#include "output.h"
#include "data_management.h"
#include "options.h"



/* === DEFINITIONS === */

#define DM_OUT_FORMAT_MAX_LEN 80

#define box_cross '+'
#define box_hsep  '-'
#define box_vsep  '|'

#define out_cross() fputc(box_cross, stdout)
#define out_hsep() fputc(box_hsep, stdout)
#define out_vsep() fputc(box_vsep, stdout)

#define out_print_newline()  fputc('\n', stdout)
#define out_print_fname(x)   printf(" %s ", x->fname)
#define out_print_uid(x)     printf(" %4u ", x->ln_stat.st_gid)
#define out_print_gid(x)     printf(" %4u ", x->ln_stat.st_uid)

void out_print_group_name(struct list_node *ptr);
void out_print_permissions(struct list_node *ptr);
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);
void out_print_type(struct list_node *ptr);
void out_print_user_name(struct list_node *ptr);

void out_print_format_string_header(const char *format, size_t longest_fname);

size_t estimate_formatted_line_length(const char *format, size_t longest_fname);
size_t get_longest_filename(struct list_head *list);


char *global_sep_line = NULL;

/* === 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;
}

/* basic 4 due to formatted output */
inline size_t get_longest_filename(struct list_head *list) {
    size_t rc = 4, tmp = 0;
    struct list_node *ptr = NULL;
    if (list == NULL) { return 0; }
    ptr = list->first;
    while (ptr != NULL) {
        tmp = strlen(ptr->fname);
        if (tmp>rc) { rc = tmp; }
        ptr = ptr->next;
    }
    return rc;
}


/* === FORMATTING RELATED FUNCTIONS === */
/* Be aware: getting the name via uid takes a lot of time depending on the
 * systems setup. local, NIS, LDAP ...
 * Caching happens outside the executable, minimal here for skipping funcion calls. */
inline void out_print_group_name(struct list_node *ptr) {
    struct group *group = NULL;
    static gid_t prev_gid = UINT32_MAX;
    static char prev_group_name[64] = "";
    size_t group_name_length = 0;

    /* For the unlikely case of multi threading this program: lock required here... */
    if (prev_gid == ptr->ln_stat.st_gid) {
        if (prev_group_name[0] == '\0') { /* first call of function and GID UINT32_MAX ? */
            if ((group = getgrgid(prev_gid)) == NULL) {
                LOGERR("ERROR: Failed to resolve information for gid %u: %s (errno %d)\n",
                        prev_gid, strerror(errno), errno);
                return;
            }
            group_name_length = strlen(group->gr_name);
            group_name_length = (group_name_length > 63) ? 63 : group_name_length;
            explicit_bzero(prev_group_name, 64);
            memcpy(prev_group_name, group->gr_name, group_name_length);
        }
        printf(" %-10s ", prev_group_name);
        return;
    }

    prev_gid = ptr->ln_stat.st_gid;
    if ((group = getgrgid(prev_gid)) == NULL) {
        LOGERR("ERROR: Failed to resolve information for gid %u: %s (errno %d)\n",
                prev_gid, strerror(errno), errno);
        return;
    }
    group_name_length = strlen(group->gr_name);
    group_name_length = (group_name_length > 63) ? 63 : group_name_length;
    explicit_bzero(prev_group_name, 64);
    memcpy(prev_group_name, group->gr_name, group_name_length);
    printf(" %-10s ", prev_group_name);
}

/* Reminder: resolving may take some time even with cache in libraries,
 * minimal cache here to prevent function call */
inline void out_print_user_name(struct list_node *ptr) {
    struct passwd *pwd = NULL;
    static uid_t prev_uid = UINT32_MAX;
    static char prev_user_name[64] = "";
    size_t user_name_length = 0;

    /* For the unlikely case of multi threading this program: lock required here... */
    if (prev_uid == ptr->ln_stat.st_uid) {
        if (prev_user_name[0] == '\0') { /* first call of function and UID UINT32_MAX ? */
            if ((pwd = getpwuid(prev_uid)) == NULL) {
                LOGERR("ERROR: Failed to resolve information for uid %u: %s (errno %d)\n",
                        prev_uid, strerror(errno), errno);
                return;
            }
            user_name_length = strlen(pwd->pw_name);
            user_name_length = (user_name_length > 63) ? 63 : user_name_length;
            explicit_bzero(prev_user_name, 64);
            memcpy(prev_user_name, pwd->pw_name, user_name_length);
        }
        printf(" %-10s ", prev_user_name);
        return;
    }

    prev_uid = ptr->ln_stat.st_uid;
    if ((pwd = getpwuid(prev_uid)) == NULL) {
        LOGERR("ERROR: Failed to resolve information for uid %u: %s (errno %d)\n",
                prev_uid, strerror(errno), errno);
        return;
    }
    user_name_length = strlen(pwd->pw_name);
    user_name_length = (user_name_length > 63) ? 63 : user_name_length;
    explicit_bzero(prev_user_name, 64);
    memcpy(prev_user_name, pwd->pw_name, user_name_length);
    printf(" %-10s ", prev_user_name);
}

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(" %-22s ", 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);
}

inline void out_print_type(struct list_node *ptr) {
    char entry[16] = "";

    /* regular file, directory and symlinks are most common */
    if (S_ISREG(ptr->ln_stat.st_mode)) {
        sprintf(entry, "regular file");
    } else if (S_ISDIR(ptr->ln_stat.st_mode)) {
        sprintf(entry, "directory");
    } else if (S_ISLNK(ptr->ln_stat.st_mode)) {
        sprintf(entry, "symlink");
    } else if (S_ISBLK(ptr->ln_stat.st_mode)) {
        sprintf(entry, "block");
    } else if (S_ISCHR(ptr->ln_stat.st_mode)) {
        sprintf(entry, "char");
    } else if (S_ISFIFO(ptr->ln_stat.st_mode)) {
        sprintf(entry, "fifo");
    } else if (S_ISSOCK(ptr->ln_stat.st_mode)) {
        sprintf(entry, "socket");
    /* The following macros operate on the complete struct stat */
    } else if (S_TYPEISMQ(&(ptr->ln_stat))) {
        sprintf(entry, "message queue");
    } else if (S_TYPEISSEM(&(ptr->ln_stat))) {
        sprintf(entry, "semaphore");
    } else if (S_TYPEISSHM(&(ptr->ln_stat))) {
        sprintf(entry, "shared memory");
#if 0
    /* Although mentioned in the POSIX man page, not part of glibc */
    } else if (S_TYPEISTMO(&(ptr->ln_stat))) {
        sprintf(entry, "memory object");
#endif
    } else {
        sprintf(entry, "unidentified");
    }
    /* The ones longer than 12 characters are rare. */
    printf(" %-12s ", entry);
}

inline void out_print_permissions(struct list_node *ptr) {
    mode_t mode = 0;
    /* User permissions */
    if (ptr->ln_stat.st_mode & S_IXUSR) {
        mode |= S_IXUSR;
    }
    if (ptr->ln_stat.st_mode & S_IWUSR) {
        mode |= S_IWUSR;
    }
    if (ptr->ln_stat.st_mode & S_IRUSR) {
        mode |= S_IRUSR;
    }
    /* Group permissions */
    if (ptr->ln_stat.st_mode & S_IXGRP) {
        mode |= S_IXGRP;
    }
    if (ptr->ln_stat.st_mode & S_IWGRP) {
        mode |= S_IWGRP;
    }
    if (ptr->ln_stat.st_mode & S_IRGRP) {
        mode |= S_IRGRP;
    }
    /* Other permissions */
    if (ptr->ln_stat.st_mode & S_IXOTH) {
        mode |= S_IXOTH;
    }
    if (ptr->ln_stat.st_mode & S_IWOTH) {
        mode |= S_IWOTH;
    }
    if (ptr->ln_stat.st_mode & S_IROTH) {
        mode |= S_IROTH;
    }
    /* special bits */
    if (ptr->ln_stat.st_mode & S_ISVTX) {
        mode |= S_ISVTX;
    }
    if (ptr->ln_stat.st_mode & S_ISGID) {
        mode |= S_ISGID;
    }
    if (ptr->ln_stat.st_mode & S_ISUID) {
        mode |= S_ISUID;
    }
    printf(" %4o ", mode);
}

/* Interpret the format string and based on it generate a size which will be
 * able to store everything including visual additions. Use strlen later.  */
inline size_t estimate_formatted_line_length(const char *format, size_t longest_fname) {
    size_t rc = 0, i=0, format_len=0;
    static int login_name_max = -1;
    if (login_name_max < 0) {
        /* TODO: Check whether all UNIX likes use this for user and group name*/
        if ((login_name_max=(int)sysconf(_SC_LOGIN_NAME_MAX)) < 0) {
            LOGERR("ERROR: sysconf(_SC_LOGIN_NAME_MAX) failed: %s (errno %d)\n",
                strerror(errno), errno);
            return 0;
        }
        DBGTRC("DEBUG: sysconf(_SC_LOGIN_NAME_MAX) = %d\n", login_name_max);
        login_name_max += 4; /* formatting additions */
    }
    format_len = strnlen(format, DM_OUT_FORMAT_MAX_LEN);
    for (i=0; i<format_len; i++) {
        switch (format[i]) {
            case 'n': rc += longest_fname+4; break;
            case 'G':
            case 'U': rc += ((unsigned int)login_name_max); break;
            case 'g': /* Short ones */
            case 'p':
            case 'u': rc += 8; break;
            case ' ': break;
            default: rc += 28; /* time, and strings >10 in format*/
        };
    }
    DBGTRC("DEBUG: format string '%s' estimated with %lu bytes\n", format, rc);
    return rc;
}

inline void out_print_format_string_header(const char *format, size_t longest_fname) {
    size_t i=0, j=0, k=0, format_len=0, est_length=0, tmp = 0;
    char *header = NULL, *sep_line = NULL;
    char *pos = NULL;

    if (format == NULL || format[0] == '\0' || longest_fname == 0) return;
    DBGTRC("DEBUG: format string: \"%s\"\n", format);

    format_len = strnlen(format, DM_OUT_FORMAT_MAX_LEN);

    est_length = estimate_formatted_line_length(format, longest_fname);
    if ((header=calloc(est_length, sizeof(char))) == NULL) {
        LOGERR("ERROR: Failed to allocate memory for header\n");
        return;
    }

    pos = header;

    if (option_print_boxed_table) {
        if ((sep_line=calloc(est_length, sizeof(char))) == NULL) {
            free(header);
            LOGERR("ERROR: Failed to allocate memory for sep line\n");
            return;
        }
        if (global_sep_line != NULL) { free(global_sep_line); }
        pos[0] = box_vsep;
        pos++;
        sep_line[j++] = box_cross;
    }

#define LOCAL_EXTEND_SEP_LINE() { \
    if (option_print_boxed_table) { \
        tmp = strlen(pos); \
        for (k=0; k<tmp; k++) { sep_line[j++] = box_hsep; } \
        sep_line[j++] = box_cross;   pos[tmp] = box_vsep; } }

    /* Prevent buffer overflow, with the header string 32 byte buffer */
    for (i=0; (i < format_len) && (strlen(header) < 4064); i++) {
        switch (format[i]) {
            case 'n':
                sprintf(pos, " FILE ");
                tmp = strlen(pos);
                for (k=0; k<longest_fname-4; k++) {
                    pos[tmp+k] = ' ';
                }
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 's':
                sprintf(pos, " %11s ", "SIZE   ");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 't':
                if (option_timestamp_long) {
                    sprintf(pos, " %c%-21s ", toupper(option_time_field), "TIME");
                } else {
                    sprintf(pos, " %c%-7s ", toupper(option_time_field), "TIME");
                }
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'A':
                if (option_timestamp_long) {
                    sprintf(pos, " %-22s ", "ACCESS TIME");
                } else {
                    sprintf(pos, " %-8s ", "ATIME");
                }
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'C':
                if (option_timestamp_long) {
                    sprintf(pos, " %-22s ", "CREATION TIME");
                } else {
                    sprintf(pos, " %-8s ", "CTIME");
                }
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'M':
                if (option_timestamp_long) {
                    sprintf(pos, " %-22s ", "MODIFICATION TIME");
                } else {
                    sprintf(pos, " %-8s ", "MTIME");
                }
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'T':
                sprintf(pos, " %-12s ", "TYPE");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'u':
                sprintf(pos, " %-4s ", "UID");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'g':
                sprintf(pos, " %-4s ", "GID");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'U':
                sprintf(pos, " %-10s ", "USER");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'G':
                sprintf(pos, " %-10s ", "GROUP");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case 'p':
                sprintf(pos, " %-4s ", "PERM");
                LOCAL_EXTEND_SEP_LINE();
                break;
            case ' ':
                break;
            default:
                LOGERR("WARNING: Invalid character 0x%02X ignoring it\n", format[i]);
                break;
        };
        pos = &header[strlen(header)];
    }
    /* Output to stdout */
    if (option_print_boxed_table) {
        fputs(sep_line, stdout);
        out_print_newline();
    }
    fputs(header, stdout);
    out_print_newline();
    if (option_print_boxed_table) {
        tmp = strlen(sep_line);
        fputs(sep_line, stdout);
        if ((global_sep_line = calloc(tmp+1, sizeof(char))) != NULL) {
            memcpy(global_sep_line, sep_line, tmp);
            free(sep_line);
        }
    } else {
        fputc_width_x('-', strlen(header), stdout);
    }
    out_print_newline();
    free(header);
#undef LOCAL_EXTEND_SEP_LINE
}
/* === 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;
    size_t longest_fname = 0, i = 0;

    if (list == NULL) return;
    if (option_sort_reverse_order) {
        lh = create_list_sort_reversed(list);
        /* be tolerant */
        if (lh == NULL) {
            lh = list;
        }
    }

    longest_fname = get_longest_filename(list);

    if (option_print_header) {
        out_print_format_string_header("stn", longest_fname);
    }

    ptr = lh->first;
    while (ptr != NULL) {
        if (option_print_boxed_table) out_vsep();
        out_print_size(ptr);
        if (option_print_boxed_table) out_vsep();
        out_print_time_by_option(ptr);
        if (option_print_boxed_table) out_vsep();
        out_print_fname(ptr);
        if (option_print_boxed_table) {
            for (i=0; i<(longest_fname-strlen(ptr->fname)); i++) fputc(' ', stdout);
            out_vsep();
        }
        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;
    }
    if (option_print_boxed_table && global_sep_line != NULL) {
        fputs(global_sep_line, stdout);
    } else {
        fputc_all_cols('=', stdout);
    }
    out_print_newline();
    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
 * T - type
 * U - user name
 * u - user id
 * G - group name
 * g - group id
 * p - permissions and special bits
 *
 * 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, j = 0;
    size_t longest_fname = 0;
    size_t total_size = 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, DM_OUT_FORMAT_MAX_LEN);
    DBGTRC("DEBUG: format_len = %lu\n", format_len);

    longest_fname = get_longest_filename(list);

    if (option_print_header) {
        out_print_format_string_header(format, longest_fname);
    }

    ptr = lh->first;
    while (ptr != NULL) {
        if (option_print_boxed_table) { out_vsep(); }
        for (i=0; i < format_len; i++) {
            switch (format[i]) {
                case 'n':
                    out_print_fname(ptr);
                    for (j=0; j<(longest_fname - strlen(ptr->fname)); j++) {
                        fputc(' ', stdout);
                    }
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 's':
                    out_print_size(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 't':
                    out_print_time_by_option(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'A':
                    out_print_time(ptr->ln_stat.st_atim.tv_sec);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'C':
                    out_print_time(ptr->ln_stat.st_ctim.tv_sec);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'M':
                    out_print_time(ptr->ln_stat.st_mtim.tv_sec);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'T':
                    out_print_type(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'u':
                    out_print_uid(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'g':
                    out_print_gid(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'U':
                    out_print_user_name(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'G':
                    out_print_group_name(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case 'p':
                    out_print_permissions(ptr);
                    if (option_print_boxed_table) { out_vsep(); }
                    break;
                case ' ': /* just ignore this without warning */
                    break;
                default:
                    LOGERR("WARNING: Invalid character 0x%02X ignoring it\n", format[i]);
                    break;
            };
        }
        out_print_newline();
        total_size += (ptr->ln_stat.st_size>0) ? (unsigned long int)ptr->ln_stat.st_size : 0;
        ptr = ptr->next;
    }
    if (option_print_boxed_table && global_sep_line != NULL) {
        fputs(global_sep_line, stdout);
    } else {
        fputc_all_cols('=', stdout);
    }
    out_print_newline();
    printf("\nTotal size: %lu %s\n", ((total_size>1024) ? total_size/1024 : total_size),
            ((total_size >= 1024) ? "kB" : ""));
    if (lh != list) destroy_list(lh);
}