umka-lang icon indicating copy to clipboard operation
umka-lang copied to clipboard

`std::freadall` puts 0's at the end if reading from stdin pipe for some reason.

Open ske2004 opened this issue 6 months ago • 8 comments

On windows

ske2004 avatar Jun 15 '25 07:06 ske2004

@ske2004 How can I reproduce it?

vtereshkov avatar Jun 15 '25 17:06 vtereshkov

import"std.um"

fn main() {
  printf("%v", std::freadall(std::stdin()).item0)
}
W:\umka-extras>cat freadall.um | umka freadall.um
['i' 'm' 'p' 'o' 'r' 't' '"' 's' 't' 'd' '.' 'u' 'm' '"' 0x0A 0x0A 'f' 'n' ' ' 'm' 'a' 'i' 'n' '(' ')' ' ' '{' 0x0A ' ' ' ' 'p' 'r' 'i' 'n' 't' 'f' '(' '"' '%' 'v' '"' ',' ' ' 's' 't' 'd' ':' ':' 'f' 'r' 'e' 'a' 'd' 'a' 'l' 'l' '(' 's' 't' 'd' ':' ':' 's' 't' 'd' 'i' 'n' '(' ')' ')' '.' 'i' 't' 'e' 'm' '0' ')' 0x0A '}' 0x0A 0x00 0x00 0x00 0x00 0x00]

ske2004 avatar Jun 15 '25 17:06 ske2004

I think I see why, std::stdin() is opened with r instead of rb

ske2004 avatar Jun 15 '25 17:06 ske2004

@ske2004 Yes, you'll get as many 0's as there were 0x0D's removed when converting line endings. Then it will return the EOF error.

If it's a practical concern, I perhaps need to add std::freopen().

vtereshkov avatar Jun 15 '25 19:06 vtereshkov

@ske2004 Yes, you'll get as many 0's as there were 0x0D's removed when converting line endings. Then it will return the EOF error.

If it's a practical concern, I perhaps need to add std::freopen().

Is the default stdin pipe open with "r" on Windows? It would be pretty bad because CR could be part of binary stream instead.

ske2004 avatar Jun 15 '25 19:06 ske2004

@ske2004 It is:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/text-and-binary-mode-file-i-o?view=msvc-170

https://www.quora.com/How-do-you-read-from-a-binary-stream-from-stdin-in-C

vtereshkov avatar Jun 15 '25 20:06 vtereshkov

@vtereshkov huh weird, i feel like we discussed this before. If it can be re-opened without ruining the host program, maybe it should be default

ske2004 avatar Jun 16 '25 06:06 ske2004

@ske2004 I remember we discussed the codepage, but not the opening mode.

I doubt how scanf() will work if we open stdin as "rb".

vtereshkov avatar Jun 16 '25 14:06 vtereshkov