59aa2b2ff316c7f2cb1bf2e47d6ff32d151d0c48
[ghc-hetmet.git] / ghc / lib / std / cbits / writeError.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1998
3  *
4  * $Id: writeError.c,v 1.4 1999/11/09 10:46:27 simonmar Exp $
5  *
6  * hPutStr Runtime Support
7  */
8
9 /*
10 Writing out error messages. This is done outside Haskell
11 (i.e., no use of the IO implementation is made), since it
12 might be in an unstable state (e.g., hClose stderr >> error "foo")
13
14 (A secondary reason is that ``error'' is used by the IO
15 implementation in one or two places.)
16
17 */
18
19 #include "Rts.h"
20 #include "RtsUtils.h"
21 #include "stgio.h"
22
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
26
27 void
28 writeErrString__ (msg_hdr, msg, len)
29 StgAddr msg_hdr;
30 StgByteArray msg;
31 StgInt len;
32 {
33   int count = 0;
34   char* p  = (char*)msg;
35   char  nl = '\n';
36
37   resetNonBlockingFd(2);
38
39   /* Print error msg header */
40   if (msg_hdr) {
41     ((void (*)(int))msg_hdr)(2/*stderr*/);
42   }
43
44   while ( (count = write(2,p,len)) < len) {
45      if (errno != EINTR ) {
46         return;
47      }
48      len -= count;
49      p   += count;
50   }
51   write(2, &nl, 1);
52 }