aboutsummaryrefslogtreecommitdiff
path: root/src/dir_monitor.c
blob: 419b9f4358a2171d821d09d709dd7659577bedeb (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
/* SPDX-License-Identifier: Apache-2.0 */

/* Copyright 2025 Thorsten Töpper
 *
 * dir_monitor - print specified stat() information from entries of a given
 * directory to stdout. Call it via watch for repeated output to a terminal.
 *
 * vim:ts=4:sw=4:expandtab
 */
#include <stdlib.h>

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



int main(int argc, char **argv) {
    struct list_head *list = NULL;
    int path_index = 1;

    if (argc > 2) {
        path_index = parse_arguments(argc, argv);
        if (path_index == argc) {
            usage(argv[0]);
            return EXIT_FAILURE;
        }
        list = get_data_from_directory(argv[path_index]);
    } else if (argc == 2) {
        if (argv[1][0] == '-') {
            fputc('\n', stderr);
            LOGERR("WARNING: if '%s' is a valid path use a prefix like ./\n\n",
                    argv[1]);
            usage(argv[0]);
            return EXIT_FAILURE;
        }
        list = get_data_from_directory(argv[1]);
    } else {
        list = get_data_from_directory(".");
    }

    /* Make output code simpler, check whether there were any symlinks resolved */
    if (option_resolve_symlinks) {
        option_resolve_symlinks = contains_resolved_symlinks(list);
    }

    if (option_format_string == NULL) {
        print_list(list);
    } else {
        print_list_formatted(option_format_string, list);
    }
    destroy_list(list);

    return EXIT_SUCCESS;
}