1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 /*
19 FUNCTION
20 <<gets>>---get character string (obsolete, use <<fgets>> instead)
21 
22 INDEX
23 	gets
24 INDEX
25 	_gets_r
26 
27 SYNOPSIS
28         #include <stdio.h>
29 
30 	char *gets(char *<[buf]>);
31 
32 	char *gets( char *<[buf]>);
33 
34 DESCRIPTION
35 	Reads characters from standard input until a newline is found.
36 	The characters up to the newline are stored in <[buf]>. The
37 	newline is discarded, and the buffer is terminated with a 0.
38 
39 	This is a @emph{dangerous} function, as it has no way of checking
40 	the amount of space available in <[buf]>. One of the attacks
41 	used by the Internet Worm of 1988 used this to overrun a
42 	buffer allocated on the stack of the finger daemon and
43 	overwrite the return address, causing the daemon to execute
44 	code downloaded into it over the connection.
45 
46 	The alternate function <<_gets_r>> is a reentrant version.  The extra
47 	argument <[reent]> is a pointer to a reentrancy structure.
48 
49 
50 RETURNS
51 	<<gets>> returns the buffer passed to it, with the data filled
52 	in. If end of file occurs with some data already accumulated,
53 	the data is returned with no other indication. If end of file
54 	occurs with no data in the buffer, NULL is returned.
55 
56 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
57 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
58 */
59 
60 #define _DEFAULT_SOURCE
61 #include <_ansi.h>
62 #include <stdio.h>
63 #include "local.h"
64 
65 #undef gets
66 
67 char *
gets(char * buf)68 gets (
69        char *buf)
70 {
71   register int c;
72   register char *s = buf;
73   FILE *fp;
74 
75   _REENT_SMALL_CHECK_INIT (ptr);
76   fp = _stdin_r (ptr);
77   CHECK_INIT (ptr, fp);
78   _newlib_flockfile_start (fp);
79   while ((c = _sgetc ( fp)) != '\n')
80     if (c == EOF)
81       if (s == buf)
82 	{
83 	  _newlib_flockfile_exit (fp);
84 	  return NULL;
85 	}
86       else
87 	break;
88     else
89       *s++ = c;
90   *s = 0;
91   _newlib_flockfile_end (fp);
92   return buf;
93 }
94