~ubuntu-branches/ubuntu/feisty/elinks/feisty-updates

« back to all changes in this revision

Viewing changes to src/osdep/beos/overrides.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Gervai
  • Date: 2004-01-21 22:13:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040121221345-ju33hai1yhhqt6kn
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* BeOS system-specific routines emulating POSIX. */
 
2
/* $Id: overrides.c,v 1.1 2003/10/27 23:46:55 pasky Exp $ */
 
3
 
 
4
/* Note that this file is currently unmaintained and basically dead. Noone
 
5
 * cares about BeOS support, apparently. This file may yet survive for some
 
6
 * time, but it will probably be removed if noone will adopt it. */
 
7
 
 
8
#ifdef HAVE_CONFIG_H
 
9
#include "config.h"
 
10
#endif
 
11
 
 
12
#define BEOS_SELF
 
13
 
 
14
#include "osdep/system.h"
 
15
 
 
16
#ifdef BEOS
 
17
 
 
18
#include <errno.h>
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <unistd.h>
 
22
#include <fcntl.h>
 
23
#include <sys/types.h>
 
24
#include <sys/socket.h>
 
25
#include <netinet/in.h>
 
26
#include <be/kernel/OS.h>
 
27
 
 
28
#include "elinks.h"
 
29
 
 
30
#include "osdep/beos/overrides.h"
 
31
 
 
32
 
 
33
#define SHS     128
 
34
 
 
35
int
 
36
be_read(int s, void *ptr, int len)
 
37
{
 
38
        if (s >= SHS) return recv(s - SHS, ptr, len, 0);
 
39
        return read(s, ptr, len);
 
40
}
 
41
 
 
42
int
 
43
be_write(int s, void *ptr, int len)
 
44
{
 
45
        if (s >= SHS) return send(s - SHS, ptr, len, 0);
 
46
        return write(s, ptr, len);
 
47
}
 
48
 
 
49
int
 
50
be_close(int s)
 
51
{
 
52
        if (s >= SHS) return closesocket(s - SHS);
 
53
        return close(s);
 
54
}
 
55
 
 
56
int
 
57
be_socket(int af, int sock, int prot)
 
58
{
 
59
        int h = socket(af, sock, prot);
 
60
 
 
61
        if (h < 0) return h;
 
62
        return h + SHS;
 
63
}
 
64
 
 
65
int
 
66
be_connect(int s, struct sockaddr *sa, int sal)
 
67
{
 
68
        return connect(s - SHS, sa, sal);
 
69
}
 
70
 
 
71
int
 
72
be_getpeername(int s, struct sockaddr *sa, int *sal)
 
73
{
 
74
        return getpeername(s - SHS, sa, sal);
 
75
}
 
76
 
 
77
int
 
78
be_getsockname(int s, struct sockaddr *sa, int *sal)
 
79
{
 
80
        return getsockname(s - SHS, sa, sal);
 
81
}
 
82
 
 
83
int
 
84
be_listen(int s, int c)
 
85
{
 
86
        return listen(s - SHS, c);
 
87
}
 
88
 
 
89
int
 
90
be_accept(int s, struct sockaddr *sa, int *sal)
 
91
{
 
92
        int a = accept(s - SHS, sa, sal);
 
93
 
 
94
        if (a < 0) return -1;
 
95
        return a + SHS;
 
96
}
 
97
 
 
98
int
 
99
be_bind(int s, struct sockaddr *sa, int sal)
 
100
{
 
101
#if 0
 
102
        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
 
103
 
 
104
        if (!sin->sin_port) {
 
105
                int i;
 
106
                for (i = 16384; i < 49152; i++) {
 
107
                        sin->sin_port = i;
 
108
                        if (!be_bind(s, sa, sal)) return 0;
 
109
                }
 
110
                return -1;
 
111
        }
 
112
#endif
 
113
        if (bind(s - SHS, sa, sal)) return -1;
 
114
        getsockname(s - SHS, sa, &sal);
 
115
        return 0;
 
116
}
 
117
 
 
118
#define PIPE_RETRIES    10
 
119
 
 
120
int
 
121
be_pipe(int *fd)
 
122
{
 
123
        int s1, s2, s3, l;
 
124
        struct sockaddr_in sa1, sa2;
 
125
        int retry_count = 0;
 
126
 
 
127
again:
 
128
        s1 = be_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
129
        if (s1 < 0) {
 
130
                /*perror("socket1");*/
 
131
                goto fatal_retry;
 
132
        }
 
133
 
 
134
        s2 = be_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
135
        if (s2 < 0) {
 
136
                /*perror("socket2");*/
 
137
                be_close(s1);
 
138
                goto fatal_retry;
 
139
        }
 
140
        memset(&sa1, 0, sizeof(sa1));
 
141
        sa1.sin_family = AF_INET;
 
142
        sa1.sin_port = 0;
 
143
        sa1.sin_addr.s_addr = INADDR_ANY;
 
144
        if (be_bind(s1, (struct sockaddr *)&sa1, sizeof(sa1))) {
 
145
                /*perror("bind");*/
 
146
 
 
147
clo:
 
148
                be_close(s1);
 
149
                be_close(s2);
 
150
                goto fatal_retry;
 
151
        }
 
152
        if (be_listen(s1, 1)) {
 
153
                /*perror("listen");*/
 
154
                goto clo;
 
155
        }
 
156
        if (be_connect(s2, (struct sockaddr *)&sa1, sizeof(sa1))) {
 
157
                /*perror("connect");*/
 
158
                goto clo;
 
159
        }
 
160
        l = sizeof(sa2);
 
161
        if ((s3 = be_accept(s1, (struct sockaddr *)&sa2, &l)) < 0) {
 
162
                /*perror("accept");*/
 
163
                goto clo;
 
164
        }
 
165
        be_getsockname(s3, (struct sockaddr *)&sa1, &l);
 
166
        if (sa1.sin_addr.s_addr != sa2.sin_addr.s_addr) {
 
167
                be_close(s3);
 
168
                goto clo;
 
169
        }
 
170
        be_close(s1);
 
171
        fd[0] = s2;
 
172
        fd[1] = s3;
 
173
        return 0;
 
174
 
 
175
fatal_retry:
 
176
        if (++retry_count > PIPE_RETRIES) return -1;
 
177
        sleep(1);
 
178
        goto again;
 
179
}
 
180
 
 
181
int
 
182
be_select(int n, struct fd_set *rd, struct fd_set *wr, struct fd_set *exc,
 
183
          struct timeval *tm)
 
184
{
 
185
        int i, s;
 
186
        struct fd_set d, rrd;
 
187
 
 
188
        FD_ZERO(&d);
 
189
        if (!rd) rd = &d;
 
190
        if (!wr) wr = &d;
 
191
        if (!exc) exc = &d;
 
192
        if (n >= FDSETSIZE) n = FDSETSIZE;
 
193
        FD_ZERO(exc);
 
194
        for (i = 0; i < n; i++) if ((i < SHS && FD_ISSET(i, rd)) || FD_ISSET(i, wr)) {
 
195
                for (i = SHS; i < n; i++) FD_CLR(i, rd);
 
196
                return MAXINT;
 
197
        }
 
198
        FD_ZERO(&rrd);
 
199
        for (i = SHS; i < n; i++) if (FD_ISSET(i, rd)) FD_SET(i - SHS, &rrd);
 
200
        if ((s = select(FD_SETSIZE, &rrd, &d, &d, tm)) < 0) {
 
201
                FD_ZERO(rd);
 
202
                return 0;
 
203
        }
 
204
        FD_ZERO(rd);
 
205
        for (i = SHS; i < n; i++) if (FD_ISSET(i - SHS, &rrd)) FD_SET(i, rd);
 
206
        return s;
 
207
}
 
208
 
 
209
#ifndef SO_ERROR
 
210
#define SO_ERROR        10001
 
211
#endif
 
212
 
 
213
int
 
214
be_getsockopt(int s, int level, int optname, void *optval, int *optlen)
 
215
{
 
216
        if (optname == SO_ERROR && *optlen >= sizeof(int)) {
 
217
                *(int *)optval = 0;
 
218
                *optlen = sizeof(int);
 
219
                return 0;
 
220
        }
 
221
        return -1;
 
222
}
 
223
 
 
224
#endif