Cygwin: Unix Under Windows

The modern Windows operating systems are sophisticated enough to run Unix programs. Why would you want to do this when you can run Linux for free? Perhaps you have to use Windows and miss your Unix tools. Perhaps you want to use the GNU development suite for building Windows and Unix programs.

The Cygwin project by Red Hat seeks to create a Unix environment under Windows. Its basis is a library that implements Unix API calls. The distribution includes ports of the GNU development tools and utilities. Lots of other programs have been ported to run under Cygwin. Most Unix programs will compile with only minor changes. Cygwin works with any 32-bit Windows operating system including Windows 95, 98, ME, NT, 2000, and XP. It works much better with the Windows NT derived ones.

Cygwin is easy to install. Download the setup program and run it. You can select where to download the packages, where to install them (the default is c:\cygwin), and which packages to install. There are plenty of packages to choose from. Since you can always install more packages later, I suggest choosing the defaults to get started unless you want particular packages.

Since there are major difference between the Windows and Unix worlds, you need to be aware of some of them when working with Cygwin. The biggest difference is that Cygwin uses a Unix style paths. All paths start from the root (/) and use forward slashes to separate directories. Windows paths use backward slashes and start with drive letters. The Cygwin library translates between Windows and Unix paths. The Cygwin base directory (c:\cygwin) is the Unix root (/). Windows drives are accessible under /cygdrive. For example, e:\ is automatically mapped to /cygdrive/e. Cygwin will even map paths with Windows-style drive letters.

The main problems occur when mixing the two worlds. Windows programs won't understand Cygwinq paths; they need to be translated first. Cygwin programs will mostly understand Windows paths but only if you use forward slashes to separate directories. Also, some of them will get confused with the drive letters even though the Cygwin library will handle them. I suggest that you always use forward slashes. Most Windows program support both forward and backward slashes; command.com doesn't but with bash you have a real shell available.

Cygwin controls the mapping between the Unix and Windows paths through the use of the mount table. The mount table is global to all Cygwin programs. The mount command is used to change the table. Each mount point can also control the line translation. Cygwin provides the cygpath utility for doing the mapping in shell scripts.

Another problem is with the line endings for text files. Windows uses CR and LF characters to separate lines in text files while Unix uses just LF. Windows programs distinguish between handling files in text and binary mode. In text mode, they translate between CRLF and LF at the end of the lines so all the C code that uses "\n" works properly. Most Unix programs don't make this distinction and can have trouble when reading Windows-style end-of-line characters. Cygwin allows changing this behavior for each mount point. The problem mainly occurs when mixing DOS-style and Unix-style text files and programs. Stick to one environment and you should be okay.

Cygwin also maps between Windows and Unix permissions. NT has a sophisticated security model based on Access Control Lists (ACLs). The chmod and chown commands work mostly as expected. Since many programs expect /etc/group and /etc/passwd files, Cygwin provides commands to construct them from the Windows equivalents. Windows 9x doesn't have any security model so Cygwin fakes file ownership and permissions.

Another wrinkle is that quite a few Unix programs have been ported to Windows without using Cygwin. These programs won't understand the Unix paths but sometimes they integrate better with Windows environment. I'll have more articles dealing with Emacs, Perl, Tcl/Tk, Apache, TeX, and XFree86.

There are other Unix environments for Windows. MinGW is a good minmialist implementation. It uses the GNU C compiler to create Windows programs that don't have any dependency on other libraries. Most of the ports that don't use Cygwin use MinGW. There are also commercial solutions like those from MKS.

Resources