Friday, February 10, 2012

Day Thirteen: Fixing the Code

Chapter 7 in the online Teach Open Source textbook deals entirely with patching. It includes many helpful tips and exercises that prepare readers for a real patch submission. The assignment for this class includes exercises 7.2.2, 7.8 and 7.9.

Exercise 7.2.2: Comparing Diff Formats
In the previous sub-section, 7.2.1, the author provides a simple "Hello World." C program for the reader to use. After creating a copy of the file and changing the period to an exclamation mark in the new file, the author runs the following command to diff the two files:

diff -u hello.c.punct hello.c

The "-u" flag represents the unified diff format and creates the following output:


--- hello.c.punct 2012-02-10 01:06:26.431385116 -0500
+++ hello.c 2012-02-10 01:04:49.221384225 -0500
@@ -5,6 +5,6 @@
 #include <stdio.h>

 int main() {
-    printf("Hello, World!\n");
+    printf("Hello, World.\n");
     return 0;
 }

Without this flag, the output is much simpler and shows only the lines that are different, but this format lacks context without the rest of the code. Here is the same command but without the "-u" flag:

8c8
<     printf("Hello, World!\n");
---
>     printf("Hello, World.\n");

Exercise 7.8: Create a Patch for a New File
I  am not sure that I fully understand this exercise. The directions are to "create a patch file that represents a new file, foo being created with the contents bar." I assume this means to create a folder, "foo," that does not contain anything. I diff'd foo with /dev/null, but I received an error (diff: foo/null: No such file or directory). The patch file was created, but it is empty. Here is the command that I ran:

diff -u foo /dev/null >exercise-7.8.patch

Exercise 7.9: Patch Echo
This exercise is very straightforward. Just follow the instructions, and everything works perfectly. I ran the modified echo file and received the reversed message. Here is a copy of my patch:

--- src/echo.c.reverse 2012-02-10 01:48:15.131391331 -0500
+++ src/echo.c 2012-02-10 01:49:16.921559563 -0500
@@ -260,9 +260,8 @@
     {
       while (argc > 0)
         {
-          fputs (argv[0], stdout);
           argc--;
-          argv++;
+          fputs (argv[argc], stdout);
           if (argc > 0)
             putchar (' ');
         }

No comments:

Post a Comment