time.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. char output[4096];
  12. void add(char*, ...);
  13. void error(char*);
  14. void notifyf(void *c, char*);
  15. void
  16. main(int argc, char *argv[])
  17. {
  18. int i;
  19. Waitmsg *w;
  20. int32_t l;
  21. char *p;
  22. char err[ERRMAX];
  23. if(argc <= 1){
  24. fprint(2, "usage: time command\n");
  25. exits("usage");
  26. }
  27. switch(fork()){
  28. case -1:
  29. error("fork");
  30. case 0:
  31. exec(argv[1], &argv[1]);
  32. if(argv[1][0] != '/' && strncmp(argv[1], "./", 2) &&
  33. strncmp(argv[1], "../", 3)){
  34. sprint(output, "/bin/%s", argv[1]);
  35. exec(output, &argv[1]);
  36. }
  37. error(argv[1]);
  38. }
  39. notify(notifyf);
  40. loop:
  41. w = wait();
  42. if(w == nil){
  43. errstr(err, sizeof err);
  44. if(strcmp(err, "interrupted") == 0)
  45. goto loop;
  46. error("wait");
  47. }
  48. l = w->time[0];
  49. add("%ld.%.2ldu", l/1000, (l%1000)/10);
  50. l = w->time[1];
  51. add("%ld.%.2lds", l/1000, (l%1000)/10);
  52. l = w->time[2];
  53. add("%ld.%.2ldr", l/1000, (l%1000)/10);
  54. add("\t");
  55. for(i=1; i<argc; i++){
  56. add("%s", argv[i], 0);
  57. if(i>4){
  58. add("...");
  59. break;
  60. }
  61. }
  62. if(w->msg[0]){
  63. p = utfrune(w->msg, ':');
  64. if(p && p[1])
  65. p++;
  66. else
  67. p = w->msg;
  68. add(" # status=%s", p);
  69. }
  70. fprint(2, "%s\n", output);
  71. exits(w->msg);
  72. }
  73. void
  74. add(char *a, ...)
  75. {
  76. static int beenhere=0;
  77. va_list arg;
  78. if(beenhere)
  79. strcat(output, " ");
  80. va_start(arg, a);
  81. vseprint(output+strlen(output), output+sizeof(output), a, arg);
  82. va_end(arg);
  83. beenhere++;
  84. }
  85. void
  86. error(char *s)
  87. {
  88. fprint(2, "time: %s: %r\n", s);
  89. exits(s);
  90. }
  91. void
  92. notifyf(void *a, char *s)
  93. {
  94. USED(a);
  95. if(strcmp(s, "interrupt") == 0)
  96. noted(NCONT);
  97. noted(NDFLT);
  98. }