Release 20010305.
[wine] / library / port.c
1 /*
2  * Misc. functions for systems that don't have them
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #ifdef __BEOS__
10 #include <be/kernel/fs_info.h>
11 #include <be/kernel/OS.h>
12 #endif
13
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
45
46 #include "wine/port.h"
47
48 /***********************************************************************
49  *              usleep
50  */
51 #ifndef HAVE_USLEEP
52 unsigned int usleep (unsigned int useconds)
53 {
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;
61
62     delay.tv_sec = useconds / 1000000;
63     delay.tv_usec = useconds % 1000000;
64
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) */
71 }
72 #endif /* HAVE_USLEEP */
73
74 /***********************************************************************
75  *              memmove
76  */
77 #ifndef HAVE_MEMMOVE
78 void *memmove( void *dest, const void *src, unsigned int len )
79 {
80     register char *dst = dest;
81
82     /* Use memcpy if not overlapping */
83     if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
84     {
85         memcpy( dst, src, len );
86     }
87     /* Otherwise do it the hard way (FIXME: could do better than this) */
88     else if (dst < src)
89     {
90         while (len--) *dst++ = *((char *)src)++;
91     }
92     else
93     {
94         dst += len - 1;
95         src = (char *)src + len - 1;
96         while (len--) *dst-- = *((char *)src)--;
97     }
98     return dest;
99 }
100 #endif  /* HAVE_MEMMOVE */
101
102 /***********************************************************************
103  *              strerror
104  */
105 #ifndef HAVE_STRERROR
106 const char *strerror( int err )
107 {
108     /* Let's hope we have sys_errlist then */
109     return sys_errlist[err];
110 }
111 #endif  /* HAVE_STRERROR */
112
113
114 /***********************************************************************
115  *              getpagesize
116  */
117 #ifndef HAVE_GETPAGESIZE
118 size_t getpagesize(void)
119 {
120 # ifdef __svr4__
121     return sysconf(_SC_PAGESIZE);
122 # else
123 #  error Cannot get the page size on this platform
124 # endif
125 }
126 #endif  /* HAVE_GETPAGESIZE */
127
128
129 /***********************************************************************
130  *              clone
131  */
132 #if !defined(HAVE_CLONE) && defined(__linux__)
133 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
134 {
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__ */
160 }
161 #endif  /* !HAVE_CLONE && __linux__ */
162
163 /***********************************************************************
164  *              strcasecmp
165  */
166 #ifndef HAVE_STRCASECMP
167 int strcasecmp( const char *str1, const char *str2 )
168 {
169     const unsigned char *ustr1 = (const unsigned char *)str1;
170     const unsigned char *ustr2 = (const unsigned char *)str2;
171
172     while (*ustr1 && toupper(*ustr1) == toupper(*ustr2)) {
173         ustr1++;
174         ustr2++;
175     }
176     return toupper(*ustr1) - toupper(*ustr2);
177 }
178 #endif /* HAVE_STRCASECMP */
179
180 /***********************************************************************
181  *              strncasecmp
182  */
183 #ifndef HAVE_STRNCASECMP
184 int strncasecmp( const char *str1, const char *str2, size_t n )
185 {
186     const unsigned char *ustr1 = (const unsigned char *)str1;
187     const unsigned char *ustr2 = (const unsigned char *)str2;
188     int res;
189
190     if (!n) return 0;
191     while ((--n > 0) && *ustr1) {
192         if ((res = toupper(*ustr1) - toupper(*ustr2))) return res;
193         ustr1++;
194         ustr2++;
195     }
196     return toupper(*ustr1) - toupper(*ustr2);
197 }
198 #endif /* HAVE_STRNCASECMP */
199
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.
206  *
207  * FIXME
208  *   We should have a autoconf check for this.
209  */
210 int wine_openpty(int *master, int *slave, char *name,
211                  struct termios *term, struct winsize *winsize)
212 {
213 #ifdef HAVE_OPENPTY
214     return openpty(master, slave, name, term, winsize);
215 #else
216     const char *ptr1, *ptr2;
217     char pts_name[512];
218
219     strcpy (pts_name, "/dev/ptyXY");
220
221     for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
222         pts_name[8] = *ptr1;
223         for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
224             pts_name[9] = *ptr2;
225
226             if ((*master = open(pts_name, O_RDWR)) < 0) {
227                 if (errno == ENOENT)
228                     return -1;
229                 else
230                     continue;
231             }
232             pts_name[5] = 't';
233             if ((*slave = open(pts_name, O_RDWR)) < 0) {
234                 pts_name[5] = 'p';
235                 close (*master);
236                 continue;
237             }
238
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;
246         }
247     }
248     errno = EMFILE;
249     return -1;
250 #endif
251 }
252
253 /***********************************************************************
254  *              getnetbyaddr
255  */
256 #ifndef HAVE_GETNETBYADDR
257 struct netent *getnetbyaddr(unsigned long net, int type)
258 {
259     errno = ENOSYS;
260     return NULL;
261 }
262 #endif /* defined(HAVE_GETNETBYNAME) */
263
264 /***********************************************************************
265  *              getnetbyname
266  */
267 #ifndef HAVE_GETNETBYNAME
268 struct netent *getnetbyname(const char *name)
269 {
270     errno = ENOSYS;
271     return NULL;
272 }
273 #endif /* defined(HAVE_GETNETBYNAME) */
274
275 /***********************************************************************
276  *              getprotobyname
277  */
278 #ifndef HAVE_GETPROTOBYNAME
279 struct protoent *getprotobyname(const char *name)
280 {
281     errno = ENOSYS;
282     return NULL;
283 }
284 #endif /* !defined(HAVE_GETPROTOBYNAME) */
285
286 /***********************************************************************
287  *              getprotobynumber
288  */
289 #ifndef HAVE_GETPROTOBYNUMBER
290 struct protoent *getprotobynumber(int proto)
291 {
292     errno = ENOSYS;
293     return NULL;
294 }
295 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
296
297 /***********************************************************************
298  *              getservbyport
299  */
300 #ifndef HAVE_GETSERVBYPORT
301 struct servent *getservbyport(int port, const char *proto)
302 {
303     errno = ENOSYS;
304     return NULL;
305 }
306 #endif /* !defined(HAVE_GETSERVBYPORT) */
307
308 /***********************************************************************
309  *              getsockopt
310  */
311 #ifndef HAVE_GETSOCKOPT
312 int getsockopt(int socket, int level, int option_name,
313                void *option_value, size_t *option_len)
314 {
315     errno = ENOSYS;
316     return -1;
317 }
318 #endif /* !defined(HAVE_GETSOCKOPT) */
319
320 /***********************************************************************
321  *              inet_network
322  */
323 #ifndef HAVE_INET_NETWORK
324 unsigned long inet_network(const char *cp)
325 {
326     errno = ENOSYS;
327     return 0;
328 }
329 #endif /* defined(HAVE_INET_NETWORK) */
330
331 /***********************************************************************
332  *              settimeofday
333  */
334 #ifndef HAVE_SETTIMEOFDAY
335 int settimeofday(struct timeval *tp, void *reserved)
336 {
337     tp->tv_sec = 0;
338     tp->tv_usec = 0;
339
340     errno = ENOSYS;
341     return -1;
342 }
343 #endif /* HAVE_SETTIMEOFDAY */
344
345 /***********************************************************************
346  *              statfs
347  */
348 #ifndef HAVE_STATFS
349 int statfs(const char *name, struct statfs *info)
350 {
351 #ifdef __BEOS__
352     dev_t mydev;
353     fs_info fsinfo;
354
355     if(!info) {
356         errno = ENOSYS;
357         return -1;
358     }
359
360     if ((mydev = dev_for_path(name)) < 0) {
361         errno = ENOSYS;
362         return -1;
363     }
364
365     if (fs_stat_dev(mydev,&fsinfo) < 0) {
366         errno = ENOSYS;
367         return -1;
368     }
369
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__) */
378 }
379 #endif /* !defined(HAVE_STATFS) */
380
381
382 /***********************************************************************
383  *              lstat
384  */
385 #ifndef HAVE_LSTAT
386 int lstat(const char *file_name, struct stat *buf)
387 {
388     return stat( file_name, buf );
389 }
390 #endif /* HAVE_LSTAT */
391
392
393 /***********************************************************************
394  *              getrlimit
395  */
396 #ifndef HAVE_GETRLIMIT
397 int getrlimit (int resource, struct rlimit *rlim)
398 {
399     return -1; /* FAIL */
400 }
401 #endif /* HAVE_GETRLIMIT */
402
403 /***********************************************************************
404  *              wine_anon_mmap
405  *
406  * Portable wrapper for anonymous mmaps
407  */
408 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
409 {
410     static int fdzero = -1;
411
412 #ifdef MAP_ANON
413     flags |= MAP_ANON;
414 #else
415     if (fdzero == -1)
416     {
417         if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
418         {
419             perror( "/dev/zero: open" );
420             exit(1);
421         }
422     }
423 #endif  /* MAP_ANON */
424
425 #ifdef MAP_SHARED
426     flags &= ~MAP_SHARED;
427 #endif
428
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
433
434     return mmap( start, size, prot, flags, fdzero, 0 );
435 }
436
437
438 /*
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.
446  */
447
448 /***********************************************************************
449  *              wine_dlopen
450  */
451 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
452 {
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)
460     {
461         strncpy( error, s ? s : "", errorsize );
462         error[errorsize - 1] = '\0';
463     }
464     dlerror();
465     return ret;
466 #else
467     if (error)
468     {
469         strncpy( error, "dlopen interface not detected by configure", errorsize );
470         error[errorsize - 1] = '\0';
471     }
472     return NULL;
473 #endif
474 }
475
476 /***********************************************************************
477  *              wine_dlsym
478  */
479 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
480 {
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)
488     {
489         strncpy( error, s ? s : "", errorsize );
490         error[errorsize - 1] = '\0';
491     }
492     dlerror();
493     return ret;
494 #else
495     if (error)
496     {
497         strncpy( error, "dlopen interface not detected by configure", errorsize );
498         error[errorsize - 1] = '\0';
499     }
500     return NULL;
501 #endif
502 }
503
504 /***********************************************************************
505  *              wine_dlclose
506  */
507 int wine_dlclose( void *handle, char *error, int errorsize )
508 {
509 #ifdef HAVE_DL_API
510     int ret;
511     char *s;
512     dlerror(); dlerror();
513     ret = dlclose( handle );
514     s = dlerror();
515     if (error)
516     {
517         strncpy( error, s ? s : "", errorsize );
518         error[errorsize - 1] = '\0';
519     }
520     dlerror();
521     return ret;
522 #else
523     if (error)
524     {
525         strncpy( error, "dlopen interface not detected by configure", errorsize );
526         error[errorsize - 1] = '\0';
527     }
528     return 1;
529 #endif
530 }
531
532 /***********************************************************************
533  *              wine_rewrite_s4tos2
534  *
535  * Convert 4 byte Unicode strings to 2 byte Unicode strings in-place.
536  * This is only practical if literal strings are writable.
537  */
538 unsigned short* wine_rewrite_s4tos2(const wchar_t* str4 )
539 {
540     unsigned short *str2,*s2;
541
542     if (str4==NULL)
543       return NULL;
544
545     if ((*str4 & 0xffff0000) != 0) {
546         /* This string has already been converted. Return it as is */
547         return (unsigned short*)str4;
548     }
549
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.
553      */
554     str2=s2=(unsigned short*)str4;
555     do {
556         *s2=(unsigned short)*str4;
557         s2++;
558     } while (*str4++ != L'\0');
559
560     return str2;
561 }
562
563 #ifndef HAVE_ECVT
564 /*
565  * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though.
566  * Fix/verify these implementations !
567  */
568
569 /***********************************************************************
570  *              ecvt
571  */
572 char *ecvt (double number, int  ndigits,  int  *decpt,  int *sign)
573 {
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;
581 }
582
583 /***********************************************************************
584  *              fcvt
585  */
586 char *fcvt (double number, int  ndigits,  int  *decpt,  int *sign)
587 {
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;
595 }
596
597 /***********************************************************************
598  *              gcvt
599  *
600  * FIXME: uses both E and F.
601  */
602 char *gcvt (double number, size_t  ndigit,  char *buff)
603 {
604     sprintf(buff, "%.*E", ndigit, number);
605     return buff;
606 }
607 #endif /* HAVE_ECVT */