1272753Sbrooks/*	$NetBSD: gets.c,v 1.15 2003/08/07 16:43:27 agc Exp $	*/
2241236Sbrooks
3241236Sbrooks/*-
4241236Sbrooks * Copyright (c) 1990, 1993
5241236Sbrooks *	The Regents of the University of California.  All rights reserved.
6241236Sbrooks *
7241236Sbrooks * This code is derived from software contributed to Berkeley by
8241236Sbrooks * Chris Torek.
9241236Sbrooks *
10241236Sbrooks * Redistribution and use in source and binary forms, with or without
11241236Sbrooks * modification, are permitted provided that the following conditions
12241236Sbrooks * are met:
13241236Sbrooks * 1. Redistributions of source code must retain the above copyright
14241236Sbrooks *    notice, this list of conditions and the following disclaimer.
15241236Sbrooks * 2. Redistributions in binary form must reproduce the above copyright
16241236Sbrooks *    notice, this list of conditions and the following disclaimer in the
17241236Sbrooks *    documentation and/or other materials provided with the distribution.
18241236Sbrooks * 3. Neither the name of the University nor the names of its contributors
19241236Sbrooks *    may be used to endorse or promote products derived from this software
20241236Sbrooks *    without specific prior written permission.
21241236Sbrooks *
22241236Sbrooks * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23241236Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24241236Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25241236Sbrooks * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26241236Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27241236Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28241236Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29241236Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30241236Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31241236Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32241236Sbrooks * SUCH DAMAGE.
33241236Sbrooks */
34241236Sbrooks
35241236Sbrooks#include <sys/cdefs.h>
36241236Sbrooks#if defined(LIBC_SCCS) && !defined(lint)
37241236Sbrooks#if 0
38241236Sbrooksstatic char sccsid[] = "@(#)gets.c	8.1 (Berkeley) 6/4/93";
39241236Sbrooks#else
40241236Sbrooks__RCSID("$NetBSD: gets.c,v 1.15 2003/08/07 16:43:27 agc Exp $");
41241236Sbrooks#endif
42241236Sbrooks#endif /* LIBC_SCCS and not lint */
43241236Sbrooks
44241236Sbrooks#include <assert.h>
45241236Sbrooks#include <errno.h>
46241236Sbrooks#include <stdio.h>
47241236Sbrooks#include "reentrant.h"
48241236Sbrooks#include "local.h"
49241236Sbrooks#ifdef _FORTIFY_SOURCE
50241236Sbrooks#undef gets
51241236Sbrooks#endif
52241236Sbrooks
53241236Sbrooks__warn_references(gets, "warning: this program uses gets(), which is unsafe.")
54241236Sbrooks
55241236Sbrookschar *
56241236Sbrooksgets(buf)
57241236Sbrooks	char *buf;
58241236Sbrooks{
59241236Sbrooks	int c;
60272753Sbrooks	char *s;
61241236Sbrooks
62248302Sbrooks	_DIAGASSERT(buf != NULL);
63244401Sbrooks
64248302Sbrooks	FLOCKFILE(stdin);
65248302Sbrooks	for (s = buf; (c = getchar_unlocked()) != '\n'; ) {
66241236Sbrooks		if (c == EOF) {
67241236Sbrooks			if (s == buf) {
68241236Sbrooks				FUNLOCKFILE(stdin);
69248302Sbrooks				return (NULL);
70241236Sbrooks			} else {
71241236Sbrooks				break;
72241236Sbrooks			}
73241236Sbrooks		} else {
74241236Sbrooks			*s++ = c;
75248302Sbrooks		}
76248302Sbrooks	}
77241236Sbrooks	*s = 0;
78241236Sbrooks	FUNLOCKFILE(stdin);
79241236Sbrooks	return (buf);
80241236Sbrooks}
81241236Sbrooks