diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-04-02 16:48:05 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-11 21:09:42 +0200 |
commit | f0aba519c33058f1ddbb0a63fe2a9c1efe646ac9 (patch) | |
tree | 78b8f5a8e127c3626150f9864a3eccba8ad4a8ea /Userland | |
parent | 84b67754c0026123236b7bbc88230d37000868de (diff) | |
download | serenity-f0aba519c33058f1ddbb0a63fe2a9c1efe646ac9.zip |
Utilities: Read positional arguments as Strings not char*s
This is a pretty trivial change so they're all batched together.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Utilities/cksum.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/copy.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/echo.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/file.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/gml-format.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/grep.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/groups.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/head.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/mkdir.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/mv.cpp | 10 | ||||
-rw-r--r-- | Userland/Utilities/nl.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/pathchk.cpp | 26 | ||||
-rw-r--r-- | Userland/Utilities/rmdir.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/tar.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/touch.cpp | 8 | ||||
-rw-r--r-- | Userland/Utilities/tree.cpp | 4 | ||||
-rw-r--r-- | Userland/Utilities/wc.cpp | 2 | ||||
-rw-r--r-- | Userland/Utilities/xargs.cpp | 12 |
18 files changed, 46 insertions, 52 deletions
diff --git a/Userland/Utilities/cksum.cpp b/Userland/Utilities/cksum.cpp index d94f96c4f2..3cf7f1e226 100644 --- a/Userland/Utilities/cksum.cpp +++ b/Userland/Utilities/cksum.cpp @@ -13,7 +13,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { - Vector<char const*> paths; + Vector<String> paths; char const* opt_algorithm = nullptr; Core::ArgsParser args_parser; @@ -43,7 +43,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) bool fail = false; for (auto& path : paths) { - auto filepath = (StringView(path) == "-") ? "/dev/stdin" : path; + auto filepath = (path == "-") ? "/dev/stdin" : path; auto file = Core::File::construct(filepath); if (!file->open(Core::OpenMode::ReadOnly)) { warnln("{}: {}: {}", arguments.strings[0], path, file->error_string()); diff --git a/Userland/Utilities/copy.cpp b/Userland/Utilities/copy.cpp index f5f21cb858..04562db98e 100644 --- a/Userland/Utilities/copy.cpp +++ b/Userland/Utilities/copy.cpp @@ -25,7 +25,7 @@ struct Options { static Options parse_options(Main::Arguments arguments) { char const* type = "text/plain"; - Vector<char const*> text; + Vector<String> text; bool clear = false; Core::ArgsParser args_parser; diff --git a/Userland/Utilities/echo.cpp b/Userland/Utilities/echo.cpp index b5f37189ba..d48a227ca0 100644 --- a/Userland/Utilities/echo.cpp +++ b/Userland/Utilities/echo.cpp @@ -101,7 +101,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio")); - Vector<char const*> text; + Vector<String> text; bool no_trailing_newline = false; bool should_interpret_backslash_escapes = false; diff --git a/Userland/Utilities/file.cpp b/Userland/Utilities/file.cpp index 09ebb1a742..797aa27b0c 100644 --- a/Userland/Utilities/file.cpp +++ b/Userland/Utilities/file.cpp @@ -141,7 +141,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath")); - Vector<char const*> paths; + Vector<String> paths; bool flag_mime_only = false; Core::ArgsParser args_parser; @@ -155,7 +155,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) for (auto path : paths) { auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly); if (file_or_error.is_error()) { - perror(path); + perror(path.characters()); all_ok = false; continue; } diff --git a/Userland/Utilities/gml-format.cpp b/Userland/Utilities/gml-format.cpp index 5912a3bb0e..cd8cf97aa3 100644 --- a/Userland/Utilities/gml-format.cpp +++ b/Userland/Utilities/gml-format.cpp @@ -53,7 +53,7 @@ ErrorOr<int> serenity_main(Main::Arguments args) #endif bool inplace = false; - Vector<char const*> files; + Vector<String> files; Core::ArgsParser args_parser; args_parser.set_general_help("Format GML files."); diff --git a/Userland/Utilities/grep.cpp b/Userland/Utilities/grep.cpp index c9e3dd8116..fecbcd08df 100644 --- a/Userland/Utilities/grep.cpp +++ b/Userland/Utilities/grep.cpp @@ -39,11 +39,11 @@ ErrorOr<int> serenity_main(Main::Arguments args) String program_name = AK::LexicalPath::basename(args.strings[0]); - Vector<char const*> files; + Vector<String> files; bool recursive = (program_name == "rgrep"sv); bool use_ere = (program_name == "egrep"sv); - Vector<char const*> patterns; + Vector<String> patterns; BinaryFileMode binary_mode { BinaryFileMode::Binary }; bool case_insensitive = false; bool line_numbers = false; diff --git a/Userland/Utilities/groups.cpp b/Userland/Utilities/groups.cpp index caa9c6a55f..09eddadac0 100644 --- a/Userland/Utilities/groups.cpp +++ b/Userland/Utilities/groups.cpp @@ -35,7 +35,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) TRY(Core::System::unveil(nullptr, nullptr)); TRY(Core::System::pledge("stdio rpath")); - Vector<char const*> usernames; + Vector<String> usernames; Core::ArgsParser args_parser; args_parser.set_general_help("Print group memberships for each username or, if no username is specified, for the current process."); @@ -48,7 +48,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) } for (auto username : usernames) { - auto result = Core::Account::from_name(username, Core::Account::Read::PasswdOnly); + auto result = Core::Account::from_name(username.characters(), Core::Account::Read::PasswdOnly); if (result.is_error()) { warnln("{} '{}'", result.error(), username); continue; diff --git a/Userland/Utilities/head.cpp b/Userland/Utilities/head.cpp index d3d45e2bb7..1845239c92 100644 --- a/Userland/Utilities/head.cpp +++ b/Userland/Utilities/head.cpp @@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments args) int byte_count = -1; bool never_print_filenames = false; bool always_print_filenames = false; - Vector<char const*> files; + Vector<String> files; Core::ArgsParser args_parser; args_parser.set_general_help("Print the beginning ('head') of a file."); diff --git a/Userland/Utilities/mkdir.cpp b/Userland/Utilities/mkdir.cpp index 5ba85d498c..b0e7d6653e 100644 --- a/Userland/Utilities/mkdir.cpp +++ b/Userland/Utilities/mkdir.cpp @@ -22,7 +22,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) bool create_parents = false; String mode_string; - Vector<char const*> directories; + Vector<String> directories; Core::ArgsParser args_parser; args_parser.add_option(create_parents, "Create parent directories if they don't exist", "parents", 'p'); diff --git a/Userland/Utilities/mv.cpp b/Userland/Utilities/mv.cpp index 23e374fa85..fbfda02ee8 100644 --- a/Userland/Utilities/mv.cpp +++ b/Userland/Utilities/mv.cpp @@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) bool force = false; bool verbose = false; - Vector<char const*> paths; + Vector<String> paths; Core::ArgsParser args_parser; args_parser.add_option(force, "Force", "force", 'f'); @@ -41,7 +41,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) struct stat st; - int rc = lstat(original_new_path, &st); + int rc = lstat(original_new_path.characters(), &st); if (rc != 0 && errno != ENOENT) { perror("lstat"); return 1; @@ -57,14 +57,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) for (auto& old_path : paths) { String combined_new_path; - char const* new_path = original_new_path; + auto new_path = original_new_path; if (S_ISDIR(st.st_mode)) { auto old_basename = LexicalPath::basename(old_path); combined_new_path = String::formatted("{}/{}", original_new_path, old_basename); new_path = combined_new_path.characters(); } - rc = rename(old_path, new_path); + rc = rename(old_path.characters(), new_path.characters()); if (rc < 0) { if (errno == EXDEV) { auto result = Core::File::copy_file_or_directory( @@ -77,7 +77,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) warnln("mv: could not move '{}': {}", old_path, static_cast<Error const&>(result.error())); return 1; } - rc = unlink(old_path); + rc = unlink(old_path.characters()); if (rc < 0) warnln("mv: unlink '{}': {}", old_path, strerror(errno)); } else { diff --git a/Userland/Utilities/nl.cpp b/Userland/Utilities/nl.cpp index 63787c8511..d88428cb30 100644 --- a/Userland/Utilities/nl.cpp +++ b/Userland/Utilities/nl.cpp @@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) char const* separator = " "; int start_number = 1; int number_width = 6; - Vector<char const*> files; + Vector<String> files; Core::ArgsParser args_parser; @@ -60,7 +60,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) Vector<FILE*> file_pointers; if (!files.is_empty()) { for (auto& file : files) { - FILE* file_pointer = fopen(file, "r"); + FILE* file_pointer = fopen(file.characters(), "r"); if (!file_pointer) { warnln("Failed to open {}: {}", file, strerror(errno)); continue; diff --git a/Userland/Utilities/pathchk.cpp b/Userland/Utilities/pathchk.cpp index aeb1594dc1..a661474578 100644 --- a/Userland/Utilities/pathchk.cpp +++ b/Userland/Utilities/pathchk.cpp @@ -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; } diff --git a/Userland/Utilities/rmdir.cpp b/Userland/Utilities/rmdir.cpp index fbdd929889..55b43f8f18 100644 --- a/Userland/Utilities/rmdir.cpp +++ b/Userland/Utilities/rmdir.cpp @@ -15,7 +15,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio cpath")); - Vector<char const*> paths; + Vector<String> paths; Core::ArgsParser args_parser; args_parser.add_positional_argument(paths, "Directories to remove", "paths"); @@ -23,7 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) int status = 0; for (auto path : paths) { - int rc = rmdir(path); + int rc = rmdir(path.characters()); if (rc < 0) { perror("rmdir"); status = 1; diff --git a/Userland/Utilities/tar.cpp b/Userland/Utilities/tar.cpp index 13bb34d9a9..dcc261208e 100644 --- a/Userland/Utilities/tar.cpp +++ b/Userland/Utilities/tar.cpp @@ -34,7 +34,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) bool no_auto_compress = false; StringView archive_file; char const* directory = nullptr; - Vector<char const*> paths; + Vector<String> paths; Core::ArgsParser args_parser; args_parser.add_option(create, "Create archive", "create", 'c'); diff --git a/Userland/Utilities/touch.cpp b/Userland/Utilities/touch.cpp index d745eabdcf..202440f4bc 100644 --- a/Userland/Utilities/touch.cpp +++ b/Userland/Utilities/touch.cpp @@ -12,13 +12,11 @@ #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> -#include <unistd.h> -#include <utime.h> -static bool file_exists(char const* path) +static bool file_exists(String const& path) { struct stat st; - int rc = stat(path, &st); + int rc = stat(path.characters(), &st); if (rc < 0) { if (errno == ENOENT) return false; @@ -34,7 +32,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath cpath fattr")); - Vector<char const*> paths; + Vector<String> paths; Core::ArgsParser args_parser; args_parser.set_general_help("Create a file, or update its mtime (time of last modification)."); diff --git a/Userland/Utilities/tree.cpp b/Userland/Utilities/tree.cpp index f90c10e669..c7e8fa5485 100644 --- a/Userland/Utilities/tree.cpp +++ b/Userland/Utilities/tree.cpp @@ -106,7 +106,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath tty")); - Vector<char const*> directories; + Vector<String> directories; Core::ArgsParser args_parser; args_parser.add_option(flag_show_hidden_files, "Show hidden files", "all", 'a'); @@ -124,7 +124,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) print_directory_tree(".", 0, ""); puts(""); } else { - for (char const* directory : directories) { + for (auto const& directory : directories) { print_directory_tree(directory, 0, ""); puts(""); } diff --git a/Userland/Utilities/wc.cpp b/Userland/Utilities/wc.cpp index b37383175f..4ce7c5884a 100644 --- a/Userland/Utilities/wc.cpp +++ b/Userland/Utilities/wc.cpp @@ -90,7 +90,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath")); - Vector<char const*> file_specifiers; + Vector<String> file_specifiers; Core::ArgsParser args_parser; args_parser.add_option(g_output_line, "Output line count", "lines", 'l'); diff --git a/Userland/Utilities/xargs.cpp b/Userland/Utilities/xargs.cpp index a7e65302ad..929a39f182 100644 --- a/Userland/Utilities/xargs.cpp +++ b/Userland/Utilities/xargs.cpp @@ -28,7 +28,7 @@ bool read_items(FILE* fp, char entry_separator, Function<Decision(StringView)>); class ParsedInitialArguments { public: - ParsedInitialArguments(Vector<char const*>&, StringView placeholder); + ParsedInitialArguments(Vector<String>&, StringView placeholder); void for_each_joined_argument(StringView, Function<void(String const&)>) const; @@ -45,7 +45,7 @@ ErrorOr<int> serenity_main(Main::Arguments main_arguments) char const* placeholder = nullptr; bool split_with_nulls = false; char const* specified_delimiter = "\n"; - Vector<char const*> arguments; + Vector<String> arguments; bool verbose = false; char const* file_to_read = "-"; int max_lines_for_one_command = 0; @@ -238,18 +238,16 @@ bool run_command(Vector<char*>&& child_argv, bool verbose, bool is_stdin, int de return true; } -ParsedInitialArguments::ParsedInitialArguments(Vector<char const*>& arguments, StringView placeholder) +ParsedInitialArguments::ParsedInitialArguments(Vector<String>& arguments, StringView placeholder) { m_all_parts.ensure_capacity(arguments.size()); bool some_argument_has_placeholder = false; - for (auto argument : arguments) { - StringView arg { argument }; - + for (auto arg : arguments) { if (placeholder.is_empty()) { m_all_parts.append({ arg }); } else { - auto parts = arg.split_view(placeholder, true); + auto parts = arg.view().split_view(placeholder, true); some_argument_has_placeholder = some_argument_has_placeholder || parts.size() > 1; m_all_parts.append(move(parts)); } |