2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
11 #include <be/kernel/fs_info.h>
12 #include <be/kernel/OS.h>
21 #include <sys/types.h>
24 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_MMAN_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 "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*/
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;
159 #endif /* __i386__ */
161 #endif /* !HAVE_CLONE && __linux__ */
163 /***********************************************************************
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)) {
176 return toupper(*ustr1) - toupper(*ustr2);
178 #endif /* HAVE_STRCASECMP */
180 /***********************************************************************
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;
191 while ((--n > 0) && *ustr1) {
192 if ((res = toupper(*ustr1) - toupper(*ustr2))) return res;
196 return toupper(*ustr1) - toupper(*ustr2);
198 #endif /* HAVE_STRNCASECMP */
200 /***********************************************************************
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.
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)
214 return openpty(master, slave, name, term, winsize);
216 const char *ptr1, *ptr2;
219 strcpy (pts_name, "/dev/ptyXY");
221 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
223 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
226 if ((*master = open(pts_name, O_RDWR)) < 0) {
233 if ((*slave = open(pts_name, O_RDWR)) < 0) {
240 tcsetattr(*slave, TCSANOW, term);
242 ioctl(*slave, TIOCSWINSZ, winsize);
244 strcpy(name, pts_name);
253 /***********************************************************************
256 #ifndef HAVE_GETNETBYADDR
257 struct netent *getnetbyaddr(unsigned long net, int type)
262 #endif /* defined(HAVE_GETNETBYNAME) */
264 /***********************************************************************
267 #ifndef HAVE_GETNETBYNAME
268 struct netent *getnetbyname(const char *name)
273 #endif /* defined(HAVE_GETNETBYNAME) */
275 /***********************************************************************
278 #ifndef HAVE_GETPROTOBYNAME
279 struct protoent *getprotobyname(const char *name)
284 #endif /* !defined(HAVE_GETPROTOBYNAME) */
286 /***********************************************************************
289 #ifndef HAVE_GETPROTOBYNUMBER
290 struct protoent *getprotobynumber(int proto)
295 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
297 /***********************************************************************
300 #ifndef HAVE_GETSERVBYPORT
301 struct servent *getservbyport(int port, const char *proto)
306 #endif /* !defined(HAVE_GETSERVBYPORT) */
308 /***********************************************************************
311 #ifndef HAVE_GETSOCKOPT
312 int getsockopt(int socket, int level, int option_name,
313 void *option_value, size_t *option_len)
318 #endif /* !defined(HAVE_GETSOCKOPT) */
320 /***********************************************************************
323 #ifndef HAVE_INET_NETWORK
324 unsigned long inet_network(const char *cp)
329 #endif /* defined(HAVE_INET_NETWORK) */
331 /***********************************************************************
334 #ifndef HAVE_SETTIMEOFDAY
335 int settimeofday(struct timeval *tp, void *reserved)
343 #endif /* HAVE_SETTIMEOFDAY */
345 /***********************************************************************
349 int statfs(const char *name, struct statfs *info)
360 if ((mydev = dev_for_path(name)) < 0) {
365 if (fs_stat_dev(mydev,&fsinfo) < 0) {
370 info->f_bsize = fsinfo.block_size;
371 info->f_blocks = fsinfo.total_blocks;
372 info->f_bfree = fsinfo.free_blocks;
374 #else /* defined(__BEOS__) */
377 #endif /* defined(__BEOS__) */
379 #endif /* !defined(HAVE_STATFS) */
382 /***********************************************************************
386 int lstat(const char *file_name, struct stat *buf)
388 return stat( file_name, buf );
390 #endif /* HAVE_LSTAT */
392 #if !defined(HAVE_STAT64) || !defined(HAVE_LSTAT64) || !defined(HAVE_FSTAT64)
393 static void _convert_stat_stat64(struct stat64 *stto,struct stat *stfrom)
395 stto->st_dev = stfrom->st_dev;
396 stto->st_ino = stfrom->st_ino;
397 stto->st_mode = stfrom->st_mode;
398 stto->st_nlink = stfrom->st_nlink;
399 stto->st_uid = stfrom->st_uid;
400 stto->st_gid = stfrom->st_gid;
401 stto->st_rdev = stfrom->st_rdev;
402 stto->st_blksize = stfrom->st_blksize;
403 stto->st_blocks = stfrom->st_blocks;
404 stto->st_atime = stfrom->st_atime;
405 stto->st_mtime = stfrom->st_mtime;
406 stto->st_ctime = stfrom->st_ctime;
407 stto->st_size = (off64_t)stfrom->st_size;
409 #endif /* HAVE_STAT64 || HAVE_LSTAT64 || HAVE_FSTAT64 */
411 /***********************************************************************
415 int stat64(const char *file_name, struct stat64 *buf)
418 int res = stat(file_name,&stbuf);
419 _convert_stat_stat64(buf,&stbuf);
422 #endif /* HAVE_STAT64 */
424 /***********************************************************************
428 int lstat64(const char *file_name, struct stat64 *buf)
431 int res = lstat(file_name,&stbuf);
432 _convert_stat_stat64(buf,&stbuf);
435 #endif /* HAVE_LSTAT64 */
437 /***********************************************************************
441 int fstat64(int fd, struct stat64 *buf)
444 int res = fstat(fd,&stbuf);
445 _convert_stat_stat64(buf,&stbuf);
448 #endif /* HAVE_FSTAT */
450 /***********************************************************************
454 off64_t lseek64(int fd, off64_t where, int whence)
457 if ((where >= 0x8000000LL) || ( where <= -0x7fffffffLL)) {
458 errno = EFBIG; /* FIXME: hack */
462 res = lseek(fd,(off_t)where,whence);
465 #endif /* HAVE_LSEEK64 */
467 /***********************************************************************
470 #ifndef HAVE_FTRUNCATE64
471 int ftruncate64(int fd, off64_t where)
473 if ((where >= 0x8000000LL) || ( where <= -0x7fffffffLL)) {
474 errno = EFBIG; /* FIXME: hack */
477 return ftruncate(fd,(off_t)where);
479 #endif /* HAVE_LSEEK64 */
480 /***********************************************************************
483 #ifndef HAVE_GETRLIMIT
484 int getrlimit (int resource, struct rlimit *rlim)
486 return -1; /* FAIL */
488 #endif /* HAVE_GETRLIMIT */
490 /***********************************************************************
493 * Portable wrapper for anonymous mmaps
495 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
497 static int fdzero = -1;
504 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
506 perror( "/dev/zero: open" );
510 #endif /* MAP_ANON */
513 flags &= ~MAP_SHARED;
516 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
518 flags |= MAP_PRIVATE;
521 return mmap( start, size, prot, flags, fdzero, 0 );
526 * These functions provide wrappers around dlopen() and associated
527 * functions. They work around a bug in glibc 2.1.x where calling
528 * a dl*() function after a previous dl*() function has failed
529 * without a dlerror() call between the two will cause a crash.
530 * They all take a pointer to a buffer that
531 * will receive the error description (from dlerror()). This
532 * parameter may be NULL if the error description is not required.
535 /***********************************************************************
538 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
543 dlerror(); dlerror();
544 ret = dlopen( filename, flag );
548 strncpy( error, s ? s : "", errorsize );
549 error[errorsize - 1] = '\0';
556 strncpy( error, "dlopen interface not detected by configure", errorsize );
557 error[errorsize - 1] = '\0';
563 /***********************************************************************
566 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
571 dlerror(); dlerror();
572 ret = dlsym( handle, symbol );
576 strncpy( error, s ? s : "", errorsize );
577 error[errorsize - 1] = '\0';
584 strncpy( error, "dlopen interface not detected by configure", errorsize );
585 error[errorsize - 1] = '\0';
591 /***********************************************************************
594 int wine_dlclose( void *handle, char *error, int errorsize )
599 dlerror(); dlerror();
600 ret = dlclose( handle );
604 strncpy( error, s ? s : "", errorsize );
605 error[errorsize - 1] = '\0';
612 strncpy( error, "dlopen interface not detected by configure", errorsize );
613 error[errorsize - 1] = '\0';
619 /***********************************************************************
620 * wine_rewrite_s4tos2
622 * Convert 4 byte Unicode strings to 2 byte Unicode strings in-place.
623 * This is only practical if literal strings are writable.
625 unsigned short* wine_rewrite_s4tos2(const wchar_t* str4 )
627 unsigned short *str2,*s2;
632 if ((*str4 & 0xffff0000) != 0) {
633 /* This string has already been converted. Return it as is */
634 return (unsigned short*)str4;
637 /* Note that we can also end up here if the string has a single
638 * character. In such a case we will convert the string over and
639 * over again. But this is harmless.
641 str2=s2=(unsigned short*)str4;
643 *s2=(unsigned short)*str4;
645 } while (*str4++ != L'\0');
652 * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though.
653 * Fix/verify these implementations !
656 /***********************************************************************
659 char *ecvt (double number, int ndigits, int *decpt, int *sign)
661 static buf[40]; /* ought to be enough */
663 sprintf(buf, "%.*e", ndigits /* FIXME wrong */, number);
664 *sign = (number < 0);
665 dec = strchr(buf, '.');
666 *decpt = (dec) ? (int)dec - (int)buf : -1;
670 /***********************************************************************
673 char *fcvt (double number, int ndigits, int *decpt, int *sign)
675 static buf[40]; /* ought to be enough */
677 sprintf(buf, "%.*e", ndigits, number);
678 *sign = (number < 0);
679 dec = strchr(buf, '.');
680 *decpt = (dec) ? (int)dec - (int)buf : -1;
684 /***********************************************************************
687 * FIXME: uses both E and F.
689 char *gcvt (double number, size_t ndigit, char *buff)
691 sprintf(buff, "%.*E", ndigit, number);
694 #endif /* HAVE_ECVT */