msgbartop
I will happily conduct a FREE basic web security scan for any genuine organization interested in my services to point out whether or not I can find vulnerabilities in your application. Just contact me.
Need a PHP Programmer, PHP staff or project manager? Contact me now.
msgbarbottom

05 Mar 09 Linux diff and patch

diff and patch are a complimentary pair of commands for viewing and applying changes to a file. Let’s assume that we have a simple C program (hello.c):

ns3:~/test# cat hello.c
#include <stdio.h>
int main(void)
{
printf(“Hello World\n”);
return 0;
}


I’m now going to make some modifications to it in a NEW file (hello_extended.c):

ns3:~/test# cat hello_extended.c
#include <stdio.h>

int main(int argc, char **argv)
{

if (argc == 2)
{
printf(“Hi! %s\n”, argv[1]);
}
printf(“Hello World\n”);
return 0;
}

My next move is to take a ‘diff’ file of the two which is an instruction set of changes:

ns3:~/test# diff -Naur hello.c hello_extended.c > hello.diff
ns3:~/test# cat hello.diff
— hello.c     2009-03-05 13:32:20.000000000 +0000
+++ hello_extended.c    2009-03-05 13:37:17.000000000 +0000
@@ -1,7 +1,12 @@
#include <stdio.h>

-int main(void)
+int main(int argc, char **argv)
{
+
+       if (argc == 2)
+       {
+               printf(“Hi! %s\n”, argv[1]);
+       }
printf(“Hello World\n”);
return 0;
}

Now I can pass my hello.diff file to the developers of hello.c and they can perform:

ns3:~/test# patch -p0 < hello.diff
patching file hello.c

hello.c is now a patched version containing my changes.

ns3:~/test# cat hello.c
#include <stdio.h>

int main(int argc, char **argv)
{

if (argc == 2)
{
printf(“Hi! %s\n”, argv[1]);
}
printf(“Hello World\n”);
return 0;
}

Why dont I just pass along my hello_extended.c rather than going to this trouble?

Well, if you look at hello.diff it contains date information, line numbers and positions which will help in the event of conflicts between other changes, as well as an exact map on how to reproduce the changes, rather than just the changes themselves, which is a lot more welcome on a larger file.

Tags: , ,



Leave a Comment

You must be logged in to post a comment.