Author | Jamozed <[email protected]> |
Date | 2020-07-05 15:21:49 |
Commit | 29dd2d0e47d76bcdf262a0ef505615633be4b097 |
Parent | e2d04da279f51fec216c5cbce0f7defca0f38c71 |
time: Add POSIX time
Diffstat
M | CMakeLists.txt | | | 1 | + |
M | README.md | | | 1 | + |
A | src/time.c | | | 119 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 121 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 3660de4..6590661 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ ADD_EXECUTABLE(rmdir ${CMAKE_SOURCE_DIR}/src/rmdir.c) ADD_EXECUTABLE(sleep ${CMAKE_SOURCE_DIR}/src/sleep.c) ADD_EXECUTABLE(sync ${CMAKE_SOURCE_DIR}/src/sync.c) ADD_EXECUTABLE(tee ${CMAKE_SOURCE_DIR}/src/tee.c) +ADD_EXECUTABLE(time ${CMAKE_SOURCE_DIR}/src/time.c) ADD_EXECUTABLE(touch ${CMAKE_SOURCE_DIR}/src/touch.c) ADD_EXECUTABLE(true ${CMAKE_SOURCE_DIR}/src/true.c) ADD_EXECUTABLE(tty ${CMAKE_SOURCE_DIR}/src/tty.c) diff --git a/README.md b/README.md index e007a0c..09b1ae7 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ UNIX-like systems. | sleep | Suspend execution for an interval | POSIX | | sync | Synchronise file system caches to disk | | | tee | Duplicate standard input | POSIX | +| time | Time a simple command | POSIX | | touch | Change file access and modify times | POSIX | | true | Return true value | POSIX | | tty | Return user's terminal name | POSIX | diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..83d281c --- /dev/null +++ b/src/time.c @@ -0,0 +1,119 @@ +// time.c, version 1.0.0 +// OMKOV coreutils implementation of POSIX time +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Copyright (C) 2020, Jakob Wakeling +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "error.h" +#include "optget.h" + +#include <sys/times.h> +#include <sys/wait.h> +#include <unistd.h> + +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <time.h> + +#define VERSION "1.0.0" + +static bool pflag; + +static inline void help(void); +static inline void version(void); + +int main(int ac, char *av[]) { + lop_t lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } + }; + + opt_t opt = OPTGET_INIT; opt.str = "p"; opt.lops = lops; int o; + while ((o = optget(&opt, av, 0)) != -1) switch (o) { + case 'p': { pflag = true; break; } + case 256: { help(); return 0; } + case 257: { version(); return 0; } + default: { return 1; } + } + + struct tms tm; clock_t cl = times(&tm); + + pid_t pid = fork(); + if (pid == -1) { error(1, "%s: %s", av[0]); } + else if (pid == 0) { + execvp(av[opt.ind], &av[opt.ind]); + warn("%s: %s: %s", av[0], av[opt.ind], serrno); + return errno = ENOENT ? 127 : 126; + } + + int status; waitpid(pid, &status, 0); + cl = times(&tm) - cl; + + double tick = (double)sysconf(_SC_CLK_TCK); + double real = (double)cl / tick; + double user = (double)(tm.tms_utime + tm.tms_cutime) / tick; + double sys = (double)(tm.tms_stime + tm.tms_cstime) / tick; + + if (pflag) { + fprintf(stderr, "real %0.2f\n", real); + fprintf(stderr, "user %0.2f\n", user); + fprintf(stderr, "sys %0.2f\n", sys); + } + else { + fprintf(stderr, "real %0.2fs\n", real); + fprintf(stderr, "user %0.2fs\n", user); + fprintf(stderr, "sys %0.2fs\n", sys); + } + + return WEXITSTATUS(status); +} + +static inline void help(void) { + puts("time - time a simple command\n"); + puts("usage: time [-p] command [argument...]\n"); + puts("options:"); + puts(" -p Use portable format"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +static inline void version() { + puts("OMKOV coreutils time, version " VERSION); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}