About Me

My photo
Charleston, South Carolina, United States
Computer Science undergraduate student (Senior) at the College of Charleston.

Labels

Thursday, February 24, 2011

Fixing the code exercises

I'm going to quickly point out a new feature that I have added to the blog.  At the top of the page there are now two labels to choose from, "OpenMRS" and "Other". Posts labelled "OpenMRS" will include information specific to my progress in contributing to OpenMRS.  Posts marked "Other" will pertain to other things that I am doing for class which do not pertain to OpenMRS.  This post will be an "Other" post.

  
My current assignment deals with gaining experience with fixing/submitting code and using patches.  The first part of this assignment is to play around with the diff command and to note the differences between using the -u flag and not using the -u flag.  Here is the file that I used to play with.

/* hello.c - a simple program to print out hello
 * to the screen
 */

#include <stdio.h>

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


The only change I made to this file was to change the period after 'world' to a exclamation point.  Here is the output of running a diff command on these two files with the -u flag.

--- hello.c.punct    2011-02-24 10:12:22.211431001 -0500
+++ hello.c    2011-02-24 10:12:12.931431005 -0500
@@ -5,7 +5,7 @@
 #include <stdio.h>

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


Here is what the output looks like without the -u flag.

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



So clearly, the -u flag makes the output of the diff more readable.  Without the -u flag, you are only shown the line that changes.  With the -u, you are shown the context of the changed line (the function it is in).  The -u flag also uses intuitive symbols like the '+' to indicate that the line was added/changed and the '-' to indicate that the line was deleted/changed.  It took a little bit of research for me to understand that the "8c8" indicates the line number of the change.  The '<' symbol indicates that the line was removed and likewise the '>' symbol indicates that the line was added.

Next up for this assignment is to "create a patch file that represents a new file, foo being created with the contents bar."  The way to do this is to use /dev/null as one of the files you're diffing in creating the patch file.  So here is the diff command

diff -u /dev/null hello.c > new-feature.patch

Once this patch file has been created, you can apply the patch and a new hello.c will be created matching the contents of the hello.c that was used in the diff.  If there already is a hello.c in the directory where you are trying to apply this patch you will get an error of sorts that looks like this...

The next patch would create the file hello.c,
which already exists!  Assume -R? [n] Y
Apply anyway? [n] Y
Skipping patch.
1 out of 1 hunk ignored

 

So, a new hello.c was not created and the old one was not modified in any way.  The final part of this assignment is to create a patch for an actual project.  The project being used is Coreutils ("basic file,shell and text manipulation utilities of the GNU operating system").  The assignment pretty much holds your hand through this task so it was pretty easy but I guess it was worth it to create a patch for an actual project.

 






 


 
 

No comments:

Post a Comment