genericopenlibs/cstdlib/LSTDIO/GETS.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* GETS.C
       
     2  * 
       
     3  * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     4  * All rights reserved.
       
     5  */
       
     6 
       
     7 /*
       
     8  * Copyright (c) 1990 The Regents of the University of California.
       
     9  * All rights reserved.
       
    10  *
       
    11  * Redistribution and use in source and binary forms are permitted
       
    12  * provided that the above copyright notice and this paragraph are
       
    13  * duplicated in all such forms and that any documentation,
       
    14  * advertising materials, and other materials related to such
       
    15  * distribution and use acknowledge that the software was developed
       
    16  * by the University of California, Berkeley.  The name of the
       
    17  * University may not be used to endorse or promote products derived
       
    18  * from this software without specific prior written permission.
       
    19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
       
    20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
       
    21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
       
    22  */
       
    23 
       
    24 
       
    25 /*
       
    26 
       
    27 FUNCTION
       
    28         <<gets>>---get character string (obsolete, use <<fgets>> instead)
       
    29 INDEX
       
    30 	gets
       
    31 INDEX
       
    32 	_gets_r
       
    33 
       
    34 ANSI_SYNOPSIS
       
    35         #include <stdio.h>
       
    36 
       
    37 	char *gets(char *<[buf]>);
       
    38 
       
    39 	char *_gets_r(void *<[reent]>, char *<[buf]>);
       
    40 
       
    41 TRAD_SYNOPSIS
       
    42 	#include <stdio.h>
       
    43 
       
    44 	char *gets(<[buf]>)
       
    45 	char *<[buf]>;
       
    46 
       
    47 	char *_gets_r(<[reent]>, <[buf]>)
       
    48 	char *<[reent]>;
       
    49 	char *<[buf]>;
       
    50 
       
    51 DESCRIPTION
       
    52 	Reads characters from standard input until a newline is found.
       
    53 	The characters up to the newline are stored in <[buf]>. The
       
    54 	newline is discarded, and the buffer is terminated with a 0.
       
    55 
       
    56 	This is a @emph{dangerous} function, as it has no way of checking
       
    57 	the amount of space available in <[buf]>. One of the attacks
       
    58 	used by the Internet Worm of 1988 used this to overrun a
       
    59 	buffer allocated on the stack of the finger daemon and
       
    60 	overwrite the return address, causing the daemon to execute
       
    61 	code downloaded into it over the connection.
       
    62 
       
    63 	The alternate function <<_gets_r>> is a reentrant version.  The extra
       
    64 	argument <[reent]> is a pointer to a reentrancy structure.
       
    65 
       
    66 
       
    67 RETURNS
       
    68 	<<gets>> returns the buffer passed to it, with the data filled
       
    69 	in. If end of file occurs with some data already accumulated,
       
    70 	the data is returned with no other indication. If end of file
       
    71 	occurs with no data in the buffer, NULL is returned.
       
    72 
       
    73 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
       
    74 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
       
    75 */
       
    76 
       
    77 #include <stdio_r.h>
       
    78 #include "LOCAL.H"
       
    79 
       
    80 /**
       
    81 A reentrant version of gets().
       
    82 */
       
    83 EXPORT_C char *
       
    84 _gets_r (struct _reent *ptr, char *buf)
       
    85 {
       
    86   register int c;
       
    87   register char *s = buf;
       
    88 
       
    89   while ((c = _getchar_r (ptr)) != '\n')
       
    90     if (c == EOF)
       
    91       if (s == buf)
       
    92 	return NULL;
       
    93       else
       
    94 	break;
       
    95     else
       
    96       *s++ = (char)c;
       
    97   *s = 0;
       
    98   return buf;
       
    99 }
       
   100 
       
   101 #ifndef _REENT_ONLY
       
   102 
       
   103 /**
       
   104 Get a string from stdin.
       
   105 Reads characters from stdin and stores them into buffer
       
   106 until a newline (\n) or EOF character is encountered.
       
   107 @return On success, the buffer parameter is returned.
       
   108 On end-of-file or error, a null pointer is returned. 
       
   109 @param pointer to a buffer where to receive the resulting string.
       
   110 */
       
   111 EXPORT_C char *
       
   112 gets (char *buf)
       
   113 {
       
   114   return _gets_r (_REENT, buf);
       
   115 }
       
   116 
       
   117 #endif