Subject: write and fwrite send extra bytes when writing binary data to stdout
From: Emilio Hernandez-Garcia <[email-masked]>
To: [email-masked]
Date: Tue, 25 Feb 2003 17:53:47 +0100

Hi all,
This code is supposed to use 'write' to write in binary format a float 
array of 12
elements (thus 4*12=48 bytes) to stdout (on execution it is redirected 
to a file):

/* ************************************* */
#include <stdio.h>
main()  {
  float const vector[12]={1.,0.2,2.,0.,1.,-1.,512.,512.,0.01,100.,1.,0.};
  fprintf(stderr," %ld bytes writen to stdout\n",write(1,vector,48));
}
/* *************************************** */

- I compile it with gcc 3.2-3 under cygwin 1.3.17-1.
- The fprintf command tell us that the program has written to stdout 48 
bytes.  But looking
at the actual file size, it is of 49 bytes.
- Examining the file with an hex viewer, there is an extra byte 0D
between the 8th and the 9th element of the array. This makes the file
useless to be used by external applications.
- The extra byte does not appear if you change some of the numbers (for 
example replace 0.01 by
0.001) but if you try to write a large enough array, extra bytes always 
appear somewhere.
- The same behavior appears by using fwrite(...,stdout). It does not
happen if fwrite writes to a file open with fopen. I've tried the same 
code under gcc and
linux Suse7.*, several versions of c compilers under SGI and HP unix, 
... ,and the extra bytes never appear.

if you try to read the file generated under cygwin with read (or 
fread(...,stdin)) the behavior is still
more curious: the following code reads 'vector' from stdin (which on
execution is conected to the binary file written before):

/* ************************************** */
#include <stdio.h>
main()  {
        float vector[12];
        int i;
        fprintf(stderr,"%ld bytes read\n",read(0,vector,48));
        for( i=0; i<12; i++){fprintf(stderr,"%f ", vector[i]);}
}
/* ************************************** */

The fprintf statement tell us that the program has read 47 bytes, which
reflects some error. But in fact it has read 49, since all the numbers
in the file have been read correctly.

Still more: if you read stdin from a file written by the first program under
linux, where it behaves correctly and outputs 48 bytes, the reading 
program also
reads in the numbers correctly, and reports the correct byte count.

All of this is quite puzzling, and frustrating when trying to use cygwin
to write binary to be piped to other applications.  Any insight?

I attach the output of 'cygcheck -s -v -r > cygcheck.out'
-- 
                Emilio Hernandez-Garcia

Instituto Mediterraneo de Estudios Avanzados (IMEDEA) Tel:34 971 171307
CSIC-UIB, Campus Universitat Illes Balears            Fax:34 971 172309
E-07122 Palma de Mallorca                  e-mail: [email-masked]
Spain                                 http://www.imedea.uib.es/~emilio/

Cygwin Win95/NT Configuration Diagnostics
Current System Time: Tue Feb 25 17:04:07 2003

Windows XP Professional Ver 5.1 Build 2600 

Path:	C:\cygwin\usr\local\bin
	C:\cygwin\bin
	C:\cygwin\bin
	c:\Archivos de programa\texmf\miktex\bin
	c:\WINDOWS\system32
	c:\WINDOWS
	c:\WINDOWS\System32\Wbem
	c:\Archivos de programa\Archivos comunes\Adaptec Shared\System
	c:\WINDOWS\system32\config\systemprofile\bin
	d:\Users\Emilio\bin
	c:\matlab6p5\bin\win32

SysDir: C:\WINDOWS\System32
WinDir: C:\WINDOWS

HOME = `C:\cygwin\home\Emilio'
MAKE_MODE = `unix'
PWD = `/home/Emilio/Mis Documentos/cgle/coupled/WritingCFtests'
USER = `Emilio'

ALLUSERSPROFILE = `C:\Documents and Settings\All Users'
APPDATA = `D:\Users\Emilio\Datos de programa'
CLASSPATH = `"C:\Archivos de programa\JavaSoft\JRE\1.3.1_02\lib\ext\QTJava.zip"'
CLIENTNAME = `Console'
COMMONPROGRAMFILES = `C:\Archivos de programa\Archivos comunes'
COMPUTERNAME = `EMILIOPC'
COMSPEC = `C:\WINDOWS\system32\cmd.exe'
HOMEDRIVE = `C:'
HOMEPATH = `\Users\Emilio'
LOGONSERVER = `\\EMILIOPC'
MANPATH = `:/usr/ssl/man'
NUMBER_OF_PROCESSORS = `1'
OLDPWD = `/home/Emilio/Mis Documentos/cgle/coupled'
OS = `Windows_NT'
PATHEXT = `.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH'
PROCESSOR_ARCHITECTURE = `x86'
PROCESSOR_IDENTIFIER = `x86 Family 15 Model 2 Stepping 4, GenuineIntel'
PROCESSOR_LEVEL = `15'
PROCESSOR_REVISION = `0204'
PROGRAMFILES = `C:\Archivos de programa'
PROMPT = `$P$G'
PS1 = `\[\033]0;\w\007
\033[32m\]\u@\h \[\033[33m\w\033[0m\]
$ '
QTJAVA = `"C:\Archivos de programa\JavaSoft\JRE\1.3.1_02\lib\ext\QTJava.zip"'
SESSIONNAME = `Console'
SHLVL = `1'
SYSTEMDRIVE = `C:'
SYSTEMROOT = `C:\WINDOWS'
TEMP = `d:\Users\Emilio\CONFIG~1\Temp'
TERM = `cygwin'
TMP = `d:\Users\Emilio\CONFIG~1\Temp'
USERDOMAIN = `EMILIOPC'
USERNAME = `Emilio'
USERPROFILE = `D:\Users\Emilio'
WINDIR = `C:\WINDOWS'
_ = `/usr/bin/cygcheck'

HKEY_CURRENT_USER\Software\Cygnus Solutions
HKEY_CURRENT_USER\Software\Cygnus Solutions\Cygwin
HKEY_CURRENT_USER\Software\Cygnus Solutions\Cygwin\mounts v2
HKEY_CURRENT_USER\Software\Cygnus Solutions\Cygwin\Program Options
HKEY_CURRENT_USER\Software\CygnusSoftware
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\AutoExploreSettings
  (default) = 0x00000001
  FindNumber = 0x00000000
  SavePoint = 0x0000001e
  BaseName = `D:\transfer3\Fract&Chaos\cygnus\Fractal eXtreme\autofound0000.fx'
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\LeftButton
  (default) = 0x00000000
  ShiftClick = 0x00000001
  CtrlClick = 0x00000002
  AltClick = 0x00000003
  CtrlShiftClick = 0x00000004
  AltShiftClick = 0x00000005
  CtrlAltClick = 0x00000005
  CtrlShiftAltClick = 0x00000005
  DoubleClick = 0x00000000
  ShiftDoubleClick = 0x00000002
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\ProjectSettings
  (default) = `False'
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\RightButton
  (default) = 0x00000000
  ShiftClick = 0x00000001
  CtrlClick = 0x00000002
  AltClick = 0x00000003
  CtrlShiftClick = 0x00000004
  AltShiftClick = 0x00000005
  CtrlAltClick = 0x00000005
  CtrlShiftAltClick = 0x00000005
  DoubleClick = 0x00000004
  ShiftDoubleClick = 0x00000006
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\StatusInfo
  (default) = `False'
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\VariousSettings
  (default) = `0.000000'
  ZoomName = `'
  MaxCalcSize = 0x00000003
  MaxDragCalcSize = 0x00000001
  MaxDragFrameRate = 0x00000005
  CustomZoom = `True'
  Advanced = `False'
  ShowSplash = `True'
  ZoomSpeed = 0x00000002
  PaletteLoading = 0x00000000
  ZoomDest = 0x00000001
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions\About
  (default) = 0x0000004d
  TopEdge = 0x00000073
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions\FractalSelector
  (default) = 0x0000001a
  TopEdge = 0x0000004f
  Width = 0x00000167
  Height = 0x00000154
  Zoomed = `False'
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions\MainWindow
  (default) = 0x00000016
  TopEdge = 0x0000001d
  Width = 0x0000041a
  Height = 0x000002e6
  Zoomed = `False'
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions\PreSale
  (default) = 0x0000001a
  TopEdge = 0x0000004f
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions\Reminder
  (default) = 0x0000001a
  TopEdge = 0x0000004f
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\WindowPositions\TipOfTheDay
  (default) = 0x0000001a
  TopEdge = 0x0000004f
HKEY_CURRENT_USER\Software\CygnusSoftware\FractaleXtreme\ZoomSettings
  (default) = `D:\transfer3\Fract&Chaos\cygnus\Fractal eXtreme\Movies\'
  StartZoom = 0x00000000
HKEY_CURRENT_USER\Software\CygnusSoftware\FXMPlay
HKEY_CURRENT_USER\Software\CygnusSoftware\FXMPlay\WindowPositions
HKEY_CURRENT_USER\Software\CygnusSoftware\FXMPlay\WindowPositions\ZoomPlayer
  (default) = 0x00000000
  TopEdge = 0x00000104
  Width = 0x000002da
  Height = 0x000001a3
  Zoomed = `False'
HKEY_CURRENT_USER\Software\CygnusSoftware\FXMPlay\WindowPositions\ZoomPlayerDialog
  (default) = 0x00000000
  TopEdge = 0x00000000
  Width = 0x0000015a
  Height = 0x00000104
  Zoomed = `False'
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2
  (default) = `/cygdrive'
  cygdrive flags = 0x00000020
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/
  (default) = `C:\cygwin'
  flags = 0x00000008
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/bin
  (default) = `C:\cygwin/bin'
  flags = 0x00000008
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/lib
  (default) = `C:\cygwin/lib'
  flags = 0x00000008
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\Program Options

a:  fd           N/A    N/A                    
c:  hd  NTFS   10001Mb  64% CP CS UN PA FC     Cdisk
d:  hd  NTFS   35000Mb  19% CP CS UN PA FC     Ddisk
e:  cd           N/A    N/A                    
f:  net NTFS   228856Mb  20% CP CS    PA        emilio

C:\cygwin      /          system  textmode
C:\cygwin/bin  /usr/bin   system  textmode
C:\cygwin/lib  /usr/lib   system  textmode
.              /cygdrive  user    textmode,cygdrive

Found: C:\cygwin\bin\bash.exe
Found: C:\cygwin\bin\cat.exe
Found: C:\cygwin\bin\cpp.exe
Found: C:\cygwin\bin\find.exe
Found: C:\cygwin\bin\gcc.exe
Found: C:\cygwin\bin\gdb.exe
Found: C:\cygwin\bin\ld.exe
Found: C:\cygwin\bin\ls.exe
Found: C:\cygwin\bin\make.exe
Found: C:\cygwin\bin\sh.exe

  644k 2002/12/08 C:\cygwin\bin\cygcrypto.dll - os=4.0 img=1.0 sys=4.0
                  "cygcrypto.dll" v0.0 ts=2002/12/8 20:57
   45k 2001/04/25 C:\cygwin\bin\cygform5.dll - os=4.0 img=1.0 sys=4.0
                  "cygform5.dll" v0.0 ts=2001/4/25 7:28
   35k 2002/01/09 C:\cygwin\bin\cygform6.dll - os=4.0 img=1.0 sys=4.0
                  "cygform6.dll" v0.0 ts=2002/1/9 7:03
   19k 2002/02/20 C:\cygwin\bin\cyggdbm.dll - os=4.0 img=1.0 sys=4.0
                  "cyggdbm.dll" v0.0 ts=2002/2/20 4:05
   17k 2001/06/28 C:\cygwin\bin\cyghistory4.dll - os=4.0 img=1.0 sys=4.0
                  "cyghistory4.dll" v0.0 ts=2001/1/7 5:34
   20k 2002/10/10 C:\cygwin\bin\cyghistory5.dll - os=4.0 img=1.0 sys=4.0
                  "cyghistory5.dll" v0.0 ts=2002/10/10 19:28
  929k 2002/06/24 C:\cygwin\bin\cygiconv-2.dll - os=4.0 img=1.0 sys=4.0
                  "cygiconv-2.dll" v0.0 ts=2002/6/24 20:24
   22k 2001/12/13 C:\cygwin\bin\cygintl-1.dll - os=4.0 img=1.0 sys=4.0
                  "cygintl-1.dll" v0.0 ts=2001/12/13 10:28
   28k 2002/09/20 C:\cygwin\bin\cygintl-2.dll - os=4.0 img=1.0 sys=4.0
                  "cygintl-2.dll" v0.0 ts=2002/9/20 5:13
   26k 2001/04/25 C:\cygwin\bin\cygmenu5.dll - os=4.0 img=1.0 sys=4.0
                  "cygmenu5.dll" v0.0 ts=2001/4/25 7:27
   20k 2002/01/09 C:\cygwin\bin\cygmenu6.dll - os=4.0 img=1.0 sys=4.0
                  "cygmenu6.dll" v0.0 ts=2002/1/9 7:03
  156k 2001/04/25 C:\cygwin\bin\cygncurses++5.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses++5.dll" v0.0 ts=2001/4/25 7:29
  175k 2002/01/09 C:\cygwin\bin\cygncurses++6.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses++6.dll" v0.0 ts=2002/1/9 7:03
  226k 2001/04/25 C:\cygwin\bin\cygncurses5.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses5.dll" v0.0 ts=2001/4/25 7:17
  202k 2002/01/09 C:\cygwin\bin\cygncurses6.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses6.dll" v0.0 ts=2002/1/9 7:03
   15k 2001/04/25 C:\cygwin\bin\cygpanel5.dll - os=4.0 img=1.0 sys=4.0
                  "cygpanel5.dll" v0.0 ts=2001/4/25 7:27
   12k 2002/01/09 C:\cygwin\bin\cygpanel6.dll - os=4.0 img=1.0 sys=4.0
                  "cygpanel6.dll" v0.0 ts=2002/1/9 7:03
   40k 2001/11/21 C:\cygwin\bin\cygpcre.dll - os=4.0 img=1.0 sys=4.0
                  "cygpcre.dll" v0.0 ts=2001/11/21 23:15
   39k 2001/11/21 C:\cygwin\bin\cygpcreposix.dll - os=4.0 img=1.0 sys=4.0
                  "cygpcreposix.dll" v0.0 ts=2001/11/21 23:15
   22k 2002/06/09 C:\cygwin\bin\cygpopt-0.dll - os=4.0 img=1.0 sys=4.0
                  "cygpopt-0.dll" v0.0 ts=2002/6/9 7:45
  108k 2001/06/28 C:\cygwin\bin\cygreadline4.dll - os=4.0 img=1.0 sys=4.0
                  "cygreadline4.dll" v0.0 ts=2001/1/7 5:34
  127k 2002/10/10 C:\cygwin\bin\cygreadline5.dll - os=4.0 img=1.0 sys=4.0
                  "cygreadline5.dll" v0.0 ts=2002/10/10 19:28
  165k 2002/12/08 C:\cygwin\bin\cygssl.dll - os=4.0 img=1.0 sys=4.0
                  "cygssl.dll" v0.0 ts=2002/12/8 20:57
  550k 2002/12/16 C:\cygwin\bin\cygtcl83.dll - os=4.0 img=1.0 sys=4.0
                  "cygtcl83.dll" v0.0 ts=2002/12/16 3:49
   12k 2002/12/16 C:\cygwin\bin\cygtclpip83.dll - os=4.0 img=1.0 sys=4.0
  217k 2002/12/16 C:\cygwin\bin\cygtix4183.dll - os=4.0 img=1.0 sys=4.0
                  "cygtix4183.dll" v0.0 ts=2002/12/16 3:53
  830k 2002/12/16 C:\cygwin\bin\cygtk83.dll - os=4.0 img=1.0 sys=4.0
                  "cygtk83.dll" v0.0 ts=2002/12/16 3:52
   50k 2002/03/12 C:\cygwin\bin\cygz.dll - os=4.0 img=1.0 sys=4.0
                  "cygz.dll" v0.0 ts=2002/3/12 5:38
  923k 2002/11/27 C:\cygwin\bin\cygwin1.dll - os=4.0 img=1.0 sys=4.0
                  "cygwin1.dll" v0.0 ts=2002/11/28 0:54
    Cygwin DLL version info:
        DLL version: 1.3.17
        DLL epoch: 19
        DLL bad signal mask: 19005
        DLL old termios: 5
        DLL malloc env: 28
        API major: 0
        API minor: 67
        Shared data: 3
        DLL identifier: cygwin1
        Mount registry: 2
        Cygnus registry name: Cygnus Solutions
        Cygwin registry name: Cygwin
        Program options name: Program Options
        Cygwin mount registry name: mounts v2
        Cygdrive flags: cygdrive flags
        Cygdrive prefix: cygdrive prefix
        Cygdrive default prefix: 
        Build date: Wed Nov 27 18:54:29 EST 2002
        Shared id: cygwin1S3

Cygwin Package Information
Last downloaded files to: D:\src\cygwin
Last downloaded files from: ftp://ftp.rediris.es/mirror/cygwin

Package             Version             
_update-info-dir    00102-1             
ash                 20020731-1          
base-files          1.1-1               
base-passwd         1.0-1               
bash                2.05b-8             
bc                  1.06-1              
binutils            20021117-1          
clear               1.0-1               
cygutils            1.1.3-1             
cygwin              1.3.17-1            
cygwin-doc          1.2-1               
diff                1.0-1               
diffutils           2.8.1-1             
fileutils           4.1-1               
findutils           4.1.7-4             
gawk                3.1.1-5             
gcc                 3.2-3               
gcc-mingw           20020817-3          
gdb                 20021215-1          
gdbm                1.8.0-4             
grep                2.5-1               
groff               1.18.1-2            
gzip                1.3.3-4             
less                378-1               
libiconv2           1.8-2               
libintl1            0.10.40-1           
libintl2            0.11.5-1            
libncurses5         5.2-1               
libncurses6         5.2-8               
libpopt0            1.6.4-4             
libreadline4        4.1-2               
libreadline5        4.3-2               
login               1.7-1               
make                3.79.1-7            
man                 1.5j-1              
mingw-runtime       2.2-1               
more                2.11o-1             
ncurses             5.2-8               
newlib-man          20020801            
openssl             0.9.6h-1            
pcre                3.7-1               
readline            4.3-2               
sed                 4.0.1-1             
sh-utils            2.0-3               
tar                 1.13.25-1           
tcsh                6.11.00-5           
termcap             20020930-1          
terminfo            5.2-3               
texinfo             4.2-4               
textutils           2.0.21-1            
w32api              2.1-1               
wget                1.8.2-1             
which               1.5-1               
zlib                1.1.4-1             

Use -h to see help about each section

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/