update for HEAD-2003091401
[reactos.git] / lib / msvcrt / stdio / gets.c
1 /* $Id$
2  *
3  *  ReactOS msvcrt library
4  *
5  *  gets.c
6  *
7  *  Copyright (C) 2002  Robert Dickenson <robd@reactos.org>
8  *
9  *  Based on original work Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
10  *                         28/12/1998: Appropriated for Reactos
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
27
28 #include <msvcrt/stdio.h>
29
30 char* gets(char* s)
31 {
32     int c;
33     char* cs;
34
35     cs = s;
36     while ((c = getc(stdin)) != '\n' && c != EOF)
37         *cs++ = c;
38     if (c == EOF && cs == s)
39         return NULL;
40     *cs++ = '\0';
41     return s;
42 }
43
44 #ifndef WEOF
45 #define WEOF    (wchar_t)(0xFFFF)
46 #endif
47
48 /*
49  * Get a line from the stdin stream.
50  *
51  * @implemented
52  */
53 wchar_t* _getws(wchar_t* s)
54 {
55     wchar_t c;
56     wchar_t* cs;
57
58     cs = s;
59     while ((c = getwc(stdin)) != L'\n' && c != WEOF)
60         *cs++ = c;
61     if (c == WEOF && cs == s)
62         return NULL;
63     *cs++ = L'\0';
64     return s;
65 }
66
67 #if 0
68 /* Copyright (C) 1991, 1994, 1995, 1996 Free Software Foundation, Inc.
69 This file is part of the GNU C Library.
70
71 The GNU C Library is free software; you can redistribute it and/or
72 modify it under the terms of the GNU Library General Public License as
73 published by the Free Software Foundation; either version 2 of the
74 License, or (at your option) any later version.
75
76 The GNU C Library is distributed in the hope that it will be useful,
77 but WITHOUT ANY WARRANTY; without even the implied warranty of
78 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
79 Library General Public License for more details.
80
81 You should have received a copy of the GNU Library General Public
82 License along with the GNU C Library; see the file COPYING.LIB.  If
83 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
84 Cambridge, MA 02139, USA.  */
85
86 #include <stdio.h>
87 #include <errno.h>
88 #include <string.h>
89
90 link_warning (gets, "the `gets' function is dangerous and should not be used.")
91
92
93 /* Read a newline-terminated multibyte string from stdin into S,
94    removing the trailing newline.  Return S or NULL.  */
95   
96 char *
97 gets (s)
98      char *s;
99 {
100   register char *p = s;
101   register int c;
102   FILE *stream = stdin;
103   int l;
104
105   if (!__validfp (stream) || p == NULL)
106     {
107       __set_errno (EINVAL);
108       return NULL;
109     }
110
111   if (feof (stream) || ferror (stream))
112     return NULL;
113
114   while ((c = getc(stdin)) != EOF) {
115     if (c == '\n')
116         break;
117     if ( isascii(c) ) 
118         *cs++ = c;
119 #ifdef _MULTIBYTE
120     else if ( isleadbyte(c) ) {
121         l = mblen(c);
122         while(l > 0 ) {
123                 c = getchar();
124                 if ( isleadbyte(c) || c == EOF )
125                         return NULL; // encoding error
126                 *cs++ = c;
127                 l--;
128         }
129     }
130 #endif
131     else
132         return NULL; // suspicious input
133   }
134
135   *p = '\0';
136
137   /* Return null if we had an error, or if we got EOF
138      before writing any characters.  */
139
140   if (ferror (stream) || (feof (stream) && p == s))
141     return NULL;
142
143   return s;
144 }
145   
146 #endif