2013-05-02 07:30:25 +08:00
|
|
|
<?xml version="1.0" encoding='UTF-8'?>
|
|
|
|
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook V4.5//EN"
|
|
|
|
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
<sect1 id="using-textbinary"><title>Text and Binary modes</title>
|
|
|
|
|
2008-07-17 19:49:45 +08:00
|
|
|
<sect2 id="textbin-issue"> <title>The Issue</title>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
<para>On a UNIX system, when an application reads from a file it gets
|
|
|
|
exactly what's in the file on disk and the converse is true for writing.
|
|
|
|
The situation is different in the DOS/Windows world where a file can
|
|
|
|
be opened in one of two modes, binary or text. In the binary mode the
|
2004-02-20 15:26:16 +08:00
|
|
|
system behaves exactly as in UNIX. However on writing in text mode, a
|
|
|
|
NL (\n, ^J) is transformed into the sequence CR (\r, ^M) NL.
|
|
|
|
</para>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
<para>This can wreak havoc with the seek/fseek calls since the number
|
|
|
|
of bytes actually in the file may differ from that seen by the
|
|
|
|
application.</para>
|
|
|
|
|
|
|
|
<para>The mode can be specified explicitly as explained in the Programming
|
|
|
|
section below. In an ideal DOS/Windows world, all programs using lines as
|
|
|
|
records (such as <command>bash</command>, <command>make</command>,
|
|
|
|
<command>sed</command> ...) would open files (and change the mode of their
|
|
|
|
standard input and output) as text. All other programs (such as
|
|
|
|
<command>cat</command>, <command>cmp</command>, <command>tr</command> ...)
|
|
|
|
would use binary mode. In practice with Cygwin, programs that deal
|
|
|
|
explicitly with object files specify binary mode (this is the case of
|
|
|
|
<command>od</command>, which is helpful to diagnose CR problems). Most
|
2010-07-20 02:25:41 +08:00
|
|
|
other programs (such as <command>sed</command>, <command>cmp</command>,
|
2000-02-18 03:38:33 +08:00
|
|
|
<command>tr</command>) use the default mode.</para>
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
2008-07-17 19:49:45 +08:00
|
|
|
<sect2 id="textbin-default"><title>The default Cygwin behavior</title>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
<para>The Cygwin system gives us some flexibility in deciding how files
|
|
|
|
are to be opened when the mode is not specified explicitly.
|
|
|
|
The rules are evolving, this section gives the design goals.</para>
|
2009-03-18 19:05:45 +08:00
|
|
|
|
2004-12-25 05:58:38 +08:00
|
|
|
<orderedlist numeration="loweralpha">
|
2000-02-18 03:38:33 +08:00
|
|
|
<listitem>
|
2005-03-06 10:46:54 +08:00
|
|
|
<para>If the filename is specified as a POSIX path and it appears to
|
|
|
|
reside on a file system that is mounted (i.e. if its pathname starts
|
|
|
|
with a directory displayed by <command>mount</command>), then the
|
|
|
|
default is specified by the mount flag. If the file is a symbolic link,
|
|
|
|
the mode of the target file system applies.</para>
|
2000-02-18 03:38:33 +08:00
|
|
|
</listitem>
|
2009-03-18 19:05:45 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
<listitem>
|
2005-03-06 10:46:54 +08:00
|
|
|
<para>If the file is specified via a MS-DOS pathname (i.e., it contains a
|
|
|
|
backslash or a colon), the default is binary.
|
2000-02-18 03:38:33 +08:00
|
|
|
</para>
|
|
|
|
</listitem>
|
2009-03-18 19:05:45 +08:00
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>Pipes, sockets and non-file devices are opened in binary mode.
|
|
|
|
For pipes opened through the pipe() system call you can use the setmode()
|
|
|
|
function (see <xref linkend="textbin-devel"></xref> to switch to textmode.
|
|
|
|
For pipes opened through popen(), you can simply specify text or binary
|
|
|
|
mode just like in calls to fopen().</para>
|
|
|
|
</listitem>
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
<listitem>
|
2009-03-18 19:05:45 +08:00
|
|
|
<para>Sockets and other non-file devices are always opened in binary mode.
|
|
|
|
</para>
|
2000-02-18 03:38:33 +08:00
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
2009-03-18 19:05:45 +08:00
|
|
|
<para> When redirecting, the Cygwin shells uses rules (a-d).
|
2000-02-18 03:38:33 +08:00
|
|
|
Non-Cygwin shells always pipe and redirect with binary mode. With
|
|
|
|
non-Cygwin shells the commands <command> cat filename | program </command>
|
2004-01-24 16:09:45 +08:00
|
|
|
and <command> program < filename </command> are not equivalent when
|
2000-02-18 03:38:33 +08:00
|
|
|
<filename>filename</filename> is on a text-mounted partition. </para>
|
2010-03-27 13:26:51 +08:00
|
|
|
<para>The programs <command>u2d</command> and <command>d2u</command> can
|
|
|
|
be used to add or remove CR's from a file. <command>u2d</command> add's CR's before a NL.
|
|
|
|
<command>d2u</command> removes CR's. Use the --help option to these commands
|
|
|
|
for more information.
|
2010-03-28 01:10:23 +08:00
|
|
|
</para>
|
2000-02-18 03:38:33 +08:00
|
|
|
</listitem>
|
2004-12-25 05:58:38 +08:00
|
|
|
</orderedlist>
|
2000-02-18 03:38:33 +08:00
|
|
|
</sect2>
|
|
|
|
|
2008-07-17 19:49:45 +08:00
|
|
|
<sect2 id="textbin-question"><title>Binary or text?</title>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
<para>UNIX programs that have been written for maximum portability
|
|
|
|
will know the difference between text and binary files and act
|
2009-03-18 19:05:45 +08:00
|
|
|
appropriately under Cygwin. Most programs included in the official
|
|
|
|
Cygwin distributions should work well in the default mode. </para>
|
|
|
|
|
|
|
|
<para>Binmode is the best choice usually since it's faster and
|
|
|
|
easier to handle, unless you want to exchange files with native Win32
|
|
|
|
applications. It makes most sense to keep the Cygwin distribution
|
|
|
|
and your Cygwin home directory in binmode and generate text files in
|
|
|
|
binmode (with UNIX LF lineendings). Most Windows applications can
|
|
|
|
handle binmode files just fine. A notable exception is the mini-editor
|
|
|
|
<command>Notepad</command>, which handles UNIX lineendings incorrectly
|
|
|
|
and only produces output files with DOS CRLF lineendings.</para>
|
|
|
|
|
|
|
|
<para>You can convert files between CRLF and LF lineendings by using
|
|
|
|
certain tools in the Cygwin distribution like <command>d2u</command> and
|
|
|
|
<command>u2d</command> from the cygutils package. You can also specify
|
|
|
|
a directory in the mount table to be mounted in textmode so you can use
|
|
|
|
that directory for exchange purposes.</para>
|
|
|
|
|
|
|
|
<para>As application programmer you can decide on a file by file base,
|
|
|
|
or you can specify default open modes depending on the purpose for which
|
|
|
|
the application open files. See the next section for a description of
|
|
|
|
your choices.</para>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
2008-07-17 19:49:45 +08:00
|
|
|
<sect2 id="textbin-devel"><title>Programming</title>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
<para>In the <function>open()</function> function call, binary mode can be
|
|
|
|
specified with the flag <literal>O_BINARY</literal> and text mode with
|
|
|
|
<literal>O_TEXT</literal>. These symbols are defined in
|
|
|
|
<filename>fcntl.h</filename>.</para>
|
|
|
|
|
2010-07-20 02:25:41 +08:00
|
|
|
<para>The <function>mkstemp()</function> and <function>mkstemps()</function>
|
|
|
|
calls force binary mode. Use <function>mkostemp()</function> or
|
|
|
|
<function>mkostemps()</function> with the same flags
|
|
|
|
as <function>open()</function> for more control on temporary files.</para>
|
|
|
|
|
2009-03-18 19:05:45 +08:00
|
|
|
<para>In the <function>fopen()</function> and <function>popen()</function>
|
|
|
|
function calls, binary mode can be specified by adding a <literal>b</literal>
|
|
|
|
to the mode string. Text mode is specified by adding a <literal>t</literal>
|
|
|
|
to the mode string.</para>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
<para>The mode of a file can be changed by the call
|
|
|
|
<function>setmode(fd,mode)</function> where <literal>fd</literal> is a file
|
|
|
|
descriptor (an integer) and <literal>mode</literal> is
|
|
|
|
<literal>O_BINARY</literal> or <literal>O_TEXT</literal>. The function
|
|
|
|
returns <literal>O_BINARY</literal> or <literal>O_TEXT</literal> depending
|
|
|
|
on the mode before the call, and <literal>EOF</literal> on error.</para>
|
|
|
|
|
2009-03-18 19:05:45 +08:00
|
|
|
<para>There's also a convenient way to set the default open modes used
|
|
|
|
in an application by just linking against various object files provided
|
|
|
|
by Cygwin. For instance, if you want to make sure that all files are
|
|
|
|
always opened in binary mode by an application, regardless of the mode
|
|
|
|
of the underlying mount point, just add the file
|
|
|
|
<filename>/lib/binmode.o</filename> to the link stage of the application
|
|
|
|
in your project, like this:</para>
|
|
|
|
|
|
|
|
<screen>
|
|
|
|
$ gcc my_tiny_app.c /lib/binmode.o -o my_tiny_app
|
|
|
|
</screen>
|
|
|
|
|
2010-08-30 21:19:39 +08:00
|
|
|
<para>Starting with Cygwin 1.7.7, you can use the even simpler:</para>
|
|
|
|
|
|
|
|
<screen>
|
|
|
|
$ gcc my_tiny_app.c -lbinmode -o my_tiny_app
|
|
|
|
</screen>
|
|
|
|
|
2009-03-18 19:05:45 +08:00
|
|
|
<para>This adds code which sets the default open mode for all files
|
|
|
|
opened by <command>my_tiny_app</command> to binary for reading and
|
|
|
|
writing.</para>
|
|
|
|
|
2010-08-30 21:19:39 +08:00
|
|
|
<para>Cygwin provides the following libraries and object files to set the
|
|
|
|
default open mode just by linking an application against them:</para>
|
2009-03-18 19:05:45 +08:00
|
|
|
|
|
|
|
<itemizedlist mark="bullet">
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<screen>
|
2010-08-30 21:19:39 +08:00
|
|
|
/lib/libautomode.a - Open files for reading in textmode,
|
|
|
|
/lib/automode.o open files for writing in binary mode
|
2009-03-18 19:05:45 +08:00
|
|
|
</screen>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<screen>
|
2010-08-30 21:19:39 +08:00
|
|
|
/lib/libbinmode.a - Open files for reading and writing in binary mode
|
|
|
|
/lib/binmode.o
|
2009-03-18 19:05:45 +08:00
|
|
|
</screen>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<screen>
|
2010-08-30 21:19:39 +08:00
|
|
|
/lib/libtextmode.a - Open files for reading and writing in textmode
|
|
|
|
/lib/textmode.o
|
2009-03-18 19:05:45 +08:00
|
|
|
</screen>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<screen>
|
2010-08-30 21:19:39 +08:00
|
|
|
/lib/libtextreadmode.a - Open files for reading in textmode,
|
|
|
|
/lib/textreadmode.o keep default behaviour for writing.
|
2009-03-18 19:05:45 +08:00
|
|
|
</screen>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
</sect1>
|