1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/GETS.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,117 @@
1.4 +/* GETS.C
1.5 + *
1.6 + * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Copyright (c) 1990 The Regents of the University of California.
1.12 + * All rights reserved.
1.13 + *
1.14 + * Redistribution and use in source and binary forms are permitted
1.15 + * provided that the above copyright notice and this paragraph are
1.16 + * duplicated in all such forms and that any documentation,
1.17 + * advertising materials, and other materials related to such
1.18 + * distribution and use acknowledge that the software was developed
1.19 + * by the University of California, Berkeley. The name of the
1.20 + * University may not be used to endorse or promote products derived
1.21 + * from this software without specific prior written permission.
1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1.25 + */
1.26 +
1.27 +
1.28 +/*
1.29 +
1.30 +FUNCTION
1.31 + <<gets>>---get character string (obsolete, use <<fgets>> instead)
1.32 +INDEX
1.33 + gets
1.34 +INDEX
1.35 + _gets_r
1.36 +
1.37 +ANSI_SYNOPSIS
1.38 + #include <stdio.h>
1.39 +
1.40 + char *gets(char *<[buf]>);
1.41 +
1.42 + char *_gets_r(void *<[reent]>, char *<[buf]>);
1.43 +
1.44 +TRAD_SYNOPSIS
1.45 + #include <stdio.h>
1.46 +
1.47 + char *gets(<[buf]>)
1.48 + char *<[buf]>;
1.49 +
1.50 + char *_gets_r(<[reent]>, <[buf]>)
1.51 + char *<[reent]>;
1.52 + char *<[buf]>;
1.53 +
1.54 +DESCRIPTION
1.55 + Reads characters from standard input until a newline is found.
1.56 + The characters up to the newline are stored in <[buf]>. The
1.57 + newline is discarded, and the buffer is terminated with a 0.
1.58 +
1.59 + This is a @emph{dangerous} function, as it has no way of checking
1.60 + the amount of space available in <[buf]>. One of the attacks
1.61 + used by the Internet Worm of 1988 used this to overrun a
1.62 + buffer allocated on the stack of the finger daemon and
1.63 + overwrite the return address, causing the daemon to execute
1.64 + code downloaded into it over the connection.
1.65 +
1.66 + The alternate function <<_gets_r>> is a reentrant version. The extra
1.67 + argument <[reent]> is a pointer to a reentrancy structure.
1.68 +
1.69 +
1.70 +RETURNS
1.71 + <<gets>> returns the buffer passed to it, with the data filled
1.72 + in. If end of file occurs with some data already accumulated,
1.73 + the data is returned with no other indication. If end of file
1.74 + occurs with no data in the buffer, NULL is returned.
1.75 +
1.76 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.77 +<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
1.78 +*/
1.79 +
1.80 +#include <stdio_r.h>
1.81 +#include "LOCAL.H"
1.82 +
1.83 +/**
1.84 +A reentrant version of gets().
1.85 +*/
1.86 +EXPORT_C char *
1.87 +_gets_r (struct _reent *ptr, char *buf)
1.88 +{
1.89 + register int c;
1.90 + register char *s = buf;
1.91 +
1.92 + while ((c = _getchar_r (ptr)) != '\n')
1.93 + if (c == EOF)
1.94 + if (s == buf)
1.95 + return NULL;
1.96 + else
1.97 + break;
1.98 + else
1.99 + *s++ = (char)c;
1.100 + *s = 0;
1.101 + return buf;
1.102 +}
1.103 +
1.104 +#ifndef _REENT_ONLY
1.105 +
1.106 +/**
1.107 +Get a string from stdin.
1.108 +Reads characters from stdin and stores them into buffer
1.109 +until a newline (\n) or EOF character is encountered.
1.110 +@return On success, the buffer parameter is returned.
1.111 +On end-of-file or error, a null pointer is returned.
1.112 +@param pointer to a buffer where to receive the resulting string.
1.113 +*/
1.114 +EXPORT_C char *
1.115 +gets (char *buf)
1.116 +{
1.117 + return _gets_r (_REENT, buf);
1.118 +}
1.119 +
1.120 +#endif