[project @ 1996-06-27 16:13:29 by partain]
[ghc-hetmet.git] / ghc / runtime / io / createSocket.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[createSocket.lc]{Create a socket file descriptor}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_SYS_SOCKET_H
16 #include <sys/socket.h>
17 #endif
18
19 StgInt
20 createSocket(I_ family, I_ type, I_ protocol)
21 {
22     int fd;
23
24     if ((fd = socket((int)family, (int)type, (int)protocol)) < 0) {
25       if (errno != EINTR) {
26           cvtErrno();
27           switch (ghc_errno) {
28           default:
29               stdErrno();
30               break;
31           case GHC_EACCES:
32               ghc_errtype = ERR_PERMISSIONDENIED;
33               ghc_errstr  = "cannot create socket";
34               break;
35           case GHC_EMFILE:
36               ghc_errtype = ERR_RESOURCEEXHAUSTED;
37               ghc_errstr  = "Too many open files";
38               break;
39           case GHC_ENFILE:
40               ghc_errtype = ERR_RESOURCEEXHAUSTED;
41               ghc_errstr  = "System file table overflow";
42               break;
43           case GHC_EPROTONOSUPPORT:
44               ghc_errtype = ERR_UNSUPPORTEDOPERATION;
45               ghc_errstr  = "Protocol type not supported";
46               break;
47           case GHC_EPROTOTYPE:
48               ghc_errtype = ERR_INAPPROPRIATETYPE;
49               ghc_errstr  = "Protocol wrong type for socket";
50               break;
51           }
52           return (StgInt)-1;
53       }
54     }
55     return (StgInt)fd;
56 }
57
58 \end{code}