6c25041f467e44f37a0cd1d30ab727d3a286b4f0
[reactos.git] / reactos / lib / 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 <conio.h>
10 #include <stdlib.h>
11
12 /*
13 * @implemented
14 */
15 char *_cgets(char *string)
16 {
17 unsigned len = 0;
18 unsigned int maxlen_wanted;
19 char *sp;
20 int c;
21 /*
22 * Be smart and check for NULL pointer.
23 * Don't know wether TURBOC does this.
24 */
25 if (!string)
26 return(NULL);
27 maxlen_wanted = (unsigned int)((unsigned char)string[0]);
28 sp = &(string[2]);
29 /*
30 * Should the string be shorter maxlen_wanted including or excluding
31 * the trailing '\0' ? We don't take any risk.
32 */
33 while(len < maxlen_wanted-1)
34 {
35 c=_getch();
36 /*
37 * shold we check for backspace here?
38 * TURBOC does (just checked) but doesn't in cscanf (thats harder
39 * or even impossible). We do the same.
40 */
41 if (c == '\b')
42 {
43 if (len > 0)
44 {
45 _cputs("\b \b"); /* go back, clear char on screen with space
46 and go back again */
47 len--;
48 sp[len] = '\0'; /* clear the character in the string */
49 }
50 }
51 else if (c == '\r')
52 {
53 sp[len] = '\0';
54 break;
55 }
56 else if (c == 0)
57 {
58 /* special character ends input */
59 sp[len] = '\0';
60 _ungetch(c); /* keep the char for later processing */
61 break;
62 }
63 else
64 {
65 sp[len] = _putch(c);
66 len++;
67 }
68 }
69 sp[maxlen_wanted-1] = '\0';
70 string[1] = (char)((unsigned char)len);
71 return(sp);
72 }
73
74