PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/builtin/times.c

https://git.sr.ht/~emersion/mrsh/
C | 41 lines | 35 code | 6 blank | 0 comment | 5 complexity | 4991abc9720945e0afa12e65765bb8f7 MD5 | raw file
Possible License(s): MIT
  1. #include <errno.h>
  2. #include <mrsh/builtin.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/times.h>
  7. #include <unistd.h>
  8. #include "builtin.h"
  9. static const char times_usage[] = "usage: times\n";
  10. int builtin_times(struct mrsh_state *state, int argc, char *argv[]) {
  11. if (argc > 1) {
  12. fprintf(stderr, times_usage);
  13. return 1;
  14. }
  15. struct tms buf;
  16. long clk_tck = sysconf(_SC_CLK_TCK);
  17. if (clk_tck == -1) {
  18. perror("sysconf");
  19. return 1;
  20. }
  21. if (times(&buf) == (clock_t)-1) {
  22. perror("times");
  23. return 1;
  24. }
  25. printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
  26. (int)(buf.tms_utime / clk_tck / 60),
  27. ((double) buf.tms_utime) / clk_tck,
  28. (int)(buf.tms_stime / clk_tck / 60),
  29. ((double) buf.tms_stime) / clk_tck,
  30. (int)(buf.tms_cutime / clk_tck / 60),
  31. ((double) buf.tms_cutime) / clk_tck,
  32. (int)(buf.tms_cstime / clk_tck / 60),
  33. ((double)buf.tms_cstime) / clk_tck);
  34. return 0;
  35. }