1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:38:11 +00:00

realpath: Allow multiple path arguments to be given

This commit is contained in:
Tim Ledbetter 2023-06-20 11:59:03 +01:00 committed by Jelle Raaijmakers
parent 00ed042eda
commit cbda6e1ff4
2 changed files with 17 additions and 11 deletions

View file

@ -135,6 +135,7 @@ target_link_libraries(pkill PRIVATE LibRegex)
target_link_libraries(pls PRIVATE LibCrypt) target_link_libraries(pls PRIVATE LibCrypt)
target_link_libraries(pro PRIVATE LibFileSystem LibProtocol LibHTTP) target_link_libraries(pro PRIVATE LibFileSystem LibProtocol LibHTTP)
target_link_libraries(readlink PRIVATE LibFileSystem) target_link_libraries(readlink PRIVATE LibFileSystem)
target_link_libraries(realpath PRIVATE LibFileSystem)
target_link_libraries(run-tests PRIVATE LibCoredump LibDebug LibFileSystem LibRegex) target_link_libraries(run-tests PRIVATE LibCoredump LibDebug LibFileSystem LibRegex)
target_link_libraries(rm PRIVATE LibFileSystem) target_link_libraries(rm PRIVATE LibFileSystem)
target_link_libraries(sed PRIVATE LibRegex LibFileSystem) target_link_libraries(sed PRIVATE LibRegex LibFileSystem)

View file

@ -1,33 +1,38 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <stdio.h>
#include <unistd.h>
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
TRY(Core::System::pledge("stdio rpath")); TRY(Core::System::pledge("stdio rpath"));
DeprecatedString path; Vector<StringView> paths;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.set_general_help( args_parser.set_general_help(
"Show the 'real' path of a file, by resolving all symbolic links along the way."); "Show the 'real' path of a file, by resolving all symbolic links along the way.");
args_parser.add_positional_argument(path, "Path to resolve", "path"); args_parser.add_positional_argument(paths, "Path to resolve", "paths");
args_parser.parse(arguments); args_parser.parse(arguments);
char* value = realpath(path.characters(), nullptr); auto has_errors = false;
if (value == nullptr) { for (auto path : paths) {
perror("realpath"); auto resolved_path_or_error = FileSystem::real_path(path);
return 1; if (resolved_path_or_error.is_error()) {
warnln("realpath: {}: {}", path, strerror(resolved_path_or_error.error().code()));
has_errors = true;
continue;
}
outln("{}", resolved_path_or_error.release_value());
} }
outln("{}", value);
free(value); return has_errors ? 1 : 0;
return 0;
} }