Mail Archives: djgpp/2009/04/03/09:15:49

X-Authentication-Warning: mail set sender to djgpp-bounces using -f
X-Recipient: djgpp AT delorie DOT com
Date: Fri, 03 Apr 2009 17:13:02 +0300
From: Eli Zaretskii <eliz AT gnu DOT org>
Subject: Re: setmode
In-reply-to: <gr3fv1$s9f$>
X-012-Sender: halo1 AT inter DOT net DOT il
To: djgpp AT delorie DOT com
Message-id: <>
References: <gr3fv1$s9f$1 AT aioe DOT org>
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> From: "Rod Pemberton" <do_not_have AT nohavenot DOT cmm>
> Date: Thu, 2 Apr 2009 19:02:06 -0400
>     setmode(fileno(stdout), O_BINARY);
> First, is there an equivalent to this in ANSI C?

No.  ANSI C says that everything about stdin and stdout is
implementation-defined, IIRC.

> Second, shouldn't I be able to do the exactly same as the setmode() line
> with ANSI C's freopen()?

I don't think this is possible (but I didn't try hard enough, so I
could be wrong).  Redirecting stdout explicitly affects file
descriptor 1 which was inherited from the parent shell, and `freopen'
closes that descriptor.  The new FILE object reuses the same file
descriptor 1, but the redirection is lost when the original descriptor
is closed, because the redirection is maintained by the OS, which is
told that the file is closed.

> How do I get both redirection capability and binary output with stdout when
> using ANSI C functions such as freopen()?...

Why isn't using `setmode' good enough?  That's what all applications
that need this do.  Just be careful not to switch stdout to binary if
it is not redirected, since switching the console device to binary has
some unpleasant side effects in DJGPP: e.g., SIGINT is not produced by
Ctrl-C.  So I'd suggest

    if (!isatty (fileno (stdout)))
      setmode (fileno (stdout), O_BINARY);

- Raw text -

  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019