summaryrefslogtreecommitdiff
path: root/Userland/Utilities/ln.cpp
blob: 6fa20fdacedbb63d9f13e29101e566ec6e0acca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <stdio.h>
#include <unistd.h>

ErrorOr<int> serenity_main(Main::Arguments argmuments)
{
    TRY(Core::System::pledge("stdio cpath"));

    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);
    args_parser.parse(argmuments);

    String path_buffer;
    if (!path) {
        path_buffer = LexicalPath::basename(target);
        path = path_buffer.characters();
    }

    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("unlink");
            return 1;
        }
        force = false;
    } while (true);

    return 0;
}