|
From: <ca...@kr...> - 2003-04-25 12:23:03
|
hi dev-cpp-users,
I try the following code:
<snip>
#include <stdio.h>
int main(int argc, char** argv)
{
char buffer[5];
while(1)
{
printf("prompt > ");
scanf("%4[^\n]",buffer);
printf("you typed: %s\n", buffer);
printf("you typed: %d %d %d %d %d\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
}
return 0;
}
</snap>
but it obviously doesn't work.
I want to emulate a console behaviour.
so that whenever you press <ENTER>
-the just typed letters (only 4 of them) get placed in buffer (with a
terminating 0)
-buffer gets printed
-buffer gets cleared
-stdin gets cleared
-new prompt waiting for input ()
how can i do that?
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: <ca...@kr...> - 2003-04-25 12:28:32
|
> -stdin gets cleared
> how can i do that?
fflush(stdin);
just found out how.. thanks anyway!
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: Ioannis V. <iv...@at...> - 2003-04-25 20:33:24
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Christian Sch=E4fer > Sent: Friday, April 25, 2003 3:30 PM > To: dev...@li... > Subject: Re: [Dev-C++] stdin reset... >=20 >=20 > > -stdin gets cleared > > how can i do that? >=20 > fflush(stdin); >=20 > just found out how.. thanks anyway! Which is not safe. getchar(), fgets(), etc, should be used instead, Ioannis Vranos =20 * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net Anti-racism movement: http://www.positive-youthfoundation.org Iraq Peace Pledge: http://www.peacepledge.org |
|
From: <ca...@kr...> - 2003-04-25 20:54:09
|
hi Ioannis,
>> > -stdin gets cleared
>> > how can i do that?
>>
>> fflush(stdin);
> Which is not safe. getchar(), fgets(), etc, should be used instead,
and why is that?
I am very interested in code security issues but at least i want to
understand instead of obey them. so what exactly is considered
'unsafe'?
what I need is a secure way to read as much as there is from stdin
undtil '\n' into a char* buffer. I want that buffer to match the
needed length (I don't know how to do that, since I don't know the
exact needed length until i read it from stdin...) and I need to avoid
buffer overflows (of course).
I thought scanf() is a good way, since it reads only a specified
number of characters right? (ok, this is just a workaround, I guess..)
I don't think this is a unique problem so there must be a known way to
solve it, is there?
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: Daniel K. O. <dan...@ya...> - 2003-04-25 21:57:59
|
> >> fflush(stdin);
> > Which is not safe. getchar(), fgets(), etc, should be used instead,
>
> and why is that?
> I am very interested in code security issues but at least i want to
> understand instead of obey them. so what exactly is considered
> 'unsafe'?
The same reason you shouldn't fflush(2.5), fflush("some string") nor
fflush(a, b, c), you shouldn't fflush(stdin); the standard says that
"fflush()" is for output streams; not input streams, not strings, not
anything else; the non-standard way can work on a particular system, but it
can kill some lives on others.
> what I need is a secure way to read as much as there is from stdin
> undtil '\n' into a char* buffer. I want that buffer to match the
> needed length (I don't know how to do that, since I don't know the
> exact needed length until i read it from stdin...) and I need to avoid
> buffer overflows (of course).
> I thought scanf() is a good way, since it reads only a specified
> number of characters right? (ok, this is just a workaround, I guess..)
Try fgets(), then sscanf().
> I don't think this is a unique problem so there must be a known way to
> solve it, is there?
Suggestion (for all you C programmers):
The comp.lang.c answers to frequently asked questions:
http://www.eskimo.com/~scs/C-faq/top.html
Daniel K. O.
|
|
From: Ioannis V. <iv...@at...> - 2003-04-26 01:48:03
|
> -----Original Message-----
> From: Christian Sch=E4fer [mailto:ca...@kr...]=20
> Sent: Friday, April 25, 2003 11:55 PM
> To: dev...@li...; Ioannis Vranos
> Cc: dev...@li...
> Subject: Re[2]: [Dev-C++] stdin reset...
>=20
>=20
> and why is that?
> I am very interested in code security issues but at least i want to
> understand instead of obey them. so what exactly is considered
> 'unsafe'?
>=20
> what I need is a secure way to read as much as there is from stdin
> undtil '\n' into a char* buffer. I want that buffer to match the
> needed length (I don't know how to do that, since I don't know the
> exact needed length until i read it from stdin...) and I need to avoid
> buffer overflows (of course).
> I thought scanf() is a good way, since it reads only a specified
> number of characters right? (ok, this is just a workaround, I guess..)
>=20
> I don't think this is a unique problem so there must be a known way to
> solve it, is there?
fflush() is intended for output streams, not input. For example
fflush(stdin); does not work in GNU/Linux. So you can't expect
fflush(stdin); to be portable. You will have to use aither a plain
getchar(); call after a plain scanf() or create a clean function in the
style:
void clean_stdin()
{
while(getchar()!=3D'\n')
;
}
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: <ca...@kr...> - 2003-04-26 07:22:34
|
hi Ioannis,
> fflush() is intended for output streams, not input. For example
> fflush(stdin); does not work in GNU/Linux. So you can't expect
> fflush(stdin); to be portable. You will have to use aither a plain
> getchar(); call after a plain scanf() or create a clean function in the
> style:
> void clean_stdin()
> {
> while(getchar()!='\n')
> ;
> }
ok, I now understand why not to use fflush(). (I would have noticed
when porting to linux whats the final destination... ;)
so you mean, I scanf() whatever I need and then let getchar() go
through stdin again clearing everything until the first '\n', right?
ok then, that would solve the first halve of my problem.
for the second I open another thread. thanks a lot!
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: philip M. <ph...@co...> - 2003-04-25 16:42:27
|
You could have written:
printf("prompt > ");
scanf("%4s", buffer);
while (fgetc(stdin) != '\n');
> I try the following code:
>
> <snip>
> #include <stdio.h>
>
> int main(int argc, char** argv)
> {
> char buffer[5];
> while(1)
> {
> printf("prompt > ");
> scanf("%4[^\n]",buffer);
>
> printf("you typed: %s\n", buffer);
> printf("you typed: %d %d %d %d %d\n", buffer[0], buffer[1],
buffer[2], buffer[3], buffer[4]);
> }
>
> return 0;
> }
> </snap>
--------------------------------------------------------------------------
Philip Mortimer
www: http://mysite.freeserve.com/coniferway/index.html.
Available for short and long term contracts
---------------------------------------------------------------------------
|