Re: simple read char app return wrong value?



naunetr wrote:

....
but when i enter several characters after my app exits bash somehow reads them and complains. here is some examples.

Annoying, but normal.

shouldn't it be considered "risky" to give one apps input to another app? shouldnt kernel remove all the pending input for readc before handing bash control?

I guess you're right. It probably should... but it doesn't...

....
okay. i thought that would happen only within a process, not across them also.

Now that you mention it, that *is* surprising.

how can avoid input to readc from being read by bash?

We'll have to flush the buffer!

this isnt needed in c programs. i guess the standerd library does this flushing before exiting.

I don't have much experience with C. There's open(), read(), write() - which I think is the same code as we'd get with the int 80h interface. Then there's fopen(), fread(), fwrite() which use a "*FILE" - these ones do internal buffering, and definitely do some flushing. For example, if we use printf to say "hit enter to continue" - *without* a newline - and then wait for "enter"... we don't see the prompt until *after* the program exits. A newline in the format string will flush the buffer and cause it to print right away.

Another way we can see what the C library is doing for us... The way we're doing d2u and u3d - reading one character at a time - is slow as hell. A C program that reads a file one character at a time with fgetc() is much faster, because of this internal buffering. We can provide buffering, too, of course.

I'll have to try an experiment, comparing read() and fread(). I wouldn't be surprised to see read() show this same behavior, and fread() not. I'm not sure.

also the exit code for example 1 is 1 and for example 2 is 0 but for example 3 it is 127. does anyone know what 127 means? 1 and 0 seem to be correct as said in man 2 read.

Well, I think 127 is what bash is returning after complaining, no longer the return code from your program. Damned if I know what it means! I would expect an "error" indication - a "negative" number, which "echo $?" should display as >128 (since it doesn't do "signed") I can confirm that it *does* return 127, but I don't know why.

Thanks for the info on that, Chuck! RTFM is a good idea... but there's so *much* of it! :)

....
mov bl, [the_char]

wont some leftover value in rest of ebx corrupt the return code?

Good point! Could have used "movzx ebx, byte [buf]". I'm not worried about it for two reasons: One, I think only bl - the low byte of ebx - is "valid". "echo $?" only displays the low byte (mov ebx, 0FFFFFF00 and exit just displays 0). Also, we got here from reading stdin, so ebx should be zero anyway (this sort of assumption results in unmaintainable code!)

....
okay when i run this program and press ctrl-d and then enter it return a value of 0. wikipedia says that ctrl-d is end of transmission and has a decimal value of 4 and it also used as end of file in unix. so why doesnt the app return 4 instead of 0?

I'm not sure what to make of this. There's some "translation" going on when we read a key (we gan get several degrees of "raw" input by teaking stdin with sys_ioctl). I assume that's what we're seeing - ctrl-d showing up as zero, ctrl-j and ctrl-m both showing 10, etc.

anyway thanks for that example Frank. slowly im progressing... :)

Your questions indicate that you're progressing at a pretty good rate, I'd say!

Best,
Frank
.



Relevant Pages

  • Re: Leaving files open on CE device 100% of the time
    ... The OS doesn't just kill your app, ... you have your own internal buffering, and didn't actually do the write). ... Have an opinion on the effectiveness of Microsoft Embedded newsgroups? ... > It would probably be better to keep all of the data in memory (if that is ...
    (microsoft.public.windowsce.embedded)
  • Re: Double Buffering and Initial Paint
    ... > when I turn on double buffering ... ... > the control which is being double buffered momentarily has the same ... > When this is set to false, flickering once again occurs, but the ... > my app, but STILL not have any flicker. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Weird Java 6 Double-buffering Problem
    ... I'm having a printing issue since upgrading my Swing app from JDK1.4.2_06 to JDK6.0. ... As many of you may know, before printing, one must disable double buffering. ... Ever since I upgraded my app, my JTable components have been experiencing this exact problem. ... I confirmed that double buffering is disabled both by setting breakpoints in my JTable cell renderer and noticing that my app flickers badly with any onscreen rendering. ...
    (comp.lang.java.gui)
  • Re: memory usage minimized
    ... I have developed an app that uses alot of double buffered forms. ... Does garbage collection get triggered when you minimize? ... Is the double buffering using garbage collected ...
    (microsoft.public.dotnet.framework.performance)