[project @ 1999-09-16 13:14:38 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / cbits / filePutc.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: filePutc.c,v 1.8 1999/09/16 13:14:43 simonmar Exp $
5  *
6  * hPutChar Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11 #include "error.h"
12
13 #if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
14 #define USE_WINSOCK
15 #endif
16
17 #ifdef USE_WINSOCK
18 #include <winsock.h>
19 #endif
20
21 #define TERMINATE_LINE(x)   ((x) == '\n')
22
23 StgInt
24 filePutc(ptr, c)
25 StgForeignPtr ptr;
26 StgChar c;
27 {
28     IOFileObject* fo = (IOFileObject*)ptr;
29     int rc = 0;
30
31     /* What filePutc needs to do:
32
33          - if there's no buffering => write it out.
34          - if the buffer is line-buffered
35                 write out buffer (+char), iff buffer would be full afterwards ||
36                                               new char is the newline character
37                 add to buffer , otherwise
38          - if the buffer is fully-buffered
39                write out buffer (+char), iff adding char fills up buffer.
40                add char to buffer, otherwise.
41
42      In the cases where a file is buffered, the invariant is that operations
43      that fill up a buffer also flushes them. A consequence of this here, is 
44      that we're guaranteed to be passed a buffer with space for (at least)
45      the one char we're adding.
46
47      Supporting RW objects adds yet another twist, since we have to make
48      sure that if such objects have been read from just previously, we
49      flush(i.e., empty) the buffer first. (We could be smarter about this,
50      but aren't!)
51
52     */
53
54     if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) {
55         rc = flushReadBuffer(ptr);
56         if (rc<0) return rc;
57     }
58
59     fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE;
60               
61     /* check whether we can just add it to the buffer.. */
62     if ( FILEOBJ_UNBUFFERED(fo) ) {
63         ; 
64     } else {
65         /* We're buffered, add it to the pack */
66        ((char*)fo->buf)[fo->bufWPtr] = (char)c;
67        fo->bufWPtr++;
68       /* If the buffer filled up as a result, *or*
69          the added character terminated a line
70             => flush.
71       */
72       if ( FILEOBJ_BUFFER_FULL(fo) || 
73            (FILEOBJ_LINEBUFFERED(fo) && TERMINATE_LINE(c)) ) {
74         rc = writeBuffer(ptr, fo->bufWPtr);
75         /* Undo the write if we're blocking..*/
76         if (rc == FILEOBJ_BLOCKED_WRITE ) fo->bufWPtr--;
77       }
78       return rc;
79     }
80
81     /* Unbuffered, write the character directly. */
82     while (rc = (
83 #ifdef USE_WINSOCK
84                  fo->flags & FILEOBJ_WINSOCK ?
85                  send(fo->fd, &c, 1, 0) :
86                  write(fo->fd, &c, 1)) <= 0) {
87 #else
88                  write(fo->fd, &c, 1)) <= 0) {
89 #endif
90
91         if ( rc == -1 && errno == EAGAIN) {
92             errno = 0;
93             return FILEOBJ_BLOCKED_WRITE;
94         } else if (rc == 0 || rc == -1 && errno != EINTR) {
95             cvtErrno();
96             stdErrno();
97             return -1;
98         }
99     }
100
101     return 0;
102 }