Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #163732

Re: ot: on stdin, stdout, stderr and crlf on windows

From John Forkosh <forkosh@panix.com>
Newsgroups comp.lang.c
Subject Re: ot: on stdin, stdout, stderr and crlf on windows
Date 2021-12-08 08:13 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <soppeu$hq9$1@reader1.panix.com> (permalink)
References (3 earlier) <87zgpem34j.fsf@bsb.me.uk> <soku6k$oo4$1@dont-email.me> <87o85tnc8v.fsf@bsb.me.uk> <somcbq$frv$1@reader1.panix.com> <sonb3f$dp0$1@dont-email.me>

Show all headers | View raw


Bart <bc@freeuk.com> wrote:
> John Forkosh wrote:
>> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> 
>>> Anyway, if the most widely used C library does not support it, it's not
>>> portable in any useful way.
>> 
>> Thanks, Ben and Bonita (and Malcom and Manfred), yeah I also "feared
>> this would be the case", i.e., no portable solution like freopen(),
>> which would be syntactically identical on all platforms and "just work",
>> whether needed (windows) or not (linux).
>> 
>> By the way, at least at the time when I wrote that stackexchange post,
>> some windows C compilers used _setmode(_fileno(stderr),_O_BINARY)
>> like Bonita said, but some others used setmode(fileno(stderr),O_BINARY).
>> So additional #ifdefs needed to resolve that. And I'm also not sure
>> that _MSC_VER always identifies all windows C compilers. The elaborate
>> #ifdefs illustrated on that stackexchange post were what I ended up
>> with to accommodate all the variations I'd encountered at the time
>> I wrote it. So I was hoping to find some less elaborate and fewer
>> lines of gobbledy-gook code, if not entirely 100% portable, to identify
>> and handle all possible windows situations.
>> 
> 
> Every Windows system comes with a C library called msvcrt.dll. And 
> msvcrt.dll (usually located in C:\windows\system32) defines _setmode.
> 
> That means I can reliably call that function even from a script language 
> as follows:
> 
>     importdll msvcrt=
>         clang function _setmode(int32,int32)int32
>     end
> 
>     println _setmode(1,32768)     # 1=stdout, 32768=O_BINARY
> 
> Ironically, this appears difficult to do from C!
> 
> First because it's hard to pinpoint that function in that specific 
> library. But also because it needs to be the same library that is used 
> by your *printf and other functions, otherwise you're setting the mode 
> of the wrong library.
> 
> You would need, somehow, to specifically link with this library, but 
> even then, there might be shortcomings: some parts of it might too slow; 
> some other functions could be missing.
> 
> (With my script, the call works both for native 'print' which eventually 
> calls 'printf' of the C library dynamically linked to the interpreter 
> executable, and for direct 'printf' calls within that msvcrt loaded by 
> the script (printf declaration and use not shown).
> 
> I expected there to be two instances of msvcrt, but presumably the OS 
> sees them as both invoked by the same program so they must share data.)

Thanks, Bart. I'll try that approach next time I take a look at
that code, though probably not soon (I only posted the followup
question since the op was already barking up the very same tree).
In any case, seems like there's no way around an overly-elaborate
solution to what should be a completely trivial task (i.e., the
one-line freopen() ought to "just work"). So if/when I do take
another look, I'll almost-certainly encapsulate the inevitable
gobbledy-gook #ifdefs/code/whatever into its own little function,
so it doesn't interrupt main()'s more straightforward logical flow.
-- 
John Forkosh  ( mailto:  j@f.com  where j=john and f=forkosh )

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

ot: on stdin, stdout, stderr and crlf on windows Meredith Montgomery <mmontgomery@levado.to> - 2021-12-04 20:24 -0300
  Re: ot: on stdin, stdout, stderr and crlf on windows Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-12-05 00:01 +0000
  Re: ot: on stdin, stdout, stderr and crlf on windows Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-12-05 00:13 +0000
    Re: ot: on stdin, stdout, stderr and crlf on windows Meredith Montgomery <mmontgomery@levado.to> - 2021-12-04 23:09 -0300
      Re: ot: on stdin, stdout, stderr and crlf on windows Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-04 18:27 -0800
      Re: ot: on stdin, stdout, stderr and crlf on windows Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-12-05 03:33 +0000
        Re: ot: on stdin, stdout, stderr and crlf on windows Meredith Montgomery <mmontgomery@levado.to> - 2021-12-05 09:17 -0300
  Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-05 15:04 +0100
  Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-05 15:13 +0100
    Re: ot: on stdin, stdout, stderr and crlf on windows John Forkosh <forkosh@panix.com> - 2021-12-06 08:20 +0000
      Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-06 09:45 +0100
      Re: ot: on stdin, stdout, stderr and crlf on windows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-06 10:36 +0000
        Re: ot: on stdin, stdout, stderr and crlf on windows Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-06 13:03 +0100
          Re: ot: on stdin, stdout, stderr and crlf on windows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-06 12:34 +0000
            Re: ot: on stdin, stdout, stderr and crlf on windows Manfred <noname@add.invalid> - 2021-12-06 14:38 +0100
              Re: ot: on stdin, stdout, stderr and crlf on windows Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-06 06:00 -0800
                Re: ot: on stdin, stdout, stderr and crlf on windows Manfred <noname@add.invalid> - 2021-12-06 18:59 +0100
              Re: ot: on stdin, stdout, stderr and crlf on windows Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-06 17:08 +0000
                Re: ot: on stdin, stdout, stderr and crlf on windows scott@slp53.sl.home (Scott Lurndal) - 2021-12-06 17:18 +0000
            Re: ot: on stdin, stdout, stderr and crlf on windows John Forkosh <forkosh@panix.com> - 2021-12-07 01:11 +0000
              Re: ot: on stdin, stdout, stderr and crlf on windows Bart <bc@freeuk.com> - 2021-12-07 09:56 +0000
                Re: ot: on stdin, stdout, stderr and crlf on windows John Forkosh <forkosh@panix.com> - 2021-12-08 08:13 +0000
  Re: ot: on stdin, stdout, stderr and crlf on windows Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-10 01:10 -0800
    Re: ot: on stdin, stdout, stderr and crlf on windows gazelle@shell.xmission.com (Kenny McCormack) - 2021-12-10 11:00 +0000

csiph-web