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