Don't make the spec file constructors static so that they don't get
[wine/multimedia.git] / library / port.c
blob064c1833dde5660a3c8ed3265a6b5e868fb046a3
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_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #ifdef HAVE_LIBIO_H
30 # include <libio.h>
31 #endif
32 #ifdef HAVE_SYSCALL_H
33 # include <syscall.h>
34 #endif
35 #ifdef HAVE_PTY_H
36 # include <pty.h>
37 #endif
38 #ifdef HAVE_LIBUTIL_H
39 # include <libutil.h>
40 #endif
42 #include "wine/port.h"
44 /***********************************************************************
45 * usleep
47 #ifndef HAVE_USLEEP
48 unsigned int usleep (unsigned int useconds)
50 #if defined(__EMX__)
51 DosSleep(useconds);
52 return 0;
53 #elif defined(__BEOS__)
54 return snooze(useconds);
55 #elif defined(HAVE_SELECT)
56 struct timeval delay;
58 delay.tv_sec = useconds / 1000000;
59 delay.tv_usec = useconds % 1000000;
61 select( 0, 0, 0, 0, &delay );
62 return 0;
63 #else /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
64 errno = ENOSYS;
65 return -1;
66 #endif /* defined(__EMX__) || defined(__BEOS__) || defined(HAVE_SELECT) */
68 #endif /* HAVE_USLEEP */
70 /***********************************************************************
71 * memmove
73 #ifndef HAVE_MEMMOVE
74 void *memmove( void *dest, const void *src, unsigned int len )
76 register char *dst = dest;
78 /* Use memcpy if not overlapping */
79 if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
81 memcpy( dst, src, len );
83 /* Otherwise do it the hard way (FIXME: could do better than this) */
84 else if (dst < src)
86 while (len--) *dst++ = *((char *)src)++;
88 else
90 dst += len - 1;
91 src = (char *)src + len - 1;
92 while (len--) *dst-- = *((char *)src)--;
94 return dest;
96 #endif /* HAVE_MEMMOVE */
98 /***********************************************************************
99 * strerror
101 #ifndef HAVE_STRERROR
102 const char *strerror( int err )
104 /* Let's hope we have sys_errlist then */
105 return sys_errlist[err];
107 #endif /* HAVE_STRERROR */
110 /***********************************************************************
111 * getpagesize
113 #ifndef HAVE_GETPAGESIZE
114 size_t getpagesize(void)
116 # ifdef __svr4__
117 return sysconf(_SC_PAGESIZE);
118 # else
119 # error Cannot get the page size on this platform
120 # endif
122 #endif /* HAVE_GETPAGESIZE */
125 /***********************************************************************
126 * clone
128 #if !defined(HAVE_CLONE) && defined(__linux__)
129 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
131 #ifdef __i386__
132 int ret;
133 void **stack_ptr = (void **)stack;
134 *--stack_ptr = arg; /* Push argument on stack */
135 *--stack_ptr = fn; /* Push function pointer (popped into ebx) */
136 __asm__ __volatile__( "pushl %%ebx\n\t"
137 "movl %2,%%ebx\n\t"
138 "int $0x80\n\t"
139 "popl %%ebx\n\t" /* Contains fn in the child */
140 "testl %%eax,%%eax\n\t"
141 "jnz 0f\n\t"
142 "call *%%ebx\n\t" /* Should never return */
143 "xorl %%eax,%%eax\n\t" /* Just in case it does*/
144 "0:"
145 : "=a" (ret)
146 : "0" (SYS_clone), "r" (flags), "c" (stack_ptr) );
147 assert( ret ); /* If ret is 0, we returned from the child function */
148 if (ret > 0) return ret;
149 errno = -ret;
150 return -1;
151 #else
152 errno = EINVAL;
153 return -1;
154 #endif /* __i386__ */
156 #endif /* !HAVE_CLONE && __linux__ */
158 /***********************************************************************
159 * strcasecmp
161 #ifndef HAVE_STRCASECMP
162 int strcasecmp( const char *str1, const char *str2 )
164 while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
165 return toupper(*str1) - toupper(*str2);
167 #endif /* HAVE_STRCASECMP */
169 /***********************************************************************
170 * strncasecmp
172 #ifndef HAVE_STRNCASECMP
173 int strncasecmp( const char *str1, const char *str2, size_t n )
175 int res;
176 if (!n) return 0;
177 while ((--n > 0) && *str1)
178 if ((res = toupper(*str1++) - toupper(*str2++))) return res;
179 return toupper(*str1) - toupper(*str2);
181 #endif /* HAVE_STRNCASECMP */
183 /***********************************************************************
184 * wine_openpty
185 * NOTE
186 * It looks like the openpty that comes with glibc in RedHat 5.0
187 * is buggy (second call returns what looks like a dup of 0 and 1
188 * instead of a new pty), this is a generic replacement.
190 * FIXME
191 * We should have a autoconf check for this.
193 int wine_openpty(int *master, int *slave, char *name,
194 struct termios *term, struct winsize *winsize)
196 #ifdef HAVE_OPENPTY
197 return openpty(master,slave,name,term,winsize);
198 #else
199 char *ptr1, *ptr2;
200 char pts_name[512];
202 strcpy (pts_name, "/dev/ptyXY");
204 for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
205 pts_name[8] = *ptr1;
206 for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
207 pts_name[9] = *ptr2;
209 if ((*master = open(pts_name, O_RDWR)) < 0) {
210 if (errno == ENOENT)
211 return -1;
212 else
213 continue;
215 pts_name[5] = 't';
216 if ((*slave = open(pts_name, O_RDWR)) < 0) {
217 pts_name[5] = 'p';
218 continue;
221 if (term != NULL)
222 tcsetattr(*slave, TCSANOW, term);
223 if (winsize != NULL)
224 ioctl(*slave, TIOCSWINSZ, winsize);
225 if (name != NULL)
226 strcpy(name, pts_name);
227 return *slave;
230 return -1;
231 #endif
234 /***********************************************************************
235 * getnetbyaddr
237 #ifndef HAVE_GETNETBYADDR
238 struct netent *getnetbyaddr(unsigned long net, int type)
240 errno = ENOSYS;
241 return NULL;
243 #endif /* defined(HAVE_GETNETBYNAME) */
245 /***********************************************************************
246 * getnetbyname
248 #ifndef HAVE_GETNETBYNAME
249 struct netent *getnetbyname(const char *name)
251 errno = ENOSYS;
252 return NULL;
254 #endif /* defined(HAVE_GETNETBYNAME) */
256 /***********************************************************************
257 * getprotobyname
259 #ifndef HAVE_GETPROTOBYNAME
260 struct protoent *getprotobyname(const char *name)
262 errno = ENOSYS;
263 return NULL;
265 #endif /* !defined(HAVE_GETPROTOBYNAME) */
267 /***********************************************************************
268 * getprotobynumber
270 #ifndef HAVE_GETPROTOBYNUMBER
271 struct protoent *getprotobynumber(int proto)
273 errno = ENOSYS;
274 return NULL;
276 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
278 /***********************************************************************
279 * getservbyport
281 #ifndef HAVE_GETSERVBYPORT
282 struct servent *getservbyport(int port, const char *proto)
284 errno = ENOSYS;
285 return NULL;
287 #endif /* !defined(HAVE_GETSERVBYPORT) */
289 /***********************************************************************
290 * getsockopt
292 #ifndef HAVE_GETSOCKOPT
293 int getsockopt(int socket, int level, int option_name,
294 void *option_value, size_t *option_len)
296 errno = ENOSYS;
297 return -1;
299 #endif /* !defined(HAVE_GETSOCKOPT) */
301 /***********************************************************************
302 * inet_network
304 #ifndef HAVE_INET_NETWORK
305 unsigned long inet_network(const char *cp)
307 errno = ENOSYS;
308 return 0;
310 #endif /* defined(HAVE_INET_NETWORK) */
312 /***********************************************************************
313 * settimeofday
315 #ifndef HAVE_SETTIMEOFDAY
316 int settimeofday(struct timeval *tp, void *reserved)
318 tp->tv_sec = 0;
319 tp->tv_usec = 0;
321 errno = ENOSYS;
322 return -1;
324 #endif /* HAVE_SETTIMEOFDAY */
326 /***********************************************************************
327 * statfs
329 #ifndef HAVE_STATFS
330 int statfs(const char *name, struct statfs *info)
332 #ifdef __BEOS__
333 dev_t mydev;
334 fs_info fsinfo;
336 if(!info) {
337 errno = ENOSYS;
338 return -1;
341 if ((mydev = dev_for_path(name)) < 0) {
342 errno = ENOSYS;
343 return -1;
346 if (fs_stat_dev(mydev,&fsinfo) < 0) {
347 errno = ENOSYS;
348 return -1;
351 info->f_bsize = fsinfo.block_size;
352 info->f_blocks = fsinfo.total_blocks;
353 info->f_bfree = fsinfo.free_blocks;
354 return 0;
355 #else /* defined(__BEOS__) */
356 errno = ENOSYS;
357 return -1;
358 #endif /* defined(__BEOS__) */
360 #endif /* !defined(HAVE_STATFS) */
363 /***********************************************************************
364 * lstat
366 #ifndef HAVE_LSTAT
367 int lstat(const char *file_name, struct stat *buf)
369 return stat( file_name, buf );
371 #endif /* HAVE_LSTAT */
374 /***********************************************************************
375 * getrlimit
377 #ifndef HAVE_GETRLIMIT
378 int getrlimit (int resource, struct rlimit *rlim)
380 return -1; /* FAIL */
382 #endif /* HAVE_GETRLIMIT */
384 /***********************************************************************
385 * wine_anon_mmap
387 * Portable wrapper for anonymous mmaps
389 void *wine_anon_mmap( void *start, size_t size, int prot, int flags )
391 static int fdzero = -1;
393 #ifdef MAP_ANON
394 flags |= MAP_ANON;
395 #else
396 if (fdzero == -1)
398 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
400 perror( "/dev/zero: open" );
401 exit(1);
404 #endif /* MAP_ANON */
405 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
406 #ifdef MAP_SHARED
407 flags &= ~MAP_SHARED;
408 #endif
409 #ifdef MAP_PRIVATE
410 flags |= MAP_PRIVATE;
411 #endif
412 return mmap( start, size, prot, flags, fdzero, 0 );