Product SiteDocumentation Site

7.9. Exercise - Patch echo

(Adapted from Make A Simple Patch under Creative Commons Attribution-ShareAlike 2.0 England & Wales Licence.)
Patch the echo command from the coreutils project so that it echoes out arguments in reverse order.
Download the latest coreutils compressed archive file (or tarball) (8.4 as of this writing) from http://ftp.gnu.org/gnu/coreutils/coreutils-8.4.tar.gz:
$ curl -O http://ftp.gnu.org/gnu/coreutils/coreutils-8.4.tar.gz
Open the tarball to create a coreutils-8.4 directory containing the source code:
$ tar zxf coreutils-8.4.tar.gz
Edit the file src/echo.c in order to modify the echo command. First, create a backup copy of that file:
$ cd coreutils-8.4/src
$ cp echo.c echo.c.reverse
Now, edit the file echo.c. The code to modify is near the very bottom of the file -- go to line 261 and change the following block of code:
while (argc > 0)
        {
          fputs (argv[0], stdout);
          argc--;
          argv++;
          if (argc > 0)
            putchar (' ');
        }
Update the code to be:
while (argc > 0)
        {
          argc--;
          fputs (argv[argc], stdout);
          if (argc > 0)
            putchar (' ');
        }
Create a patch to represent your change by changing in to the directory and running the following diff command:
$ cd coreutils-8.4
$ diff -u src/echo.c.reverse src/echo.c > echo-reverse-output.patch
Your patch file should look something like:
--- src/echo.c.reverse  2010-03-14 09:45:40.959888410 -0700
+++ src/echo.c  2010-03-14 09:51:58.189768045 -0700
@@ -260,9 +260,8 @@
     {
       while (argc > 0)
         {
-          fputs (argv[0], stdout);
           argc--;
-          argv++;
+          fputs (argv[0], stdout);
           if (argc > 0)
             putchar (' ');
         }
If you want to test out your changes, run the following commands to build the code:
$ ./configure$ make
You should now have a working echo binary. You can run the following command to test it out:
$ src/echo is this reversedreversed this is