Fix remaining signed/unsigned mismatches.
[wine.git] / library / port.c
blob810b6823a562a7bf27e39e81028b9618f78d4c04
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 "call *%%ebx\n\t" /* Should never return */
147 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
148 "0:"
149 : "=a" (ret)
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;
153 errno = -ret;
154 return -1;
155 #else
156 errno = EINVAL;
157 return -1;
158 #endif /* __i386__ */
160 #endif /* !HAVE_CLONE && __linux__ */
162 /***********************************************************************
163 * strcasecmp
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)) {
172 ustr1++;
173 ustr2++;
175 return toupper(*ustr1) - toupper(*ustr2);
177 #endif /* HAVE_STRCASECMP */
179 /***********************************************************************
180 * strncasecmp
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;
187 int res;
189 if (!n) return 0;
190 while ((--n > 0) && *ustr1) {
191 if ((res = toupper(*ustr1) - toupper(*ustr2))) return res;
192 ustr1++;
193 ustr2++;
195 return toupper(*ustr1) - toupper(*ustr2);
197 #endif /* HAVE_STRNCASECMP */
199 /***********************************************************************
200 * wine_openpty
201 * NOTE
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.
206 * FIXME
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)
212 #ifdef HAVE_OPENPTY
213 return openpty(master, slave, name, term, winsize);
214 #else
215 const char *ptr1, *ptr2;
216 char pts_name[512];
218 strcpy (pts_name, "/dev/ptyXY");
220 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
221 pts_name[8] = *ptr1;
222 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
223 pts_name[9] = *ptr2;
225 if ((*master = open(pts_name, O_RDWR)) < 0) {
226 if (errno == ENOENT)
227 return -1;
228 else
229 continue;
231 pts_name[5] = 't';
232 if ((*slave = open(pts_name, O_RDWR)) < 0) {
233 pts_name[5] = 'p';
234 close (*master);
235 continue;
238 if (term != NULL)
239 tcsetattr(*slave, TCSANOW, term);
240 if (winsize != NULL)
241 ioctl(*slave, TIOCSWINSZ, winsize);
242 if (name != NULL)
243 strcpy(name, pts_name);
244 return *slave;
247 errno = EMFILE;
248 return -1;
249 #endif
252 /***********************************************************************
253 * getnetbyaddr
255 #ifndef HAVE_GETNETBYADDR
256 struct netent *getnetbyaddr(unsigned long net, int type)
258 errno = ENOSYS;
259 return NULL;
261 #endif /* defined(HAVE_GETNETBYNAME) */
263 /***********************************************************************
264 * getnetbyname
266 #ifndef HAVE_GETNETBYNAME
267 struct netent *getnetbyname(const char *name)
269 errno = ENOSYS;
270 return NULL;
272 #endif /* defined(HAVE_GETNETBYNAME) */
274 /***********************************************************************
275 * getprotobyname
277 #ifndef HAVE_GETPROTOBYNAME
278 struct protoent *getprotobyname(const char *name)
280 errno = ENOSYS;
281 return NULL;
283 #endif /* !defined(HAVE_GETPROTOBYNAME) */
285 /***********************************************************************
286 * getprotobynumber
288 #ifndef HAVE_GETPROTOBYNUMBER
289 struct protoent *getprotobynumber(int proto)
291 errno = ENOSYS;
292 return NULL;
294 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
296 /***********************************************************************
297 * getservbyport
299 #ifndef HAVE_GETSERVBYPORT
300 struct servent *getservbyport(int port, const char *proto)
302 errno = ENOSYS;
303 return NULL;
305 #endif /* !defined(HAVE_GETSERVBYPORT) */
307 /***********************************************************************
308 * getsockopt
310 #ifndef HAVE_GETSOCKOPT
311 int getsockopt(int socket, int level, int option_name,
312 void *option_value, size_t *option_len)
314 errno = ENOSYS;
315 return -1;
317 #endif /* !defined(HAVE_GETSOCKOPT) */
319 /***********************************************************************
320 * inet_network
322 #ifndef HAVE_INET_NETWORK
323 unsigned long inet_network(const char *cp)
325 errno = ENOSYS;
326 return 0;
328 #endif /* defined(HAVE_INET_NETWORK) */
330 /***********************************************************************
331 * settimeofday
333 #ifndef HAVE_SETTIMEOFDAY
334 int settimeofday(struct timeval *tp, void *reserved)
336 tp->tv_sec = 0;
337 tp->tv_usec = 0;
339 errno = ENOSYS;
340 return -1;
342 #endif /* HAVE_SETTIMEOFDAY */
344 /***********************************************************************
345 * statfs
347 #ifndef HAVE_STATFS
348 int statfs(const char *name, struct statfs *info)
350 #ifdef __BEOS__
351 dev_t mydev;
352 fs_info fsinfo;
354 if(!info) {
355 errno = ENOSYS;
356 return -1;
359 if ((mydev = dev_for_path(name)) < 0) {
360 errno = ENOSYS;
361 return -1;
364 if (fs_stat_dev(mydev,&fsinfo) < 0) {
365 errno = ENOSYS;
366 return -1;
369 info->f_bsize = fsinfo.block_size;
370 info->f_blocks = fsinfo.total_blocks;
371 info->f_bfree = fsinfo.free_blocks;
372 return 0;
373 #else /* defined(__BEOS__) */
374 errno = ENOSYS;
375 return -1;
376 #endif /* defined(__BEOS__) */
378 #endif /* !defined(HAVE_STATFS) */
381 /***********************************************************************
382 * lstat
384 #ifndef HAVE_LSTAT
385 int lstat(const char *file_name, struct stat *buf)
387 return stat( file_name, buf );
389 #endif /* HAVE_LSTAT */
392 /***********************************************************************
393 * getrlimit
395 #ifndef HAVE_GETRLIMIT
396 int getrlimit (int resource, struct rlimit *rlim)
398 return -1; /* FAIL */
400 #endif /* HAVE_GETRLIMIT */
402 /***********************************************************************
403 * wine_anon_mmap
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;
411 #ifdef MAP_ANON
412 flags |= MAP_ANON;
413 #else
414 if (fdzero == -1)
416 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
418 perror( "/dev/zero: open" );
419 exit(1);
422 #endif /* MAP_ANON */
424 #ifdef MAP_SHARED
425 flags &= ~MAP_SHARED;
426 #endif
428 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
429 #ifdef MAP_PRIVATE
430 flags |= MAP_PRIVATE;
431 #endif
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 /***********************************************************************
448 * wine_dlopen
450 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
452 #ifdef HAVE_DL_API
453 void *ret;
454 char *s;
455 dlerror(); dlerror();
456 ret = dlopen( filename, flag );
457 s = dlerror();
458 if (error)
460 strncpy( error, s ? s : "", errorsize );
461 error[errorsize - 1] = '\0';
463 dlerror();
464 return ret;
465 #else
466 if (error)
468 strncpy( error, "dlopen interface not detected by configure", errorsize );
469 error[errorsize - 1] = '\0';
471 return NULL;
472 #endif
475 /***********************************************************************
476 * wine_dlsym
478 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
480 #ifdef HAVE_DL_API
481 void *ret;
482 char *s;
483 dlerror(); dlerror();
484 ret = dlsym( handle, symbol );
485 s = dlerror();
486 if (error)
488 strncpy( error, s ? s : "", errorsize );
489 error[errorsize - 1] = '\0';
491 dlerror();
492 return ret;
493 #else
494 if (error)
496 strncpy( error, "dlopen interface not detected by configure", errorsize );
497 error[errorsize - 1] = '\0';
499 return NULL;
500 #endif
503 /***********************************************************************
504 * wine_dlclose
506 int wine_dlclose( void *handle, char *error, int errorsize )
508 #ifdef HAVE_DL_API
509 int ret;
510 char *s;
511 dlerror(); dlerror();
512 ret = dlclose( handle );
513 s = dlerror();
514 if (error)
516 strncpy( error, s ? s : "", errorsize );
517 error[errorsize - 1] = '\0';
519 dlerror();
520 return ret;
521 #else
522 if (error)
524 strncpy( error, "dlopen interface not detected by configure", errorsize );
525 error[errorsize - 1] = '\0';
527 return 1;
528 #endif
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;
541 if (str4==NULL)
542 return NULL;
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;
554 do {
555 *s2=(unsigned short)*str4;
556 s2++;
557 } while (*str4++ != L'\0');
559 return str2;