do the same optimization as in time(2) Reference: /n/sources/patch/applied/ape-gettod-hold Date: Wed Jul 22 15:41:18 CES 2009 Signed-off-by: cinap_lenrek@gmx.de --- /sys/src/ape/lib/bsd/gettimeofday.c Wed Jul 22 15:40:59 2009 +++ /sys/src/ape/lib/bsd/gettimeofday.c Wed Jul 22 15:40:56 2009 @@ -25,15 +25,21 @@ int gettimeofday(struct timeval *tp, struct timezone *tzp) { - int f; + static int fd = -1; uchar b[8]; vlong t; + int opened; - memset(b, 0, sizeof b); - f = _OPEN("/dev/bintime", 0); - if(f >= 0) { - _PREAD(f, b, sizeof(b), 0); - _CLOSE(f); + opened = 0; + if(fd < 0){ + reopen: + if(opened++ || (fd = _OPEN("/dev/bintime", OREAD|OCEXEC)) < 0) + return 0; + } + if(_PREAD(fd, b, sizeof b, 0) != sizeof b){ + _CLOSE(fd); + fd = -1; + goto reopen; } be2vlong(&t, b);