From 5117b410db783b74cdc896631ac93c8b80a2bafe Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 May 2023 12:48:25 +0200 Subject: [PATCH] apps: Add time --- apps/CMakeLists.txt | 1 + apps/time.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 apps/time.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 9dec132e..d1db2c3d 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -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) diff --git a/apps/time.cpp b/apps/time.cpp new file mode 100644 index 00000000..d3e4dc34 --- /dev/null +++ b/apps/time.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include + +Result luna_main(int argc, char** argv) +{ + Vector 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; +}