Implement window size passing between real tty and virtual console.
[dragonfly.git] / contrib / tar / lib / msleep.c
1 /* Sleep a given number of milliseconds.
2    Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
3    François Pinard <pinard@iro.umontreal.ca>, 1992.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* This code is heavily borrowed from Taylor UUCP 1.03.  Ian picks one of
24    usleep, nap, napms, poll, select and sleep, in decreasing order of
25    preference.  The sleep function is always available.  */
26
27 /* In many cases, we will sleep if the wanted number of milliseconds
28    is higher than this value.  */
29 #define THRESHOLD_FOR_SLEEP 30000
30
31 /* Include some header files.  */
32
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36
37 #if HAVE_POLL
38 # if HAVE_STROPTS_H
39 #  include <stropts.h>
40 # endif
41 # if HAVE_POLL_H
42 #  include <sys/types.h>
43 #  include <poll.h>
44 # endif
45 # if !HAVE_STROPTS_H && !HAVE_POLL_H
46 /* We need a definition for struct pollfd, although it doesn't matter
47    what it contains.  */
48 struct pollfd
49 {
50   int idummy;
51 };
52 # endif
53 #else
54 # if HAVE_SELECT
55 #  include <sys/time.h>
56 # endif
57 #endif
58
59 /*---------------------------------------.
60 | Sleep a given number of milliseconds.  |
61 `---------------------------------------*/
62
63 void
64 msleep (milliseconds)
65      int milliseconds;
66 {
67 #if HAVE_USLEEP
68
69   if (milliseconds > 0)
70     usleep (milliseconds * (long) 1000);
71
72 #else
73 # if HAVE_NAP
74
75   if (milliseconds > 0)
76     nap ((long) milliseconds);
77
78 # else
79 #  if HAVE_NAPMS
80
81   if (milliseconds >= THRESHOLD_FOR_SLEEP)
82     {
83       sleep (milliseconds / 1000);
84       milliseconds %= 1000;
85     }
86   if (milliseconds > 0)
87     napms (milliseconds);
88
89 #  else
90 #   if HAVE_POLL
91
92   struct pollfd sdummy;         /* poll(2) checks this address */
93
94   if (milliseconds >= THRESHOLD_FOR_SLEEP)
95     {
96       sleep (milliseconds / 1000);
97       milliseconds %= 1000;
98     }
99   if (milliseconds > 0)
100     poll (&sdummy, 0, milliseconds);
101
102 #   else
103 #    if HAVE_SELECT
104
105   struct timeval s;
106
107   if (milliseconds >= THRESHOLD_FOR_SLEEP)
108     {
109       sleep (milliseconds / 1000);
110       milliseconds %= 1000;
111     }
112   if (milliseconds > 0)
113     {
114       s.tv_sec = milliseconds / 1000;
115       s.tv_usec = (milliseconds % 1000) * (long) 1000;
116       select (0, NULL, NULL, NULL, &s);
117     }
118
119 #    else
120
121   /* Round the time up to the next full second.  */
122
123   if (milliseconds > 0)
124     sleep ((milliseconds + 999) / 1000);
125
126 #    endif
127 #   endif
128 #  endif
129 # endif
130 #endif
131 }