[project @ 1998-02-02 17:27:26 by simonm]
[ghc-hetmet.git] / ghc / lib / misc / cbits / getSockName.c
1 #if 0
2 %
3 % (c) The GRASP/AQUA Project, Glasgow University, 1996
4 %
5 \subsection[getSockName.lc]{Return name of process assoc with socket}
6
7 \begin{code}
8 #endif
9
10 #define NON_POSIX_SOURCE
11 #include "rtsdefs.h"
12 #include "ghcSockets.h"
13
14 StgInt
15 getSockName(I_ sockfd, A_ peer, A_ namelen)
16 {
17     StgInt name;
18     
19     while ((name = getsockname((int) sockfd, (struct sockaddr *) peer, (int *) namelen)) < 0) {
20       if (errno != EINTR) {
21           cvtErrno();
22           switch (ghc_errno) {
23           default:
24               stdErrno();
25               break;
26           case GHC_EBADF:
27               ghc_errtype = ERR_INVALIDARGUMENT;
28               ghc_errstr  = "Not a valid write descriptor";
29               break;
30           case GHC_EFAULT:
31               ghc_errtype = ERR_INVALIDARGUMENT;
32               ghc_errstr  = "Data not in writeable part of user address space";
33               break;
34           case GHC_ENOBUFS:
35               ghc_errtype = ERR_RESOURCEEXHAUSTED;
36               ghc_errstr  = "Insuffcient resources";
37               break;
38           case GHC_ENOTSOCK:
39               ghc_errtype = ERR_INVALIDARGUMENT;
40               ghc_errstr  = "Descriptor is not a socket";
41               break;
42           }
43           return -1;
44       }
45     }
46     return name;
47 }