Moved FatalAppExit functions to win32/except.c.
[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 "config.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 #ifdef HAVE_PTY_H
33 # include <pty.h>
34 #endif
35
36 #include "wine/port.h"
37
38 /***********************************************************************
39  *              usleep
40  */
41 #ifndef HAVE_USLEEP
42 unsigned int usleep (unsigned int useconds)
43 {
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;
51
52     delay.tv_sec = 0;
53     delay.tv_usec = useconds;
54
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) */
61 }
62 #endif /* HAVE_USLEEP */
63
64 /***********************************************************************
65  *              memmove
66  */
67 #ifndef HAVE_MEMMOVE
68 void *memmove( void *dest, const void *src, unsigned int len )
69 {
70     register char *dst = dest;
71
72     /* Use memcpy if not overlapping */
73     if ((dst + len <= (char *)src) || ((char *)src + len <= dst))
74     {
75         memcpy( dst, src, len );
76     }
77     /* Otherwise do it the hard way (FIXME: could do better than this) */
78     else if (dst < src)
79     {
80         while (len--) *dst++ = *((char *)src)++;
81     }
82     else
83     {
84         dst += len - 1;
85         src = (char *)src + len - 1;
86         while (len--) *dst-- = *((char *)src)--;
87     }
88     return dest;
89 }
90 #endif  /* HAVE_MEMMOVE */
91
92 /***********************************************************************
93  *              strerror
94  */
95 #ifndef HAVE_STRERROR
96 const char *strerror( int err )
97 {
98     /* Let's hope we have sys_errlist then */
99     return sys_errlist[err];
100 }
101 #endif  /* HAVE_STRERROR */
102
103 /***********************************************************************
104  *              clone
105  */
106 #if !defined(HAVE_CLONE) && defined(__linux__)
107 int clone( int (*fn)(void *), void *stack, int flags, void *arg )
108 {
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__ */
133 }
134 #endif  /* !HAVE_CLONE && __linux__ */
135
136 /***********************************************************************
137  *              strcasecmp
138  */
139 #ifndef HAVE_STRCASECMP
140 int strcasecmp( const char *str1, const char *str2 )
141 {
142     while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
143     return toupper(*str1) - toupper(*str2);
144 }
145 #endif /* HAVE_STRCASECMP */
146
147 /***********************************************************************
148  *              strncasecmp
149  */
150 #ifndef HAVE_STRNCASECMP
151 int strncasecmp( const char *str1, const char *str2, size_t n )
152 {
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);
158 }
159 #endif /* HAVE_STRNCASECMP */
160
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.
167  *
168  * FIXME
169  *   We should have a autoconf check for this.
170  */
171 int wine_openpty(int *master, int *slave, char *name, 
172                         struct termios *term, struct winsize *winsize)
173 {
174 #ifdef HAVE_OPENPTY
175     return openpty(master,slave,name,term,winsize);
176 #else
177     char *ptr1, *ptr2;
178     char pts_name[512];
179
180     strcpy (pts_name, "/dev/ptyXY");
181
182     for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
183         pts_name[8] = *ptr1;
184         for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
185             pts_name[9] = *ptr2;
186
187             if ((*master = open(pts_name, O_RDWR)) < 0) {
188                 if (errno == ENOENT)
189                     return -1;
190                 else
191                     continue;
192             }
193             pts_name[5] = 't';
194             if ((*slave = open(pts_name, O_RDWR)) < 0) {
195                 pts_name[5] = 'p';
196                 continue;
197             }
198
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;
206         }
207     }
208     return -1;
209 #endif
210 }
211
212 /***********************************************************************
213  *              getnetbyaddr
214  */
215 #ifndef HAVE_GETNETBYADDR
216 struct netent *getnetbyaddr(unsigned long net, int type)
217 {
218     errno = ENOSYS;
219     return NULL;
220 }
221 #endif /* defined(HAVE_GETNETBYNAME) */
222
223 /***********************************************************************
224  *              getnetbyname
225  */
226 #ifndef HAVE_GETNETBYNAME
227 struct netent *getnetbyname(const char *name)
228 {
229     errno = ENOSYS;
230     return NULL;
231 }
232 #endif /* defined(HAVE_GETNETBYNAME) */
233
234 /***********************************************************************
235  *              getprotobyname
236  */
237 #ifndef HAVE_GETPROTOBYNAME
238 struct protoent *getprotobyname(const char *name)
239 {
240     errno = ENOSYS;
241     return NULL;
242 }
243 #endif /* !defined(HAVE_GETPROTOBYNAME) */
244
245 /***********************************************************************
246  *              getprotobynumber
247  */
248 #ifndef HAVE_GETPROTOBYNUMBER
249 struct protoent *getprotobynumber(int proto)
250 {
251     errno = ENOSYS;
252     return NULL;
253 }
254 #endif /* !defined(HAVE_GETPROTOBYNUMBER) */
255
256 /***********************************************************************
257  *              getservbyport
258  */
259 #ifndef HAVE_GETSERVBYPORT
260 struct servent *getservbyport(int port, const char *proto)
261 {
262     errno = ENOSYS;
263     return NULL;
264 }
265 #endif /* !defined(HAVE_GETSERVBYPORT) */
266
267 /***********************************************************************
268  *              getsockopt
269  */
270 #ifndef HAVE_GETSOCKOPT
271 int getsockopt(int socket, int level, int option_name,
272                void *option_value, size_t *option_len)
273 {
274     errno = ENOSYS;
275     return -1;
276 }
277 #endif /* !defined(HAVE_GETSOCKOPT) */
278
279 /***********************************************************************
280  *              inet_network
281  */
282 #ifndef HAVE_INET_NETWORK
283 unsigned long inet_network(const char *cp)
284 {
285     errno = ENOSYS;
286     return 0;
287 }
288 #endif /* defined(HAVE_INET_NETWORK) */
289
290 /***********************************************************************
291  *              settimeofday
292  */
293 #ifndef HAVE_SETTIMEOFDAY
294 int settimeofday(struct timeval *tp, void *reserved)
295 {
296     tp->tv_sec = 0;
297     tp->tv_usec = 0;
298
299     errno = ENOSYS;
300     return -1;
301 }
302 #endif /* HAVE_SETTIMEOFDAY */
303
304 /***********************************************************************
305  *              statfs
306  */
307 #ifndef HAVE_STATFS
308 int statfs(const char *name, struct statfs *info)
309 {
310 #ifdef __BEOS__
311     dev_t mydev;
312     fs_info fsinfo;
313     
314     if(!info) {
315         errno = ENOSYS;
316         return -1;
317     }
318
319     if ((mydev = dev_for_path(name)) < 0) {
320         errno = ENOSYS;
321         return -1;
322     }
323
324     if (fs_stat_dev(mydev,&fsinfo) < 0) {
325         errno = ENOSYS;
326         return -1;
327     }
328
329     info->f_bsize = fsinfo.block_size;
330     info->f_blocks = fsinfo.total_blocks;
331     info->f_bfree = fsinfo.free_blocks;
332   
333     return 0;
334 #else /* defined(__BEOS__) */
335     errno = ENOSYS;
336     return -1;
337 #endif /* defined(__BEOS__) */
338 }
339 #endif /* !defined(HAVE_STATFS) */