getospace.c (1021B)
1 2 #include <unistd.h> 3 #include <errno.h> 4 5 #include "libvxc/private.h" 6 #include "ioprivate.h" 7 8 9 static char stdoutbuf[BUFSIZ]; 10 static char stderrbuf[BUFSIZ]; 11 12 FILE __stdout = {STDOUT_FILENO, _IOLBF}; 13 FILE __stderr = {STDERR_FILENO, _IOLBF}; 14 15 16 static void flushstdout() 17 { 18 fflush(stdout); 19 fflush(stderr); 20 } 21 22 // Get some output buffer space, 23 // allocating a new output buffer or flushing the existing one if necessary. 24 int __getospace(FILE *f) 25 { 26 if (f->obuf == NULL) { 27 // No output buffer at all (yet). 28 29 // Give stdout and stderr static buffers, 30 // so that we can get printf without pulling in malloc. 31 if (f == stdout || f == stderr) { 32 33 stdout->obuf = stdoutbuf; 34 stdout->omax = BUFSIZ; 35 stderr->obuf = stderrbuf; 36 stderr->omax = BUFSIZ; 37 __exit_flush = flushstdout; 38 39 } else { 40 41 // File must not be open for writing. 42 errno = EBADF; 43 f->errflag = 1; 44 return EOF; 45 } 46 47 } else { 48 49 // We have an output buffer but it may be full - flush it. 50 if (fflush(f) < 0) 51 return EOF; 52 } 53 54 return 0; 55 } 56