vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

getinput.c (965B)


      1 
      2 #include <assert.h>
      3 #include <unistd.h>
      4 #include <errno.h>
      5 
      6 #include "libvxc/private.h"
      7 #include "ioprivate.h"
      8 
      9 
     10 static char stdinbuf[BUFSIZ];
     11 
     12 FILE __stdin = {STDIN_FILENO};
     13 
     14 
     15 // Prefetch some input data if possible,
     16 // allocating a new input buffer if necessary.
     17 // Assumes the input buffer is currently empty.
     18 int __getinput(FILE *f)
     19 {
     20 	if (f->ibuf == NULL) {
     21 		// No input buffer at all (yet).
     22 
     23 		// Give stdin a static buffer,
     24 		// so that we can read stdin without pulling in malloc.
     25 		if (f == stdin) {
     26 
     27 			stdin->ibuf = stdinbuf;
     28 			stdin->imax = BUFSIZ;
     29 
     30 		} else {
     31 
     32 			// File must not be open for writing.
     33 			errno = EBADF;
     34 			f->errflag = 1;
     35 			return EOF;
     36 		}
     37 	}
     38 
     39 	// Update offset.
     40 	f->ioffset += f->ilim;
     41 	f->ipos = 0;
     42 	f->ilim = 0;
     43 
     44 	// Read some input data
     45 	ssize_t rc = read(f->fd, f->ibuf, f->imax);
     46 	if (rc < 0) {
     47 		f->errflag = 1;
     48 		return EOF;
     49 	}
     50 	if (rc == 0) {
     51 		f->eofflag = 1;
     52 		return EOF;
     53 	}
     54 
     55 	f->ipos = 0;
     56 	f->ilim = rc;
     57 	return 0;
     58 }
     59