textbinary.sgml
来自「cygwin, 著名的在win32下模拟unix操作系统的东东」· SGML 代码 · 共 182 行
SGML
182 行
<sect1 id="using-textbinary"><title>Text and Binary modes</title><sect2> <title>The Issue</title><para>On a UNIX system, when an application reads from a file it getsexactly 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 canbe opened in one of two modes, binary or text. In the binary mode thesystem behaves exactly as in UNIX. However in text mode there aremajor differences:</para><OrderedList Numeration="Loweralpha" Spacing="Compact"><listitem><para>On writing in text mode, a NL (\n, ^J) is transformed into the sequence CR (\r, ^M) NL.</para></listitem><listitem><para>On reading in text mode, a CR followed by an NL is deleted and a ^Zcharacter signals the end of file.</para></listitem></OrderedList><para>This can wreak havoc with the seek/fseek calls since the numberof bytes actually in the file may differ from that seen by theapplication.</para><para>The mode can be specified explicitly as explained in the Programmingsection below. In an ideal DOS/Windows world, all programs using lines asrecords (such as <command>bash</command>, <command>make</command>,<command>sed</command> ...) would open files (and change the mode of theirstandard 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 dealexplicitly with object files specify binary mode (this is the case of<command>od</command>, which is helpful to diagnose CR problems). Mostother programs (such as <command>cat</command>, <command>cmp</command>,<command>tr</command>) use the default mode.</para></sect2><sect2><title>The default Cygwin behavior</title><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><OrderedList Numeration="Loweralpha"><listitem><para>If the file 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 mountflag. If the file is a symbolic link, the mode of the target file systemapplies.</para></listitem><listitem><para>If the file appears to reside on a file system that is not mounted(as can happen when the path contains a drive letter), the default is text.</para></listitem><listitem><para>Pipes and non-file devices are opened in binary mode,except if the <EnVar>CYGWIN</EnVar> environment variable contains<literal>nobinmode</literal>.</para><warning><Title>Warning!</Title><para>In b20.1 of 12/98, a file will be openedin binary mode if any of the following conditions hold:</para> <OrderedList Numeration="arabic" Spacing="Compact"><listitem><para>binary mode is specified in the open call</para></listitem><listitem><para><envar>CYGWIN</envar> contains <literal>binmode</literal></para></listitem><listitem><para>the file resides in a binary mounted partition</para></listitem><listitem><para>the file is not a disk file</para></listitem></OrderedList></warning></listitem><listitem><para>When a Cygwin program is launched by a shell, its standard input,output and error are in binary mode if the <envar>CYGWIN</envar> variablecontains <literal>tty</literal>, else in text mode, except if they are pipedor redirected.</para><para> When redirecting, the Cygwin shells uses rules (a-c). Forthese shells the relevant value of <envar>CYGWIN</envar> is that at the timethe shell was launched and not that at the time the program is executed.Non-Cygwin shells always pipe and redirect with binary mode. Withnon-Cygwin shells the commands <command> cat filename | program </command>and <command> program < filename </command> are not equivalent when<filename>filename</filename> is on a text-mounted partition. </para></listitem></OrderedList></sect2><sect2><title>Example</title><para>To illustrate the various rules, we provide scripts to delete CRsfrom files by using the <command>tr</command> program, which can only writeto standard output. The script</para><screen>#!/bin/sh# Remove \r from the file given as argumenttr -d '\r' < "$1" > "$1".nocr</screen><para> will not work on a text mounted systems because the \r will be reintroduced on writing. However scripts such as </para><screen>#!/bin/sh# Remove \r from the file given as argumenttr -d '\r' | gzip | gunzip > "$1".nocr</screen><para>and the .bat file</para><screen>REM Remove \r from the file given as argument@echo offtr -d \r < %1 > %1.nocr</screen><para> work fine. In the first case (assuming the pipes are binary)we rely on <command>gunzip</command> to set its output to binary mode,possibly overriding the mode used by the shell.In the second case we rely on the DOS shell to redirect in binary mode.</para></sect2><sect2><title>Binary or text?</title><para>UNIX programs that have been written for maximum portabilitywill know the difference between text and binary files and actappropriately under Cygwin. For those programs, the text mode defaultis a good choice. Programs included in official Cygwin distributionsshould work well in the default mode. </para><para>Text mode makes it much easier to mix files between Cygwin andWindows programs, since Windows programs will usually use the CRLFformat. Unfortunately you may still have some problems with textmode. First, some of the utilities included with Cygwin do not yetspecify binary mode when they should, e.g. <command>cat</command> willnot work with binary files (input will stop at ^Z, CRs will beintroduced in the output). Second, you will introduce CRs in textfiles you write, which can cause problems when moving them back to aUNIX system. </para><para>If you are mounting a remote file system from a UNIX machine,or moving files back and forth to a UNIX machine, you may want toaccess the files in binary mode. The text files found there will normallybe in UNIX NL format, and you would want any files put there by Cygwinprograms to be stored in a format understood by UNIX.Be sure to remove CRs from all Makefiles andshell scripts and make sure that you only edit the files withDOS/Windows editors that can cope with and preserve NL terminated lines.</para><para>Note that you can decide this on a disk by disk basis (forexample, mounting local disks in text mode and network disks in binarymode). You can also partition a disk, for example by mounting<filename>c:</filename> in text mode, and <filename>c:\home</filename>in binary mode.</para></sect2><sect2><title>Programming</title><para>In the <function>open()</function> function call, binary mode can bespecified 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><para>In the <function>fopen()</function> function call, binary mode can bespecified by adding a <literal>b</literal> to the mode string. There is nodirect way to specify text mode.</para><para>The mode of a file can be changed by the call<function>setmode(fd,mode)</function> where <literal>fd</literal> is a filedescriptor (an integer) and <literal>mode</literal> is<literal>O_BINARY</literal> or <literal>O_TEXT</literal>. The functionreturns <literal>O_BINARY</literal> or <literal>O_TEXT</literal> dependingon the mode before the call, and <literal>EOF</literal> on error.</para></sect2></sect1>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?