summaryrefslogtreecommitdiff
path: root/Userland/Utilities/pathchk.cpp
blob: 77caa9757059d3928e0583593cd833477bd6818e (plain)
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
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <bits/posix1_lim.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    bool fail = false;
    static bool flag_most_posix = false;
    static bool flag_portability = false;
    static bool flag_empty_name_and_leading_dash = false;

    if (pledge("stdio rpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    Vector<const char*> paths;

    Core::ArgsParser args_parser;
    args_parser.add_option(flag_most_posix, "Check for most POSIX systems", nullptr, 'p');
    args_parser.add_option(flag_empty_name_and_leading_dash, "Check for empty names and leading dash", nullptr, 'P');
    args_parser.add_option(flag_portability, "Check portability (equivalent to -p and -P)", "portability", '\0');
    args_parser.add_positional_argument(paths, "Path to check", "path", Core::ArgsParser::Required::Yes);
    args_parser.parse(argc, argv);

    if (flag_portability) {
        flag_most_posix = true;
        flag_empty_name_and_leading_dash = true;
    }

    for (auto& path : paths) {
        auto str_path = String(path);
        unsigned long path_max = flag_most_posix ? _POSIX_PATH_MAX : pathconf(str_path.characters(), _PC_PATH_MAX);
        unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(str_path.characters(), _PC_NAME_MAX);

        if (str_path.length() > path_max) {
            warnln("{}: limit {} exceeded by length {} of filename '{}'", argv[0], path_max, str_path.length(), str_path);
            fail = true;
            continue;
        }

        if (flag_most_posix) {
            // POSIX portable filename character set (a-z A-Z 0-9 . _ -)
            for (long unsigned i = 0; i < str_path.length(); ++i) {
                auto c = path[i];
                if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != '/' && c != '.' && c != '-' && c != '_') {
                    warnln("{}: non-portable character '{}' in filename '{}'", argv[0], path[i], str_path);
                    fail = true;
                    continue;
                }
            }
        } else {
            struct stat st;
            if (lstat(str_path.characters(), &st) < 0) {
                if (errno != ENOENT) {
                    warnln("{}: directory is not searchable '{}'", argv[0], str_path);
                    fail = true;
                    continue;
                }
            }
        }

        if (flag_empty_name_and_leading_dash) {
            if (str_path.is_empty()) {
                warnln("{}: empty filename", argv[0]);
                fail = true;
                continue;
            }
        }

        for (auto& component : str_path.split('/')) {
            if (flag_empty_name_and_leading_dash) {
                if (component.starts_with('-')) {
                    warnln("{}: leading '-' in a component of filename '{}'", argv[0], str_path);
                    fail = true;
                    break;
                }
            }
            if (component.length() > name_max) {
                warnln("{}: limit {} exceeded by length {} of filename component '{}'", argv[0], name_max, component.length(), component.characters());
                fail = true;
                break;
            }
        }
    }

    return fail;
}