OSDN Git Service

Added missing article
[pf3gnuchains/pf3gnuchains4x.git] / winsup / doc / pathnames.sgml
1 <sect1 id="using-pathnames"><title>Mapping path names</title>
2
3 <sect2><title>Introduction</title>
4
5 <para>Cygwin supports both Win32- and POSIX-style paths, where
6 directory delimiters may be either forward or back slashes.  UNC
7 pathnames (starting with two slashes and a network name) are also
8 supported.</para>
9
10 <para>POSIX operating systems (such as Linux) do not have the concept
11 of drive letters.  Instead, all absolute paths begin with a
12 slash (instead of a drive letter such as "c:") and all file systems
13 appear as subdirectories (for example, you might buy a new disk and
14 make it be the <filename>/disk2</filename> directory).</para>
15
16 <para>Because many programs written to run on UNIX systems assume
17 the existance of a single unified POSIX file system structure, Cygwin
18 maintains a special internal POSIX view of the Win32 file system
19 that allows these programs to successfully run under Windows.  Cygwin
20 uses this mapping to translate between Win32 and POSIX paths as
21 necessary.</para>
22
23 </sect2>
24
25 <sect2 id="mount-table"><title>The Cygwin Mount Table</title>
26
27 <para>The <command>mount</command> utility program is used to
28 to map Win32 drives and network shares into Cygwin's internal POSIX
29 directory tree.  This is a similar concept to the typical UNIX mount
30 program.  For those people coming from a Windows background, the
31 <command>mount</command> utility is very similar to the old DOS
32 <command>join</command>, in that it makes your drive letters appear as
33 subdirectories somewhere else.</para>
34
35 <para>The mapping is stored in the current user's Cygwin
36 <FirstTerm>mount table</FirstTerm> in the Windows registry so that the
37 information will be retrieved next time the user logs in.  Because it
38 is sometimes desirable to have system-wide as well as user-specific
39 mounts, there is also a system-wide mount table that all Cygwin users
40 inherit.  The system-wide table may only be modified by a user with
41 the appropriate priviledges (Administrator priviledges in Windows
42 NT).</para>
43
44 <para>The current user's table is located under
45 "HKEY_CURRENT_USER/Software/Cygnus Solutions/Cygwin/mounts
46 v&lt;version&gt;"
47 where &lt;version&gt; is the latest registry version associated with
48 the Cygwin library (this version is not the same as the release
49 number).  The system-wide table is located under the same subkeys
50 under HKEY_LOCAL_SYSTEM.</para>
51
52 <para>By default, the POSIX root <filename>/</filename> points to the
53 system partition but it can be relocated to any directory in the
54 Windows file system using the <command>mount</command> command.
55 Whenever Cygwin generates a POSIX path from a Win32 one, it uses the
56 longest matching prefix in the mount table.  Thus, if
57 <filename>C:</filename> is mounted as <filename>/c</filename> and also
58 as <filename>/</filename>, then Cygwin would translate
59 <filename>C:/foo/bar</filename> to <filename>/c/foo/bar</filename>.</para>
60
61 <para>Invoking <command>mount</command> without any arguments displays
62 Cygwin's current set of mount points.
63 In the following example, the C
64 drive is the POSIX root and D drive is mapped to
65 <filename>/d</filename>.  Note that in this case, the root mount is a
66 system-wide mount point that is visible to all users running Cygwin
67 programs, whereas the <filename>/d</filename> mount is only visible
68 to the current user.</para>
69
70 <example>
71 <title>Displaying the current set of mount points</title>
72 <screen>
73 <prompt>c:\cygnus\&gt;</prompt> <userinput>mount</userinput>
74 Device           Directory           Type        Flags
75 D:               /d                  user        textmode
76 C:               /                   system      textmode
77 </screen>
78 </example>
79
80 <para>You can also use the <command>mount</command> command to add
81 new mount points, and the <command>umount</command> to delete
82 them.  See <Xref Linkend="mount"> and <Xref Linkend="umount"> for more
83 information on how to use these utilities to set up your Cygwin POSIX
84 file system.</para>
85
86 <para>Whenever Cygwin cannot use any of the existing mounts to convert
87 from a particular Win32 path to a POSIX one, Cygwin will
88 automatically default to an imaginary mount point under the default POSIX
89 path <filename>/cygdrive</filename>.  For example, if Cygwin accesses
90 <filename>Z:\foo</filename> and the Z drive is not currently in the
91 mount table, then <filename>Z:\</filename> would be automatically
92 converted to <filename>/cygdrive/Z</filename>.  The default
93 prefix of <filename>/cygdrive</filename> may be changed (see the
94 <Xref Linkend="mount"> for more information).</para>
95
96 <para>It is possible to assign some special attributes to each mount
97 point.  Automatically mounted partitions are displayed as "auto"
98 mounts.  Mounts can also be marked as either "textmode" or "binmode"
99 -- whether text files are read in the same manner as binary files by
100 default or not (see <Xref Linkend="using-textbinary"> for more
101 information on text and binary modes.</para>
102
103 </sect2>
104
105 <sect2><title>Cygwin Mount Table Strategies</title>
106
107 <para>Which set of mounts is right for a given Cygwin user depends
108 largely on how closely you want to simulate a POSIX environment,
109 whether you mix Windows and Cygwin programs, and how many drive
110 letters you are using.  If you want to be very POSIX-like (assuming
111 "CygwinRoot" is the top directory of your Cygwin distribution), you may
112 want to do something like this:</para>
113
114 <example><title>POSIX-like mount setup</title>
115 <screen>
116 <prompt>C:\&gt;</prompt> <userinput>mount c:\Cygnus\CygwinRoot /</userinput>
117 <prompt>C:\&gt;</prompt> <userinput>mount c:\ /c</userinput>
118 <prompt>C:\&gt;</prompt> <userinput>mount d:\ /d</userinput>
119 <prompt>C:\&gt;</prompt> <userinput>mount e:\ /cdrom</userinput>
120 </screen>
121 </example>
122
123 <para>However, if you mix Windows and Cygwin programs a lot, you might
124 want to create an "identity" mapping, so that conversions between the
125 two (see <Xref Linkend="cygpath">) can be eliminated:</para>
126
127 <example><title>Identity mount setup</title>
128 <screen>
129 <prompt>C:\&gt;</prompt> <userinput>mount c:\ /</userinput>
130 <prompt>C:\&gt;</prompt> <userinput>mount d:\foo /foo</userinput>
131 <prompt>C:\&gt;</prompt> <userinput>mount d:\bar /bar</userinput>
132 <prompt>C:\&gt;</prompt> <userinput>mount e:\grill /grill</userinput>
133 </screen>
134 </example>
135
136 <para>You'd have to repeat this for all top-level subdirectories on
137 all drives, but then you'd always have the top-level directories
138 available as the same names in both systems.</para>
139
140 </sect2>
141
142 <sect2><title>Additional Path-related Information</title>
143
144 <para>The <command>cygpath</command> program provides the ability to
145 translate between Win32 and POSIX pathnames in shell scripts. See
146 <Xref Linkend="cygpath"> for the details.</para>
147
148 <para>The <EnVar>HOME</EnVar>, <EnVar>PATH</EnVar>, and
149 <EnVar>LD_LIBRARY_PATH</EnVar> environment variables are automatically
150 converted from Win32 format to POSIX format (e.g. from
151 <filename>C:\cygnus\cygwin-b20\H-i586-cygwin32\bin</filename> to
152 <filename>/bin</filename>, if there was a mount from that Win32 path to
153 that POSIX path) when a Cygwin process first starts.</para>
154
155 <para>Symbolic links can also be used to map Win32 pathnames to POSIX.
156 For example, the command
157 <command>ln -s //pollux/home/joe/data /data</command> would have about
158 the same effect as creating a mount point from
159 <filename>//pollux/home/joe/data</filename> to <filename>/data</filename>
160 using <command>mount</command>, except that symbolic links cannot set
161 the default file access mode.  Other differences are that the mapping is
162 distributed throughout the file system and proceeds by iteratively
163 walking the directory tree instead of matching the longest prefix in a
164 kernel table.  Note that symbolic links will only work on network
165 drives that are properly configured to support the "system" file
166 attribute.  Many do not do so by default (the Unix Samba server does
167 not by default, for example).</para>
168
169 </sect2>
170
171 </sect1>
172
173 <sect1 id="using-specialnames"><title>Special filenames</title>
174
175 <sect2> <title>DOS devices</title>
176
177 <para>Windows filenames invalid under Windows are also invalid under
178 Cygwin.  This means that base filenames such as 
179 <filename>AUX</filename>, <filename>COM1</filename>,
180 <filename>LPT1</filename> or <filename>PRN</filename> (to name a few)
181 cannot be used in a regular Cygwin Windows or POSIX path, even with an
182 extension (<filename>prn.txt</filename>). However the special names can be
183 used as filename extensions (<filename>file.aux</filename>). You can use
184 the special names as you would under DOS, for example you can print on your
185 default printer with the command <command>cat filename > PRN</command>
186 (make sure to end with a Form Feed).
187 </para>
188
189 </sect2>
190
191 <sect2> <Title>POSIX devices</title>
192 <para>There is no need to create a POSIX <filename>/dev</filename> 
193 directory as it is simulated within Cygwin automatically.
194 It supports the following devices: <filename>/dev/null</filename>,
195 <filename>/dev/zero</filename>, <filename>/dev/tty</filename>,
196 <filename>/dev/ttyX</filename>, <filename>/dev/ptmx</filename>,
197 <filename>/dev/comX</filename> (the serial ports),
198 <filename>/dev/windows</filename> (the windows message queue),
199 <filename>/dev/random</filename> and <filename>/dev/urandom</filename>.
200 These devices cannot be seen with the command <command>ls /dev</command>
201 although commands such as <command>ls /dev/tty</command> work fine.
202 </para>
203
204 <para>However, on Windows NT/W2K there are different devices which are
205 supported but have to be created as mount points. These are the raw block
206 special devices and tape devices. These devices need a special handling
207 which is enabled through the mount points. The usage of the native Windows
208 device names is not sufficent.
209 </para>
210
211 <para>NT/W2K supports raw block special device support for partitions
212 and drives. The device names for partitions is the drive letter
213 with leading <filename>\\.\</filename>, so the floppy would be
214 <filename>\\.\A:</filename>, the first partition typically
215 <filename>\\.\C:</filename>. Complete drives (except floppies
216 which are supported as partitions only) are named
217 <filename>\\.\PHYSICALDRIVEx</filename>. The <literal>x</literal>
218 is the drive number which you can check in the disk manager.
219 Each drive line has prepended the text "Disk x".
220 </para>
221
222 <para>To access tape drives, NT/W2K uses the file name
223 <filename>\\.\TAPEx</filename>. For example the first installed tape device
224 is named <filename>\\.\tape0</filename>.
225 </para>
226
227 <para>To access those devices you have to mount them and you have to
228 use the posix name of the device to be recognized by Cygwin.
229 The naming convention is simple: The name has to begin with
230 <filename>/dev/</filename> and the rest is as you like. The only
231 exception are tape devices. To identify if the tape device is
232 used as a rewind or a no-rewind device the name must not begin with
233 <literal>n</literal> (rewind) or has to begin with <literal>n</literal>
234 (no-rewind).
235 </para>
236
237 <para>Some examples:</para>
238
239 <screen>
240 mount -b //./A: /dev/fd0              # mount floppy as raw block special
241 mount -b //./physicaldrive1 /dev/hdb  # mount "Disk 1"
242 mount -b //./tape0 /dev/st0           # mount first tape as the rewind device...
243 mount -b //./tape0 /dev/nst0          # ...and as the no-rewind device
244 </screen>
245
246 <para>Note the usage of the <literal>-b</literal> option. It is best to
247 include the -b option when mounting these devices to ensure that all
248 file I/O is in "binary mode".
249 </para>
250
251 </sect2>
252
253 <sect2><title>The .exe extension</title>
254
255 <para> Executable program filenames end with .exe but the .exe need
256 not be included in the command, so that traditional UNIX names can be
257 used.  However, for programs that end in ".bat" and ".com", you cannot
258 omit the extension.
259 </para>
260
261 <para>As a side effect, the <command> ls filename</command> gives
262 information about <filename>filename.exe</filename> if
263 <filename>filename.exe</filename> exists and <filename>filename</filename>
264 does not.  In the same situation the function call
265 <function>stat("filename",..)</function> gives information about
266 <filename>filename.exe</filename>.  The two files can be distinguished
267 by examining their inodes, as demonstrated below.
268 <screen>
269 <prompt>C:\Cygnus\&gt;</prompt> <userinput>ls * </userinput>
270 a      a.exe     b.exe
271 <prompt>C:\Cygnus\&gt;</prompt> <userinput>ls -i a a.exe</userinput>
272 445885548 a       435996602 a.exe
273 <prompt>C:\Cygnus\&gt;</prompt> <userinput>ls -i b b.exe</userinput>
274 432961010 b       432961010 b.exe
275 </screen>
276 If a shell script <filename>myprog</filename> and a program
277 <filename>myprog.exe</filename> coexist in a directory, the program
278 has precedence and is selected for execution of
279 <command>myprog</command>.</para>
280
281 <para>The <command>gcc</command> compiler produces an executable named
282 <filename>filename.exe</filename> when asked to produce
283 <filename>filename</filename>. This allows many makefiles written
284 for UNIX systems to work well under Cygwin.</para>
285
286 <para>Unfortunately, the <command>install</command> and
287 <command>strip</command> commands do distinguish between
288 <filename>filename</filename> and <filename>filename.exe</filename>. They
289 fail when working on a non-existing <filename>filename</filename> even if
290 <filename>filename.exe</filename> exists, thus breaking some makefiles.
291 This problem can be solved by writing <command>install</command> and
292 <command>strip</command> shell scripts to provide the extension ".exe"
293 when needed.
294 </para>
295 </sect2>
296
297 <sect2><title>The @pathnames</title> 
298 <para>To circumvent the limitations on shell line length in the native
299 Windows command shells, Cygwin programs expand their arguments
300 starting with "@" in a special way.  If a file
301 <filename>pathname</filename> exists, the argument
302 <filename>@pathname</filename> expands recursively to the content of
303 <filename>pathname</filename>. Double quotes can be used inside the
304 file to delimit strings containing blank space. 
305 Embedded double quotes must be repeated.
306 In the following example compare the behaviors of the bash built-in
307 <command>echo</command> and of the program <command>/bin/echo</command>.</para>
308
309 <example><title> Using @pathname</title>
310 <screen>
311 <prompt>/Cygnus$</prompt> <userinput>echo  'This   is   "a     long"  line' > mylist</userinput>
312 <prompt>/Cygnus$</prompt> <userinput>echo @mylist</userinput>
313 @mylist
314 <prompt>/Cygnus$</prompt> <userinput>/bin/echo @mylist</userinput>
315 This is a     long line
316 <prompt>/Cygnus$</prompt> <userinput>rm mylist</userinput>
317 <prompt>/Cygnus$</prompt> <userinput>/bin/echo @mylist</userinput>
318 @mylist
319 </screen>
320 </example>
321 </sect2> 
322 </sect1>