gets.c revision 92986
1239310Sdim/*- 2239310Sdim * Copyright (c) 1990, 1993 3239310Sdim * The Regents of the University of California. All rights reserved. 4239310Sdim * 5239310Sdim * This code is derived from software contributed to Berkeley by 6239310Sdim * Chris Torek. 7239310Sdim * 8239310Sdim * Redistribution and use in source and binary forms, with or without 9239310Sdim * modification, are permitted provided that the following conditions 10239310Sdim * are met: 11239310Sdim * 1. Redistributions of source code must retain the above copyright 12239310Sdim * notice, this list of conditions and the following disclaimer. 13239310Sdim * 2. Redistributions in binary form must reproduce the above copyright 14239310Sdim * notice, this list of conditions and the following disclaimer in the 15239310Sdim * documentation and/or other materials provided with the distribution. 16239310Sdim * 3. All advertising materials mentioning features or use of this software 17239310Sdim * must display the following acknowledgement: 18239310Sdim * This product includes software developed by the University of 19239310Sdim * California, Berkeley and its contributors. 20239310Sdim * 4. Neither the name of the University nor the names of its contributors 21239310Sdim * may be used to endorse or promote products derived from this software 22239310Sdim * without specific prior written permission. 23239310Sdim * 24239310Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25239310Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26239310Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27239310Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28239310Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29239310Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30239310Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31239310Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32239310Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33239310Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34239310Sdim * SUCH DAMAGE. 35239310Sdim */ 36239310Sdim 37239310Sdim#if defined(LIBC_SCCS) && !defined(lint) 38239310Sdimstatic char sccsid[] = "@(#)gets.c 8.1 (Berkeley) 6/4/93"; 39239310Sdim#endif /* LIBC_SCCS and not lint */ 40239310Sdim#include <sys/cdefs.h> 41239310Sdim__FBSDID("$FreeBSD: head/lib/libc/stdio/gets.c 92986 2002-03-22 21:53:29Z obrien $"); 42239310Sdim 43239310Sdim#include "namespace.h" 44239310Sdim#include <unistd.h> 45239310Sdim#include <stdio.h> 46239310Sdim#include <sys/cdefs.h> 47239310Sdim#include "un-namespace.h" 48239310Sdim 49239310Sdim__warn_references(gets, "warning: this program uses gets(), which is unsafe."); 50239310Sdim 51239310Sdimchar * 52239310Sdimgets(buf) 53239310Sdim char *buf; 54239310Sdim{ 55239310Sdim int c; 56239310Sdim char *s; 57239310Sdim static int warned; 58239310Sdim static char w[] = 59239310Sdim "warning: this program uses gets(), which is unsafe.\n"; 60239310Sdim 61239310Sdim if (!warned) { 62239310Sdim (void) _write(STDERR_FILENO, w, sizeof(w) - 1); 63239310Sdim warned = 1; 64239310Sdim } 65239310Sdim for (s = buf; (c = getchar()) != '\n';) 66239310Sdim if (c == EOF) 67239310Sdim if (s == buf) 68239310Sdim return (NULL); 69239310Sdim else 70239310Sdim break; 71239310Sdim else 72239310Sdim *s++ = c; 73239310Sdim *s = 0; 74239310Sdim return (buf); 75239310Sdim} 76239310Sdim