Chapter 3. Using Cygwin

Table of Contents
Mapping path names
Text and Binary modes
File permissions
Special filenames
The CYGWIN environment variable
Cygwin Utilities

This chapter explains some key differences between the Cygwin environment and traditional UNIX systems. It assumes a working knowledge of standard UNIX commands.

Mapping path names

Win32 and POSIX paths

Cygwin supports both Win32- and POSIX-style paths, using either forward or back slashes as the directory delimiter. UNC pathnames (starting with two slashes and a network name) are also supported.

In POSIX operating systems (such as Linux) there is no concept of "drives" nor drive letters. All absolute paths begin with a slash instead of "c:" and all file systems appear as subdirectories (for example, you might buy a new disk and make it be the /disk2 directory). This practice is simulated by Cygwin to assist in porting POSIX programs to Windows.

The mount program is used to map drives and shares onto the POSIX directory tree, much like as is done by the POSIX mount utility. This is very similar to the old DOS join, in that it makes your drive letters appear as subdirectories somewhere else. By default the POSIX root / points to the system partition but it can be relocated to any directory in the Windows file system.

The mapping between Win32 pathnames and POSIX styles appears in the mount table. Cygwin maps between Win32 and POSIX paths by finding the longest matching prefix in the mount table. This table is stored in in the Windows registry and can be accessed under "HKEY_CURRENT_USER/Software/Cygnus Solutions/CYGWIN.DLL setup/<version>/mounts" where <version> is the latest registry version associated with the Cygwin library (this version is not the same as the release number).

The mount table is displayed with mount and it is modified by mount [-switch] Win32path POSIXpath. The optional switches -b and -t specify a default file mode, a concept detailed in the section called Text and Binary modes.

The following example demonstrates using the mount utility to mount the directory C:\cygnus\cygwin-b20\H-i586-cygwin32\bin to /bin and the network directory \\pollux\home\joe\data to /data. /bin is assumed to already exist. exist

Example 3-1. Adding mount points

c:\cygnus\> ls /bin /data
ls: /data: No such file or directory
c:\cygnus\> mount C:\cygnus\cygwin-b20\H-i586-cygwin32\bin /bin
c:\cygnus\> mount \\pollux\home\joe\data /data
Warning: /data does not exist!
c:\cygnus\> mount
Device           Directory           Type        Flags
\\pollux\home\joe\data   /data       native      text!=binary 
C:\cygnus\cygwin-b20\H-i586-cygwin32\bin   /bin   native   text!=binary
D:               /d                  native      text!=binary
\\.\tape1:       /dev/st1            native      text!=binary
\\.\tape0:       /dev/st0            native      text!=binary
\\.\b:           /dev/fd1            native      text!=binary
\\.\a:           /dev/fd0            native      text!=binary
C:               /                   native      text!=binary
c:\cygnus\> ls /bin/sh
/bin/sh

The previous example was shown in the DOS shell. In bash it is legal and convenient to use the forward "/" in the Win32 pathname as the "\" is the shell escape.

The cygpath program can be used to translate between Win32 and POSIX pathnames in shell scripts. See the section called Cygwin Utilities for details.

The HOME, PATH, and LD_LIBRARY_PATH environment variables are automatically converted from Win32 format to POSIX format (e.g. from C:\cygnus\cygwin-b20\H-i586-cygwin32\bin to /bin) when a Cygwin process first starts.

Symbolic links can also be used to map Win32 pathnames to POSIX. For example the command ln -s //pollux/home/joe/data /data would have about the same effect as the mount in the example above, except that symbolic links do not set a default file mode. Other differences are that the mapping is distributed troughout the file system and proceeds by iteratively walking the directory tree instead of matching the longest prefix in a kernel table. Note that symbolic links work on network drives only if they support the "system" file attribute.

Mount point types

Usually the POSIX mount point is an existing empty directory, as in standard UNIX. If this is the case, or if there is a place-holder for the mount point, such as a file, a symbolic link (pointing to anywhere) or a non-empty directory, you get the expected behavior. Files present in a mount point directory before the mount become invisible to Cygwin programs.

It is sometimes desirable to mount to a non-existent directory, for example to avoid cluttering the root directory with names such as a, b, c pointing to disks. Although you will get a warning, most everything will work properly when you refer to the mount point explicitly. Some strange effects can occur however. For example if your current working directory is /dir, say, and /dir/mtpt is a mount point, then mtpt will not show up in an ls or echo * command and find . will not find mtpt.

Although you can mount to pathnames that do not start with "/", there is no way to make use of such mount points.

Where to mount

Which set of mounts is right for a given Cygwin user depends largely on how closely you want to simulate a POSIX environment, whether you mix Windows and Cygwin programs, and how many drive letters you are using. If you want to be very POSIX-like, you may want to do something like this:

Example 3-2. POSIX-like mount setup

C:\> mount c:\Cygnus\98r2 /
C:\> mount c:\ /c
C:\> mount d:\ /d
C:\> mount e:\ /cdrom

However, if you mix Windows and Cygwin programs a lot, you might want to create an "identity" mapping, so that conversions between the two (see the cygpath command) can be eliminated:

Example 3-3. Identity mount setup

C:\> mount c:\ \
C:\> mount d:\foo /foo
C:\> mount d:\bar /bar
C:\> mount e:\grill /grill

You'd have to repeat this for all top-level subdirectories on all drives, but then you'd always have the top-level directories available as the same names in both systems.