diff options
author | Tim Schumacher <timschumi@gmx.de> | 2022-07-25 16:05:20 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-25 22:21:01 +0100 |
commit | 26d4a44a0f47f95670576a0ef322ca4b4b936c3e (patch) | |
tree | 218a0de4552e84bef80756f9562673e0c41e747c | |
parent | c44b9acac3c3f88d7f7d7cc3e8a847f52c47c5c1 (diff) | |
download | serenity-26d4a44a0f47f95670576a0ef322ca4b4b936c3e.zip |
ln: Rework to use LibCore syscall wrappers
-rw-r--r-- | Userland/Utilities/ln.cpp | 50 |
1 files changed, 19 insertions, 31 deletions
diff --git a/Userland/Utilities/ln.cpp b/Userland/Utilities/ln.cpp index 66b3309af1..dc935c99a9 100644 --- a/Userland/Utilities/ln.cpp +++ b/Userland/Utilities/ln.cpp @@ -7,17 +7,15 @@ #include <AK/LexicalPath.h> #include <LibCore/ArgsParser.h> #include <LibCore/System.h> -#include <stdio.h> -#include <unistd.h> ErrorOr<int> serenity_main(Main::Arguments arguments) { - TRY(Core::System::pledge("stdio cpath")); + TRY(Core::System::pledge("stdio cpath rpath")); bool force = false; bool symbolic = false; - char const* target = nullptr; - char const* path = nullptr; + StringView target; + StringView path; Core::ArgsParser args_parser; args_parser.add_option(force, "Force the creation", "force", 'f'); @@ -27,37 +25,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) args_parser.parse(arguments); String path_buffer; - if (!path) { + if (path.is_empty()) { path_buffer = LexicalPath::basename(target); - path = path_buffer.characters(); + path = path_buffer.view(); } - do { - if (symbolic) { - int rc = symlink(target, path); - if (rc < 0 && !force) { - perror("symlink"); - return 1; - } else if (rc == 0) { - return 0; - } - } else { - int rc = link(target, path); - if (rc < 0 && !force) { - perror("link"); - return 1; - } else if (rc == 0) { - return 0; - } - } + if (force) { + auto stat = Core::System::lstat(path); + + if (stat.is_error() && stat.error().code() != ENOENT) + return stat.error(); - int rc = unlink(path); - if (rc < 0) { - perror("unlink"); - return 1; + if (!stat.is_error()) { + TRY(Core::System::unlink(path)); } - force = false; - } while (true); + } + + if (symbolic) { + TRY(Core::System::symlink(target, path)); + } else { + TRY(Core::System::link(target, path)); + } return 0; } |