Implemented new messages.
[wine.git] / library / port.c
bloba23711a5648d4ba42b579dd147d5b58a1ad94592
1 /*
2 * Misc. functions for systems that don't have them
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.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
32 #ifdef HAVE_PTY_H
33 # include <pty.h>
34 #endif
35 #ifdef HAVE_LIBUTIL_H
36 # include <libutil.h>
37 #endif
39 #include "wine/port.h"
41 /***********************************************************************
42 * usleep
44 #ifndef HAVE_USLEEP
45 unsigned int usleep (unsigned int useconds)
47 #if defined(__EMX__)
48 DosSleep(useconds);
49 return 0;
50 #elif defined(__BEOS__)
51 return snooze(useconds);
52 #elif defined(HAVE_SELECT)
53 struct timeval delay;
55 delay.tv_sec = useconds / 1000000;
56 delay.tv_usec = useconds % 1000000;
58 select( 0, 0, 0, 0, &delay );
59 return 0;
60 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
61 errno = ENOSYS;
62 return -1;
63 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
65 #endif /* HAVE_USLEEP */
67 /***********************************************************************
68 * memmove
70 #ifndef HAVE_MEMMOVE
71 void *memmove( void *dest, const void *src, unsigned int len )
73 register char *dst = dest;
75 /* Use memcpy if not overlapping */
76 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
78 memcpy( dst, src, len );
80 /* Otherwise do it the hard way (FIXME: could do better than this) */
81 else if (dst < src)
83 while (len--) *dst++ = *((char *)src)++;
85 else
87 dst += len - 1;
88 src = (char *)src + len - 1;
89 while (len--) *dst-- = *((char *)src)--;
91 return dest;
93 #endif /* HAVE_MEMMOVE */
95 /***********************************************************************
96 * strerror
98 #ifndef HAVE_STRERROR
99 const char *strerror( int err )
101 /* Let's hope we have sys_errlist then */
102 return sys_errlist[err];
104 #endif /* HAVE_STRERROR */
106 /***********************************************************************
107 * clone
109 #if !defined(HAVE_CLONE) && defined(__linux__)
110 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
112 #ifdef __i386__
113 int ret;
114 void **stack_ptr = (void **)stack;
115 *--stack_ptr = arg; /* Push argument on stack */
116 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
117 __asm__ __volatile__( "pushl %%ebx\n\t"
118 "movl %2,%%ebx\n\t"
119 "int $0x80\n\t"
120 "popl %%ebx\n\t" /* Contains fn in the child */
121 "testl %%eax,%%eax\n\t"
122 "jnz 0f\n\t"
123 "call *%%ebx\n\t" /* Should never return */
124 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
125 "0:"
126 : "=a" (ret)
127 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
128 assert( ret ); /* If ret is 0, we returned from the child function */
129 if (ret > 0) return ret;
130 errno = -ret;
131 return -1;
132 #else
133 errno = EINVAL;
134 return -1;
135 #endif /* __i386__ */
137 #endif /* !HAVE_CLONE && __linux__ */
139 /***********************************************************************
140 * strcasecmp
142 #ifndef HAVE_STRCASECMP
143 int strcasecmp( const char *str1, const char *str2 )
145 while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
146 return toupper(*str1) - toupper(*str2);
148 #endif /* HAVE_STRCASECMP */
150 /***********************************************************************
151 * strncasecmp
153 #ifndef HAVE_STRNCASECMP
154 int strncasecmp( const char *str1, const char *str2, size_t n )
156 int res;
157 if (!n) return 0;
158 while ((--n > 0) && *str1)
159 if ((res = toupper(*str1++) - toupper(*str2++))) return res;
160 return toupper(*str1) - toupper(*str2);
162 #endif /* HAVE_STRNCASECMP */
164 /***********************************************************************
165 * wine_openpty
166 * NOTE
167 * It looks like the openpty that comes with glibc in RedHat 5.0
168 * is buggy (second call returns what looks like a dup of 0 and 1
169 * instead of a new pty), this is a generic replacement.
171 * FIXME
172 * We should have a autoconf check for this.
174 int wine_openpty(int *master, int *slave, char *name,
175 struct termios *term, struct winsize *winsize)
177 #ifdef HAVE_OPENPTY
178 return openpty(master,slave,name,term,winsize);
179 #else
180 char *ptr1, *ptr2;
181 char pts_name[512];
183 strcpy (pts_name, "/dev/ptyXY");
185 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
186 pts_name[8] = *ptr1;
187 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
188 pts_name[9] = *ptr2;
190 if ((*master = open(pts_name, O_RDWR)) < 0) {
191 if (errno == ENOENT)
192 return -1;
193 else
194 continue;
196 pts_name[5] = 't';
197 if ((*slave = open(pts_name, O_RDWR)) < 0) {
198 pts_name[5] = 'p';
199 continue;
202 if (term != NULL)
203 tcsetattr(*slave, TCSANOW, term);
204 if (winsize != NULL)
205 ioctl(*slave, TIOCSWINSZ, winsize);
206 if (name != NULL)
207 strcpy(name, pts_name);
208 return *slave;
211 return -1;
212 #endif
215 /***********************************************************************
216 * getnetbyaddr
218 #ifndef HAVE_GETNETBYADDR
219 struct netent *getnetbyaddr(unsigned long net, int type)
221 errno = ENOSYS;
222 return NULL;
224 #endif /* defined(HAVE_GETNETBYNAME) */
226 /***********************************************************************
227 * getnetbyname
229 #ifndef HAVE_GETNETBYNAME
230 struct netent *getnetbyname(const char *name)
232 errno = ENOSYS;
233 return NULL;
235 #endif /* defined(HAVE_GETNETBYNAME) */
237 /***********************************************************************
238 * getprotobyname
240 #ifndef HAVE_GETPROTOBYNAME
241 struct protoent *getprotobyname(const char *name)
243 errno = ENOSYS;
244 return NULL;
246 #endif /* !defined(HAVE_GETPROTOBYNAME) */
248 /***********************************************************************
249 * getprotobynumber
251 #ifndef HAVE_GETPROTOBYNUMBER
252 struct protoent *getprotobynumber(int proto)
254 errno = ENOSYS;
255 return NULL;
257 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
259 /***********************************************************************
260 * getservbyport
262 #ifndef HAVE_GETSERVBYPORT
263 struct servent *getservbyport(int port, const char *proto)
265 errno = ENOSYS;
266 return NULL;
268 #endif /* !defined(HAVE_GETSERVBYPORT) */
270 /***********************************************************************
271 * getsockopt
273 #ifndef HAVE_GETSOCKOPT
274 int getsockopt(int socket, int level, int option_name,
275 void *option_value, size_t *option_len)
277 errno = ENOSYS;
278 return -1;
280 #endif /* !defined(HAVE_GETSOCKOPT) */
282 /***********************************************************************
283 * inet_network
285 #ifndef HAVE_INET_NETWORK
286 unsigned long inet_network(const char *cp)
288 errno = ENOSYS;
289 return 0;
291 #endif /* defined(HAVE_INET_NETWORK) */
293 /***********************************************************************
294 * settimeofday
296 #ifndef HAVE_SETTIMEOFDAY
297 int settimeofday(struct timeval *tp, void *reserved)
299 tp->tv_sec = 0;
300 tp->tv_usec = 0;
302 errno = ENOSYS;
303 return -1;
305 #endif /* HAVE_SETTIMEOFDAY */
307 /***********************************************************************
308 * statfs
310 #ifndef HAVE_STATFS
311 int statfs(const char *name, struct statfs *info)
313 #ifdef __BEOS__
314 dev_t mydev;
315 fs_info fsinfo;
317 if(!info) {
318 errno = ENOSYS;
319 return -1;
322 if ((mydev = dev_for_path(name)) < 0) {
323 errno = ENOSYS;
324 return -1;
327 if (fs_stat_dev(mydev,&fsinfo) < 0) {
328 errno = ENOSYS;
329 return -1;
332 info->f_bsize = fsinfo.block_size;
333 info->f_blocks = fsinfo.total_blocks;
334 info->f_bfree = fsinfo.free_blocks;
335 return 0;
336 #else /* defined(__BEOS__) */
337 errno = ENOSYS;
338 return -1;
339 #endif /* defined(__BEOS__) */
341 #endif /* !defined(HAVE_STATFS) */