summaryrefslogtreecommitdiffstats
path: root/private/crt32/stdio/gets.c
blob: fc2ac48862f9e467e931510a77da1c8ef4f486b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/***
*gets.c - read a line from stdin
*
*	Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	defines gets() - read a line from stdin into buffer
*
*Revision History:
*	09-02-83  RN	initial version
*	11-06-87  JCR	Multi-thread support
*	12-11-87  JCR	Added "_LOAD_DS" to declaration
*	05-27-88  PHG	Merged DLL and normal versions
*	02-15-90  GJF	Fixed copyright, indents
*	03-19-90  GJF	Replaced _LOAD_DS with _CALLTYPE1, added #include
*			<cruntime.h> and removed #include <register.h>.
*	07-24-90  SBM	Replaced <assertm.h> by <assert.h>
*	08-14-90  SBM	Compiles cleanly with -W3
*	10-02-90  GJF	New-style function declarator.
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <assert.h>
#include <file2.h>
#include <os2dll.h>

/***
*char *gets(string) - read a line from stdin
*
*Purpose:
*	Gets a string from stdin terminated by '\n' or EOF; don't include '\n';
*	append '\0'.
*
*Entry:
*	char *string - place to store read string, assumes enough room.
*
*Exit:
*	returns string, filled in with the line of input
*	null string if \n found immediately
*	NULL if EOF found immediately
*
*Exceptions:
*
*******************************************************************************/

char * _CALLTYPE1 gets (
	REG3 char *string
	)
{
	REG2 int ch;
	REG1 char *pointer = string;
	char *retval = string;
#ifdef MTHREAD
	int index;
#endif

	assert(string != NULL);

#ifdef MTHREAD
	index = _iob_index((FILE *)stdin);
#endif
	_lock_str(index);

	while ((ch = _getchar_lk()) != '\n')
	{
		if (ch == EOF)
		{
			if (pointer == string)
			{
				retval = NULL;
				goto done;
			}

			break;
		}

		*pointer++ = (char)ch;
	}

	*pointer = '\0';

/* Common return */
done:
	_unlock_str(index);
	return(retval);

}