[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / misc / cbits / acceptSocket.c
1 #if 0
2 %
3 % (c) The GRASP/AQUA Project, Glasgow University, 1996
4 %
5 \subsection[acceptSocket.lc]{Server wait for client to connect}
6
7 \begin{code}
8 #endif
9
10 #define NON_POSIX_SOURCE
11 #include "Rts.h"
12 #include "ghcSockets.h"
13 #include "stgio.h"
14
15 StgInt
16 acceptSocket(I_ sockfd, A_ peer, A_ addrlen)
17 {
18     StgInt fd;
19     
20     while ((fd = accept((int)sockfd, (struct sockaddr *)peer, (int *)addrlen)) < 0) {
21       if (errno != EINTR) {
22           cvtErrno();
23           switch (ghc_errno) {
24           default:
25               stdErrno();
26               break;
27           case GHC_EBADF:
28               ghc_errtype = ERR_INVALIDARGUMENT;
29               ghc_errstr  = "Not a valid descriptor";
30               break;
31           case GHC_EFAULT:
32               ghc_errtype = ERR_INVALIDARGUMENT;
33               ghc_errstr  = "Address not in writeable part of user address space";
34               break;
35           case GHC_ENOTSOCK:
36               ghc_errtype = ERR_INVALIDARGUMENT;
37               ghc_errstr  = "Descriptor not a socket";
38               break;
39           case GHC_EOPNOTSUPP:
40               ghc_errtype = ERR_INVALIDARGUMENT;
41               ghc_errstr  = "Socket not of type that supports listen";
42               break;
43           case GHC_EWOULDBLOCK:
44               ghc_errtype = ERR_OTHERERROR;
45               ghc_errstr  = "No sockets are present to be accepted";
46               break;
47           }
48           return -1;
49       }
50     }
51     return fd;
52 }