gets.c revision 110127
1184610Salfred/*-
2184610Salfred * Copyright (c) 1990, 1993
3184610Salfred *	The Regents of the University of California.  All rights reserved.
4184610Salfred *
5184610Salfred * This code is derived from software contributed to Berkeley by
6184610Salfred * Chris Torek.
7184610Salfred *
8184610Salfred * Redistribution and use in source and binary forms, with or without
9184610Salfred * modification, are permitted provided that the following conditions
10184610Salfred * are met:
11184610Salfred * 1. Redistributions of source code must retain the above copyright
12184610Salfred *    notice, this list of conditions and the following disclaimer.
13184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
14184610Salfred *    notice, this list of conditions and the following disclaimer in the
15184610Salfred *    documentation and/or other materials provided with the distribution.
16184610Salfred * 3. All advertising materials mentioning features or use of this software
17184610Salfred *    must display the following acknowledgement:
18184610Salfred *	This product includes software developed by the University of
19184610Salfred *	California, Berkeley and its contributors.
20184610Salfred * 4. Neither the name of the University nor the names of its contributors
21184610Salfred *    may be used to endorse or promote products derived from this software
22184610Salfred *    without specific prior written permission.
23184610Salfred *
24184610Salfred * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27190754Sthompsa * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29188942Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30188942Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31188942Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32188942Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33188942Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34184610Salfred * SUCH DAMAGE.
35184610Salfred */
36184610Salfred
37188942Sthompsa#if defined(LIBC_SCCS) && !defined(lint)
38188942Sthompsastatic char sccsid[] = "@(#)gets.c	8.1 (Berkeley) 6/4/93";
39188942Sthompsa#endif /* LIBC_SCCS and not lint */
40188942Sthompsa#include <sys/cdefs.h>
41188942Sthompsa__FBSDID("$FreeBSD: head/lib/libc/stdio/gets.c 110127 2003-01-30 23:32:53Z tjr $");
42188942Sthompsa
43188942Sthompsa#include "namespace.h"
44188942Sthompsa#include <unistd.h>
45188942Sthompsa#include <stdio.h>
46184610Salfred#include <sys/cdefs.h>
47188942Sthompsa#include "un-namespace.h"
48188942Sthompsa#include "libc_private.h"
49184610Salfred#include "local.h"
50184610Salfred
51184610Salfred__warn_references(gets, "warning: this program uses gets(), which is unsafe.");
52184610Salfred
53184610Salfredchar *
54184610Salfredgets(buf)
55184610Salfred	char *buf;
56192502Sthompsa{
57184610Salfred	int c;
58192502Sthompsa	char *s;
59184610Salfred	static int warned;
60192502Sthompsa	static char w[] =
61184610Salfred	    "warning: this program uses gets(), which is unsafe.\n";
62184610Salfred
63184610Salfred	FLOCKFILE(stdin);
64184610Salfred	ORIENT(stdin, -1);
65184610Salfred	if (!warned) {
66184610Salfred		(void) _write(STDERR_FILENO, w, sizeof(w) - 1);
67184610Salfred		warned = 1;
68184610Salfred	}
69184610Salfred	for (s = buf; (c = __sgetc(stdin)) != '\n';)
70184610Salfred		if (c == EOF)
71192984Sthompsa			if (s == buf) {
72184610Salfred				FUNLOCKFILE(stdin);
73184610Salfred				return (NULL);
74184610Salfred			} else
75184610Salfred				break;
76184610Salfred		else
77184610Salfred			*s++ = c;
78184610Salfred	*s = 0;
79184610Salfred	FUNLOCKFILE(stdin);
80184610Salfred	return (buf);
81184610Salfred}
82187173Sthompsa