summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorJean-Baptiste Boric <jblbeurope@gmail.com>2021-10-23 13:52:09 +0200
committerAndreas Kling <kling@serenityos.org>2021-10-23 15:06:33 +0200
commiteaeed259b0fbcde20fcd0044203687e534abd170 (patch)
tree240fc2943c5f84ce82530cef8941dbc3fa09a39a /Userland/Utilities
parent8bbf43318f1019a878191941d7d49e297f72aaad (diff)
downloadserenity-eaeed259b0fbcde20fcd0044203687e534abd170.zip
Utilities: Add -f option to ln
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/ln.cpp35
1 files changed, 25 insertions, 10 deletions
diff --git a/Userland/Utilities/ln.cpp b/Userland/Utilities/ln.cpp
index 2b44b6dc9e..cb8b4af41a 100644
--- a/Userland/Utilities/ln.cpp
+++ b/Userland/Utilities/ln.cpp
@@ -16,11 +16,13 @@ int main(int argc, char** argv)
return 1;
}
+ bool force = false;
bool symbolic = false;
const char* target = nullptr;
const char* path = nullptr;
Core::ArgsParser args_parser;
+ args_parser.add_option(force, "Force the creation", "force", 'f');
args_parser.add_option(symbolic, "Create a symlink", "symbolic", 's');
args_parser.add_positional_argument(target, "Link target", "target");
args_parser.add_positional_argument(path, "Link path", "path", Core::ArgsParser::Required::No);
@@ -32,19 +34,32 @@ int main(int argc, char** argv)
path = path_buffer.characters();
}
- if (symbolic) {
- int rc = symlink(target, path);
+ 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;
+ }
+ }
+
+ int rc = unlink(path);
if (rc < 0) {
- perror("symlink");
+ perror("unlink");
return 1;
}
- return 0;
- }
+ force = false;
+ } while (true);
- int rc = link(target, path);
- if (rc < 0) {
- perror("link");
- return 1;
- }
return 0;
}