File Under: Programming, Software

Patch Open Source Software

One of the countless joys of open source software is, because its source code is open, developers modify programs to make its behavior exactly what they want. Usually, these modifications are pretty useful and address an issue the rest of us share. Luckily for us, they distribute their changes so the rest of the world can enjoy them too, depending on the software license of course.

Commonly, people distribute it in the form of a patch. Users download the original source to the software as well as the patch code, use the patch to modify the software, compile and enjoy. Here’s how.

Apply a Patch

Alpine is an excellent e-mail program but also an excellent example of patchability. There are quite a few patches available for Alpine adding and modifying little features. Patching is done on the command line. Command lines are part of the system on Linux or OS X. You can install a command-line operator using Cygwin for Windows-based systems.

To get started, first download the original source code for Alpine. Also download Eduardo Chappa’s handy patch that implements a delete-to-end-of-file command in Alpine.

At the command line, unzip the patch using ‘gunzip.’

gunzip DelText.patch.gz

If you’re curious, you can look at the unpackaged files; it’s a series of code snippets along with instructions describing where it belongs in the Alpine source. Each of these snippets is called a “hunk.”

Next, unpack the Alpine source code using “tar”:

tar xzvf alpine.tar.gz

This will create a new directory; cd into that directory. To apply the patch, use the “patch” command:

patch -p1 -i ../DelText.patch

This command patches files in the current directory, using the patch it’s told to use. It should describe what it’s doing while it is doing it:

Patching file pico/basic.c

Patching file pico/efunc.h

Now build the patched source code as normal.


Occasionally, a patch will fail. Typically it happens if you’re trying to apply a patch that was written for a different version of the software than the one you’re patching. It’ll say something like:

Hunk #6 FAILED at 804.

Often that means that code has been rearranged and the patch can’t find where it’s supposed to go. You can try to manually apply the patch code by hand-editing the file, or just try to find the proper patch.

If you decide you want to undo the patching job you just did, the patch can be reversed with the command:

patch -R -p1 -i ../DelText.patch