aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/time.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2003-11-23 18:04:47 +0000
committerrsc <devnull@localhost>2003-11-23 18:04:47 +0000
commitbc7cb1a15a67c859c8c71c4b52bb35fe9425a63d (patch)
tree8ca0fe4e2418e6aa18dc74a236c577a719f6c6ed /src/cmd/time.c
parentf08fdedcee12c06e3ce9ac9bec363915978e8289 (diff)
downloadplan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.gz
plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.bz2
plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.zip
new utilities.
the .C files compile but are renamed to avoid building automatically.
Diffstat (limited to 'src/cmd/time.c')
-rw-r--r--src/cmd/time.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/cmd/time.c b/src/cmd/time.c
new file mode 100644
index 00000000..d82eaa94
--- /dev/null
+++ b/src/cmd/time.c
@@ -0,0 +1,101 @@
+#include <u.h>
+#include <libc.h>
+
+char output[4096];
+void add(char*, ...);
+void error(char*);
+void notifyf(void*, char*);
+
+void
+main(int argc, char *argv[])
+{
+ int i;
+ Waitmsg *w;
+ long l;
+ char *p;
+ char err[ERRMAX];
+
+ if(argc <= 1){
+ fprint(2, "usage: time command\n");
+ exits("usage");
+ }
+
+ switch(fork()){
+ case -1:
+ error("fork");
+ case 0:
+ exec(argv[1], &argv[1]);
+ if(argv[1][0] != '/' && strncmp(argv[1], "./", 2) &&
+ strncmp(argv[1], "../", 3)){
+ sprint(output, "/bin/%s", argv[1]);
+ exec(output, &argv[1]);
+ }
+ error(argv[1]);
+ }
+
+ notify(notifyf);
+
+ loop:
+ w = wait();
+ if(w == nil){
+ errstr(err, sizeof err);
+ if(strcmp(err, "interrupted") == 0)
+ goto loop;
+ error("wait");
+ }
+ l = w->time[0];
+ add("%ld.%.2ldu", l/1000, (l%1000)/10);
+ l = w->time[1];
+ add("%ld.%.2lds", l/1000, (l%1000)/10);
+ l = w->time[2];
+ add("%ld.%.2ldr", l/1000, (l%1000)/10);
+ add("\t");
+ for(i=1; i<argc; i++){
+ add("%s", argv[i], 0);
+ if(i>4){
+ add("...");
+ break;
+ }
+ }
+ if(w->msg[0]){
+ p = utfrune(w->msg, ':');
+ if(p && p[1])
+ p++;
+ else
+ p = w->msg;
+ add(" # status=%s", p);
+ }
+ fprint(2, "%s\n", output);
+ exits(w->msg);
+}
+
+void
+add(char *a, ...)
+{
+ static int beenhere=0;
+ va_list arg;
+
+ if(beenhere)
+ strcat(output, " ");
+ va_start(arg, a);
+ vseprint(output+strlen(output), output+sizeof(output), a, arg);
+ va_end(arg);
+ beenhere++;
+}
+
+void
+error(char *s)
+{
+
+ fprint(2, "time: %s: %r\n", s);
+ exits(s);
+}
+
+void
+notifyf(void *a, char *s)
+{
+ USED(a);
+ if(strcmp(s, "interrupt") == 0)
+ noted(NCONT);
+ noted(NDFLT);
+}