Added better missing function emulation.
[wine/hacks.git] / misc / port.c
blobc03819b28f3b8005f469a9bbda22a34e0a6adfd5
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "wine/port.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 <string.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
22 #include <sys/ioctl.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <termios.h>
26 #ifdef HAVE_LIBIO_H
27 # include <libio.h>
28 #endif
29 #ifdef HAVE_SYSCALL_H
30 # include <syscall.h>
31 #endif
33 /***********************************************************************
34 * usleep
36 #ifndef HAVE_USLEEP
37 unsigned int usleep (unsigned int useconds)
39 #if defined(__EMX__)
40 DosSleep(useconds);
41 return 0;
42 #elif defined(__BEOS__)
43 return snooze(useconds);
44 #elif defined(HAVE_SELECT)
45 struct timeval delay;
47 delay.tv_sec = 0;
48 delay.tv_usec = useconds;
50 select( 0, 0, 0, 0, &delay );
51 return 0;
52 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
53 errno = ENOSYS;
54 return -1;
55 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
57 #endif /* HAVE_USLEEP */
59 /***********************************************************************
60 * memmove
62 #ifndef HAVE_MEMMOVE
63 void *memmove( void *dest, const void *src, unsigned int len )
65 register char *dst = dest;
67 /* Use memcpy if not overlapping */
68 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
70 memcpy( dst, src, len );
72 /* Otherwise do it the hard way (FIXME: could do better than this) */
73 else if (dst < src)
75 while (len--) *dst++ = *((char *)src)++;
77 else
79 dst += len - 1;
80 src = (char *)src + len - 1;
81 while (len--) *dst-- = *((char *)src)--;
83 return dest;
85 #endif /* HAVE_MEMMOVE */
87 /***********************************************************************
88 * strerror
90 #ifndef HAVE_STRERROR
91 const char *strerror( int err )
93 /* Let's hope we have sys_errlist then */
94 return sys_errlist[err];
96 #endif /* HAVE_STRERROR */
98 /***********************************************************************
99 * clone
101 #if !defined(HAVE_CLONE) && defined(__linux__)
102 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
104 #ifdef __i386__
105 int ret;
106 void **stack_ptr = (void **)stack;
107 *--stack_ptr = arg; /* Push argument on stack */
108 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
109 __asm__ __volatile__( "pushl %%ebx\n\t"
110 "movl %2,%%ebx\n\t"
111 "int $0x80\n\t"
112 "popl %%ebx\n\t" /* Contains fn in the child */
113 "testl %%eax,%%eax\n\t"
114 "jnz 0f\n\t"
115 "call *%%ebx\n\t" /* Should never return */
116 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
117 "0:"
118 : "=a" (ret)
119 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
120 assert( ret ); /* If ret is 0, we returned from the child function */
121 if (ret > 0) return ret;
122 errno = -ret;
123 return -1;
124 #else
125 errno = EINVAL;
126 return -1;
127 #endif /* __i386__ */
129 #endif /* !HAVE_CLONE && __linux__ */
131 /***********************************************************************
132 * strcasecmp
134 #ifndef HAVE_STRCASECMP
135 int strcasecmp( const char *str1, const char *str2 )
137 while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
138 return toupper(*str1) - toupper(*str2);
140 #endif /* HAVE_STRCASECMP */
142 /***********************************************************************
143 * strncasecmp
145 #ifndef HAVE_STRNCASECMP
146 int strncasecmp( const char *str1, const char *str2, size_t n )
148 int res;
149 if (!n) return 0;
150 while ((--n > 0) && *str1)
151 if ((res = toupper(*str1++) - toupper(*str2++))) return res;
152 return toupper(*str1) - toupper(*str2);
154 #endif /* HAVE_STRNCASECMP */
156 /***********************************************************************
157 * wine_openpty
158 * NOTE
159 * It looks like the openpty that comes with glibc in RedHat 5.0
160 * is buggy (second call returns what looks like a dup of 0 and 1
161 * instead of a new pty), this is a generic replacement.
163 * FIXME
164 * We should have a autoconf check for this.
166 int wine_openpty(int *master, int *slave, char *name,
167 struct termios *term, struct winsize *winsize)
169 char *ptr1, *ptr2;
170 char pts_name[512];
172 strcpy (pts_name, "/dev/ptyXY");
174 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
175 pts_name[8] = *ptr1;
176 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
177 pts_name[9] = *ptr2;
179 if ((*master = open(pts_name, O_RDWR)) < 0) {
180 if (errno == ENOENT)
181 return -1;
182 else
183 continue;
185 pts_name[5] = 't';
186 if ((*slave = open(pts_name, O_RDWR)) < 0) {
187 pts_name[5] = 'p';
188 continue;
191 if (term != NULL)
192 tcsetattr(*slave, TCSANOW, term);
193 if (winsize != NULL)
194 ioctl(*slave, TIOCSWINSZ, winsize);
195 if (name != NULL)
196 strcpy(name, pts_name);
197 return *slave;
200 return -1;
203 /***********************************************************************
204 * getnetbyaddr
206 #ifndef HAVE_GETNETBYADDR
207 struct netent *getnetbyaddr(unsigned long net, int type)
209 errno = ENOSYS;
210 return NULL;
212 #endif /* defined(HAVE_GETNETBYNAME) */
214 /***********************************************************************
215 * getnetbyname
217 #ifndef HAVE_GETNETBYNAME
218 struct netent *getnetbyname(const char *name)
220 errno = ENOSYS;
221 return NULL;
223 #endif /* defined(HAVE_GETNETBYNAME) */
225 /***********************************************************************
226 * getprotobyname
228 #ifndef HAVE_GETPROTOBYNAME
229 struct protoent *getprotobyname(const char *name)
231 errno = ENOSYS;
232 return NULL;
234 #endif /* !defined(HAVE_GETPROTOBYNAME) */
236 /***********************************************************************
237 * getprotobynumber
239 #ifndef HAVE_GETPROTOBYNUMBER
240 struct protoent *getprotobynumber(int proto)
242 errno = ENOSYS;
243 return NULL;
245 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
247 /***********************************************************************
248 * getservbyport
250 #ifndef HAVE_GETSERVBYPORT
251 struct servent *getservbyport(int port, const char *proto)
253 errno = ENOSYS;
254 return NULL;
256 #endif /* !defined(HAVE_GETSERVBYPORT) */
258 /***********************************************************************
259 * getsockopt
261 #ifndef HAVE_GETSOCKOPT
262 int getsockopt(int socket, int level, int option_name,
263 void *option_value, size_t *option_len)
265 errno = ENOSYS;
266 return -1;
268 #endif /* !defined(HAVE_GETSOCKOPT) */
270 /***********************************************************************
271 * inet_network
273 #ifndef HAVE_INET_NETWORK
274 unsigned long inet_network(const char *cp)
276 errno = ENOSYS;
277 return 0;
279 #endif /* defined(HAVE_INET_NETWORK) */
281 /***********************************************************************
282 * settimeofday
284 #ifndef HAVE_SETTIMEOFDAY
285 int settimeofday(struct timeval *tp, void *reserved)
287 tp->tv_sec = 0;
288 tp->tv_usec = 0;
290 errno = ENOSYS;
291 return -1;
293 #endif /* HAVE_SETTIMEOFDAY */
295 /***********************************************************************
296 * statfs
298 #ifndef HAVE_STATFS
299 int statfs(const char *name, struct statfs *info)
301 #ifdef __BEOS__
302 dev_t mydev;
303 fs_info fsinfo;
305 if(!info) {
306 errno = ENOSYS;
307 return -1;
310 if ((mydev = dev_for_path(name)) < 0) {
311 errno = ENOSYS;
312 return -1;
315 if (fs_stat_dev(mydev,&fsinfo) < 0) {
316 errno = ENOSYS;
317 return -1;
320 info->f_bsize = fsinfo.block_size;
321 info->f_blocks = fsinfo.total_blocks;
322 info->f_bfree = fsinfo.free_blocks;
324 return 0;
325 #else /* defined(__BEOS__) */
326 errno = ENOSYS;
327 return -1;
328 #endif /* defined(__BEOS__) */
330 #endif /* !defined(HAVE_STATFS) */