* cppfiles.c (_cpp_execute_include): Move `len` initialisation
[official-gcc.git] / libjava / posix.cc
blobd470a644da3c9d92c31a606f65a3c9baf59d4863
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include "posix.h"
15 #include <errno.h>
17 #if defined (ECOS)
18 extern "C" unsigned long long _clock (void);
19 #endif
21 // gettimeofday implementation.
22 void
23 _Jv_gettimeofday (struct timeval *tv)
25 #if defined (HAVE_GETTIMEOFDAY)
26 gettimeofday (tv, NULL);
27 #elif defined (HAVE_TIME)
28 tv->tv_sec = time (NULL);
29 tv->tv_usec = 0;
30 #elif defined (HAVE_FTIME)
31 struct timeb t;
32 ftime (&t);
33 tv->tv_sec = t.time;
34 tv->tv_usec = t.millitm * 1000;
35 #elif defined (ECOS)
36 // FIXME.
37 tv->tv_sec = _clock () / 1000;
38 tv->tv_usec = 0;
39 #else
40 // In the absence of any function, time remains forever fixed.
41 tv->tv_sec = 23;
42 tv->tv_usec = 0;
43 #endif
46 // A wrapper for select() which ignores EINTR.
47 int
48 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
49 fd_set *exceptfds, struct timeval *timeout)
51 #ifdef HAVE_SELECT
52 // If we have a timeout, compute the absolute ending time.
53 struct timeval end, delay;
54 if (timeout)
56 _Jv_gettimeofday (&end);
57 end.tv_usec += timeout->tv_usec;
58 if (end.tv_usec >= 1000000)
60 ++end.tv_sec;
61 end.tv_usec -= 1000000;
63 end.tv_sec += timeout->tv_sec;
64 delay = *timeout;
66 else
68 // Placate compiler.
69 delay.tv_sec = delay.tv_usec = 0;
72 while (1)
74 int r = select (n, readfds, writefds, exceptfds,
75 timeout ? &delay : NULL);
76 if (r != -1 || errno != EINTR)
77 return r;
79 struct timeval after;
80 if (timeout)
82 _Jv_gettimeofday (&after);
83 // Now compute new timeout argument.
84 delay.tv_usec = end.tv_usec - after.tv_usec;
85 delay.tv_sec = end.tv_sec - after.tv_sec;
86 if (delay.tv_usec < 0)
88 --delay.tv_sec;
89 delay.tv_usec += 1000000;
91 if (delay.tv_sec < 0)
93 // We assume that the user wants a valid select() call
94 // more than precise timing. So if we get a series of
95 // EINTR we just keep trying with delay 0 until we get a
96 // valid result.
97 delay.tv_sec = 0;
101 #else /* HAVE_SELECT */
102 return 0;
103 #endif