- Cleanup the /lib directory, by putting more 3rd-party libs in /3rdparty, and by...
[reactos.git] / reactos / lib / sdk / crt / conio / cgets.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: msvcrt/conio/cgets.c
5 * PURPOSE: C Runtime
6 * PROGRAMMER: Eric Kohl (Imported from DJGPP)
7 */
8
9 #include <precomp.h>
10
11 /*
12 * @implemented
13 */
14 char *_cgets(char *string)
15 {
16 unsigned len = 0;
17 unsigned int maxlen_wanted;
18 char *sp;
19 int c;
20 /*
21 * Be smart and check for NULL pointer.
22 * Don't know wether TURBOC does this.
23 */
24 if (!string)
25 return(NULL);
26 maxlen_wanted = (unsigned int)((unsigned char)string[0]);
27 sp = &(string[2]);
28 /*
29 * Should the string be shorter maxlen_wanted including or excluding
30 * the trailing '\0' ? We don't take any risk.
31 */
32 while(len < maxlen_wanted-1)
33 {
34 c=_getch();
35 /*
36 * shold we check for backspace here?
37 * TURBOC does (just checked) but doesn't in cscanf (thats harder
38 * or even impossible). We do the same.
39 */
40 if (c == '\b')
41 {
42 if (len > 0)
43 {
44 _cputs("\b \b"); /* go back, clear char on screen with space
45 and go back again */
46 len--;
47 sp[len] = '\0'; /* clear the character in the string */
48 }
49 }
50 else if (c == '\r')
51 {
52 sp[len] = '\0';
53 break;
54 }
55 else if (c == 0)
56 {
57 /* special character ends input */
58 sp[len] = '\0';
59 _ungetch(c); /* keep the char for later processing */
60 break;
61 }
62 else
63 {
64 sp[len] = _putch(c);
65 len++;
66 }
67 }
68 sp[maxlen_wanted-1] = '\0';
69 string[1] = (char)((unsigned char)len);
70 return(sp);
71 }
72
73