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