mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
Utilities: Port pathchk to LibMain
This commit is contained in:
parent
e0de892f8f
commit
665505db42
2 changed files with 12 additions and 14 deletions
|
@ -160,6 +160,7 @@ target_link_libraries(profile LibMain)
|
||||||
target_link_libraries(ps LibMain)
|
target_link_libraries(ps LibMain)
|
||||||
target_link_libraries(pwd LibMain)
|
target_link_libraries(pwd LibMain)
|
||||||
target_link_libraries(realpath LibMain)
|
target_link_libraries(realpath LibMain)
|
||||||
|
target_link_libraries(pathchk LibMain)
|
||||||
target_link_libraries(rev LibMain)
|
target_link_libraries(rev LibMain)
|
||||||
target_link_libraries(rm LibMain)
|
target_link_libraries(rm LibMain)
|
||||||
target_link_libraries(rmdir LibMain)
|
target_link_libraries(rmdir LibMain)
|
||||||
|
|
|
@ -7,21 +7,18 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <bits/posix1_lim.h>
|
#include <bits/posix1_lim.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
|
TRY(Core::System::pledge("stdio rpath"));
|
||||||
bool fail = false;
|
bool fail = false;
|
||||||
static bool flag_most_posix = false;
|
static bool flag_most_posix = false;
|
||||||
static bool flag_portability = false;
|
static bool flag_portability = false;
|
||||||
static bool flag_empty_name_and_leading_dash = false;
|
static bool flag_empty_name_and_leading_dash = false;
|
||||||
|
|
||||||
if (pledge("stdio rpath", nullptr) < 0) {
|
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector<const char*> paths;
|
Vector<const char*> paths;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
@ -29,7 +26,7 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_option(flag_empty_name_and_leading_dash, "Check for empty names and leading dash", 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_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.add_positional_argument(paths, "Path to check", "path", Core::ArgsParser::Required::Yes);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
if (flag_portability) {
|
if (flag_portability) {
|
||||||
flag_most_posix = true;
|
flag_most_posix = true;
|
||||||
|
@ -42,7 +39,7 @@ int main(int argc, char** argv)
|
||||||
unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(str_path.characters(), _PC_NAME_MAX);
|
unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(str_path.characters(), _PC_NAME_MAX);
|
||||||
|
|
||||||
if (str_path.length() > path_max) {
|
if (str_path.length() > path_max) {
|
||||||
warnln("{}: limit {} exceeded by length {} of filename '{}'", argv[0], path_max, str_path.length(), str_path);
|
warnln("Limit {} exceeded by length {} of filename '{}'", path_max, str_path.length(), str_path);
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -52,7 +49,7 @@ int main(int argc, char** argv)
|
||||||
for (long unsigned i = 0; i < str_path.length(); ++i) {
|
for (long unsigned i = 0; i < str_path.length(); ++i) {
|
||||||
auto c = path[i];
|
auto c = path[i];
|
||||||
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9') && c != '/' && c != '.' && c != '-' && c != '_') {
|
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);
|
warnln("Non-portable character '{}' in filename '{}'", path[i], str_path);
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -61,7 +58,7 @@ int main(int argc, char** argv)
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (lstat(str_path.characters(), &st) < 0) {
|
if (lstat(str_path.characters(), &st) < 0) {
|
||||||
if (errno != ENOENT) {
|
if (errno != ENOENT) {
|
||||||
warnln("{}: directory is not searchable '{}'", argv[0], str_path);
|
warnln("Directory is not searchable '{}'", str_path);
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +67,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
if (flag_empty_name_and_leading_dash) {
|
if (flag_empty_name_and_leading_dash) {
|
||||||
if (str_path.is_empty()) {
|
if (str_path.is_empty()) {
|
||||||
warnln("{}: empty filename", argv[0]);
|
warnln("Empty filename");
|
||||||
fail = true;
|
fail = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -79,13 +76,13 @@ int main(int argc, char** argv)
|
||||||
for (auto& component : str_path.split('/')) {
|
for (auto& component : str_path.split('/')) {
|
||||||
if (flag_empty_name_and_leading_dash) {
|
if (flag_empty_name_and_leading_dash) {
|
||||||
if (component.starts_with('-')) {
|
if (component.starts_with('-')) {
|
||||||
warnln("{}: leading '-' in a component of filename '{}'", argv[0], str_path);
|
warnln("Leading '-' in a component of filename '{}'", str_path);
|
||||||
fail = true;
|
fail = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (component.length() > name_max) {
|
if (component.length() > name_max) {
|
||||||
warnln("{}: limit {} exceeded by length {} of filename component '{}'", argv[0], name_max, component.length(), component.characters());
|
warnln("Limit {} exceeded by length {} of filename component '{}'", name_max, component.length(), component.characters());
|
||||||
fail = true;
|
fail = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue