2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
10 #include <be/kernel/fs_info.h>
11 #include <be/kernel/OS.h>
20 #include <sys/types.h>
23 #include <sys/ioctl.h>
27 #ifdef HAVE_SYS_MMAN_H
46 #include "wine/port.h"
48 /***********************************************************************
52 unsigned int usleep (unsigned int useconds)
57 #elif defined(__BEOS__)
58 return snooze(useconds);
59 #elif defined(HAVE_SELECT)
62 delay.tv_sec = useconds / 1000000;
63 delay.tv_usec = useconds % 1000000;
65 select( 0, 0, 0, 0, &delay );
67 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
70 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
72 #endif /* HAVE_USLEEP */
74 /***********************************************************************
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) */
90 while (len--) *dst++ = *((char *)src)++;
95 src = (char *)src + len - 1;
96 while (len--) *dst-- = *((char *)src)--;
100 #endif /* HAVE_MEMMOVE */
102 /***********************************************************************
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 /***********************************************************************
117 #ifndef HAVE_GETPAGESIZE
118 size_t getpagesize(void)
121 return sysconf(_SC_PAGESIZE);
123 # error Cannot get the page size on this platform
126 #endif /* HAVE_GETPAGESIZE */
129 /***********************************************************************
132 #if !defined(HAVE_CLONE) && defined(__linux__)
133 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
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"
143 "popl %%ebx\n\t" /* Contains fn in the child */
144 "testl %%eax,%%eax\n\t"
146 "call *%%ebx\n\t" /* Should never return */
147 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
150 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
151 assert( ret ); /* If ret is 0, we returned from the child function */
152 if (ret > 0) return ret;
158 #endif /* __i386__ */
160 #endif /* !HAVE_CLONE && __linux__ */
162 /***********************************************************************
165 #ifndef HAVE_STRCASECMP
166 int strcasecmp( const char *str1, const char *str2 )
168 const unsigned char *ustr1 = (const unsigned char *)str1;
169 const unsigned char *ustr2 = (const unsigned char *)str2;
171 while (*ustr1 && toupper(*ustr1) == toupper(*ustr2)) {
175 return toupper(*ustr1) - toupper(*ustr2);
177 #endif /* HAVE_STRCASECMP */
179 /***********************************************************************
182 #ifndef HAVE_STRNCASECMP
183 int strncasecmp( const char *str1, const char *str2, size_t n )
185 const unsigned char *ustr1 = (const unsigned char *)str1;
186 const unsigned char *ustr2 = (const unsigned char *)str2;
190 while ((--n > 0) && *ustr1) {
191 if ((res = toupper(*ustr1) - toupper(*ustr2))) return res;
195 return toupper(*ustr1) - toupper(*ustr2);
197 #endif /* HAVE_STRNCASECMP */
199 /***********************************************************************
202 * It looks like the openpty that comes with glibc in RedHat 5.0
203 * is buggy (second call returns what looks like a dup of 0 and 1
204 * instead of a new pty), this is a generic replacement.
207 * We should have a autoconf check for this.
209 int wine_openpty(int *master, int *slave, char *name,
210 struct termios *term, struct winsize *winsize)
213 return openpty(master, slave, name, term, winsize);
215 const char *ptr1, *ptr2;
218 strcpy (pts_name, "/dev/ptyXY");
220 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
222 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
225 if ((*master = open(pts_name, O_RDWR)) < 0) {
232 if ((*slave = open(pts_name, O_RDWR)) < 0) {
239 tcsetattr(*slave, TCSANOW, term);
241 ioctl(*slave, TIOCSWINSZ, winsize);
243 strcpy(name, pts_name);
252 /***********************************************************************
255 #ifndef HAVE_GETNETBYADDR
256 struct netent *getnetbyaddr(unsigned long net, int type)
261 #endif /* defined(HAVE_GETNETBYNAME) */
263 /***********************************************************************
266 #ifndef HAVE_GETNETBYNAME
267 struct netent *getnetbyname(const char *name)
272 #endif /* defined(HAVE_GETNETBYNAME) */
274 /***********************************************************************
277 #ifndef HAVE_GETPROTOBYNAME
278 struct protoent *getprotobyname(const char *name)
283 #endif /* !defined(HAVE_GETPROTOBYNAME) */
285 /***********************************************************************
288 #ifndef HAVE_GETPROTOBYNUMBER
289 struct protoent *getprotobynumber(int proto)
294 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
296 /***********************************************************************
299 #ifndef HAVE_GETSERVBYPORT
300 struct servent *getservbyport(int port, const char *proto)
305 #endif /* !defined(HAVE_GETSERVBYPORT) */
307 /***********************************************************************
310 #ifndef HAVE_GETSOCKOPT
311 int getsockopt(int socket, int level, int option_name,
312 void *option_value, size_t *option_len)
317 #endif /* !defined(HAVE_GETSOCKOPT) */
319 /***********************************************************************
322 #ifndef HAVE_INET_NETWORK
323 unsigned long inet_network(const char *cp)
328 #endif /* defined(HAVE_INET_NETWORK) */
330 /***********************************************************************
333 #ifndef HAVE_SETTIMEOFDAY
334 int settimeofday(struct timeval *tp, void *reserved)
342 #endif /* HAVE_SETTIMEOFDAY */
344 /***********************************************************************
348 int statfs(const char *name, struct statfs *info)
359 if ((mydev = dev_for_path(name)) < 0) {
364 if (fs_stat_dev(mydev,&fsinfo) < 0) {
369 info->f_bsize = fsinfo.block_size;
370 info->f_blocks = fsinfo.total_blocks;
371 info->f_bfree = fsinfo.free_blocks;
373 #else /* defined(__BEOS__) */
376 #endif /* defined(__BEOS__) */
378 #endif /* !defined(HAVE_STATFS) */
381 /***********************************************************************
385 int lstat(const char *file_name, struct stat *buf)
387 return stat( file_name, buf );
389 #endif /* HAVE_LSTAT */
392 /***********************************************************************
395 #ifndef HAVE_GETRLIMIT
396 int getrlimit (int resource, struct rlimit *rlim)
398 return -1; /* FAIL */
400 #endif /* HAVE_GETRLIMIT */
402 /***********************************************************************
405 * Portable wrapper for anonymous mmaps
407 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
409 static int fdzero = -1;
416 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
418 perror( "/dev/zero: open" );
422 #endif /* MAP_ANON */
425 flags &= ~MAP_SHARED;
428 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
430 flags |= MAP_PRIVATE;
433 return mmap( start, size, prot, flags, fdzero, 0 );
438 * These functions provide wrappers around dlopen() and associated
439 * functions. They work around a bug in glibc 2.1.x where calling
440 * a dl*() function after a previous dl*() function has failed
441 * without a dlerror() call between the two will cause a crash.
442 * They all take a pointer to a buffer that
443 * will receive the error description (from dlerror()). This
444 * parameter may be NULL if the error description is not required.
447 /***********************************************************************
450 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
455 dlerror(); dlerror();
456 ret = dlopen( filename, flag );
460 strncpy( error, s ? s : "", errorsize );
461 error[errorsize - 1] = '\0';
468 strncpy( error, "dlopen interface not detected by configure", errorsize );
469 error[errorsize - 1] = '\0';
475 /***********************************************************************
478 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
483 dlerror(); dlerror();
484 ret = dlsym( handle, symbol );
488 strncpy( error, s ? s : "", errorsize );
489 error[errorsize - 1] = '\0';
496 strncpy( error, "dlopen interface not detected by configure", errorsize );
497 error[errorsize - 1] = '\0';
503 /***********************************************************************
506 int wine_dlclose( void *handle, char *error, int errorsize )
511 dlerror(); dlerror();
512 ret = dlclose( handle );
516 strncpy( error, s ? s : "", errorsize );
517 error[errorsize - 1] = '\0';
524 strncpy( error, "dlopen interface not detected by configure", errorsize );
525 error[errorsize - 1] = '\0';
531 /***********************************************************************
532 * wine_rewrite_s4tos2
534 * Convert 4 byte Unicode strings to 2 byte Unicode strings in-place.
535 * This is only practical if literal strings are writable.
537 unsigned short* wine_rewrite_s4tos2(const wchar_t* str4 )
539 unsigned short *str2,*s2;
544 if ((*str4 & 0xffff0000) != 0) {
545 /* This string has already been converted. Return it as is */
546 return (unsigned short*)str4;
549 /* Note that we can also end up here if the string has a single
550 * character. In such a case we will convert the string over and
551 * over again. But this is harmless.
553 str2=s2=(unsigned short*)str4;
555 *s2=(unsigned short)*str4;
557 } while (*str4++ != L'\0');
564 * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though.
565 * Fix/verify these implementations !
568 /***********************************************************************
571 char *ecvt (double number, int ndigits, int *decpt, int *sign)
573 static buf[40]; /* ought to be enough */
575 sprintf(buf, "%.*e", ndigits /* FIXME wrong */, number);
576 *sign = (number < 0);
577 dec = strchr(buf, '.');
578 *decpt = (dec) ? (int)dec - (int)buf : -1;
582 /***********************************************************************
585 char *fcvt (double number, int ndigits, int *decpt, int *sign)
587 static buf[40]; /* ought to be enough */
589 sprintf(buf, "%.*e", ndigits, number);
590 *sign = (number < 0);
591 dec = strchr(buf, '.');
592 *decpt = (dec) ? (int)dec - (int)buf : -1;
596 /***********************************************************************
599 * FIXME: uses both E and F.
601 char *gcvt (double number, size_t ndigit, char *buff)
603 sprintf(buff, "%.*E", ndigit, number);
606 #endif /* HAVE_ECVT */