From: "Joel Hunsberger" Subject: Using cgets (a !FAQ) Newsgroups: comp.os.msdos.djgpp Message-ID: <01bfafc6$387a7c80$da06017e@gr-356146> X-Newsreader: Microsoft Internet News 4.70.1155 NNTP-Posting-Host: help-desk-005.si.com Date: 26 Apr 2000 17:28:17 -0500 X-Trace: 26 Apr 2000 17:28:17 -0500, help-desk-005.si.com Organization: Smiths Industries Lines: 64 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I call this a !FAQ because I found the answer with gdb, and maybe what I found will help someone else. I started (sort of a newbieon MSDOS) to use the package because I wanted to break away from input piped to stdin and prompt the user for additional input... etc... The basic approach (final code) ended up as follows: #include char linebuf[256]; /*.... snippett is out of context ....*/ { /* Input is from stdin */ if (freopen("CON","r", stdin) == NULL) { /* Assure that we connect to stdin properly */ fprintf(stderr,"ERROR - Cannot open console for input.\n"); } cprintf("Enter file names, followed by CTRL-Z or q:"); cputs("\x0d\x0a"); cputs("files...> "); /* Caution!!! 255 or more causes stack problems !!*/ linebuf[0] = 127; linebuf[1] = 0; cgets(linebuf); /* Return is actually in linebuf */ while ((linebuf[2] != 'q') && (linebuf[2] != 26) && (linebuf[2] != 27)) { currentlength = stashstring(linebuf,-1); cputs("\x0d\x0a"); cputs("files...> "); /* \x0afile causes problems */ linebuf[0] = 127; linebuf[1] = 0; cgets(linebuf); } cputs("\x0d\x0a"); } /* end - Input is from stdin */ However, the first spin was not at all successful... I was getting crashes every time, but they were occurring at the next 'return' after this code... The problem (in the original code) was that I had stated: linebuf[0]=255; /* setup for cgets */ then called... cgets(linebuf); The problem was that this code extensively corrupted the stack every time. It turns out that stating 255 as the buffer length was causing an implicit conversion overflow when converted for use in the string, and subsequently for use by cputs. Alas, it came out as buffer length of -1, which caused manifest stack corruption (for reasons I can only imagine.) When I reduce the (largely arbitrary) requirement to 127 for console line input... things are fine! No hints in the info documentation for cgets, unfortunately., I would have seen the error if I had used the compiler switch -pedantic SOONER. Hope this inspires someone. Joel Hunsberger