Handle (console) applications that never created a queue correctly
[wine/dcerpc.git] / misc / port.c
blobaba2d0a1c3d4af436150703e6c6642ea9b14d9b9
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
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 #include <libio.h>
17 #ifndef HAVE_USLEEP
18 #ifdef __EMX__
19 unsigned int usleep (unsigned int useconds) { DosSleep(useconds); }
20 #else
21 unsigned int usleep (unsigned int useconds)
23 struct timeval delay;
25 delay.tv_sec = 0;
26 delay.tv_usec = useconds;
28 select( 0, 0, 0, 0, &delay );
29 return 0;
31 #endif
32 #endif /* HAVE_USLEEP */
34 #ifndef HAVE_MEMMOVE
35 void *memmove( void *dest, const void *src, unsigned int len )
37 register char *dst = dest;
39 /* Use memcpy if not overlapping */
40 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
42 memcpy( dst, src, len );
44 /* Otherwise do it the hard way (FIXME: could do better than this) */
45 else if (dst < src)
47 while (len--) *dst++ = *((char *)src)++;
49 else
51 dst += len - 1;
52 src = (char *)src + len - 1;
53 while (len--) *dst-- = *((char *)src)--;
55 return dest;
57 #endif /* HAVE_MEMMOVE */
59 #ifndef HAVE_STRERROR
60 const char *strerror( int err )
62 /* Let's hope we have sys_errlist then */
63 return sys_errlist[err];
65 #endif /* HAVE_STRERROR */
67 #if !defined(HAVE_CLONE) && defined(__linux__)
68 #include <assert.h>
69 #include <errno.h>
70 #include <syscall.h>
71 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
73 #ifdef __i386__
74 int ret;
75 void **stack_ptr = (void **)stack;
76 *--stack_ptr = arg; /* Push argument on stack */
77 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
78 __asm__ __volatile__( "pushl %%ebx\n\t"
79 "movl %2,%%ebx\n\t"
80 "int $0x80\n\t"
81 "popl %%ebx\n\t" /* Contains fn in the child */
82 "testl %%eax,%%eax\n\t"
83 "jnz 0f\n\t"
84 "call *%%ebx\n\t" /* Should never return */
85 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
86 "0:"
87 : "=a" (ret)
88 : "0" (SYS_clone), "g" (flags), "c" (stack_ptr) );
89 assert( ret ); /* If ret is 0, we returned from the child function */
90 if (ret > 0) return ret;
91 errno = -ret;
92 return -1;
93 #else
94 errno = EINVAL;
95 return -1;
96 #endif /* __i386__ */
98 #endif /* !HAVE_CLONE && __linux__ */
101 /**
102 * It looks like the openpty that comes with glibc in RedHat 5.0
103 * is buggy (second call returns what looks like a dup of 0 and 1
104 * instead of a new pty), this is a generic replacement.
106 /** We will have an autoconf check for this soon... */
108 int wine_openpty(int *master, int *slave, char *name,
109 struct termios *term, struct winsize *winsize)
111 char *ptr1, *ptr2;
112 char pts_name[512];
114 strcpy (pts_name, "/dev/ptyXY");
116 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
117 pts_name[8] = *ptr1;
118 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
119 pts_name[9] = *ptr2;
121 if ((*master = open(pts_name, O_RDWR)) < 0) {
122 if (errno == ENOENT)
123 return -1;
124 else
125 continue;
127 pts_name[5] = 't';
128 if ((*slave = open(pts_name, O_RDWR)) < 0) {
129 pts_name[5] = 'p';
130 continue;
133 if (term != NULL)
134 tcsetattr(*slave, TCSANOW, term);
135 if (winsize != NULL)
136 ioctl(*slave, TIOCSWINSZ, winsize);
137 if (name != NULL)
138 strcpy(name, pts_name);
139 return *slave;
142 return -1;