Added an unknown VxD error code.
[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 #include "wine/port.h"
9
10 #ifdef __BEOS__
11 #include <be/kernel/fs_info.h>
12 #include <be/kernel/OS.h>
13 #endif
14
15 #include <assert.h>
16 #include <ctype.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <termios.h>
28 #ifdef HAVE_SYS_MMAN_H
29 #include <sys/mman.h>
30 #endif
31 #ifdef HAVE_LIBIO_H
32 # include <libio.h>
33 #endif
34 #ifdef HAVE_SYSCALL_H
35 # include <syscall.h>
36 #endif
37 #ifdef HAVE_PTY_H
38 # include <pty.h>
39 #endif
40 #ifdef HAVE_LIBUTIL_H
41 # include <libutil.h>
42 #endif
43 #ifdef HAVE_DL_API
44 # include <dlfcn.h>
45 #endif
46
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 #if !defined(HAVE_STAT64) || !defined(HAVE_LSTAT64) || !defined(HAVE_FSTAT64)
393 static void _convert_stat_stat64(struct stat64 *stto,struct stat *stfrom)
394 {
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;
408 }
409 #endif  /* HAVE_STAT64 || HAVE_LSTAT64 || HAVE_FSTAT64 */
410
411 /***********************************************************************
412  *              stat64
413  */
414 #ifndef HAVE_STAT64
415 int stat64(const char *file_name, struct stat64 *buf)
416 {
417     struct stat stbuf;
418     int res = stat(file_name,&stbuf);
419     _convert_stat_stat64(buf,&stbuf);
420     return res;
421 }
422 #endif /* HAVE_STAT64 */
423
424 /***********************************************************************
425  *              lstat64
426  */
427 #ifndef HAVE_LSTAT64
428 int lstat64(const char *file_name, struct stat64 *buf)
429 {
430     struct stat stbuf;
431     int res = lstat(file_name,&stbuf);
432     _convert_stat_stat64(buf,&stbuf);
433     return res;
434 }
435 #endif /* HAVE_LSTAT64 */
436
437 /***********************************************************************
438  *              fstat64
439  */
440 #ifndef HAVE_FSTAT64
441 int fstat64(int fd, struct stat64 *buf)
442 {
443     struct stat stbuf;
444     int res = fstat(fd,&stbuf);
445     _convert_stat_stat64(buf,&stbuf);
446     return res;
447 }
448 #endif /* HAVE_FSTAT */
449
450 /***********************************************************************
451  *              lseek64
452  */
453 #ifndef HAVE_LSEEK64
454 off64_t lseek64(int fd, off64_t where, int whence)
455 {
456     off_t res;
457     if ((where >= 0x8000000LL)  || ( where <= -0x7fffffffLL)) {
458         errno = EFBIG; /* FIXME: hack */
459         return -1;
460     }
461
462     res = lseek(fd,(off_t)where,whence);
463     return (off64_t)res;
464 }
465 #endif /* HAVE_LSEEK64 */
466
467 /***********************************************************************
468  *              ftruncate64
469  */
470 #ifndef HAVE_FTRUNCATE64
471 int ftruncate64(int fd, off64_t where)
472 {
473     if ((where >= 0x8000000LL)  || ( where <= -0x7fffffffLL)) {
474         errno = EFBIG; /* FIXME: hack */
475         return -1;
476     }
477     return ftruncate(fd,(off_t)where);
478 }
479 #endif /* HAVE_LSEEK64 */
480 /***********************************************************************
481  *              getrlimit
482  */
483 #ifndef HAVE_GETRLIMIT
484 int getrlimit (int resource, struct rlimit *rlim)
485 {
486     return -1; /* FAIL */
487 }
488 #endif /* HAVE_GETRLIMIT */
489
490 /***********************************************************************
491  *              wine_anon_mmap
492  *
493  * Portable wrapper for anonymous mmaps
494  */
495 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
496 {
497     static int fdzero = -1;
498
499 #ifdef MAP_ANON
500     flags |= MAP_ANON;
501 #else
502     if (fdzero == -1)
503     {
504         if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
505         {
506             perror( "/dev/zero: open" );
507             exit(1);
508         }
509     }
510 #endif  /* MAP_ANON */
511
512 #ifdef MAP_SHARED
513     flags &= ~MAP_SHARED;
514 #endif
515
516     /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
517 #ifdef MAP_PRIVATE
518     flags |= MAP_PRIVATE;
519 #endif
520
521     return mmap( start, size, prot, flags, fdzero, 0 );
522 }
523
524
525 /*
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.
533  */
534
535 /***********************************************************************
536  *              wine_dlopen
537  */
538 void *wine_dlopen( const char *filename, int flag, char *error, int errorsize )
539 {
540 #ifdef HAVE_DL_API
541     void *ret;
542     char *s;
543     dlerror(); dlerror();
544     ret = dlopen( filename, flag );
545     s = dlerror();
546     if (error)
547     {
548         strncpy( error, s ? s : "", errorsize );
549         error[errorsize - 1] = '\0';
550     }
551     dlerror();
552     return ret;
553 #else
554     if (error)
555     {
556         strncpy( error, "dlopen interface not detected by configure", errorsize );
557         error[errorsize - 1] = '\0';
558     }
559     return NULL;
560 #endif
561 }
562
563 /***********************************************************************
564  *              wine_dlsym
565  */
566 void *wine_dlsym( void *handle, const char *symbol, char *error, int errorsize )
567 {
568 #ifdef HAVE_DL_API
569     void *ret;
570     char *s;
571     dlerror(); dlerror();
572     ret = dlsym( handle, symbol );
573     s = dlerror();
574     if (error)
575     {
576         strncpy( error, s ? s : "", errorsize );
577         error[errorsize - 1] = '\0';
578     }
579     dlerror();
580     return ret;
581 #else
582     if (error)
583     {
584         strncpy( error, "dlopen interface not detected by configure", errorsize );
585         error[errorsize - 1] = '\0';
586     }
587     return NULL;
588 #endif
589 }
590
591 /***********************************************************************
592  *              wine_dlclose
593  */
594 int wine_dlclose( void *handle, char *error, int errorsize )
595 {
596 #ifdef HAVE_DL_API
597     int ret;
598     char *s;
599     dlerror(); dlerror();
600     ret = dlclose( handle );
601     s = dlerror();
602     if (error)
603     {
604         strncpy( error, s ? s : "", errorsize );
605         error[errorsize - 1] = '\0';
606     }
607     dlerror();
608     return ret;
609 #else
610     if (error)
611     {
612         strncpy( error, "dlopen interface not detected by configure", errorsize );
613         error[errorsize - 1] = '\0';
614     }
615     return 1;
616 #endif
617 }
618
619 /***********************************************************************
620  *              wine_rewrite_s4tos2
621  *
622  * Convert 4 byte Unicode strings to 2 byte Unicode strings in-place.
623  * This is only practical if literal strings are writable.
624  */
625 unsigned short* wine_rewrite_s4tos2(const wchar_t* str4 )
626 {
627     unsigned short *str2,*s2;
628
629     if (str4==NULL)
630       return NULL;
631
632     if ((*str4 & 0xffff0000) != 0) {
633         /* This string has already been converted. Return it as is */
634         return (unsigned short*)str4;
635     }
636
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.
640      */
641     str2=s2=(unsigned short*)str4;
642     do {
643         *s2=(unsigned short)*str4;
644         s2++;
645     } while (*str4++ != L'\0');
646
647     return str2;
648 }
649
650 #ifndef HAVE_ECVT
651 /*
652  * NetBSD 1.5 doesn't have ecvt, fcvt, gcvt. We just check for ecvt, though.
653  * Fix/verify these implementations !
654  */
655
656 /***********************************************************************
657  *              ecvt
658  */
659 char *ecvt (double number, int  ndigits,  int  *decpt,  int *sign)
660 {
661     static buf[40]; /* ought to be enough */
662     char *dec;
663     sprintf(buf, "%.*e", ndigits /* FIXME wrong */, number); 
664     *sign = (number < 0);
665     dec = strchr(buf, '.');
666     *decpt = (dec) ? (int)dec - (int)buf : -1;
667     return buf;
668 }
669
670 /***********************************************************************
671  *              fcvt
672  */
673 char *fcvt (double number, int  ndigits,  int  *decpt,  int *sign)
674 {
675     static buf[40]; /* ought to be enough */
676     char *dec;
677     sprintf(buf, "%.*e", ndigits, number);
678     *sign = (number < 0);
679     dec = strchr(buf, '.');
680     *decpt = (dec) ? (int)dec - (int)buf : -1;
681     return buf;
682 }
683
684 /***********************************************************************
685  *              gcvt
686  *
687  * FIXME: uses both E and F.
688  */
689 char *gcvt (double number, size_t  ndigit,  char *buff)
690 {
691     sprintf(buff, "%.*E", ndigit, number);
692     return buff;
693 }
694 #endif /* HAVE_ECVT */