[project @ 1999-09-16 13:14:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / misc / cbits / createSocket.c
1 #if 0
2 %
3 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 %
5 \subsection[createSocket.lc]{Create a socket file descriptor}
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 createSocket(I_ family, I_ type, I_ protocol)
17 {
18     int fd;
19     long flags;
20
21     if ((fd = socket((int)family, (int)type, (int)protocol)) < 0) {
22       if (errno != EINTR) {
23           cvtErrno();
24           switch (ghc_errno) {
25           default:
26               stdErrno();
27               break;
28           case GHC_EACCES:
29               ghc_errtype = ERR_PERMISSIONDENIED;
30               ghc_errstr  = "cannot create socket";
31               break;
32           case GHC_EMFILE:
33               ghc_errtype = ERR_RESOURCEEXHAUSTED;
34               ghc_errstr  = "Too many open files";
35               break;
36           case GHC_ENFILE:
37               ghc_errtype = ERR_RESOURCEEXHAUSTED;
38               ghc_errstr  = "System file table overflow";
39               break;
40           case GHC_EPROTONOSUPPORT:
41               ghc_errtype = ERR_UNSUPPORTEDOPERATION;
42               ghc_errstr  = "Protocol type not supported";
43               break;
44           case GHC_EPROTOTYPE:
45               ghc_errtype = ERR_INAPPROPRIATETYPE;
46               ghc_errstr  = "Protocol wrong type for socket";
47               break;
48           }
49           return (StgInt)-1;
50       }
51     }
52
53     /* set the non-blocking flag on this file descriptor */
54     flags = fcntl(fd, F_GETFL);
55     fcntl(fd, F_SETFL, flags | O_NONBLOCK);
56
57     return (StgInt)fd;
58 }