3311
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
4086
|
27 #include <stdarg.h> |
|
28 #include <stdio.h> |
|
29 #include <stdlib.h> |
|
30 #include <string.h> |
|
31 |
|
32 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
33 |
|
34 #include <windows.h> |
|
35 |
|
36 #else |
|
37 |
3311
|
38 #ifdef HAVE_UNISTD_H |
|
39 #ifdef HAVE_SYS_TYPES_H |
|
40 #include <sys/types.h> |
|
41 #endif |
|
42 #include <unistd.h> |
|
43 #endif |
|
44 |
|
45 #include "systime.h" |
|
46 |
|
47 #ifdef HAVE_POLL_H |
|
48 #include <poll.h> |
|
49 #elif HAVE_SYS_POLL_H |
|
50 #include <sys/poll.h> |
|
51 #endif |
|
52 |
|
53 #endif |
|
54 |
3317
|
55 void |
4067
|
56 octave_sleep (unsigned int seconds) |
|
57 { |
4084
|
58 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
59 Sleep (1000 * seconds); |
|
60 #else |
4067
|
61 sleep (seconds); |
4084
|
62 #endif |
4067
|
63 } |
|
64 |
|
65 void |
3317
|
66 octave_usleep (unsigned int useconds) |
|
67 { |
|
68 unsigned int sec = useconds / 1000000; |
|
69 unsigned int usec = useconds % 1000000; |
|
70 |
|
71 if (sec > 0) |
4086
|
72 octave_sleep (sec); |
|
73 |
|
74 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
75 |
|
76 /* Round to the nearest millisecond, with a minimum of 1 millisecond |
|
77 if usleep was called with a a non-zero value. */ |
|
78 |
|
79 if (usec > 500) |
|
80 Sleep ((usec+500)/1000); |
|
81 else if (usec > 0) |
|
82 Sleep (1); |
|
83 else |
|
84 Sleep (0); |
|
85 |
|
86 #elif defined (HAVE_USLEEP) |
|
87 |
|
88 usleep (usec); |
3317
|
89 |
4086
|
90 #elif defined (HAVE_SELECT) |
|
91 |
|
92 struct timeval delay; |
|
93 |
|
94 delay.tv_sec = 0; |
|
95 delay.tv_usec = usec; |
|
96 |
|
97 select (0, 0, 0, 0, &delay); |
|
98 |
|
99 #elif defined (HAVE_POLL) |
|
100 |
|
101 struct pollfd pfd; |
|
102 int delay = usec / 1000; |
|
103 |
|
104 if (delay > 0) |
|
105 poll (&fd, 0, delay); |
|
106 |
|
107 #endif |
3317
|
108 } |
|
109 |
3350
|
110 int |
|
111 octave_strcasecmp (const char *s1, const char *s2) |
|
112 { |
|
113 return strcasecmp (s1, s2); |
|
114 } |
|
115 |
|
116 int |
|
117 octave_strncasecmp (const char *s1, const char *s2, size_t n) |
|
118 { |
|
119 return strncasecmp (s1, s2, n); |
|
120 } |
|
121 |
4269
|
122 /* XXX FIXME XXX -- we really need a configure test for this. */ |
4160
|
123 |
|
124 #if defined __GNUC__ && __GNUC__ >= 3 |
|
125 #define HAVE_C99_VSNPRINTF 1 |
|
126 #endif |
|
127 |
4269
|
128 /* We manage storage. User should not free it, and its contents are |
|
129 only valid until next call to vsnprintf. */ |
4124
|
130 |
3620
|
131 char * |
3623
|
132 octave_vsnprintf (const char *fmt, va_list args) |
3620
|
133 { |
4124
|
134 static size_t size = 100; |
3620
|
135 |
4124
|
136 static char *buf = 0; |
|
137 |
4150
|
138 int nchars; |
|
139 |
4124
|
140 if (! buf) |
|
141 buf = malloc (size); |
3620
|
142 |
4160
|
143 if (! buf) |
|
144 return 0; |
|
145 |
|
146 #if defined (HAVE_C99_VSNPRINTF) |
|
147 |
|
148 nchars = vsnprintf (buf, size, fmt, args); |
4150
|
149 |
|
150 if (nchars >= size) |
|
151 { |
|
152 size = nchars + 1; |
|
153 buf = realloc (buf, size); |
|
154 |
|
155 if (buf) |
|
156 vsnprintf (buf, size, fmt, args); |
4156
|
157 } |
4150
|
158 |
4160
|
159 #else |
|
160 |
|
161 while (1) |
|
162 { |
|
163 nchars = vsnprintf (buf, size, fmt, args); |
|
164 |
|
165 if (nchars > -1) |
|
166 return buf; |
|
167 else |
|
168 { |
|
169 size *= 2; |
|
170 |
|
171 buf = realloc (buf, size); |
|
172 |
|
173 if (! buf) |
|
174 return 0; |
|
175 } |
|
176 } |
|
177 |
|
178 #endif |
|
179 |
4156
|
180 return buf; |
3620
|
181 } |
|
182 |
|
183 char * |
3623
|
184 octave_snprintf (const char *fmt, ...) |
3620
|
185 { |
|
186 char *retval = 0; |
|
187 |
|
188 va_list args; |
|
189 va_start (args, fmt); |
|
190 |
3625
|
191 retval = octave_vsnprintf (fmt, args); |
3620
|
192 |
|
193 va_end (args); |
|
194 |
|
195 return retval; |
|
196 } |
|
197 |
3311
|
198 /* |
|
199 ;;; Local Variables: *** |
|
200 ;;; mode: C++ *** |
|
201 ;;; End: *** |
|
202 */ |