summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrendan Coles <bcoles@gmail.com>2021-04-23 20:31:21 +0000
committerLinus Groh <mail@linusgroh.de>2021-04-24 11:48:57 +0200
commite0188d27dedcf38fc92eeeff42363498bd9d61a1 (patch)
tree8131028328981e1c9e191af66874927348c5476a
parentac98dc4f7c1742f19e92227cda1998a77a68fe2e (diff)
downloadserenity-e0188d27dedcf38fc92eeeff42363498bd9d61a1.zip
Utilities: Add pathchk
-rw-r--r--Userland/Libraries/LibC/unistd.cpp4
-rw-r--r--Userland/Utilities/pathchk.cpp96
2 files changed, 100 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp
index 13acfbdcb8..f1daf87692 100644
--- a/Userland/Libraries/LibC/unistd.cpp
+++ b/Userland/Libraries/LibC/unistd.cpp
@@ -591,6 +591,8 @@ int mknod(const char* pathname, mode_t mode, dev_t dev)
long fpathconf([[maybe_unused]] int fd, [[maybe_unused]] int name)
{
switch (name) {
+ case _PC_NAME_MAX:
+ return NAME_MAX;
case _PC_PATH_MAX:
return PATH_MAX;
case _PC_VDISABLE:
@@ -603,6 +605,8 @@ long fpathconf([[maybe_unused]] int fd, [[maybe_unused]] int name)
long pathconf([[maybe_unused]] const char* path, int name)
{
switch (name) {
+ case _PC_NAME_MAX:
+ return NAME_MAX;
case _PC_PATH_MAX:
return PATH_MAX;
case _PC_PIPE_BUF:
diff --git a/Userland/Utilities/pathchk.cpp b/Userland/Utilities/pathchk.cpp
new file mode 100644
index 0000000000..d4ff7fc9aa
--- /dev/null
+++ b/Userland/Utilities/pathchk.cpp
@@ -0,0 +1,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 file name '{}'", argv[0], path_max, str_path.length(), str_path);
+ fail = true;
+ continue;
+ }
+
+ if (flag_most_posix) {
+ // POSIX portable file name 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 file name '{}'", 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 file name", 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 file name '{}'", argv[0], str_path);
+ fail = true;
+ break;
+ }
+ }
+ if (component.length() > name_max) {
+ warnln("{}: limit {} exceeded by length {} of file name component '{}'", argv[0], name_max, component.length(), component.characters());
+ fail = true;
+ break;
+ }
+ }
+ }
+
+ return fail;
+}