summaryrefslogtreecommitdiff
path: root/Userland/Utilities/readlink.cpp
blob: 318e31ac1a828440503f09930dc3ebaac96a27d4 (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
/*
 * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    if (pledge("stdio rpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    bool no_newline = false;
    Vector<const char*> paths;

    Core::ArgsParser args_parser;
    args_parser.add_option(no_newline, "Do not append a newline", "no-newline", 'n');
    args_parser.add_positional_argument(paths, "Symlink path", "path");
    args_parser.parse(argc, argv);

    for (const char* path : paths) {
        auto destination = Core::File::read_link(path);
        if (destination.is_null()) {
            perror(path);
            return 1;
        }
        out("{}", destination);
        if (!no_newline)
            outln();
    }

    return 0;
}