- publishing free software manuals
Comparing and Merging Files with GNU diff and patch
by David MacKenzie, Paul Eggert, and Richard Stallman
Paperback (6"x9"), 120 pages
ISBN 0954161750
RRP £12.95 ($19.95)

"Well packaged... the quality of information is excellent" --- Linux User and Developer Magazine (Issue 36, Feb 2004) Get a printed copy>>>

2.6 Merging Files with If-then-else

You can use diff to merge two files of C source code. The output of diff in this format contains all the lines of both files. Lines common to both files are output just once; the differing parts are separated by the C preprocessor directives #ifdef name or #ifndef name, #else, and #endif. When compiling the output, you select which version to use by either defining or leaving undefined the macro name.

To merge two files, use diff with the -D name or --ifdef=name option. The argument name is the C preprocessor identifier to use in the #ifdef and #ifndef directives.

For example, if you change an instance of wait (&s) to waitpid (-1, &s, 0) and then merge the old and new files with the --ifdef=HAVE_WAITPID option, then the affected part of your code might look like this:

    do {
        if ((w = wait (&s)) < 0  &&  errno != EINTR)
#else /* HAVE_WAITPID */
        if ((w = waitpid (-1, &s, 0)) < 0  &&  errno != EINTR)
#endif /* HAVE_WAITPID */
            return w;
    } while (w != child);

You can specify formats for languages other than C by using line group formats and line formats, as described in the next sections.

ISBN 0954161750Comparing and Merging Files with GNU diff and patchSee the print edition