delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2009/04/02/18:00:10

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "Rod Pemberton" <do_not_have AT nohavenot DOT cmm>
Newsgroups: comp.os.msdos.djgpp
Subject: setmode
Date: Thu, 2 Apr 2009 19:02:06 -0400
Organization: Aioe.org NNTP Server
Lines: 46
Message-ID: <gr3fv1$s9f$1@aioe.org>
NNTP-Posting-Host: pldq+kT97bAAp/ObDwnZyQ.user.aioe.org
X-Complaints-To: abuse AT aioe DOT org
NNTP-Posting-Date: Thu, 2 Apr 2009 22:58:42 +0000 (UTC)
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1933
X-Notice: Filtered by postfilter v. 0.7.7
X-Newsreader: Microsoft Outlook Express 6.00.2800.1933
Cancel-Lock: sha1:rzs7tSvvzqFliTLJZoKYr/2Q4pk=
X-Priority: 3
X-MSMail-Priority: Normal
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Ok, I have a program which normally outputs to a file as binary.  I'd like
to optionally output to stdout when no output file is specified.  However,
stdout is in text mode.  Therefore, when the output of the program is
redirected to a file from the DOS command line an extra ASCII CR 0x0D is
being inserted into the file's saved output for every newline, i.e., 0x0D
0x0D 0x0A instead of 0x0D 0x0A.  The following setmode() line corrects the
problem, but it isn't ANSI C:

    setmode(fileno(stdout), O_BINARY);

First, is there an equivalent to this in ANSI C?

Second, shouldn't I be able to do the exactly same as the setmode() line
with ANSI C's freopen()?  I.e., if I freopen() stdout as "wb", shouldn't
this be functionally identical?

Third, if there isn't an ANSI C equivalent, is there a way to
surrepticiously do the equivalent to that setmode line in DJGPP - which uses
some combination of ANSI C functions?  E.g., freopen() calls open() which
then calls setmode().  So, shouldn't there be some way to do the same the
setmode() line above using freopen()?

So far, I've tried the following with freopen():

/* fails, captures extra CR when stdout redirected to file */
    freopen(NULL,"wb",stdout);

/* fails, sends to screen, but won't redirect from screen to file */
    freopen("CON","wb",stdout);

/* fails, sends to screen, but won't redirect from screen to file*/
    freopen("/dev/tty","wb",stdout);

/* fails, sends to file CONOUT$, not to screen */
    freopen("CONOUT$","wb",stdout);

/* fails, sends to file asdf, not to screen */
    freopen("asdf","wb",stdout);

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


Rod Pemberton


- Raw text -


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