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