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