xref: /plan9-contrib/sys/src/ape/lib/ap/plan9/times.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <sys/times.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <stdlib.h>
9 
10 static
11 char*
skip(char * p)12 skip(char *p)
13 {
14 
15 	while(*p == ' ')
16 		p++;
17 	while(*p != ' ' && *p != 0)
18 		p++;
19 	return p;
20 }
21 
22 clock_t
times(struct tms * buf)23 times(struct tms *buf)
24 {
25 	char b[200], *p;
26 	int f;
27 	unsigned long r;
28 
29 	memset(b, 0, sizeof(b));
30 	f = open("/dev/cputime", O_RDONLY);
31 	if(f >= 0) {
32 		lseek(f, SEEK_SET, 0);
33 		read(f, b, sizeof(b));
34 		close(f);
35 	}
36 	p = b;
37 	if(buf)
38 		buf->tms_utime = atol(p);
39 	p = skip(p);
40 	if(buf)
41 		buf->tms_stime = atol(p);
42 	p = skip(p);
43 	r = atol(p);
44 	if(buf){
45 		p = skip(p);
46 		buf->tms_cutime = atol(p);
47 		p = skip(p);
48 		buf->tms_cstime = atol(p);
49 	}
50 	return r;
51 }
52