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