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 "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 */
393 /***********************************************************************
396 #ifndef HAVE_GETRLIMIT
397 int getrlimit (int resource
, struct rlimit
*rlim
)
399 return -1; /* FAIL */
401 #endif /* HAVE_GETRLIMIT */
403 /***********************************************************************
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;
417 if ((fdzero
= open( "/dev/zero", O_RDONLY
)) == -1)
419 perror( "/dev/zero: open" );
423 #endif /* MAP_ANON */
426 flags
&= ~MAP_SHARED
;
429 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
431 flags
|= MAP_PRIVATE
;
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 /***********************************************************************
451 void *wine_dlopen( const char *filename
, int flag
, char *error
, int errorsize
)
456 dlerror(); dlerror();
457 ret
= dlopen( filename
, flag
);
461 strncpy( error
, s
? s
: "", errorsize
);
462 error
[errorsize
- 1] = '\0';
469 strncpy( error
, "dlopen interface not detected by configure", errorsize
);
470 error
[errorsize
- 1] = '\0';
476 /***********************************************************************
479 void *wine_dlsym( void *handle
, const char *symbol
, char *error
, int errorsize
)
484 dlerror(); dlerror();
485 ret
= dlsym( handle
, symbol
);
489 strncpy( error
, s
? s
: "", errorsize
);
490 error
[errorsize
- 1] = '\0';
497 strncpy( error
, "dlopen interface not detected by configure", errorsize
);
498 error
[errorsize
- 1] = '\0';
504 /***********************************************************************
507 int wine_dlclose( void *handle
, char *error
, int errorsize
)
512 dlerror(); dlerror();
513 ret
= dlclose( handle
);
517 strncpy( error
, s
? s
: "", errorsize
);
518 error
[errorsize
- 1] = '\0';
525 strncpy( error
, "dlopen interface not detected by configure", errorsize
);
526 error
[errorsize
- 1] = '\0';
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
;
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
;
556 *s2
=(unsigned short)*str4
;
558 } while (*str4
++ != L
'\0');
565 * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though.
566 * Fix/verify these implementations !
569 /***********************************************************************
572 char *ecvt (double number
, int ndigits
, int *decpt
, int *sign
)
574 static buf
[40]; /* ought to be enough */
576 sprintf(buf
, "%.*e", ndigits
/* FIXME wrong */, number
);
577 *sign
= (number
< 0);
578 dec
= strchr(buf
, '.');
579 *decpt
= (dec
) ? (int)dec
- (int)buf
: -1;
583 /***********************************************************************
586 char *fcvt (double number
, int ndigits
, int *decpt
, int *sign
)
588 static buf
[40]; /* ought to be enough */
590 sprintf(buf
, "%.*e", ndigits
, number
);
591 *sign
= (number
< 0);
592 dec
= strchr(buf
, '.');
593 *decpt
= (dec
) ? (int)dec
- (int)buf
: -1;
597 /***********************************************************************
600 * FIXME: uses both E and F.
602 char *gcvt (double number
, size_t ndigit
, char *buff
)
604 sprintf(buff
, "%.*E", ndigit
, number
);
607 #endif /* HAVE_ECVT */