Added support for WAVE_FORMAT_QUERY flag in wodOpen.
[wine/multimedia.git] / misc / port.c
blobd75d38b880190fbefa1e4fb380474c86b2f2c4d1
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "wine/port.h"
9 #ifdef __BEOS__
10 #include <be/kernel/fs_info.h>
11 #include <be/kernel/OS.h>
12 #endif
14 #include <assert.h>
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <sys/ioctl.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <termios.h>
26 #ifdef HAVE_LIBIO_H
27 # include <libio.h>
28 #endif
29 #ifdef HAVE_SYSCALL_H
30 # include <syscall.h>
31 #endif
32 #ifdef HAVE_PTY_H
33 # include <pty.h>
34 #endif
36 /***********************************************************************
37 * usleep
39 #ifndef HAVE_USLEEP
40 unsigned int usleep (unsigned int useconds)
42 #if defined(__EMX__)
43 DosSleep(useconds);
44 return 0;
45 #elif defined(__BEOS__)
46 return snooze(useconds);
47 #elif defined(HAVE_SELECT)
48 struct timeval delay;
50 delay.tv_sec = 0;
51 delay.tv_usec = useconds;
53 select( 0, 0, 0, 0, &delay );
54 return 0;
55 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
56 errno = ENOSYS;
57 return -1;
58 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
60 #endif /* HAVE_USLEEP */
62 /***********************************************************************
63 * memmove
65 #ifndef HAVE_MEMMOVE
66 void *memmove( void *dest, const void *src, unsigned int len )
68 register char *dst = dest;
70 /* Use memcpy if not overlapping */
71 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
73 memcpy( dst, src, len );
75 /* Otherwise do it the hard way (FIXME: could do better than this) */
76 else if (dst < src)
78 while (len--) *dst++ = *((char *)src)++;
80 else
82 dst += len - 1;
83 src = (char *)src + len - 1;
84 while (len--) *dst-- = *((char *)src)--;
86 return dest;
88 #endif /* HAVE_MEMMOVE */
90 /***********************************************************************
91 * strerror
93 #ifndef HAVE_STRERROR
94 const char *strerror( int err )
96 /* Let's hope we have sys_errlist then */
97 return sys_errlist[err];
99 #endif /* HAVE_STRERROR */
101 /***********************************************************************
102 * clone
104 #if !defined(HAVE_CLONE) && defined(__linux__)
105 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
107 #ifdef __i386__
108 int ret;
109 void **stack_ptr = (void **)stack;
110 *--stack_ptr = arg; /* Push argument on stack */
111 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
112 __asm__ __volatile__( "pushl %%ebx\n\t"
113 "movl %2,%%ebx\n\t"
114 "int $0x80\n\t"
115 "popl %%ebx\n\t" /* Contains fn in the child */
116 "testl %%eax,%%eax\n\t"
117 "jnz 0f\n\t"
118 "call *%%ebx\n\t" /* Should never return */
119 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
120 "0:"
121 : "=a" (ret)
122 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
123 assert( ret ); /* If ret is 0, we returned from the child function */
124 if (ret > 0) return ret;
125 errno = -ret;
126 return -1;
127 #else
128 errno = EINVAL;
129 return -1;
130 #endif /* __i386__ */
132 #endif /* !HAVE_CLONE && __linux__ */
134 /***********************************************************************
135 * strcasecmp
137 #ifndef HAVE_STRCASECMP
138 int strcasecmp( const char *str1, const char *str2 )
140 while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
141 return toupper(*str1) - toupper(*str2);
143 #endif /* HAVE_STRCASECMP */
145 /***********************************************************************
146 * strncasecmp
148 #ifndef HAVE_STRNCASECMP
149 int strncasecmp( const char *str1, const char *str2, size_t n )
151 int res;
152 if (!n) return 0;
153 while ((--n > 0) && *str1)
154 if ((res = toupper(*str1++) - toupper(*str2++))) return res;
155 return toupper(*str1) - toupper(*str2);
157 #endif /* HAVE_STRNCASECMP */
159 /***********************************************************************
160 * wine_openpty
161 * NOTE
162 * It looks like the openpty that comes with glibc in RedHat 5.0
163 * is buggy (second call returns what looks like a dup of 0 and 1
164 * instead of a new pty), this is a generic replacement.
166 * FIXME
167 * We should have a autoconf check for this.
169 int wine_openpty(int *master, int *slave, char *name,
170 struct termios *term, struct winsize *winsize)
172 #ifdef HAVE_OPENPTY
173 return openpty(master,slave,name,term,winsize);
174 #else
175 char *ptr1, *ptr2;
176 char pts_name[512];
178 strcpy (pts_name, "/dev/ptyXY");
180 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
181 pts_name[8] = *ptr1;
182 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
183 pts_name[9] = *ptr2;
185 if ((*master = open(pts_name, O_RDWR)) < 0) {
186 if (errno == ENOENT)
187 return -1;
188 else
189 continue;
191 pts_name[5] = 't';
192 if ((*slave = open(pts_name, O_RDWR)) < 0) {
193 pts_name[5] = 'p';
194 continue;
197 if (term != NULL)
198 tcsetattr(*slave, TCSANOW, term);
199 if (winsize != NULL)
200 ioctl(*slave, TIOCSWINSZ, winsize);
201 if (name != NULL)
202 strcpy(name, pts_name);
203 return *slave;
206 return -1;
207 #endif
210 /***********************************************************************
211 * getnetbyaddr
213 #ifndef HAVE_GETNETBYADDR
214 struct netent *getnetbyaddr(unsigned long net, int type)
216 errno = ENOSYS;
217 return NULL;
219 #endif /* defined(HAVE_GETNETBYNAME) */
221 /***********************************************************************
222 * getnetbyname
224 #ifndef HAVE_GETNETBYNAME
225 struct netent *getnetbyname(const char *name)
227 errno = ENOSYS;
228 return NULL;
230 #endif /* defined(HAVE_GETNETBYNAME) */
232 /***********************************************************************
233 * getprotobyname
235 #ifndef HAVE_GETPROTOBYNAME
236 struct protoent *getprotobyname(const char *name)
238 errno = ENOSYS;
239 return NULL;
241 #endif /* !defined(HAVE_GETPROTOBYNAME) */
243 /***********************************************************************
244 * getprotobynumber
246 #ifndef HAVE_GETPROTOBYNUMBER
247 struct protoent *getprotobynumber(int proto)
249 errno = ENOSYS;
250 return NULL;
252 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
254 /***********************************************************************
255 * getservbyport
257 #ifndef HAVE_GETSERVBYPORT
258 struct servent *getservbyport(int port, const char *proto)
260 errno = ENOSYS;
261 return NULL;
263 #endif /* !defined(HAVE_GETSERVBYPORT) */
265 /***********************************************************************
266 * getsockopt
268 #ifndef HAVE_GETSOCKOPT
269 int getsockopt(int socket, int level, int option_name,
270 void *option_value, size_t *option_len)
272 errno = ENOSYS;
273 return -1;
275 #endif /* !defined(HAVE_GETSOCKOPT) */
277 /***********************************************************************
278 * inet_network
280 #ifndef HAVE_INET_NETWORK
281 unsigned long inet_network(const char *cp)
283 errno = ENOSYS;
284 return 0;
286 #endif /* defined(HAVE_INET_NETWORK) */
288 /***********************************************************************
289 * settimeofday
291 #ifndef HAVE_SETTIMEOFDAY
292 int settimeofday(struct timeval *tp, void *reserved)
294 tp->tv_sec = 0;
295 tp->tv_usec = 0;
297 errno = ENOSYS;
298 return -1;
300 #endif /* HAVE_SETTIMEOFDAY */
302 /***********************************************************************
303 * statfs
305 #ifndef HAVE_STATFS
306 int statfs(const char *name, struct statfs *info)
308 #ifdef __BEOS__
309 dev_t mydev;
310 fs_info fsinfo;
312 if(!info) {
313 errno = ENOSYS;
314 return -1;
317 if ((mydev = dev_for_path(name)) < 0) {
318 errno = ENOSYS;
319 return -1;
322 if (fs_stat_dev(mydev,&fsinfo) < 0) {
323 errno = ENOSYS;
324 return -1;
327 info->f_bsize = fsinfo.block_size;
328 info->f_blocks = fsinfo.total_blocks;
329 info->f_bfree = fsinfo.free_blocks;
331 return 0;
332 #else /* defined(__BEOS__) */
333 errno = ENOSYS;
334 return -1;
335 #endif /* defined(__BEOS__) */
337 #endif /* !defined(HAVE_STATFS) */