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