apps: Add time
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-20 12:48:25 +02:00
parent 6de90b3c93
commit 5117b410db
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 46 additions and 0 deletions

View File

@ -27,3 +27,4 @@ luna_app(ipc-test.cpp ipc-test)
luna_app(mount.cpp mount)
luna_app(umount.cpp umount)
luna_app(ps.cpp ps)
luna_app(time.cpp time)

45
apps/time.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <os/Process.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/wait.h>
Result<int> luna_main(int argc, char** argv)
{
Vector<StringView> command;
os::ArgumentParser parser;
parser.add_description("Time a command.");
parser.add_system_program_info("time"_sv);
parser.set_vector_argument(command, true);
TRY(parser.parse(argc, argv));
auto pid = TRY(os::Process::fork());
if (pid == 0)
{
TRY(os::Process::exec(command[0], command.slice()));
unreachable();
}
if (waitpid(pid, nullptr, 0) < 0)
{
perror("waitpid");
return 1;
}
struct rusage usage;
if (getrusage(RUSAGE_CHILDREN, &usage) < 0)
{
perror("getrusage");
return 1;
}
auto cmdline = TRY(String::join(command, " "));
os::println("%s %d.%.2ds user %d.%.2ds system"_sv, cmdline.chars(), usage.ru_utime.tv_sec,
usage.ru_utime.tv_usec / 10000, usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 10000);
return 0;
}