[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / misc / cbits / writeDescriptor.c
1 #if 0
2 %
3 % (c) The GRASP/AQUA Project, Glasgow University, 1996
4 %
5 \subsection[writeDescriptor.lc]{Stuff bytes down a 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 writeDescriptor(I_ fd, A_ buf, I_ nbytes)
17 {
18     StgInt dumped;
19     
20     while ((dumped = write((int) fd, (char *) buf, (int) nbytes)) < 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 write descriptor";
30               break;
31           case GHC_EDQUOT:
32               ghc_errtype = ERR_RESOURCEEXHAUSTED;
33               ghc_errstr  = "Disk quota exhausted";
34               break;
35           case GHC_EFAULT:
36               ghc_errtype = ERR_INVALIDARGUMENT;
37               ghc_errstr  = "Data not in writeable part of user address space";
38               break;
39           case GHC_EFBIG:
40               ghc_errtype = ERR_RESOURCEEXHAUSTED;
41               ghc_errstr  = "Maximum process or system file size exceeded";
42               break;
43           case GHC_EINVAL:
44               ghc_errtype = ERR_INVALIDARGUMENT;
45               ghc_errstr  = "Seek pointer associated with descriptor negative";
46               break;
47           case GHC_EIO:
48               ghc_errtype = ERR_SYSTEMERROR;
49               ghc_errstr  = "I/O error occurred while writing to file system";
50               break;
51           case GHC_ENOSPC:
52               ghc_errtype = ERR_RESOURCEEXHAUSTED;
53               ghc_errstr  = "No space left on device";
54               break;
55           case GHC_ENXIO:
56               ghc_errtype = ERR_SYSTEMERROR;
57               ghc_errstr  = "Hangup occurred";
58               break;
59           case GHC_EPIPE:
60               ghc_errtype = ERR_SYSTEMERROR;
61               ghc_errstr  = "Write to not read pipe/unconnected socket caught";
62               break;
63           case GHC_ERANGE:
64               ghc_errtype = ERR_INVALIDARGUMENT;
65               ghc_errstr  = "Too much or too little written to descriptor";
66               break;
67           case GHC_EAGAIN:
68           case GHC_EWOULDBLOCK:
69               ghc_errtype = ERR_OTHERERROR;
70               ghc_errstr  = "No data could be written immediately";
71               break;
72           }
73           return -1;
74       }
75     }
76     return dumped;
77 }