C files should #include only the header files they need, not
[bpt/guile.git] / libguile / stime.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42
43#include <stdio.h>
44#include "_scm.h"
45#ifdef HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48
49\f
50# ifdef HAVE_SYS_TYPES_H
51# include <sys/types.h>
52# endif
53
54# ifdef TIME_WITH_SYS_TIME
55# include <sys/time.h>
56# include <time.h>
57# else
58# ifdef HAVE_SYS_TIME_H
59# include <sys/time.h>
60# else
61# ifdef HAVE_TIME_H
62# include <time.h>
63# endif
64# endif
65# endif
66
67# ifdef HAVE_SYS_TIMES_H
68# include <sys/times.h>
69# else
70# ifdef HAVE_SYS_TIMEB_H
71# include <sys/timeb.h>
72# endif
73# endif
74
75#ifdef CLK_TCK
76# define CLKTCK CLK_TCK
77# ifdef CLOCKS_PER_SEC
78# ifdef unix
79# ifndef ARM_ULIB
80# include <sys/times.h>
81# endif
82# define LACK_CLOCK
83 /* This is because clock() might be POSIX rather than ANSI.
84 This occurs on HP-UX machines */
85# endif
86# endif
87#else
88# ifdef CLOCKS_PER_SEC
89# define CLKTCK CLOCKS_PER_SEC
90# else
91# define LACK_CLOCK
92# define CLKTCK 60
93# endif
94#endif
95
96
97# ifdef HAVE_FTIME
98# include <sys/timeb.h>
99# endif
100
101
102#ifdef __STDC__
103# define timet time_t
104#else
105# define timet long
106#endif
107
108#ifdef HAVE_TIMES
109#ifdef __STDC__
110static
111long mytime(void)
112#else
113static
114long mytime()
115#endif
116{
117 struct tms time_buffer;
118 times(&time_buffer);
119 return time_buffer.tms_utime + time_buffer.tms_stime;
120}
121#else
122# ifdef LACK_CLOCK
123# define mytime() ((time((timet*)0) - scm_your_base) * CLKTCK)
124# else
125# define mytime clock
126# endif
127#endif
128
129
130
131#ifdef HAVE_FTIME
132
23858ad1
MD
133extern int ftime (struct timeb *);
134
0f2d19dd
JB
135struct timeb scm_your_base = {0};
136SCM_PROC(s_get_internal_real_time, "get-internal-real-time", 0, 0, 0, scm_get_internal_real_time);
137#ifdef __STDC__
138SCM
139scm_get_internal_real_time(void)
140#else
141SCM
142scm_get_internal_real_time()
143#endif
144{
145 struct timeb time_buffer;
146 long tmp;
147 ftime(&time_buffer);
148 time_buffer.time -= scm_your_base.time;
149 tmp = time_buffer.millitm - scm_your_base.millitm;
150 tmp = time_buffer.time*1000L + tmp;
151 tmp *= CLKTCK;
152 tmp /= 1000;
153 return SCM_MAKINUM(tmp);
154}
155
156#else
157
158timet scm_your_base = 0;
159SCM_PROC(s_get_internal_real_time, "get-internal-real-time", 0, 0, 0, scm_get_internal_real_time);
160#ifdef __STDC__
161SCM
162scm_get_internal_real_time(void)
163#else
164SCM
165scm_get_internal_real_time()
166#endif
167{
168 return SCM_MAKINUM((time((timet*)0) - scm_your_base) * (int)CLKTCK);
169}
170#endif
171
172
173
174static long scm_my_base = 0;
175
176SCM_PROC(s_get_internal_run_time, "get-internal-run-time", 0, 0, 0, scm_get_internal_run_time);
177#ifdef __STDC__
178SCM
179scm_get_internal_run_time(void)
180#else
181SCM
182scm_get_internal_run_time()
183#endif
184{
185 return SCM_MAKINUM(mytime()-scm_my_base);
186}
187
188SCM_PROC(s_current_time, "current-time", 0, 0, 0, scm_current_time);
189#ifdef __STDC__
190SCM
191scm_current_time(void)
192#else
193SCM
194scm_current_time()
195#endif
196{
197 timet timv = time((timet*)0);
198 SCM ans;
199 ans = scm_ulong2num(timv);
200 return SCM_BOOL_F==ans ? SCM_MAKINUM(timv) : ans;
201}
202
203#ifdef __STDC__
204long
205scm_time_in_msec(long x)
206#else
207long
208scm_time_in_msec(x)
209 long x;
210#endif
211{
212 if (CLKTCK==60) return (x*50)/3;
213 else
214 return (CLKTCK < 1000 ? x*(1000L/(long)CLKTCK) : (x*1000L)/(long)CLKTCK);
215}
216
217#ifdef __STDC__
218void
219scm_init_stime(void)
220#else
221void
222scm_init_stime()
223#endif
224{
225 scm_sysintern("internal-time-units-per-second",
226 SCM_MAKINUM((long)CLKTCK));
227
228#ifdef HAVE_FTIME
229 if (!scm_your_base.time) ftime(&scm_your_base);
230#else
231 if (!scm_your_base) time(&scm_your_base);
232#endif
233
234 if (!scm_my_base) scm_my_base = mytime();
235
236#include "stime.x"
237}
238