mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +00:00
Utilities: Read positional arguments as Strings not char*s
This is a pretty trivial change so they're all batched together.
This commit is contained in:
parent
84b67754c0
commit
f0aba519c3
18 changed files with 46 additions and 52 deletions
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <bits/posix1_lim.h>
|
||||
|
@ -19,7 +18,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
static bool flag_most_posix = false;
|
||||
static bool flag_portability = false;
|
||||
static bool flag_empty_name_and_leading_dash = false;
|
||||
Vector<char const*> paths;
|
||||
Vector<String> paths;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(flag_most_posix, "Check for most POSIX systems", nullptr, 'p');
|
||||
|
@ -34,31 +33,30 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
|
||||
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);
|
||||
unsigned long path_max = flag_most_posix ? _POSIX_PATH_MAX : pathconf(path.characters(), _PC_PATH_MAX);
|
||||
unsigned long name_max = flag_most_posix ? _POSIX_NAME_MAX : pathconf(path.characters(), _PC_NAME_MAX);
|
||||
|
||||
if (str_path.length() > path_max) {
|
||||
warnln("Limit {} exceeded by length {} of filename '{}'", path_max, str_path.length(), str_path);
|
||||
if (path.length() > path_max) {
|
||||
warnln("Limit {} exceeded by length {} of filename '{}'", path_max, path.length(), 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) {
|
||||
for (long unsigned i = 0; i < 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 '{}'", path[i], str_path);
|
||||
warnln("Non-portable character '{}' in filename '{}'", path[i], path);
|
||||
fail = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
struct stat st;
|
||||
if (lstat(str_path.characters(), &st) < 0) {
|
||||
if (lstat(path.characters(), &st) < 0) {
|
||||
if (errno != ENOENT) {
|
||||
warnln("Directory is not searchable '{}'", str_path);
|
||||
warnln("Directory is not searchable '{}'", path);
|
||||
fail = true;
|
||||
continue;
|
||||
}
|
||||
|
@ -66,17 +64,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
|
||||
if (flag_empty_name_and_leading_dash) {
|
||||
if (str_path.is_empty()) {
|
||||
if (path.is_empty()) {
|
||||
warnln("Empty filename");
|
||||
fail = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& component : str_path.split('/')) {
|
||||
for (auto& component : path.split('/')) {
|
||||
if (flag_empty_name_and_leading_dash) {
|
||||
if (component.starts_with('-')) {
|
||||
warnln("Leading '-' in a component of filename '{}'", str_path);
|
||||
warnln("Leading '-' in a component of filename '{}'", path);
|
||||
fail = true;
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue