Microsoft

How to run UNIX apps in Windows with Cygwin

UNIX has been around for a long time, so there are numerous applications available for that platform. Here's how to use Cygwin to run those apps in a Windows environment.

If your organization runs both Windows and UNIX, you may eventually find yourself in a situation that calls for Windows users to run UNIX applications. While this particular situation does present some special challenges, a solution can often be achieved by using Cygwin. I'm going to introduce you to Cygwin, and explain how your organization can benefit from it.

What is Cygwin?
Put simply, Cygwin is a UNIX environment for Windows. There are two major components to Cygwin. The first of these is a DLL file called CYGWIN1.DLL. This DLL file acts as a UNIX emulation layer, thus providing the UNIX API functionality. The other component is a collection of tools that was ported from a true UNIX environment. These tools are designed to give Cygwin that UNIX or Linux look and feel. I’ll talk more about these tools later on.

Software requirements
Cygwin supports Windows 95, 98, Me, NT, 2000, and XP. Before you install Cygwin though, you must know that although Cygwin does use a UNIX API, the application’s functionality is ultimately determined by the Windows operating system. The application makes calls to a UNIX DLL, which then makes calls to the Windows operating system. Therefore, if Windows doesn't support a particular function, then you can’t expect your UNIX application to perform the function.

You must also bear in mind that different versions of Windows have different native functionality. For example, Windows 98 can’t read an NTFS file system. Therefore, you couldn’t expect a UNIX application running on Windows 98 to access files from a local NTFS partition, just because the UNIX code told it to. Operating system limitations would make the operation impossible (without the addition of third-party drivers).

Acquiring Cygwin
Believe it or not, Cygwin is free. Parts of Cygwin are public domain, and other parts were released under the X11 license. This means that generally speaking, you can download the application, modify it, share it with your friends, and do just about anything else besides selling it for a profit. There are exceptions though, so you should read the Cygwin license for all of the details.

You can download Cygwin from its Web site, which also contains various installation files, user manuals, etc. Keep in mind that the software is completely unsupported. There are versions of Cygwin that are supported, but those versions cost money. If you’re interested in a commercial version of Cygwin, check out this one from Red Hat.

At the time that this article was written, the latest version of the standard edition of Cygwin was 1.13.18-1. There’s an Install Now link on the Cygwin home page that you can use to download the Setup program. The Setup program is a mere 167 KB in size, so it downloads very quickly on most connections.

When you run the Setup program, a Wizard will load and ask you where you’d like to install from. The reason that downloading the Setup is so fast is that most of the installation files exist on the Internet. When running Setup, just choose the Install From Internet option and the necessary files will be installed into the location that you specify. All you have to do is to answer a few simple questions about your Internet connection and about the mirror site that you want to use for the download.

As the download begins, a screen will appear, asking you which Cygwin packages that you want to install. To get everything that Cygwin has to offer, select the All Default option. The Setup program will now download a series of different files to your machine.

Setting the environment variables
When the installation process completes, you’ll have a Cygwin option on your Start | Programs menu. Before you start using Cygwin though, you may want to set some environment variables. Although Cygwin doesn’t specifically require environment variables, they can be helpful. For example, you can set a variable that points to the path of your executable files.

You can set environment variables through Windows, or through a batch file that calls the Cygwin shell, or as a direct entry from the Windows command prompt. Some of the environment variables that you can set are listed below.

The CYGWIN variable is used to configure Cygwin global settings. Initially, you can leave this variable unset, or you can set it to TTY, which will allow it to support jobs that use a [Ctrl]Z. An example of setting the CYGWIN variable is:
SET CYGWIN=TTY NOTITLE GLOB

Another variable that you must use is the PATH variable. Windows already sets a path, but you must extend the path to include the /CYGWIN/BIN folder. You can do this through a statement such as PATH=%PATH%;C:\CYGWIN\BIN. In case you’re wondering, Cygwin automatically interprets Windows style paths (\CYGWIN\BIN) as UNIX style path names ((/CYGWIN/BIN).

Another variable that you can set is the HOME directory. This variable points to a user’s home directory. Windows defines this variable as %HOME%.

An optional variable is TERM. The TERM variable allows you to specify a terminal type. This variable is automatically set to CYGWIN, unless you set it to something else.

Another variable that could come in handy is LD_LIBRARY_PATH. Occasionally, ported UNIX applications may use a function called DLOPEN to open a DLL file. If this function is used, then you must use the LD_LIBRARY_PATH variable to specify the location of your system’s DLL library. This variable is optional since most programs don’t use DLOPEN.

Cygwin tools
Cygwin comes with some UNIX tools, which are ports of some popular development tools for Windows. These tools make it possible to write Win32 applications that are based on either the standard Win32 API, the Cygwin API, or both. Basically, this means that you can easily create Windows versions of your UNIX applications based on the original UNIX source code. Often times though, you will have to make some minor changes to a UNIX application's source code before it will run on a Windows machine.

The various CYGWIN tools are included in the CYGWIN\USR\MAN\MAN1 folder. These tools include things like DIFF, BUGBASH, DIR, and EXPAND. There are 136 different UNIX tools in this folder. Most of the more common tools that you find in a true UNIX environment are also available in CYGWIN.

Libraries
In addition to the various tools that CYGWIN includes, there are also numerous library files that you can link to through your applications. These library files are specifically designed for UNIX applications running on Windows Machines. The libraries are included in the \CYGWIN\USR\INCLUDE folder and are designated by the .H extension. Some of the more common libraries are STDIO.H, IO.H, and MATH.H. These are by no means the only libraries though. There are nearly 200 library files found in the \CYGWIN\USR\INCLUYDE directory and its subdirectories.

The Terminal
The CYGWIN terminal is available simply by selecting the CYGWIN Bash Sell command from the Start | Programs | CYGWIN menu. You can see what the CYGWIN terminal looks like in Figure A, where I’ve issued the DIR command. You can also see some of the various tools that are available to you.

Figure A

Editor's Picks