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