Files
glibc/stdio/gets.c
T

60 lines
1.7 KiB
C
Raw Normal View History

1997-02-15 04:31:36 +00:00
/* Copyright (C) 1991, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
1995-02-18 01:27:10 +00:00
#include <stdio.h>
#include <errno.h>
#include <string.h>
1996-10-13 21:35:39 +00:00
link_warning (gets, "the `gets' function is dangerous and should not be used.")
1995-02-18 01:27:10 +00:00
/* Read a newline-terminated string from stdin into S,
removing the trailing newline. Return S or NULL. */
char *
1996-09-27 03:45:24 +00:00
gets (s)
char *s;
1995-02-18 01:27:10 +00:00
{
register char *p = s;
register int c;
FILE *stream = stdin;
1996-09-27 03:45:24 +00:00
if (!__validfp (stream) || p == NULL)
1995-02-18 01:27:10 +00:00
{
1996-09-27 03:45:24 +00:00
__set_errno (EINVAL);
1995-02-18 01:27:10 +00:00
return NULL;
}
1996-09-27 03:45:24 +00:00
if (feof (stream) || ferror (stream))
1995-02-18 01:27:10 +00:00
return NULL;
1996-09-27 03:45:24 +00:00
while ((c = getchar ()) != EOF)
1995-02-18 01:27:10 +00:00
if (c == '\n')
break;
else
*p++ = c;
*p = '\0';
/* Return null if we had an error, or if we got EOF
before writing any characters. */
if (ferror (stream) || (feof (stream) && p == s))
return NULL;
return s;
}