Added version16.rc in RC_SRCS list.
[wine/multimedia.git] / library / port.c
blob004f85384013e9c036f0d37aab499016bca89e4e
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.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 <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/time.h>
22 #include <sys/stat.h>
23 #include <sys/ioctl.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <termios.h>
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30 #ifdef HAVE_LIBIO_H
31 # include <libio.h>
32 #endif
33 #ifdef HAVE_SYSCALL_H
34 # include <syscall.h>
35 #endif
36 #ifdef HAVE_PTY_H
37 # include <pty.h>
38 #endif
39 #ifdef HAVE_LIBUTIL_H
40 # include <libutil.h>
41 #endif
42 #ifdef HAVE_DL_API
43 # include <dlfcn.h>
44 #endif
46 #include "wine/port.h"
48 /***********************************************************************
49 * usleep
51 #ifndef HAVE_USLEEP
52 unsigned int usleep (unsigned int useconds)
54 #if defined(__EMX__)
55 DosSleep(useconds);
56 return 0;
57 #elif defined(__BEOS__)
58 return snooze(useconds);
59 #elif defined(HAVE_SELECT)
60 struct timeval delay;
62 delay.tv_sec = useconds / 1000000;
63 delay.tv_usec = useconds % 1000000;
65 select( 0, 0, 0, 0, &delay );
66 return 0;
67 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
68 errno = ENOSYS;
69 return -1;
70 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
72 #endif /* HAVE_USLEEP */
74 /***********************************************************************
75 * memmove
77 #ifndef HAVE_MEMMOVE
78 void *memmove( void *dest, const void *src, unsigned int len )
80 register char *dst = dest;
82 /* Use memcpy if not overlapping */
83 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
85 memcpy( dst, src, len );
87 /* Otherwise do it the hard way (FIXME: could do better than this) */
88 else if (dst < src)
90 while (len--) *dst++ = *((char *)src)++;
92 else
94 dst += len - 1;
95 src = (char *)src + len - 1;
96 while (len--) *dst-- = *((char *)src)--;
98 return dest;
100 #endif /* HAVE_MEMMOVE */
102 /***********************************************************************
103 * strerror
105 #ifndef HAVE_STRERROR
106 const char *strerror( int err )
108 /* Let's hope we have sys_errlist then */
109 return sys_errlist[err];
111 #endif /* HAVE_STRERROR */
114 /***********************************************************************
115 * getpagesize
117 #ifndef HAVE_GETPAGESIZE
118 size_t getpagesize(void)
120 # ifdef __svr4__
121 return sysconf(_SC_PAGESIZE);
122 # else
123 # error Cannot get the page size on this platform
124 # endif
126 #endif /* HAVE_GETPAGESIZE */
129 /***********************************************************************
130 * clone
132 #if !defined(HAVE_CLONE) && defined(__linux__)
133 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
135 #ifdef __i386__
136 int ret;
137 void **stack_ptr = (void **)stack;
138 *--stack_ptr = arg; /* Push argument on stack */
139 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
140 __asm__ __volatile__( "pushl %%ebx\n\t"
141 "movl %2,%%ebx\n\t"
142 "int $0x80\n\t"
143 "popl %%ebx\n\t" /* Contains fn in the child */
144 "testl %%eax,%%eax\n\t"
145 "jnz 0f\n\t"
146 "xorl %ebp,%ebp\n\t" /* Terminate the stack frames */
147 "call *%%ebx\n\t" /* Should never return */
148 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
149 "0:"
150 : "=a" (ret)
151 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
152 assert( ret ); /* If ret is 0, we returned from the child function */
153 if (ret > 0) return ret;
154 errno = -ret;
155 return -1;
156 #else
157 errno = EINVAL;
158 return -1;
159 #endif /* __i386__ */
161 #endif /* !HAVE_CLONE && __linux__ */
163 /***********************************************************************
164 * strcasecmp
166 #ifndef HAVE_STRCASECMP
167 int strcasecmp( const char *str1, const char *str2 )
169 const unsigned char *ustr1 = (const unsigned char *)str1;
170 const unsigned char *ustr2 = (const unsigned char *)str2;
172 while (*ustr1 && toupper(*ustr1) == toupper(*ustr2)) {
173 ustr1++;
174 ustr2++;
176 return toupper(*ustr1) - toupper(*ustr2);
178 #endif /* HAVE_STRCASECMP */
180 /***********************************************************************
181 * strncasecmp
183 #ifndef HAVE_STRNCASECMP
184 int strncasecmp( const char *str1, const char *str2, size_t n )
186 const unsigned char *ustr1 = (const unsigned char *)str1;
187 const unsigned char *ustr2 = (const unsigned char *)str2;
188 int res;
190 if (!n) return 0;
191 while ((--n > 0) && *ustr1) {
192 if ((res = toupper(*ustr1) - toupper(*ustr2))) return res;
193 ustr1++;
194 ustr2++;
196 return toupper(*ustr1) - toupper(*ustr2);
198 #endif /* HAVE_STRNCASECMP */
200 /***********************************************************************
201 * wine_openpty
202 * NOTE
203 * It looks like the openpty that comes with glibc in RedHat 5.0
204 * is buggy (second call returns what looks like a dup of 0 and 1
205 * instead of a new pty), this is a generic replacement.
207 * FIXME
208 * We should have a autoconf check for this.
210 int wine_openpty(int *master, int *slave, char *name,
211 struct termios *term, struct winsize *winsize)
213 #ifdef HAVE_OPENPTY
214 return openpty(master, slave, name, term, winsize);
215 #else
216 const char *ptr1, *ptr2;
217 char pts_name[512];
219 strcpy (pts_name, "/dev/ptyXY");
221 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
222 pts_name[8] = *ptr1;
223 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
224 pts_name[9] = *ptr2;
226 if ((*master = open(pts_name, O_RDWR)) < 0) {
227 if (errno == ENOENT)
228 return -1;
229 else
230 continue;
232 pts_name[5] = 't';
233 if ((*slave = open(pts_name, O_RDWR)) < 0) {
234 pts_name[5] = 'p';
235 close (*master);
236 continue;
239 if (term != NULL)
240 tcsetattr(*slave, TCSANOW, term);
241 if (winsize != NULL)
242 ioctl(*slave, TIOCSWINSZ, winsize);
243 if (name != NULL)
244 strcpy(name, pts_name);
245 return *slave;
248 errno = EMFILE;
249 return -1;
250 #endif
253 /***********************************************************************
254 * getnetbyaddr
256 #ifndef HAVE_GETNETBYADDR
257 struct netent *getnetbyaddr(unsigned long net, int type)
259 errno = ENOSYS;
260 return NULL;
262 #endif /* defined(HAVE_GETNETBYNAME) */
264 /***********************************************************************
265 * getnetbyname
267 #ifndef HAVE_GETNETBYNAME
268 struct netent *getnetbyname(const char *name)
270 errno = ENOSYS;
271 return NULL;
273 #endif /* defined(HAVE_GETNETBYNAME) */
275 /***********************************************************************
276 * getprotobyname
278 #ifndef HAVE_GETPROTOBYNAME
279 struct protoent *getprotobyname(const char *name)
281 errno = ENOSYS;
282 return NULL;
284 #endif /* !defined(HAVE_GETPROTOBYNAME) */
286 /***********************************************************************
287 * getprotobynumber
289 #ifndef HAVE_GETPROTOBYNUMBER
290 struct protoent *getprotobynumber(int proto)
292 errno = ENOSYS;
293 return NULL;
295 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
297 /***********************************************************************
298 * getservbyport
300 #ifndef HAVE_GETSERVBYPORT
301 struct servent *getservbyport(int port, const char *proto)
303 errno = ENOSYS;
304 return NULL;
306 #endif /* !defined(HAVE_GETSERVBYPORT) */
308 /***********************************************************************
309 * getsockopt
311 #ifndef HAVE_GETSOCKOPT
312 int getsockopt(int socket, int level, int option_name,
313 void *option_value, size_t *option_len)
315 errno = ENOSYS;
316 return -1;
318 #endif /* !defined(HAVE_GETSOCKOPT) */
320 /***********************************************************************
321 * inet_network
323 #ifndef HAVE_INET_NETWORK
324 unsigned long inet_network(const char *cp)
326 errno = ENOSYS;
327 return 0;
329 #endif /* defined(HAVE_INET_NETWORK) */
331 /***********************************************************************
332 * settimeofday
334 #ifndef HAVE_SETTIMEOFDAY
335 int settimeofday(struct timeval *tp, void *reserved)
337 tp->tv_sec = 0;
338 tp->tv_usec = 0;
340 errno = ENOSYS;
341 return -1;
343 #endif /* HAVE_SETTIMEOFDAY */
345 /***********************************************************************
346 * statfs
348 #ifndef HAVE_STATFS
349 int statfs(const char *name, struct statfs *info)
351 #ifdef __BEOS__
352 dev_t mydev;
353 fs_info fsinfo;
355 if(!info) {
356 errno = ENOSYS;
357 return -1;
360 if ((mydev = dev_for_path(name)) < 0) {
361 errno = ENOSYS;
362 return -1;
365 if (fs_stat_dev(mydev,&fsinfo) < 0) {
366 errno = ENOSYS;
367 return -1;
370 info->f_bsize = fsinfo.block_size;
371 info->f_blocks = fsinfo.total_blocks;
372 info->f_bfree = fsinfo.free_blocks;
373 return 0;
374 #else /* defined(__BEOS__) */
375 errno = ENOSYS;
376 return -1;
377 #endif /* defined(__BEOS__) */
379 #endif /* !defined(HAVE_STATFS) */
382 /***********************************************************************
383 * lstat
385 #ifndef HAVE_LSTAT
386 int lstat(const char *file_name, struct stat *buf)
388 return stat( file_name, buf );
390 #endif /* HAVE_LSTAT */
393 /***********************************************************************
394 * getrlimit
396 #ifndef HAVE_GETRLIMIT
397 int getrlimit (int resource, struct rlimit *rlim)
399 return -1; /* FAIL */
401 #endif /* HAVE_GETRLIMIT */
403 /***********************************************************************
404 * wine_anon_mmap
406 * Portable wrapper for anonymous mmaps
408 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
410 static int fdzero = -1;
412 #ifdef MAP_ANON
413 flags |= MAP_ANON;
414 #else
415 if (fdzero == -1)
417 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
419 perror( "/dev/zero: open" );
420 exit(1);
423 #endif /* MAP_ANON */
425 #ifdef MAP_SHARED
426 flags &= ~MAP_SHARED;
427 #endif
429 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
430 #ifdef MAP_PRIVATE
431 flags |= MAP_PRIVATE;
432 #endif
434 return mmap( start, size, prot, flags, fdzero, 0 );
439 * These functions provide wrappers around dlopen() and associated
440 * functions. They work around a bug in glibc 2.1.x where calling
441 * a dl*() function after a previous dl*() function has failed
442 * without a dlerror() call between the two will cause a crash.
443 * They all take a pointer to a buffer that
444 * will receive the error description (from dlerror()). This
445 * parameter may be NULL if the error description is not required.
448 /***********************************************************************
449 * wine_dlopen
451 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
453 #ifdef HAVE_DL_API
454 void *ret;
455 char *s;
456 dlerror(); dlerror();
457 ret = dlopen( filename, flag );
458 s = dlerror();
459 if (error)
461 strncpy( error, s ? s : "", errorsize );
462 error[errorsize - 1] = '\0';
464 dlerror();
465 return ret;
466 #else
467 if (error)
469 strncpy( error, "dlopen interface not detected by configure", errorsize );
470 error[errorsize - 1] = '\0';
472 return NULL;
473 #endif
476 /***********************************************************************
477 * wine_dlsym
479 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
481 #ifdef HAVE_DL_API
482 void *ret;
483 char *s;
484 dlerror(); dlerror();
485 ret = dlsym( handle, symbol );
486 s = dlerror();
487 if (error)
489 strncpy( error, s ? s : "", errorsize );
490 error[errorsize - 1] = '\0';
492 dlerror();
493 return ret;
494 #else
495 if (error)
497 strncpy( error, "dlopen interface not detected by configure", errorsize );
498 error[errorsize - 1] = '\0';
500 return NULL;
501 #endif
504 /***********************************************************************
505 * wine_dlclose
507 int wine_dlclose( void *handle, char *error, int errorsize )
509 #ifdef HAVE_DL_API
510 int ret;
511 char *s;
512 dlerror(); dlerror();
513 ret = dlclose( handle );
514 s = dlerror();
515 if (error)
517 strncpy( error, s ? s : "", errorsize );
518 error[errorsize - 1] = '\0';
520 dlerror();
521 return ret;
522 #else
523 if (error)
525 strncpy( error, "dlopen interface not detected by configure", errorsize );
526 error[errorsize - 1] = '\0';
528 return 1;
529 #endif
532 /***********************************************************************
533 * wine_rewrite_s4tos2
535 * Convert 4 byte Unicode strings to 2 byte Unicode strings in-place.
536 * This is only practical if literal strings are writable.
538 unsigned short* wine_rewrite_s4tos2(const wchar_t* str4 )
540 unsigned short *str2,*s2;
542 if (str4==NULL)
543 return NULL;
545 if ((*str4 & 0xffff0000) != 0) {
546 /* This string has already been converted. Return it as is */
547 return (unsigned short*)str4;
550 /* Note that we can also end up here if the string has a single
551 * character. In such a case we will convert the string over and
552 * over again. But this is harmless.
554 str2=s2=(unsigned short*)str4;
555 do {
556 *s2=(unsigned short)*str4;
557 s2++;
558 } while (*str4++ != L'\0');
560 return str2;
563 #ifndef HAVE_ECVT
565 * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though.
566 * Fix/verify these implementations !
569 /***********************************************************************
570 * ecvt
572 char *ecvt (double number, int ndigits, int *decpt, int *sign)
574 static buf[40]; /* ought to be enough */
575 char *dec;
576 sprintf(buf, "%.*e", ndigits /* FIXME wrong */, number);
577 *sign = (number < 0);
578 dec = strchr(buf, '.');
579 *decpt = (dec) ? (int)dec - (int)buf : -1;
580 return buf;
583 /***********************************************************************
584 * fcvt
586 char *fcvt (double number, int ndigits, int *decpt, int *sign)
588 static buf[40]; /* ought to be enough */
589 char *dec;
590 sprintf(buf, "%.*e", ndigits, number);
591 *sign = (number < 0);
592 dec = strchr(buf, '.');
593 *decpt = (dec) ? (int)dec - (int)buf : -1;
594 return buf;
597 /***********************************************************************
598 * gcvt
600 * FIXME: uses both E and F.
602 char *gcvt (double number, size_t ndigit, char *buff)
604 sprintf(buff, "%.*E", ndigit, number);
605 return buff;
607 #endif /* HAVE_ECVT */