Changed initial process creation to avoid memory allocations.
[wine/multimedia.git] / misc / port.c
blob668bf1a690296324e5dde203a1e6d75bfaf73d48
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.h"
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/time.h>
11 #include <sys/stat.h>
12 #include <sys/ioctl.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <termios.h>
16 #ifdef HAVE_LIBIO_H
17 # include <libio.h>
18 #endif
20 #ifndef HAVE_USLEEP
21 #ifdef __EMX__
22 unsigned int usleep (unsigned int useconds) { DosSleep(useconds); }
23 #else
24 unsigned int usleep (unsigned int useconds)
26 struct timeval delay;
28 delay.tv_sec = 0;
29 delay.tv_usec = useconds;
31 select( 0, 0, 0, 0, &delay );
32 return 0;
34 #endif
35 #endif /* HAVE_USLEEP */
37 #ifndef HAVE_MEMMOVE
38 void *memmove( void *dest, const void *src, unsigned int len )
40 register char *dst = dest;
42 /* Use memcpy if not overlapping */
43 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
45 memcpy( dst, src, len );
47 /* Otherwise do it the hard way (FIXME: could do better than this) */
48 else if (dst < src)
50 while (len--) *dst++ = *((char *)src)++;
52 else
54 dst += len - 1;
55 src = (char *)src + len - 1;
56 while (len--) *dst-- = *((char *)src)--;
58 return dest;
60 #endif /* HAVE_MEMMOVE */
62 #ifndef HAVE_STRERROR
63 const char *strerror( int err )
65 /* Let's hope we have sys_errlist then */
66 return sys_errlist[err];
68 #endif /* HAVE_STRERROR */
70 #if !defined(HAVE_CLONE) && defined(__linux__)
71 #include <assert.h>
72 #include <errno.h>
73 #include <syscall.h>
74 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
76 #ifdef __i386__
77 int ret;
78 void **stack_ptr = (void **)stack;
79 *--stack_ptr = arg; /* Push argument on stack */
80 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
81 __asm__ __volatile__( "pushl %%ebx\n\t"
82 "movl %2,%%ebx\n\t"
83 "int $0x80\n\t"
84 "popl %%ebx\n\t" /* Contains fn in the child */
85 "testl %%eax,%%eax\n\t"
86 "jnz 0f\n\t"
87 "call *%%ebx\n\t" /* Should never return */
88 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
89 "0:"
90 : "=a" (ret)
91 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
92 assert( ret ); /* If ret is 0, we returned from the child function */
93 if (ret > 0) return ret;
94 errno = -ret;
95 return -1;
96 #else
97 errno = EINVAL;
98 return -1;
99 #endif /* __i386__ */
101 #endif /* !HAVE_CLONE && __linux__ */
104 /**
105 * It looks like the openpty that comes with glibc in RedHat 5.0
106 * is buggy (second call returns what looks like a dup of 0 and 1
107 * instead of a new pty), this is a generic replacement.
109 /** We will have an autoconf check for this soon... */
111 int wine_openpty(int *master, int *slave, char *name,
112 struct termios *term, struct winsize *winsize)
114 char *ptr1, *ptr2;
115 char pts_name[512];
117 strcpy (pts_name, "/dev/ptyXY");
119 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
120 pts_name[8] = *ptr1;
121 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
122 pts_name[9] = *ptr2;
124 if ((*master = open(pts_name, O_RDWR)) < 0) {
125 if (errno == ENOENT)
126 return -1;
127 else
128 continue;
130 pts_name[5] = 't';
131 if ((*slave = open(pts_name, O_RDWR)) < 0) {
132 pts_name[5] = 'p';
133 continue;
136 if (term != NULL)
137 tcsetattr(*slave, TCSANOW, term);
138 if (winsize != NULL)
139 ioctl(*slave, TIOCSWINSZ, winsize);
140 if (name != NULL)
141 strcpy(name, pts_name);
142 return *slave;
145 return -1;