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