update for HEAD-2003021201
[reactos.git] / lib / crtdll / stdio / fgetws.c
1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <msvcrt/stdio.h>
3 #include <msvcrt/internal/file.h>
4
5 //wchar_t* fgetws(wchar_t* wcaBuffer, int nBufferSize, FILE* fileRead);
6 //char* fgets(char *s, int n, FILE *f);
7 //int _getw(FILE *stream);
8 /*
9 // Read a word (int) from STREAM.
10 int _getw(FILE *stream)
11 {
12   int w;
13
14   // Is there a better way?
15   if (fread( &w, sizeof(w), 1, stream) != 1)
16     return(EOF);
17   return(w);
18 }
19
20 //getc can be a macro
21 #undef getc
22
23 int getc(FILE *fp)
24 {
25     int c = -1;
26 // check for invalid stream
27         if ( !__validfp (fp) ) {
28                 __set_errno(EINVAL);
29                 return EOF;
30         }
31 // check for read access on stream
32         if ( !OPEN4READING(fp) ) {
33                 __set_errno(EINVAL);
34                 return -1;
35         }
36         if(fp->_cnt > 0) {
37                 fp->_cnt--;
38                 c =  (int)*fp->_ptr++;
39         } 
40         else {
41                 c =  _filbuf(fp);
42         }
43         return c;
44 }
45  */
46
47 wchar_t* fgetws(wchar_t* s, int n, FILE* f)
48 {
49   int c = 0;
50   wchar_t* cs;
51
52   cs = s;
53   while (--n > 0 && (c = _getw(f)) != EOF) {
54     *cs++ = c;
55     if (c == L'\n')
56       break;
57   }
58   if (c == EOF && cs == s) {
59     return NULL;
60   }
61   *cs++ = L'\0';
62   return s;
63 }