CoGetClassObject should complain about not being able to do
[wine.git] / misc / port.c
blob934d0f82daba9417e329deb53ff7049c994948a7
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/stat.h>
16 #include <sys/ioctl.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <termios.h>
20 #ifdef HAVE_LIBIO_H
21 # include <libio.h>
22 #endif
24 #ifndef HAVE_USLEEP
25 #ifdef __EMX__
26 unsigned int usleep (unsigned int useconds) { DosSleep(useconds); }
27 #else
28 unsigned int usleep (unsigned int useconds)
30 struct timeval delay;
32 delay.tv_sec = 0;
33 delay.tv_usec = useconds;
35 select( 0, 0, 0, 0, &delay );
36 return 0;
38 #endif
39 #endif /* HAVE_USLEEP */
41 #ifndef HAVE_MEMMOVE
42 void *memmove( void *dest, const void *src, unsigned int len )
44 register char *dst = dest;
46 /* Use memcpy if not overlapping */
47 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
49 memcpy( dst, src, len );
51 /* Otherwise do it the hard way (FIXME: could do better than this) */
52 else if (dst < src)
54 while (len--) *dst++ = *((char *)src)++;
56 else
58 dst += len - 1;
59 src = (char *)src + len - 1;
60 while (len--) *dst-- = *((char *)src)--;
62 return dest;
64 #endif /* HAVE_MEMMOVE */
66 #ifndef HAVE_STRERROR
67 const char *strerror( int err )
69 /* Let's hope we have sys_errlist then */
70 return sys_errlist[err];
72 #endif /* HAVE_STRERROR */
74 #if !defined(HAVE_CLONE) && defined(__linux__)
75 #include <assert.h>
76 #include <errno.h>
77 #ifdef HAVE_SYSCALL_H
78 # include <syscall.h>
79 #endif
80 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
82 #ifdef __i386__
83 int ret;
84 void **stack_ptr = (void **)stack;
85 *--stack_ptr = arg; /* Push argument on stack */
86 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
87 __asm__ __volatile__( "pushl %%ebx\n\t"
88 "movl %2,%%ebx\n\t"
89 "int $0x80\n\t"
90 "popl %%ebx\n\t" /* Contains fn in the child */
91 "testl %%eax,%%eax\n\t"
92 "jnz 0f\n\t"
93 "call *%%ebx\n\t" /* Should never return */
94 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
95 "0:"
96 : "=a" (ret)
97 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
98 assert( ret ); /* If ret is 0, we returned from the child function */
99 if (ret > 0) return ret;
100 errno = -ret;
101 return -1;
102 #else
103 errno = EINVAL;
104 return -1;
105 #endif /* __i386__ */
107 #endif /* !HAVE_CLONE && __linux__ */
110 #ifndef HAVE_STRCASECMP
111 int strcasecmp( const char *str1, const char *str2 )
113 while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
114 return toupper(*str1) - toupper(*str2);
116 #endif /* HAVE_STRCASECMP */
118 #ifndef HAVE_STRNCASECMP
119 int strncasecmp( const char *str1, const char *str2, size_t n )
121 int res;
122 if (!n) return 0;
123 while ((--n > 0) && *str1)
124 if ((res = toupper(*str1++) - toupper(*str2++))) return res;
125 return toupper(*str1) - toupper(*str2);
127 #endif /* HAVE_STRNCASECMP */
129 /**
130 * It looks like the openpty that comes with glibc in RedHat 5.0
131 * is buggy (second call returns what looks like a dup of 0 and 1
132 * instead of a new pty), this is a generic replacement.
134 /** We will have an autoconf check for this soon... */
136 int wine_openpty(int *master, int *slave, char *name,
137 struct termios *term, struct winsize *winsize)
139 char *ptr1, *ptr2;
140 char pts_name[512];
142 strcpy (pts_name, "/dev/ptyXY");
144 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
145 pts_name[8] = *ptr1;
146 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
147 pts_name[9] = *ptr2;
149 if ((*master = open(pts_name, O_RDWR)) < 0) {
150 if (errno == ENOENT)
151 return -1;
152 else
153 continue;
155 pts_name[5] = 't';
156 if ((*slave = open(pts_name, O_RDWR)) < 0) {
157 pts_name[5] = 'p';
158 continue;
161 if (term != NULL)
162 tcsetattr(*slave, TCSANOW, term);
163 if (winsize != NULL)
164 ioctl(*slave, TIOCSWINSZ, winsize);
165 if (name != NULL)
166 strcpy(name, pts_name);
167 return *slave;
170 return -1;