[project @ 1998-04-07 08:23:07 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / writeFile.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[writeFile.lc]{hPutStr Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 StgInt
12 writeFile(buf, fp, bytes)
13 StgAddr buf;
14 StgForeignObj fp;
15 StgInt bytes;
16 {
17     int count;
18     char *p = (char *) buf;
19
20     if (bytes == 0)
21         return 0;
22
23     /* Disallow short writes */
24     while ((count = fwrite(p, 1, bytes, (FILE *) fp)) < bytes) {
25         if (errno != EINTR) {
26             cvtErrno();
27             stdErrno();
28             return -1;
29         }
30         bytes -= count;
31         p += count;
32         clearerr((FILE *) fp);
33     }
34
35     return 0;
36 }
37
38
39 StgInt
40 writeBuf(fp, elt_sz, len, buf)
41 StgForeignObj fp;
42 StgWord elt_sz;
43 StgInt  len;
44 StgAddr buf;
45 {
46     int count;
47     char *p = (char *) buf;
48
49     if (len == 0 || elt_sz == 0)
50         return 0;
51
52     /* Disallow short writes */
53     while ((count = fwrite((char *)buf, (unsigned)elt_sz, (int)len, (FILE *) fp)) < len) {
54         if (errno != EINTR) {
55             cvtErrno();
56             stdErrno();
57             return -1;
58         }
59         len -= count;
60         p += count;
61         clearerr((FILE *) fp);
62     }
63
64     return 0;
65 }
66
67 \end{code}