update for HEAD-2003091401
[reactos.git] / lib / crtdll / stdio / filbuf.c
1 /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
3 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
4
5 #include <msvcrt/stdio.h>
6 #include <msvcrt/sys/types.h>
7 #include <msvcrt/stdlib.h>
8 #include <msvcrt/string.h>
9 #include <msvcrt/internal/file.h>
10 #include <msvcrt/io.h>
11 #include <msvcrt/wchar.h>
12 #include <msvcrt/errno.h>
13
14 int _readcnv(int fn, void *buf, size_t siz  );
15
16 /*
17  * @implemented
18  */
19 int
20 _filbuf(FILE *f)
21 {
22   int size;
23   char c;
24
25  
26   if ( !OPEN4READING(f)) {
27         __set_errno (EINVAL);
28         return EOF;
29   }
30
31
32   if (f->_flag&(_IOSTRG|_IOEOF))
33     return EOF;
34   f->_flag &= ~_IOUNGETC;
35
36   if (f->_base==NULL && (f->_flag&_IONBF)==0) {
37     size = 4096;
38     if ((f->_base = malloc(size+1)) == NULL)
39     {
40         // error ENOMEM
41       f->_flag |= _IONBF;
42       f->_flag &= ~(_IOFBF|_IOLBF);
43     }
44     else
45     {
46       f->_flag |= _IOMYBUF;
47       f->_bufsiz = size;
48     }
49   }
50
51
52   if (f->_flag&_IONBF)
53     f->_base = &c;
54
55
56 // fush stdout before reading from stdin 
57   if (f == stdin) {
58     if (stdout->_flag&_IOLBF)
59       fflush(stdout);
60     if (stderr->_flag&_IOLBF)
61       fflush(stderr);
62   }
63
64 // if we have a dirty stream we flush it
65   if ( (f->_flag &_IODIRTY) == _IODIRTY )
66          fflush(f);
67
68
69
70   f->_cnt = _read(fileno(f), f->_base, f->_flag & _IONBF ? 1 : f->_bufsiz  );
71   f->_flag |= _IOAHEAD;
72
73   if(__is_text_file(f) && f->_cnt>0)
74   {
75     /* truncate text file at Ctrl-Z */
76     char *cz=memchr(f->_base, 0x1A, f->_cnt);
77     if(cz)
78     {
79       int newcnt = cz - f->_base;
80       lseek(fileno(f), -(f->_cnt - newcnt), SEEK_CUR);
81       f->_cnt = newcnt;
82     }
83   }
84
85   f->_ptr = f->_base;
86
87   if (f->_flag & _IONBF) 
88      f->_base = NULL; // statically allocated buffer for sprintf
89   
90
91 //check for error
92   if (--f->_cnt < 0) {
93     if (f->_cnt == -1) {
94       f->_flag |= _IOEOF;
95     } else
96       f->_flag |= _IOERR;
97     f->_cnt = 0;
98
99 // should set errno 
100
101     return EOF;
102   }
103
104   f->_cnt--;
105   return *f->_ptr++ & 0377;
106 }
107
108 wint_t  _filwbuf(FILE *fp)
109 {
110         return (wint_t )_filbuf(fp);
111 }
112
113 // convert the carriage return line feed pairs
114
115 int _readcnv(int fn, void *buf, size_t siz  )
116 {
117         char *bufp = (char *)buf;
118         int _bufsiz = siz;
119         int cr = 0;
120         int n;
121
122         n = _read(fn, buf, siz  );
123
124         while (_bufsiz > 0) {
125                 if (*bufp == '\r') 
126                         cr++;
127                 else if ( cr != 0 ) 
128                         *bufp = *(bufp + cr);
129                 bufp++;
130                 _bufsiz--;
131         }
132         return n + cr;
133 }
134