2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-06-20 07:59:03 -03:00
|
|
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-08-05 15:36:42 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-02-09 07:43:21 -03:00
|
|
|
#include <LibCore/System.h>
|
2023-06-20 07:59:03 -03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-02-09 07:43:21 -03:00
|
|
|
#include <LibMain/Main.h>
|
2019-08-25 13:17:47 -03:00
|
|
|
|
2022-02-09 07:43:21 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-08-25 13:17:47 -03:00
|
|
|
{
|
2022-02-09 07:43:21 -03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-02-18 09:29:18 -03:00
|
|
|
|
2023-09-30 11:20:55 -03:00
|
|
|
bool quiet { false };
|
2023-06-20 07:59:03 -03:00
|
|
|
Vector<StringView> paths;
|
2020-08-05 15:36:42 -03:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 12:22:58 -03:00
|
|
|
args_parser.set_general_help(
|
|
|
|
|
"Show the 'real' path of a file, by resolving all symbolic links along the way.");
|
2023-09-30 11:20:55 -03:00
|
|
|
args_parser.add_option(quiet, "Suppress error messages", "quiet", 'q');
|
2023-06-20 07:59:03 -03:00
|
|
|
args_parser.add_positional_argument(paths, "Path to resolve", "paths");
|
2022-02-09 07:43:21 -03:00
|
|
|
args_parser.parse(arguments);
|
2019-08-25 13:17:47 -03:00
|
|
|
|
2023-06-20 07:59:03 -03:00
|
|
|
auto has_errors = false;
|
|
|
|
|
for (auto path : paths) {
|
|
|
|
|
auto resolved_path_or_error = FileSystem::real_path(path);
|
|
|
|
|
if (resolved_path_or_error.is_error()) {
|
2023-09-30 11:20:55 -03:00
|
|
|
if (!quiet)
|
|
|
|
|
warnln("realpath: {}: {}", path, strerror(resolved_path_or_error.error().code()));
|
|
|
|
|
|
2023-06-20 07:59:03 -03:00
|
|
|
has_errors = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outln("{}", resolved_path_or_error.release_value());
|
2019-08-25 13:17:47 -03:00
|
|
|
}
|
2023-06-20 07:59:03 -03:00
|
|
|
|
|
|
|
|
return has_errors ? 1 : 0;
|
2019-08-25 13:17:47 -03:00
|
|
|
}
|