summaryrefslogtreecommitdiff
path: root/Userland/ln.cpp
blob: b5c460783b0808517c21c3d133350059ba783483 (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
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>

[[noreturn]] static void print_usage_and_exit()
{
    printf("usage: ln [-s] <target> <link-path>\n");
    exit(0);
}

int main(int argc, char** argv)
{
    bool flag_symlink = false;
    int opt;
    while ((opt = getopt(argc, argv, "s")) != -1) {
        switch (opt) {
        case 's':
            flag_symlink = true;
            break;
        default:
            print_usage_and_exit();
        }
    }

    if ((optind + 1) >= argc)
        print_usage_and_exit();

    if (flag_symlink) {
        int rc = symlink(argv[optind], argv[optind + 1]);
        if (rc < 0) {
            perror("symlink");
            return 1;
        }
        return 0;
    }

    int rc = link(argv[1], argv[2]);
    if (rc < 0) {
        perror("link");
        return 1;
    }
    return 0;
}