UNIXgod's guide to creating and applying patches

First create a working directory to experiment with this guide. I used mkdir wk which is my lazy habit of not typing workspace as the name. cd into it and use your favorite editor to make the venerable and historic version of 'Hello, world!' program in c like so (name the file hello.c):

Code:
int main()
{
	printf( "Hello, world\n");
}

type to compile with make:
% make hello

output:
Code:
[B]hello.c:1:1: [color="Magenta"]warning:[/color] type specifier missing, defaults to 'int' [-Wimplicit-int][/B]
main()
[color="Lime"]^~~~[/color]
[B]hello.c:3:2: [color="Magenta"]warning:[/color] implicitly declaring C library function 'printf' with type
      'int (const char *, ...)'[/B]
        printf( "Hello, world\n");
        [color="Lime"]^[/color]
[B]hello.c:3:2: [/B]note: please include the header <stdio.h> or explicitly provide a
      declaration for 'printf'
2 warnings generated.

If you would like to test that hello world really works you can run it in you shell as so:

% ./hello

Feel free to remove the generated file for the next step.

Now cp the hello.c and create a new file from it called hello.01.c.

Edit hello.01.c with adding what the warnings from clang wants( or gcc if that is what you used) wants :

Code:
#include <stdio.h>
int main( void)
{
	printf( "Hello, world!\n");
        printf( "You've just been patched!\n");
}

Now we can use diff to create the patch as so:

% diff -u hello.c hello.01.c > hello.patch

Feel free to view the hello.patch file to see what was generated.

Applying the patch is simple. Run the patch command as follows:

% patch < hello.patch

View the original hello.c and see that it has been updated. Run the make command as stated earlier and there will be no warnings. Pat yourself on the back for learning something useful today :)

If you would like to revert the patch using the -R switch in patch will do the job:

% patch -R < hello.patch

For more information take the time to study the diff() and patch() man pages.
 
Awesome, this is a nice little tutorial!

I just knew that diff had many to do with patching...
(Now I know I also have to use patch! XD)
 
Back
Top