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