9os

Experimental kernel using plan9 ideas for embedded device
git clone git://git.simple-cc.org/9os
Log | Files | Refs | README | LICENSE

setvbuf.c (741B)


      1 #include <errno.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 
      5 #include "../libc.h"
      6 #undef setvbuf
      7 
      8 int
      9 setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size)
     10 {
     11 	int flags;
     12 	char *p;
     13 	size_t l;
     14 
     15 	if (_flsbuf(fp) == EOF)
     16 		return EOF;
     17 
     18 	if (buf)
     19 		p = buf, l = size;
     20 	else
     21 		p = fp->buf, l = fp->len;
     22 
     23 	switch (mode) {
     24 	case _IONBF:
     25 		l = sizeof(fp->unbuf);
     26 		p = fp->unbuf;
     27 	case _IOLBF:
     28 	case _IOFBF:
     29 		fp->rp = fp->wp = p;
     30 		fp->lp = p + l;
     31 		break;
     32 	default:
     33 		errno = EINVAL;
     34 		return EOF;
     35 	}
     36 
     37 	flags = fp->flags;
     38 	if (flags&_IOALLOC && (buf || mode == _IONBF)) {
     39 		free(fp->buf);
     40 		flags &= ~_IOALLOC;
     41 	}
     42 
     43 	fp->buf = p;
     44 	fp->len = l;
     45 	flags &= ~(_IONBF | _IOLBF | _IOFBF);
     46 	flags |= mode;
     47 	fp->flags = flags;
     48 
     49 	return 0;
     50 }