update for HEAD-2003091401
[reactos.git] / lib / crtdll / stdio / gets.c
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS system libraries
4  * FILE:        lib/crtdll/stdio/gets.c
5  * PURPOSE:     Get a character string from stdin
6  * PROGRAMER:   DJ Delorie
7  * UPDATE HISTORY:
8  *              28/12/98: Appropriated for Reactos
9  */
10 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
11 #include <msvcrt/stdio.h>
12
13 /*
14  * @implemented
15  */
16 char* gets(char* s)
17 {
18   int c;
19   char *cs;
20
21   cs = s;
22   while ((c = getchar()) != '\n' && c != EOF)
23     *cs++ = c;
24   if (c == EOF && cs==s)
25     return NULL;
26   *cs++ = '\0';
27   return s;
28 }
29
30 #if 0
31 /* Copyright (C) 1991, 1994, 1995, 1996 Free Software Foundation, Inc.
32 This file is part of the GNU C Library.
33
34 The GNU C Library is free software; you can redistribute it and/or
35 modify it under the terms of the GNU Library General Public License as
36 published by the Free Software Foundation; either version 2 of the
37 License, or (at your option) any later version.
38
39 The GNU C Library is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
42 Library General Public License for more details.
43
44 You should have received a copy of the GNU Library General Public
45 License along with the GNU C Library; see the file COPYING.LIB.  If
46 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
47 Cambridge, MA 02139, USA.  */
48
49 #include <stdio.h>
50 #include <errno.h>
51 #include <string.h>
52
53 link_warning (gets, "the `gets' function is dangerous and should not be used.")
54
55
56 /* Read a newline-terminated multibyte string from stdin into S,
57    removing the trailing newline.  Return S or NULL.  */
58   
59 char *
60 gets (s)
61      char *s;
62 {
63   register char *p = s;
64   register int c;
65   FILE *stream = stdin;
66   int l;
67
68   if (!__validfp (stream) || p == NULL)
69     {
70       __set_errno (EINVAL);
71       return NULL;
72     }
73
74   if (feof (stream) || ferror (stream))
75     return NULL;
76
77   while ((c = getc(stdin)) != EOF) {
78     if (c == '\n')
79         break;
80     if ( isascii(c) ) 
81         *cs++ = c;
82 #ifdef _MULTIBYTE
83     else if ( isleadbyte(c) ) {
84         l = mblen(c);
85         while(l > 0 ) {
86                 c = getchar();
87                 if ( isleadbyte(c) || c == EOF )
88                         return NULL; // encoding error
89                 *cs++ = c;
90                 l--;
91         }
92     }
93 #endif
94     else
95         return NULL; // suspicious input
96   }
97
98   *p = '\0';
99
100   /* Return null if we had an error, or if we got EOF
101      before writing any characters.  */
102
103   if (ferror (stream) || (feof (stream) && p == s))
104     return NULL;
105
106   return s;
107 }
108   
109 #endif