prepare move old cruft
[reactos.git] / reactos / lib / crtdll / stdio / setvbuf.c
1 /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
3 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
4 #include <msvcrt/stdio.h>
5 #include <msvcrt/stdlib.h>
6 #include <msvcrt/io.h>
7 #include <msvcrt/errno.h>
8 #include <msvcrt/internal/file.h>
9
10
11 /*
12 * @implemented
13 */
14 int setvbuf(FILE *f, char *buf, int type, size_t len)
15 {
16 int mine=0;
17 if (!__validfp (f) ) {
18 __set_errno (EINVAL);
19 return 0;
20 }
21 if ( f->_base != NULL )
22 fflush(f);
23 switch (type)
24 {
25 case _IOFBF:
26 case _IOLBF:
27 if (len <= 0) {
28 __set_errno (EINVAL);
29 return EOF;
30 }
31 if (buf == 0)
32 {
33 buf = (char *)malloc(len+1);
34 if (buf == NULL) {
35 __set_errno (ENOMEM);
36 return -1;
37 }
38 mine = 1;
39 }
40 /* FALLTHROUGH */
41 case _IONBF:
42 if (f->_base != NULL && f->_flag & _IOMYBUF)
43 free(f->_base);
44 f->_cnt = 0;
45
46 f->_flag &= ~(_IONBF|_IOFBF|_IOLBF|_IOUNGETC);
47 f->_flag |= type;
48 if (type != _IONBF)
49 {
50 if (mine)
51 f->_flag |= _IOMYBUF;
52 f->_ptr = f->_base = buf;
53 f->_bufsiz = len;
54 }
55 else
56 {
57 f->_base = 0;
58 f->_bufsiz = 0;
59 }
60 return 0;
61 default:
62 __set_errno (EINVAL);
63 return EOF;
64 }
65 }