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 ebl...@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature