- implemented Get/SetThreadLocale
[wine.git] / misc / port.c
blobb1bc48b7fe8e396b71eeec7012f4a5c1c64a357f
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
36 #include "wine/port.h"
38 /***********************************************************************
39 * usleep
41 #ifndef HAVE_USLEEP
42 unsigned int usleep (unsigned int useconds)
44 #if defined(__EMX__)
45 DosSleep(useconds);
46 return 0;
47 #elif defined(__BEOS__)
48 return snooze(useconds);
49 #elif defined(HAVE_SELECT)
50 struct timeval delay;
52 delay.tv_sec = 0;
53 delay.tv_usec = useconds;
55 select( 0, 0, 0, 0, &delay );
56 return 0;
57 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
58 errno = ENOSYS;
59 return -1;
60 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
62 #endif /* HAVE_USLEEP */
64 /***********************************************************************
65 * memmove
67 #ifndef HAVE_MEMMOVE
68 void *memmove( void *dest, const void *src, unsigned int len )
70 register char *dst = dest;
72 /* Use memcpy if not overlapping */
73 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
75 memcpy( dst, src, len );
77 /* Otherwise do it the hard way (FIXME: could do better than this) */
78 else if (dst < src)
80 while (len--) *dst++ = *((char *)src)++;
82 else
84 dst += len - 1;
85 src = (char *)src + len - 1;
86 while (len--) *dst-- = *((char *)src)--;
88 return dest;
90 #endif /* HAVE_MEMMOVE */
92 /***********************************************************************
93 * strerror
95 #ifndef HAVE_STRERROR
96 const char *strerror( int err )
98 /* Let's hope we have sys_errlist then */
99 return sys_errlist[err];
101 #endif /* HAVE_STRERROR */
103 /***********************************************************************
104 * clone
106 #if !defined(HAVE_CLONE) && defined(__linux__)
107 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
109 #ifdef __i386__
110 int ret;
111 void **stack_ptr = (void **)stack;
112 *--stack_ptr = arg; /* Push argument on stack */
113 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
114 __asm__ __volatile__( "pushl %%ebx\n\t"
115 "movl %2,%%ebx\n\t"
116 "int $0x80\n\t"
117 "popl %%ebx\n\t" /* Contains fn in the child */
118 "testl %%eax,%%eax\n\t"
119 "jnz 0f\n\t"
120 "call *%%ebx\n\t" /* Should never return */
121 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
122 "0:"
123 : "=a" (ret)
124 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
125 assert( ret ); /* If ret is 0, we returned from the child function */
126 if (ret > 0) return ret;
127 errno = -ret;
128 return -1;
129 #else
130 errno = EINVAL;
131 return -1;
132 #endif /* __i386__ */
134 #endif /* !HAVE_CLONE && __linux__ */
136 /***********************************************************************
137 * strcasecmp
139 #ifndef HAVE_STRCASECMP
140 int strcasecmp( const char *str1, const char *str2 )
142 while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
143 return toupper(*str1) - toupper(*str2);
145 #endif /* HAVE_STRCASECMP */
147 /***********************************************************************
148 * strncasecmp
150 #ifndef HAVE_STRNCASECMP
151 int strncasecmp( const char *str1, const char *str2, size_t n )
153 int res;
154 if (!n) return 0;
155 while ((--n > 0) && *str1)
156 if ((res = toupper(*str1++) - toupper(*str2++))) return res;
157 return toupper(*str1) - toupper(*str2);
159 #endif /* HAVE_STRNCASECMP */
161 /***********************************************************************
162 * wine_openpty
163 * NOTE
164 * It looks like the openpty that comes with glibc in RedHat 5.0
165 * is buggy (second call returns what looks like a dup of 0 and 1
166 * instead of a new pty), this is a generic replacement.
168 * FIXME
169 * We should have a autoconf check for this.
171 int wine_openpty(int *master, int *slave, char *name,
172 struct termios *term, struct winsize *winsize)
174 #ifdef HAVE_OPENPTY
175 return openpty(master,slave,name,term,winsize);
176 #else
177 char *ptr1, *ptr2;
178 char pts_name[512];
180 strcpy (pts_name, "/dev/ptyXY");
182 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
183 pts_name[8] = *ptr1;
184 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
185 pts_name[9] = *ptr2;
187 if ((*master = open(pts_name, O_RDWR)) < 0) {
188 if (errno == ENOENT)
189 return -1;
190 else
191 continue;
193 pts_name[5] = 't';
194 if ((*slave = open(pts_name, O_RDWR)) < 0) {
195 pts_name[5] = 'p';
196 continue;
199 if (term != NULL)
200 tcsetattr(*slave, TCSANOW, term);
201 if (winsize != NULL)
202 ioctl(*slave, TIOCSWINSZ, winsize);
203 if (name != NULL)
204 strcpy(name, pts_name);
205 return *slave;
208 return -1;
209 #endif
212 /***********************************************************************
213 * getnetbyaddr
215 #ifndef HAVE_GETNETBYADDR
216 struct netent *getnetbyaddr(unsigned long net, int type)
218 errno = ENOSYS;
219 return NULL;
221 #endif /* defined(HAVE_GETNETBYNAME) */
223 /***********************************************************************
224 * getnetbyname
226 #ifndef HAVE_GETNETBYNAME
227 struct netent *getnetbyname(const char *name)
229 errno = ENOSYS;
230 return NULL;
232 #endif /* defined(HAVE_GETNETBYNAME) */
234 /***********************************************************************
235 * getprotobyname
237 #ifndef HAVE_GETPROTOBYNAME
238 struct protoent *getprotobyname(const char *name)
240 errno = ENOSYS;
241 return NULL;
243 #endif /* !defined(HAVE_GETPROTOBYNAME) */
245 /***********************************************************************
246 * getprotobynumber
248 #ifndef HAVE_GETPROTOBYNUMBER
249 struct protoent *getprotobynumber(int proto)
251 errno = ENOSYS;
252 return NULL;
254 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
256 /***********************************************************************
257 * getservbyport
259 #ifndef HAVE_GETSERVBYPORT
260 struct servent *getservbyport(int port, const char *proto)
262 errno = ENOSYS;
263 return NULL;
265 #endif /* !defined(HAVE_GETSERVBYPORT) */
267 /***********************************************************************
268 * getsockopt
270 #ifndef HAVE_GETSOCKOPT
271 int getsockopt(int socket, int level, int option_name,
272 void *option_value, size_t *option_len)
274 errno = ENOSYS;
275 return -1;
277 #endif /* !defined(HAVE_GETSOCKOPT) */
279 /***********************************************************************
280 * inet_network
282 #ifndef HAVE_INET_NETWORK
283 unsigned long inet_network(const char *cp)
285 errno = ENOSYS;
286 return 0;
288 #endif /* defined(HAVE_INET_NETWORK) */
290 /***********************************************************************
291 * settimeofday
293 #ifndef HAVE_SETTIMEOFDAY
294 int settimeofday(struct timeval *tp, void *reserved)
296 tp->tv_sec = 0;
297 tp->tv_usec = 0;
299 errno = ENOSYS;
300 return -1;
302 #endif /* HAVE_SETTIMEOFDAY */
304 /***********************************************************************
305 * statfs
307 #ifndef HAVE_STATFS
308 int statfs(const char *name, struct statfs *info)
310 #ifdef __BEOS__
311 dev_t mydev;
312 fs_info fsinfo;
314 if(!info) {
315 errno = ENOSYS;
316 return -1;
319 if ((mydev = dev_for_path(name)) < 0) {
320 errno = ENOSYS;
321 return -1;
324 if (fs_stat_dev(mydev,&fsinfo) < 0) {
325 errno = ENOSYS;
326 return -1;
329 info->f_bsize = fsinfo.block_size;
330 info->f_blocks = fsinfo.total_blocks;
331 info->f_bfree = fsinfo.free_blocks;
333 return 0;
334 #else /* defined(__BEOS__) */
335 errno = ENOSYS;
336 return -1;
337 #endif /* defined(__BEOS__) */
339 #endif /* !defined(HAVE_STATFS) */