1 /* $Id$ */
2 
3 /*
4  *
5  * Copyright (C) 1998 David Mazieres (dm@uun.org)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  *
22  */
23 
24 
25 #include "sysconf.h"
26 
27 #ifndef HAVE_CLOCK_GETTIME
28 
29 #include <sys/resource.h>
30 
31 int
clock_gettime(int id,struct timespec * tp)32 clock_gettime (int id, struct timespec *tp)
33 {
34   struct timeval tv;
35   struct rusage ru;
36 
37   switch (id) {
38   case CLOCK_REALTIME:
39     if (gettimeofday (&tv, NULL) < 0)
40       return -1;
41     tp->tv_sec = tv.tv_sec;
42     tp->tv_nsec = tv.tv_usec * 1000;
43     return 0;
44   case CLOCK_VIRTUAL:
45     if (getrusage (RUSAGE_SELF, &ru) < 0)
46       return -1;
47     tp->tv_sec = ru.ru_utime.tv_sec;
48     tp->tv_nsec = ru.ru_utime.tv_usec * 1000;
49     return 0;
50   case CLOCK_PROF:
51     if (getrusage (RUSAGE_SELF, &ru) < 0)
52       return -1;
53     tp->tv_sec = (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec);
54     tp->tv_nsec = (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) * 1000;
55     while (tp->tv_nsec > 1000000000) {
56       tp->tv_sec++;
57       tp->tv_nsec -= 1000000000;
58     }
59     return 0;
60   default:
61     errno = EINVAL;
62     return -1;
63   }
64 }
65 
66 #endif /* !HAVE_CLOCK_GETTIME */
67