]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libc/9sys/nsec.c
libtags, zuke: add *.mod support (thanks kemal)
[plan9front.git] / sys / src / libc / 9sys / nsec.c
1 #include <u.h>
2 #include <libc.h>
3 #include <tos.h>
4
5 static uvlong order = 0x0001020304050607ULL;
6
7 static void
8 be2vlong(vlong *to, uchar *f)
9 {
10         uchar *t, *o;
11         int i;
12
13         t = (uchar*)to;
14         o = (uchar*)&order;
15         for(i = 0; i < sizeof order; i++)
16                 t[o[i]] = f[i];
17 }
18
19 static int
20 stillopen(int fd, char *name)
21 {
22         char buf[64];
23
24         return fd >= 0 && fd2path(fd, buf, sizeof(buf)) == 0 && strcmp(buf, name) == 0;
25 }
26
27 vlong
28 nsec(void)
29 {
30         static char name[] = "/dev/bintime";
31         static int *pidp = nil, *fdp = nil, fd = -1;
32         uchar b[8];
33         vlong t;
34         int f;
35
36         if(pidp != nil && *pidp == _tos->pid)
37                 f = *fdp;
38         else{
39 Reopen:
40                 f = fd;
41                 if(fdp != nil && *fdp != f && stillopen(*fdp, name))
42                         f = *fdp;
43                 else if(!stillopen(f, name)){
44                         if((f = open(name, OREAD|OCEXEC)) < 0)
45                                 return 0;
46                 }
47                 fd = f;
48                 if(fdp == nil){
49                         fdp = (int*)privalloc();
50                         pidp = (int*)privalloc();
51                 }
52                 *fdp = f;
53                 *pidp = _tos->pid;
54         }
55         if(pread(f, b, sizeof b, 0) != sizeof b){
56                 if(!stillopen(f, name))
57                         goto Reopen;
58                 close(f);
59                 return 0;
60         }
61         be2vlong(&t, b);
62         return t;
63 }