[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] xfreopen: keep append mode when converting stdout to binary

From: Eric Blake
Subject: Re: [PATCH] xfreopen: keep append mode when converting stdout to binary
Date: Wed, 10 Mar 2010 16:57:52 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20100301 Fedora/3.0.3-1.fc12 Lightning/1.0b1 Thunderbird/3.0.3

On 03/10/2010 04:43 PM, Bruno Haible wrote:
> I would find it better to leave xfreopen alone and add a function
>   /* Returns the current mode of FP, combined with the additional
>      open_flags.  Allowed open_flags are O_APPEND, O_TEXT, O_BINARY,
>      or a combination of these.  */
>   const char *fgetmode (FILE *fp, int open_flags)
> Then src/head.c is changed to:
>   if (O_BINARY && ! isatty (STDOUT_FILENO))
>     xfreopen (NULL, fgetmode (stdout, O_WRONLY | O_BINARY), stdout);
> Or, alternatively, add a new function [x]fchangemode that relies on
> [x]freopen:
>   /* Changes the mode of FP, but keeping those flags that are set
>      in kept_flags and adding flags that are set in added_flags.
>      Allowed values for kept_flags and added_flags are O_RDONLY,
>      O_WRONLY, O_RDWR, O_APPEND, O_TEXT, O_BINARY, or a combination of
>      these.  */
>   void fchangemode (FILE *fp, int kept_flags, int added_flags);

I agree with the idea of adding a new API; so I won't commit my initial
patch.  Rather, I'll rework it along the lines of one of your
suggestions.  Right now, I'm leaning towards the second version,
although I'm not exactly that's the best API.  Maybe even a third option:

/* Take the existing mode of FILE, and change it such that
   mode bits in MASK are set to VALUE.  Valid bits for MASK
   and VALUE are O_APPEND, O_TEXT, and O_BINARY.  */
int xfchangemode (FILE *, int mask, int value);

where coreutils would use
if (O_BINARY && ! isatty (STDOUT_FILENO))
  xfchangemode (stdout, O_BINARY, O_BINARY);

while xfreopen(NULL,"wb",stdout) translates to:
xfchangemode (stdout, O_APPEND | O_BINARY, O_BINARY);

Or even simpler:

/* Ensure that STREAM is binary.  The append mode of
   STREAM is left untouched.  */
int fsetbinary (FILE *);

where coreutils uses:
if (O_BINARY && ! isatty (STDOUT_FILENO))
  fsetbinary (stdout);

Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]