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

touch: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-12-20 19:50:13 +01:00
parent db766d0972
commit bcd49ad834
2 changed files with 11 additions and 20 deletions

View file

@ -136,6 +136,7 @@ target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS Li
target_link_libraries(test-imap LibIMAP) target_link_libraries(test-imap LibIMAP)
target_link_libraries(test-pthread LibThreading) target_link_libraries(test-pthread LibThreading)
target_link_libraries(top LibMain) target_link_libraries(top LibMain)
target_link_libraries(touch LibMain)
target_link_libraries(truncate LibMain) target_link_libraries(truncate LibMain)
target_link_libraries(tt LibPthread) target_link_libraries(tt LibPthread)
target_link_libraries(unzip LibArchive LibCompress) target_link_libraries(unzip LibArchive LibCompress)

View file

@ -1,10 +1,12 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* *
* 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 <LibMain/Main.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -28,12 +30,9 @@ static bool file_exists(const char* path)
exit(1); exit(1);
} }
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
if (pledge("stdio rpath cpath fattr", nullptr)) { TRY(Core::System::pledge("stdio rpath cpath fattr"));
perror("pledge");
return 1;
}
Vector<const char*> paths; Vector<const char*> paths;
@ -41,24 +40,15 @@ int main(int argc, char** argv)
args_parser.set_general_help("Create a file, or update its mtime (time of last modification)."); args_parser.set_general_help("Create a file, or update its mtime (time of last modification).");
args_parser.add_ignored(nullptr, 'f'); args_parser.add_ignored(nullptr, 'f');
args_parser.add_positional_argument(paths, "Files to touch", "path", Core::ArgsParser::Required::Yes); args_parser.add_positional_argument(paths, "Files to touch", "path", Core::ArgsParser::Required::Yes);
args_parser.parse(argc, argv); args_parser.parse(arguments);
for (auto path : paths) { for (auto path : paths) {
if (file_exists(path)) { if (file_exists(path)) {
int rc = utime(path, nullptr); if (auto result = Core::System::utime(path, {}); result.is_error())
if (rc < 0) warnln("{}", result.release_error());
perror("utime");
} else { } else {
int fd = open(path, O_CREAT, 0100644); int fd = TRY(Core::System::open(path, O_CREAT, 0100644));
if (fd < 0) { TRY(Core::System::close(fd));
perror("open");
return 1;
}
int rc = close(fd);
if (rc < 0) {
perror("close");
return 1;
}
} }
} }
return 0; return 0;