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