1*75f6d617Schristos\input texinfo @c -*-texinfo-*- 2*75f6d617Schristos@c $NetBSD: diff.texi,v 1.1.1.1 2016/01/13 03:15:29 christos Exp $ 3*75f6d617Schristos@comment Id 4*75f6d617Schristos@comment %**start of header 5*75f6d617Schristos@setfilename diff.info 6*75f6d617Schristos@include version.texi 7*75f6d617Schristos@settitle Comparing and Merging Files 8*75f6d617Schristos@syncodeindex vr cp 9*75f6d617Schristos@setchapternewpage odd 10*75f6d617Schristos@comment %**end of header 11*75f6d617Schristos@copying 12*75f6d617SchristosThis manual is for GNU Diffutils 13*75f6d617Schristos(version @value{VERSION}, @value{UPDATED}), 14*75f6d617Schristosand documents the @sc{gnu} @command{diff}, @command{diff3}, 15*75f6d617Schristos@command{sdiff}, and @command{cmp} commands for showing the 16*75f6d617Schristosdifferences between files and the @sc{gnu} @command{patch} command for 17*75f6d617Schristosusing their output to update files. 18*75f6d617Schristos 19*75f6d617SchristosCopyright @copyright{} 1992, 1993, 1994, 1998, 2001, 2002 Free 20*75f6d617SchristosSoftware Foundation, Inc. 21*75f6d617Schristos 22*75f6d617Schristos@quotation 23*75f6d617SchristosPermission is granted to copy, distribute and/or modify this document 24*75f6d617Schristosunder the terms of the GNU Free Documentation License, Version 1.1 or 25*75f6d617Schristosany later version published by the Free Software Foundation; with no 26*75f6d617SchristosInvariant Sections, with the Front-Cover texts being ``A GNU Manual,'' 27*75f6d617Schristosand with the Back-Cover Texts as in (a) below. A copy of the 28*75f6d617Schristoslicense is included in the section entitled ``GNU Free Documentation 29*75f6d617SchristosLicense.'' 30*75f6d617Schristos 31*75f6d617Schristos(a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify 32*75f6d617Schristosthis GNU Manual, like GNU software. Copies published by the Free 33*75f6d617SchristosSoftware Foundation raise funds for GNU development.'' 34*75f6d617Schristos@end quotation 35*75f6d617Schristos@end copying 36*75f6d617Schristos 37*75f6d617Schristos@c Debian install-info (up through at least version 1.9.20) uses only the 38*75f6d617Schristos@c first dircategory. Put this one first, as it is more useful in practice. 39*75f6d617Schristos@dircategory Individual utilities 40*75f6d617Schristos@direntry 41*75f6d617Schristos* cmp: (diff)Invoking cmp. Compare 2 files byte by byte. 42*75f6d617Schristos* diff: (diff)Invoking diff. Compare 2 files line by line. 43*75f6d617Schristos* diff3: (diff)Invoking diff3. Compare 3 files line by line. 44*75f6d617Schristos* patch: (diff)Invoking patch. Apply a patch to a file. 45*75f6d617Schristos* sdiff: (diff)Invoking sdiff. Merge 2 files side-by-side. 46*75f6d617Schristos@end direntry 47*75f6d617Schristos 48*75f6d617Schristos@dircategory GNU packages 49*75f6d617Schristos@direntry 50*75f6d617Schristos* Diff: (diff). Comparing and merging files. 51*75f6d617Schristos@end direntry 52*75f6d617Schristos 53*75f6d617Schristos@titlepage 54*75f6d617Schristos@title Comparing and Merging Files 55*75f6d617Schristos@subtitle for Diffutils @value{VERSION} and @code{patch} 2.5.4 56*75f6d617Schristos@subtitle @value{UPDATED} 57*75f6d617Schristos@author David MacKenzie, Paul Eggert, and Richard Stallman 58*75f6d617Schristos@page 59*75f6d617Schristos@vskip 0pt plus 1filll 60*75f6d617Schristos@insertcopying 61*75f6d617Schristos@end titlepage 62*75f6d617Schristos 63*75f6d617Schristos@shortcontents 64*75f6d617Schristos@contents 65*75f6d617Schristos 66*75f6d617Schristos@ifnottex 67*75f6d617Schristos@node Top 68*75f6d617Schristos@top Comparing and Merging Files 69*75f6d617Schristos 70*75f6d617Schristos@insertcopying 71*75f6d617Schristos@end ifnottex 72*75f6d617Schristos 73*75f6d617Schristos@menu 74*75f6d617Schristos* Overview:: Preliminary information. 75*75f6d617Schristos* Comparison:: What file comparison means. 76*75f6d617Schristos 77*75f6d617Schristos* Output Formats:: Formats for two-way difference reports. 78*75f6d617Schristos* Incomplete Lines:: Lines that lack trailing newlines. 79*75f6d617Schristos* Comparing Directories:: Comparing files and directories. 80*75f6d617Schristos* Adjusting Output:: Making @command{diff} output prettier. 81*75f6d617Schristos* diff Performance:: Making @command{diff} smarter or faster. 82*75f6d617Schristos 83*75f6d617Schristos* Comparing Three Files:: Formats for three-way difference reports. 84*75f6d617Schristos* diff3 Merging:: Merging from a common ancestor. 85*75f6d617Schristos 86*75f6d617Schristos* Interactive Merging:: Interactive merging with @command{sdiff}. 87*75f6d617Schristos 88*75f6d617Schristos* Merging with patch:: Using @command{patch} to change old files into new ones. 89*75f6d617Schristos* Making Patches:: Tips for making and using patch distributions. 90*75f6d617Schristos 91*75f6d617Schristos* Invoking cmp:: Compare two files byte by byte. 92*75f6d617Schristos* Invoking diff:: Compare two files line by line. 93*75f6d617Schristos* Invoking diff3:: Compare three files line by line. 94*75f6d617Schristos* Invoking patch:: Apply a diff file to an original. 95*75f6d617Schristos* Invoking sdiff:: Side-by-side merge of file differences. 96*75f6d617Schristos 97*75f6d617Schristos* Standards conformance:: Conformance to the @sc{posix} standard. 98*75f6d617Schristos* Projects:: If you've found a bug or other shortcoming. 99*75f6d617Schristos 100*75f6d617Schristos* Copying This Manual:: How to make copies of this manual. 101*75f6d617Schristos* Index:: Index. 102*75f6d617Schristos@end menu 103*75f6d617Schristos 104*75f6d617Schristos@node Overview 105*75f6d617Schristos@unnumbered Overview 106*75f6d617Schristos@cindex overview of @command{diff} and @command{patch} 107*75f6d617Schristos 108*75f6d617SchristosComputer users often find occasion to ask how two files differ. Perhaps 109*75f6d617Schristosone file is a newer version of the other file. Or maybe the two files 110*75f6d617Schristosstarted out as identical copies but were changed by different people. 111*75f6d617Schristos 112*75f6d617SchristosYou can use the @command{diff} command to show differences between two 113*75f6d617Schristosfiles, or each corresponding file in two directories. @command{diff} 114*75f6d617Schristosoutputs differences between files line by line in any of several 115*75f6d617Schristosformats, selectable by command line options. This set of differences is 116*75f6d617Schristosoften called a @dfn{diff} or @dfn{patch}. For files that are identical, 117*75f6d617Schristos@command{diff} normally produces no output; for binary (non-text) files, 118*75f6d617Schristos@command{diff} normally reports only that they are different. 119*75f6d617Schristos 120*75f6d617SchristosYou can use the @command{cmp} command to show the byte and line numbers 121*75f6d617Schristoswhere two files differ. @command{cmp} can also show all the bytes 122*75f6d617Schristosthat differ between the two files, side by side. A way to compare 123*75f6d617Schristostwo files character by character is the Emacs command @kbd{M-x 124*75f6d617Schristoscompare-windows}. @xref{Other Window, , Other Window, emacs, The @sc{gnu} 125*75f6d617SchristosEmacs Manual}, for more information on that command. 126*75f6d617Schristos 127*75f6d617SchristosYou can use the @command{diff3} command to show differences among three 128*75f6d617Schristosfiles. When two people have made independent changes to a common 129*75f6d617Schristosoriginal, @command{diff3} can report the differences between the original 130*75f6d617Schristosand the two changed versions, and can produce a merged file that 131*75f6d617Schristoscontains both persons' changes together with warnings about conflicts. 132*75f6d617Schristos 133*75f6d617SchristosYou can use the @command{sdiff} command to merge two files interactively. 134*75f6d617Schristos 135*75f6d617SchristosYou can use the set of differences produced by @command{diff} to distribute 136*75f6d617Schristosupdates to text files (such as program source code) to other people. 137*75f6d617SchristosThis method is especially useful when the differences are small compared 138*75f6d617Schristosto the complete files. Given @command{diff} output, you can use the 139*75f6d617Schristos@command{patch} program to update, or @dfn{patch}, a copy of the file. If you 140*75f6d617Schristosthink of @command{diff} as subtracting one file from another to produce 141*75f6d617Schristostheir difference, you can think of @command{patch} as adding the difference 142*75f6d617Schristosto one file to reproduce the other. 143*75f6d617Schristos 144*75f6d617SchristosThis manual first concentrates on making diffs, and later shows how to 145*75f6d617Schristosuse diffs to update files. 146*75f6d617Schristos 147*75f6d617Schristos@sc{gnu} @command{diff} was written by Paul Eggert, Mike Haertel, 148*75f6d617SchristosDavid Hayes, Richard Stallman, and Len Tower. Wayne Davison designed and 149*75f6d617Schristosimplemented the unified output format. The basic algorithm is described 150*75f6d617Schristosin ``An O(ND) Difference Algorithm and its Variations'', Eugene W. Myers, 151*75f6d617Schristos@cite{Algorithmica} Vol.@: 1 No.@: 2, 1986, pp.@: 251--266; and in ``A File 152*75f6d617SchristosComparison Program'', Webb Miller and Eugene W. Myers, 153*75f6d617Schristos@cite{Software---Practice and Experience} Vol.@: 15 No.@: 11, 1985, 154*75f6d617Schristospp.@: 1025--1040. 155*75f6d617Schristos@c From: "Gene Myers" <gene@cs.arizona.edu> 156*75f6d617Schristos@c They are about the same basic algorithm; the Algorithmica 157*75f6d617Schristos@c paper gives a rigorous treatment and the sub-algorithm for 158*75f6d617Schristos@c delivering scripts and should be the primary reference, but 159*75f6d617Schristos@c both should be mentioned. 160*75f6d617SchristosThe algorithm was independently discovered as described in 161*75f6d617Schristos``Algorithms for Approximate String Matching'', 162*75f6d617SchristosE. Ukkonen, @cite{Information and Control} Vol.@: 64, 1985, pp.@: 100--118. 163*75f6d617Schristos@c From: "Gene Myers" <gene@cs.arizona.edu> 164*75f6d617Schristos@c Date: Wed, 29 Sep 1993 08:27:55 MST 165*75f6d617Schristos@c Ukkonen should be given credit for also discovering the algorithm used 166*75f6d617Schristos@c in GNU diff. 167*75f6d617Schristos 168*75f6d617Schristos@sc{gnu} @command{diff3} was written by Randy Smith. @sc{gnu} 169*75f6d617Schristos@command{sdiff} was written by Thomas Lord. @sc{gnu} @command{cmp} 170*75f6d617Schristoswas written by Torbjorn Granlund and David MacKenzie. 171*75f6d617Schristos 172*75f6d617Schristos@command{patch} was written mainly by Larry Wall and Paul Eggert; 173*75f6d617Schristosseveral @sc{gnu} enhancements were contributed by Wayne Davison and 174*75f6d617SchristosDavid MacKenzie. Parts of this manual are adapted from a manual page 175*75f6d617Schristoswritten by Larry Wall, with his permission. 176*75f6d617Schristos 177*75f6d617Schristos@node Comparison 178*75f6d617Schristos@chapter What Comparison Means 179*75f6d617Schristos@cindex introduction 180*75f6d617Schristos 181*75f6d617SchristosThere are several ways to think about the differences between two files. 182*75f6d617SchristosOne way to think of the differences is as a series of lines that were 183*75f6d617Schristosdeleted from, inserted in, or changed in one file to produce the other 184*75f6d617Schristosfile. @command{diff} compares two files line by line, finds groups of 185*75f6d617Schristoslines that differ, and reports each group of differing lines. It can 186*75f6d617Schristosreport the differing lines in several formats, which have different 187*75f6d617Schristospurposes. 188*75f6d617Schristos 189*75f6d617Schristos@sc{gnu} @command{diff} can show whether files are different without detailing 190*75f6d617Schristosthe differences. It also provides ways to suppress certain kinds of 191*75f6d617Schristosdifferences that are not important to you. Most commonly, such 192*75f6d617Schristosdifferences are changes in the amount of white space between words or 193*75f6d617Schristoslines. @command{diff} also provides ways to suppress differences in 194*75f6d617Schristosalphabetic case or in lines that match a regular expression that you 195*75f6d617Schristosprovide. These options can accumulate; for example, you can ignore 196*75f6d617Schristoschanges in both white space and alphabetic case. 197*75f6d617Schristos 198*75f6d617SchristosAnother way to think of the differences between two files is as a 199*75f6d617Schristossequence of pairs of bytes that can be either identical or 200*75f6d617Schristosdifferent. @command{cmp} reports the differences between two files 201*75f6d617Schristosbyte by byte, instead of line by line. As a result, it is often 202*75f6d617Schristosmore useful than @command{diff} for comparing binary files. For text 203*75f6d617Schristosfiles, @command{cmp} is useful mainly when you want to know only whether 204*75f6d617Schristostwo files are identical, or whether one file is a prefix of the other. 205*75f6d617Schristos 206*75f6d617SchristosTo illustrate the effect that considering changes byte by byte 207*75f6d617Schristoscan have compared with considering them line by line, think of what 208*75f6d617Schristoshappens if a single newline character is added to the beginning of a 209*75f6d617Schristosfile. If that file is then compared with an otherwise identical file 210*75f6d617Schristosthat lacks the newline at the beginning, @command{diff} will report that a 211*75f6d617Schristosblank line has been added to the file, while @command{cmp} will report that 212*75f6d617Schristosalmost every byte of the two files differs. 213*75f6d617Schristos 214*75f6d617Schristos@command{diff3} normally compares three input files line by line, finds 215*75f6d617Schristosgroups of lines that differ, and reports each group of differing lines. 216*75f6d617SchristosIts output is designed to make it easy to inspect two different sets of 217*75f6d617Schristoschanges to the same file. 218*75f6d617Schristos 219*75f6d617Schristos@menu 220*75f6d617Schristos* Hunks:: Groups of differing lines. 221*75f6d617Schristos* White Space:: Suppressing differences in white space. 222*75f6d617Schristos* Blank Lines:: Suppressing differences in blank lines. 223*75f6d617Schristos* Case Folding:: Suppressing differences in alphabetic case. 224*75f6d617Schristos* Specified Folding:: Suppressing differences that match regular expressions. 225*75f6d617Schristos* Brief:: Summarizing which files are different. 226*75f6d617Schristos* Binary:: Comparing binary files or forcing text comparisons. 227*75f6d617Schristos@end menu 228*75f6d617Schristos 229*75f6d617Schristos@node Hunks 230*75f6d617Schristos@section Hunks 231*75f6d617Schristos@cindex hunks 232*75f6d617Schristos 233*75f6d617SchristosWhen comparing two files, @command{diff} finds sequences of lines common to 234*75f6d617Schristosboth files, interspersed with groups of differing lines called 235*75f6d617Schristos@dfn{hunks}. Comparing two identical files yields one sequence of 236*75f6d617Schristoscommon lines and no hunks, because no lines differ. Comparing two 237*75f6d617Schristosentirely different files yields no common lines and one large hunk that 238*75f6d617Schristoscontains all lines of both files. In general, there are many ways to 239*75f6d617Schristosmatch up lines between two given files. @command{diff} tries to minimize 240*75f6d617Schristosthe total hunk size by finding large sequences of common lines 241*75f6d617Schristosinterspersed with small hunks of differing lines. 242*75f6d617Schristos 243*75f6d617SchristosFor example, suppose the file @file{F} contains the three lines 244*75f6d617Schristos@samp{a}, @samp{b}, @samp{c}, and the file @file{G} contains the same 245*75f6d617Schristosthree lines in reverse order @samp{c}, @samp{b}, @samp{a}. If 246*75f6d617Schristos@command{diff} finds the line @samp{c} as common, then the command 247*75f6d617Schristos@samp{diff F G} produces this output: 248*75f6d617Schristos 249*75f6d617Schristos@example 250*75f6d617Schristos1,2d0 251*75f6d617Schristos< a 252*75f6d617Schristos< b 253*75f6d617Schristos3a2,3 254*75f6d617Schristos> b 255*75f6d617Schristos> a 256*75f6d617Schristos@end example 257*75f6d617Schristos 258*75f6d617Schristos@noindent 259*75f6d617SchristosBut if @command{diff} notices the common line @samp{b} instead, it produces 260*75f6d617Schristosthis output: 261*75f6d617Schristos 262*75f6d617Schristos@example 263*75f6d617Schristos1c1 264*75f6d617Schristos< a 265*75f6d617Schristos--- 266*75f6d617Schristos> c 267*75f6d617Schristos3c3 268*75f6d617Schristos< c 269*75f6d617Schristos--- 270*75f6d617Schristos> a 271*75f6d617Schristos@end example 272*75f6d617Schristos 273*75f6d617Schristos@noindent 274*75f6d617SchristosIt is also possible to find @samp{a} as the common line. @command{diff} 275*75f6d617Schristosdoes not always find an optimal matching between the files; it takes 276*75f6d617Schristosshortcuts to run faster. But its output is usually close to the 277*75f6d617Schristosshortest possible. You can adjust this tradeoff with the 278*75f6d617Schristos@option{--minimal} option (@pxref{diff Performance}). 279*75f6d617Schristos 280*75f6d617Schristos@node White Space 281*75f6d617Schristos@section Suppressing Differences in Blank and Tab Spacing 282*75f6d617Schristos@cindex blank and tab difference suppression 283*75f6d617Schristos@cindex tab and blank difference suppression 284*75f6d617Schristos 285*75f6d617SchristosThe @option{-E} and @option{--ignore-tab-expansion} options ignore the 286*75f6d617Schristosdistinction between tabs and spaces on input. A tab is considered to be 287*75f6d617Schristosequivalent to the number of spaces to the next tab stop. @command{diff} 288*75f6d617Schristosassumes that tab stops are set every 8 print columns. 289*75f6d617Schristos 290*75f6d617SchristosThe @option{-b} and @option{--ignore-space-change} options are stronger. 291*75f6d617SchristosThey ignore white space at line end, and consider all other sequences of 292*75f6d617Schristosone or more white space characters to be equivalent. With these 293*75f6d617Schristosoptions, @command{diff} considers the following two lines to be equivalent, 294*75f6d617Schristoswhere @samp{$} denotes the line end: 295*75f6d617Schristos 296*75f6d617Schristos@example 297*75f6d617SchristosHere lyeth muche rychnesse in lytell space. -- John Heywood$ 298*75f6d617SchristosHere lyeth muche rychnesse in lytell space. -- John Heywood $ 299*75f6d617Schristos@end example 300*75f6d617Schristos 301*75f6d617SchristosThe @option{-w} and @option{--ignore-all-space} options are stronger still. 302*75f6d617SchristosThey ignore difference even if one line has white space where 303*75f6d617Schristosthe other line has none. @dfn{White space} characters include 304*75f6d617Schristostab, newline, vertical tab, form feed, carriage return, and space; 305*75f6d617Schristossome locales may define additional characters to be white space. 306*75f6d617SchristosWith these options, @command{diff} considers the 307*75f6d617Schristosfollowing two lines to be equivalent, where @samp{$} denotes the line 308*75f6d617Schristosend and @samp{^M} denotes a carriage return: 309*75f6d617Schristos 310*75f6d617Schristos@example 311*75f6d617SchristosHere lyeth muche rychnesse in lytell space.-- John Heywood$ 312*75f6d617Schristos He relyeth much erychnes seinly tells pace. --John Heywood ^M$ 313*75f6d617Schristos@end example 314*75f6d617Schristos 315*75f6d617Schristos@node Blank Lines 316*75f6d617Schristos@section Suppressing Differences in Blank Lines 317*75f6d617Schristos@cindex blank line difference suppression 318*75f6d617Schristos 319*75f6d617SchristosThe @option{-B} and @option{--ignore-blank-lines} options ignore insertions 320*75f6d617Schristosor deletions of blank lines. These options affect only lines 321*75f6d617Schristosthat are completely empty; they do not affect lines that look empty but 322*75f6d617Schristoscontain space or tab characters. With these options, for example, a 323*75f6d617Schristosfile containing 324*75f6d617Schristos@example 325*75f6d617Schristos1. A point is that which has no part. 326*75f6d617Schristos 327*75f6d617Schristos2. A line is breadthless length. 328*75f6d617Schristos-- Euclid, The Elements, I 329*75f6d617Schristos@end example 330*75f6d617Schristos@noindent 331*75f6d617Schristosis considered identical to a file containing 332*75f6d617Schristos@example 333*75f6d617Schristos1. A point is that which has no part. 334*75f6d617Schristos2. A line is breadthless length. 335*75f6d617Schristos 336*75f6d617Schristos 337*75f6d617Schristos-- Euclid, The Elements, I 338*75f6d617Schristos@end example 339*75f6d617Schristos 340*75f6d617Schristos@node Case Folding 341*75f6d617Schristos@section Suppressing Case Differences 342*75f6d617Schristos@cindex case difference suppression 343*75f6d617Schristos 344*75f6d617Schristos@sc{gnu} @command{diff} can treat lower case letters as equivalent to their 345*75f6d617Schristosupper case counterparts, so that, for example, it considers @samp{Funky 346*75f6d617SchristosStuff}, @samp{funky STUFF}, and @samp{fUNKy stuFf} to all be the same. 347*75f6d617SchristosTo request this, use the @option{-i} or @option{--ignore-case} option. 348*75f6d617Schristos 349*75f6d617Schristos@node Specified Folding 350*75f6d617Schristos@section Suppressing Lines Matching a Regular Expression 351*75f6d617Schristos@cindex regular expression suppression 352*75f6d617Schristos 353*75f6d617SchristosTo ignore insertions and deletions of lines that match a 354*75f6d617Schristos@command{grep}-style regular expression, use the @option{-I 355*75f6d617Schristos@var{regexp}} or @option{--ignore-matching-lines=@var{regexp}} option. 356*75f6d617SchristosYou should escape 357*75f6d617Schristosregular expressions that contain shell metacharacters to prevent the 358*75f6d617Schristosshell from expanding them. For example, @samp{diff -I '^[[:digit:]]'} ignores 359*75f6d617Schristosall changes to lines beginning with a digit. 360*75f6d617Schristos 361*75f6d617SchristosHowever, @option{-I} only ignores the insertion or deletion of lines that 362*75f6d617Schristoscontain the regular expression if every changed line in the hunk---every 363*75f6d617Schristosinsertion and every deletion---matches the regular expression. In other 364*75f6d617Schristoswords, for each nonignorable change, @command{diff} prints the complete set 365*75f6d617Schristosof changes in its vicinity, including the ignorable ones. 366*75f6d617Schristos 367*75f6d617SchristosYou can specify more than one regular expression for lines to ignore by 368*75f6d617Schristosusing more than one @option{-I} option. @command{diff} tries to match each 369*75f6d617Schristosline against each regular expression. 370*75f6d617Schristos 371*75f6d617Schristos@node Brief 372*75f6d617Schristos@section Summarizing Which Files Differ 373*75f6d617Schristos@cindex summarizing which files differ 374*75f6d617Schristos@cindex brief difference reports 375*75f6d617Schristos 376*75f6d617SchristosWhen you only want to find out whether files are different, and you 377*75f6d617Schristosdon't care what the differences are, you can use the summary output 378*75f6d617Schristosformat. In this format, instead of showing the differences between the 379*75f6d617Schristosfiles, @command{diff} simply reports whether files differ. The @option{-q} 380*75f6d617Schristosand @option{--brief} options select this output format. 381*75f6d617Schristos 382*75f6d617SchristosThis format is especially useful when comparing the contents of two 383*75f6d617Schristosdirectories. It is also much faster than doing the normal line by line 384*75f6d617Schristoscomparisons, because @command{diff} can stop analyzing the files as soon as 385*75f6d617Schristosit knows that there are any differences. 386*75f6d617Schristos 387*75f6d617SchristosYou can also get a brief indication of whether two files differ by using 388*75f6d617Schristos@command{cmp}. For files that are identical, @command{cmp} produces no 389*75f6d617Schristosoutput. When the files differ, by default, @command{cmp} outputs the byte 390*75f6d617Schristosand line number where the first difference occurs. You can use 391*75f6d617Schristosthe @option{-s} option to suppress that information, so that @command{cmp} 392*75f6d617Schristosproduces no output and reports whether the files differ using only its 393*75f6d617Schristosexit status (@pxref{Invoking cmp}). 394*75f6d617Schristos 395*75f6d617Schristos@c Fix this. 396*75f6d617SchristosUnlike @command{diff}, @command{cmp} cannot compare directories; it can only 397*75f6d617Schristoscompare two files. 398*75f6d617Schristos 399*75f6d617Schristos@node Binary 400*75f6d617Schristos@section Binary Files and Forcing Text Comparisons 401*75f6d617Schristos@cindex binary file diff 402*75f6d617Schristos@cindex text versus binary diff 403*75f6d617Schristos 404*75f6d617SchristosIf @command{diff} thinks that either of the two files it is comparing is 405*75f6d617Schristosbinary (a non-text file), it normally treats that pair of files much as 406*75f6d617Schristosif the summary output format had been selected (@pxref{Brief}), and 407*75f6d617Schristosreports only that the binary files are different. This is because line 408*75f6d617Schristosby line comparisons are usually not meaningful for binary files. 409*75f6d617Schristos 410*75f6d617Schristos@command{diff} determines whether a file is text or binary by checking the 411*75f6d617Schristosfirst few bytes in the file; the exact number of bytes is system 412*75f6d617Schristosdependent, but it is typically several thousand. If every byte in 413*75f6d617Schristosthat part of the file is non-null, @command{diff} considers the file to be 414*75f6d617Schristostext; otherwise it considers the file to be binary. 415*75f6d617Schristos 416*75f6d617SchristosSometimes you might want to force @command{diff} to consider files to be 417*75f6d617Schristostext. For example, you might be comparing text files that contain 418*75f6d617Schristosnull characters; @command{diff} would erroneously decide that those are 419*75f6d617Schristosnon-text files. Or you might be comparing documents that are in a 420*75f6d617Schristosformat used by a word processing system that uses null characters to 421*75f6d617Schristosindicate special formatting. You can force @command{diff} to consider all 422*75f6d617Schristosfiles to be text files, and compare them line by line, by using the 423*75f6d617Schristos@option{-a} or @option{--text} option. If the files you compare using this 424*75f6d617Schristosoption do not in fact contain text, they will probably contain few 425*75f6d617Schristosnewline characters, and the @command{diff} output will consist of hunks 426*75f6d617Schristosshowing differences between long lines of whatever characters the files 427*75f6d617Schristoscontain. 428*75f6d617Schristos 429*75f6d617SchristosYou can also force @command{diff} to consider all files to be binary files, 430*75f6d617Schristosand report only whether they differ (but not how). Use the 431*75f6d617Schristos@option{-q} or @option{--brief} option for this. 432*75f6d617Schristos 433*75f6d617SchristosDiffering binary files are considered to cause trouble because the 434*75f6d617Schristosresulting @command{diff} output does not capture all the differences. 435*75f6d617SchristosThis trouble causes @command{diff} to exit with status 2. However, 436*75f6d617Schristosthis trouble cannot occur with the @option{--a} or @option{--text} 437*75f6d617Schristosoption, or with the @option{-q} or @option{--brief} option, as these 438*75f6d617Schristosoptions both cause @command{diff} to treat binary files like text 439*75f6d617Schristosfiles. 440*75f6d617Schristos 441*75f6d617SchristosIn operating systems that distinguish between text and binary files, 442*75f6d617Schristos@command{diff} normally reads and writes all data as text. Use the 443*75f6d617Schristos@option{--binary} option to force @command{diff} to read and write binary 444*75f6d617Schristosdata instead. This option has no effect on a @sc{posix}-compliant system 445*75f6d617Schristoslike @sc{gnu} or traditional Unix. However, many personal computer 446*75f6d617Schristosoperating systems represent the end of a line with a carriage return 447*75f6d617Schristosfollowed by a newline. On such systems, @command{diff} normally ignores 448*75f6d617Schristosthese carriage returns on input and generates them at the end of each 449*75f6d617Schristosoutput line, but with the @option{--binary} option @command{diff} treats 450*75f6d617Schristoseach carriage return as just another input character, and does not 451*75f6d617Schristosgenerate a carriage return at the end of each output line. This can be 452*75f6d617Schristosuseful when dealing with non-text files that are meant to be 453*75f6d617Schristosinterchanged with @sc{posix}-compliant systems. 454*75f6d617Schristos 455*75f6d617SchristosThe @option{--strip-trailing-cr} causes @command{diff} to treat input 456*75f6d617Schristoslines that end in carriage return followed by newline as if they end 457*75f6d617Schristosin plain newline. This can be useful when comparing text that is 458*75f6d617Schristosimperfectly imported from many personal computer operating systems. 459*75f6d617SchristosThis option affects how lines are read, which in turn affects how they 460*75f6d617Schristosare compared and output. 461*75f6d617Schristos 462*75f6d617SchristosIf you want to compare two files byte by byte, you can use the 463*75f6d617Schristos@command{cmp} program with the @option{-l} option to show the values 464*75f6d617Schristosof each differing byte in the two files. With @sc{gnu} @command{cmp}, 465*75f6d617Schristosyou can also use the @option{-b} option to show the @sc{ascii} 466*75f6d617Schristosrepresentation of those bytes. @xref{Invoking cmp}, for more 467*75f6d617Schristosinformation. 468*75f6d617Schristos 469*75f6d617SchristosIf @command{diff3} thinks that any of the files it is comparing is binary 470*75f6d617Schristos(a non-text file), it normally reports an error, because such 471*75f6d617Schristoscomparisons are usually not useful. @command{diff3} uses the same test as 472*75f6d617Schristos@command{diff} to decide whether a file is binary. As with @command{diff}, if 473*75f6d617Schristosthe input files contain a few non-text bytes but otherwise are like 474*75f6d617Schristostext files, you can force @command{diff3} to consider all files to be text 475*75f6d617Schristosfiles and compare them line by line by using the @option{-a} or 476*75f6d617Schristos@option{--text} options. 477*75f6d617Schristos 478*75f6d617Schristos@node Output Formats 479*75f6d617Schristos@chapter @command{diff} Output Formats 480*75f6d617Schristos@cindex output formats 481*75f6d617Schristos@cindex format of @command{diff} output 482*75f6d617Schristos 483*75f6d617Schristos@command{diff} has several mutually exclusive options for output format. 484*75f6d617SchristosThe following sections describe each format, illustrating how 485*75f6d617Schristos@command{diff} reports the differences between two sample input files. 486*75f6d617Schristos 487*75f6d617Schristos@menu 488*75f6d617Schristos* Sample diff Input:: Sample @command{diff} input files for examples. 489*75f6d617Schristos* Normal:: Showing differences without surrounding text. 490*75f6d617Schristos* Context:: Showing differences with the surrounding text. 491*75f6d617Schristos* Side by Side:: Showing differences in two columns. 492*75f6d617Schristos* Scripts:: Generating scripts for other programs. 493*75f6d617Schristos* If-then-else:: Merging files with if-then-else. 494*75f6d617Schristos@end menu 495*75f6d617Schristos 496*75f6d617Schristos@node Sample diff Input 497*75f6d617Schristos@section Two Sample Input Files 498*75f6d617Schristos@cindex @command{diff} sample input 499*75f6d617Schristos@cindex sample input for @command{diff} 500*75f6d617Schristos 501*75f6d617SchristosHere are two sample files that we will use in numerous examples to 502*75f6d617Schristosillustrate the output of @command{diff} and how various options can change 503*75f6d617Schristosit. 504*75f6d617Schristos 505*75f6d617SchristosThis is the file @file{lao}: 506*75f6d617Schristos 507*75f6d617Schristos@example 508*75f6d617SchristosThe Way that can be told of is not the eternal Way; 509*75f6d617SchristosThe name that can be named is not the eternal name. 510*75f6d617SchristosThe Nameless is the origin of Heaven and Earth; 511*75f6d617SchristosThe Named is the mother of all things. 512*75f6d617SchristosTherefore let there always be non-being, 513*75f6d617Schristos so we may see their subtlety, 514*75f6d617SchristosAnd let there always be being, 515*75f6d617Schristos so we may see their outcome. 516*75f6d617SchristosThe two are the same, 517*75f6d617SchristosBut after they are produced, 518*75f6d617Schristos they have different names. 519*75f6d617Schristos@end example 520*75f6d617Schristos 521*75f6d617SchristosThis is the file @file{tzu}: 522*75f6d617Schristos 523*75f6d617Schristos@example 524*75f6d617SchristosThe Nameless is the origin of Heaven and Earth; 525*75f6d617SchristosThe named is the mother of all things. 526*75f6d617Schristos 527*75f6d617SchristosTherefore let there always be non-being, 528*75f6d617Schristos so we may see their subtlety, 529*75f6d617SchristosAnd let there always be being, 530*75f6d617Schristos so we may see their outcome. 531*75f6d617SchristosThe two are the same, 532*75f6d617SchristosBut after they are produced, 533*75f6d617Schristos they have different names. 534*75f6d617SchristosThey both may be called deep and profound. 535*75f6d617SchristosDeeper and more profound, 536*75f6d617SchristosThe door of all subtleties! 537*75f6d617Schristos@end example 538*75f6d617Schristos 539*75f6d617SchristosIn this example, the first hunk contains just the first two lines of 540*75f6d617Schristos@file{lao}, the second hunk contains the fourth line of @file{lao} 541*75f6d617Schristosopposing the second and third lines of @file{tzu}, and the last hunk 542*75f6d617Schristoscontains just the last three lines of @file{tzu}. 543*75f6d617Schristos 544*75f6d617Schristos@node Normal 545*75f6d617Schristos@section Showing Differences Without Context 546*75f6d617Schristos@cindex normal output format 547*75f6d617Schristos@cindex @samp{<} output format 548*75f6d617Schristos 549*75f6d617SchristosThe ``normal'' @command{diff} output format shows each hunk of differences 550*75f6d617Schristoswithout any surrounding context. Sometimes such output is the clearest 551*75f6d617Schristosway to see how lines have changed, without the clutter of nearby 552*75f6d617Schristosunchanged lines (although you can get similar results with the context 553*75f6d617Schristosor unified formats by using 0 lines of context). However, this format 554*75f6d617Schristosis no longer widely used for sending out patches; for that purpose, the 555*75f6d617Schristoscontext format (@pxref{Context Format}) and the unified format 556*75f6d617Schristos(@pxref{Unified Format}) are superior. Normal format is the default for 557*75f6d617Schristoscompatibility with older versions of @command{diff} and the @sc{posix} 558*75f6d617Schristosstandard. Use the @option{--normal} option to select this output 559*75f6d617Schristosformat explicitly. 560*75f6d617Schristos 561*75f6d617Schristos@menu 562*75f6d617Schristos* Detailed Normal:: A detailed description of normal output format. 563*75f6d617Schristos* Example Normal:: Sample output in the normal format. 564*75f6d617Schristos@end menu 565*75f6d617Schristos 566*75f6d617Schristos@node Detailed Normal 567*75f6d617Schristos@subsection Detailed Description of Normal Format 568*75f6d617Schristos 569*75f6d617SchristosThe normal output format consists of one or more hunks of differences; 570*75f6d617Schristoseach hunk shows one area where the files differ. Normal format hunks 571*75f6d617Schristoslook like this: 572*75f6d617Schristos 573*75f6d617Schristos@example 574*75f6d617Schristos@var{change-command} 575*75f6d617Schristos< @var{from-file-line} 576*75f6d617Schristos< @var{from-file-line}@dots{} 577*75f6d617Schristos--- 578*75f6d617Schristos> @var{to-file-line} 579*75f6d617Schristos> @var{to-file-line}@dots{} 580*75f6d617Schristos@end example 581*75f6d617Schristos 582*75f6d617SchristosThere are three types of change commands. Each consists of a line 583*75f6d617Schristosnumber or comma-separated range of lines in the first file, a single 584*75f6d617Schristoscharacter indicating the kind of change to make, and a line number or 585*75f6d617Schristoscomma-separated range of lines in the second file. All line numbers are 586*75f6d617Schristosthe original line numbers in each file. The types of change commands 587*75f6d617Schristosare: 588*75f6d617Schristos 589*75f6d617Schristos@table @samp 590*75f6d617Schristos@item @var{l}a@var{r} 591*75f6d617SchristosAdd the lines in range @var{r} of the second file after line @var{l} of 592*75f6d617Schristosthe first file. For example, @samp{8a12,15} means append lines 12--15 593*75f6d617Schristosof file 2 after line 8 of file 1; or, if changing file 2 into file 1, 594*75f6d617Schristosdelete lines 12--15 of file 2. 595*75f6d617Schristos 596*75f6d617Schristos@item @var{f}c@var{t} 597*75f6d617SchristosReplace the lines in range @var{f} of the first file with lines in range 598*75f6d617Schristos@var{t} of the second file. This is like a combined add and delete, but 599*75f6d617Schristosmore compact. For example, @samp{5,7c8,10} means change lines 5--7 of 600*75f6d617Schristosfile 1 to read as lines 8--10 of file 2; or, if changing file 2 into 601*75f6d617Schristosfile 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1. 602*75f6d617Schristos 603*75f6d617Schristos@item @var{r}d@var{l} 604*75f6d617SchristosDelete the lines in range @var{r} from the first file; line @var{l} is where 605*75f6d617Schristosthey would have appeared in the second file had they not been deleted. 606*75f6d617SchristosFor example, @samp{5,7d3} means delete lines 5--7 of file 1; or, if 607*75f6d617Schristoschanging file 2 into file 1, append lines 5--7 of file 1 after line 3 of 608*75f6d617Schristosfile 2. 609*75f6d617Schristos@end table 610*75f6d617Schristos 611*75f6d617Schristos@node Example Normal 612*75f6d617Schristos@subsection An Example of Normal Format 613*75f6d617Schristos 614*75f6d617SchristosHere is the output of the command @samp{diff lao tzu} 615*75f6d617Schristos(@pxref{Sample diff Input}, for the complete contents of the two files). 616*75f6d617SchristosNotice that it shows only the lines that are different between the two 617*75f6d617Schristosfiles. 618*75f6d617Schristos 619*75f6d617Schristos@example 620*75f6d617Schristos1,2d0 621*75f6d617Schristos< The Way that can be told of is not the eternal Way; 622*75f6d617Schristos< The name that can be named is not the eternal name. 623*75f6d617Schristos4c2,3 624*75f6d617Schristos< The Named is the mother of all things. 625*75f6d617Schristos--- 626*75f6d617Schristos> The named is the mother of all things. 627*75f6d617Schristos> 628*75f6d617Schristos11a11,13 629*75f6d617Schristos> They both may be called deep and profound. 630*75f6d617Schristos> Deeper and more profound, 631*75f6d617Schristos> The door of all subtleties! 632*75f6d617Schristos@end example 633*75f6d617Schristos 634*75f6d617Schristos@node Context 635*75f6d617Schristos@section Showing Differences in Their Context 636*75f6d617Schristos@cindex context output format 637*75f6d617Schristos@cindex @samp{!} output format 638*75f6d617Schristos 639*75f6d617SchristosUsually, when you are looking at the differences between files, you will 640*75f6d617Schristosalso want to see the parts of the files near the lines that differ, to 641*75f6d617Schristoshelp you understand exactly what has changed. These nearby parts of the 642*75f6d617Schristosfiles are called the @dfn{context}. 643*75f6d617Schristos 644*75f6d617Schristos@sc{gnu} @command{diff} provides two output formats that show context 645*75f6d617Schristosaround the differing lines: @dfn{context format} and @dfn{unified 646*75f6d617Schristosformat}. It can optionally show in which function or section of the 647*75f6d617Schristosfile the differing lines are found. 648*75f6d617Schristos 649*75f6d617SchristosIf you are distributing new versions of files to other people in the 650*75f6d617Schristosform of @command{diff} output, you should use one of the output formats 651*75f6d617Schristosthat show context so that they can apply the diffs even if they have 652*75f6d617Schristosmade small changes of their own to the files. @command{patch} can apply 653*75f6d617Schristosthe diffs in this case by searching in the files for the lines of 654*75f6d617Schristoscontext around the differing lines; if those lines are actually a few 655*75f6d617Schristoslines away from where the diff says they are, @command{patch} can adjust 656*75f6d617Schristosthe line numbers accordingly and still apply the diff correctly. 657*75f6d617Schristos@xref{Imperfect}, for more information on using @command{patch} to apply 658*75f6d617Schristosimperfect diffs. 659*75f6d617Schristos 660*75f6d617Schristos@menu 661*75f6d617Schristos* Context Format:: An output format that shows surrounding lines. 662*75f6d617Schristos* Unified Format:: A more compact output format that shows context. 663*75f6d617Schristos* Sections:: Showing which sections of the files differences are in. 664*75f6d617Schristos* Alternate Names:: Showing alternate file names in context headers. 665*75f6d617Schristos@end menu 666*75f6d617Schristos 667*75f6d617Schristos@node Context Format 668*75f6d617Schristos@subsection Context Format 669*75f6d617Schristos 670*75f6d617SchristosThe context output format shows several lines of context around the 671*75f6d617Schristoslines that differ. It is the standard format for distributing updates 672*75f6d617Schristosto source code. 673*75f6d617Schristos 674*75f6d617SchristosTo select this output format, use the @option{-C @var{lines}}, 675*75f6d617Schristos@option{--context@r{[}=@var{lines}@r{]}}, or @option{-c} option. The 676*75f6d617Schristosargument @var{lines} that some of these options take is the number of 677*75f6d617Schristoslines of context to show. If you do not specify @var{lines}, it 678*75f6d617Schristosdefaults to three. For proper operation, @command{patch} typically needs 679*75f6d617Schristosat least two lines of context. 680*75f6d617Schristos 681*75f6d617Schristos@menu 682*75f6d617Schristos* Detailed Context:: A detailed description of the context output format. 683*75f6d617Schristos* Example Context:: Sample output in context format. 684*75f6d617Schristos* Less Context:: Another sample with less context. 685*75f6d617Schristos@end menu 686*75f6d617Schristos 687*75f6d617Schristos@node Detailed Context 688*75f6d617Schristos@subsubsection Detailed Description of Context Format 689*75f6d617Schristos 690*75f6d617SchristosThe context output format starts with a two-line header, which looks 691*75f6d617Schristoslike this: 692*75f6d617Schristos 693*75f6d617Schristos@example 694*75f6d617Schristos*** @var{from-file} @var{from-file-modification-time} 695*75f6d617Schristos--- @var{to-file} @var{to-file-modification time} 696*75f6d617Schristos@end example 697*75f6d617Schristos 698*75f6d617Schristos@noindent 699*75f6d617Schristos@vindex LC_TIME 700*75f6d617Schristos@cindex time stamp format, context diffs 701*75f6d617SchristosThe time stamp normally looks like @samp{2002-02-21 23:30:39.942229878 702*75f6d617Schristos-0800} to indicate the date, time with fractional seconds, and time 703*75f6d617Schristoszone in @uref{ftp://ftp.isi.edu/in-notes/rfc2822.txt, Internet RFC 704*75f6d617Schristos2822 format}. However, a traditional time stamp like @samp{Thu Feb 21 705*75f6d617Schristos23:30:39 2002} is used if the @env{LC_TIME} locale category is either 706*75f6d617Schristos@samp{C} or @samp{POSIX}. 707*75f6d617Schristos 708*75f6d617SchristosYou can change the header's content with the 709*75f6d617Schristos@option{--label=@var{label}} option; see @ref{Alternate Names}. 710*75f6d617Schristos 711*75f6d617SchristosNext come one or more hunks of differences; each hunk shows one area 712*75f6d617Schristoswhere the files differ. Context format hunks look like this: 713*75f6d617Schristos 714*75f6d617Schristos@example 715*75f6d617Schristos*************** 716*75f6d617Schristos*** @var{from-file-line-range} **** 717*75f6d617Schristos @var{from-file-line} 718*75f6d617Schristos @var{from-file-line}@dots{} 719*75f6d617Schristos--- @var{to-file-line-range} ---- 720*75f6d617Schristos @var{to-file-line} 721*75f6d617Schristos @var{to-file-line}@dots{} 722*75f6d617Schristos@end example 723*75f6d617Schristos 724*75f6d617SchristosThe lines of context around the lines that differ start with two space 725*75f6d617Schristoscharacters. The lines that differ between the two files start with one 726*75f6d617Schristosof the following indicator characters, followed by a space character: 727*75f6d617Schristos 728*75f6d617Schristos@table @samp 729*75f6d617Schristos@item ! 730*75f6d617SchristosA line that is part of a group of one or more lines that changed between 731*75f6d617Schristosthe two files. There is a corresponding group of lines marked with 732*75f6d617Schristos@samp{!} in the part of this hunk for the other file. 733*75f6d617Schristos 734*75f6d617Schristos@item + 735*75f6d617SchristosAn ``inserted'' line in the second file that corresponds to nothing in 736*75f6d617Schristosthe first file. 737*75f6d617Schristos 738*75f6d617Schristos@item - 739*75f6d617SchristosA ``deleted'' line in the first file that corresponds to nothing in the 740*75f6d617Schristossecond file. 741*75f6d617Schristos@end table 742*75f6d617Schristos 743*75f6d617SchristosIf all of the changes in a hunk are insertions, the lines of 744*75f6d617Schristos@var{from-file} are omitted. If all of the changes are deletions, the 745*75f6d617Schristoslines of @var{to-file} are omitted. 746*75f6d617Schristos 747*75f6d617Schristos@node Example Context 748*75f6d617Schristos@subsubsection An Example of Context Format 749*75f6d617Schristos 750*75f6d617SchristosHere is the output of @samp{diff -c lao tzu} (@pxref{Sample diff Input}, 751*75f6d617Schristosfor the complete contents of the two files). Notice that up to three 752*75f6d617Schristoslines that are not different are shown around each line that is 753*75f6d617Schristosdifferent; they are the context lines. Also notice that the first two 754*75f6d617Schristoshunks have run together, because their contents overlap. 755*75f6d617Schristos 756*75f6d617Schristos@example 757*75f6d617Schristos*** lao 2002-02-21 23:30:39.942229878 -0800 758*75f6d617Schristos--- tzu 2002-02-21 23:30:50.442260588 -0800 759*75f6d617Schristos*************** 760*75f6d617Schristos*** 1,7 **** 761*75f6d617Schristos- The Way that can be told of is not the eternal Way; 762*75f6d617Schristos- The name that can be named is not the eternal name. 763*75f6d617Schristos The Nameless is the origin of Heaven and Earth; 764*75f6d617Schristos! The Named is the mother of all things. 765*75f6d617Schristos Therefore let there always be non-being, 766*75f6d617Schristos so we may see their subtlety, 767*75f6d617Schristos And let there always be being, 768*75f6d617Schristos--- 1,6 ---- 769*75f6d617Schristos The Nameless is the origin of Heaven and Earth; 770*75f6d617Schristos! The named is the mother of all things. 771*75f6d617Schristos! 772*75f6d617Schristos Therefore let there always be non-being, 773*75f6d617Schristos so we may see their subtlety, 774*75f6d617Schristos And let there always be being, 775*75f6d617Schristos*************** 776*75f6d617Schristos*** 9,11 **** 777*75f6d617Schristos--- 8,13 ---- 778*75f6d617Schristos The two are the same, 779*75f6d617Schristos But after they are produced, 780*75f6d617Schristos they have different names. 781*75f6d617Schristos+ They both may be called deep and profound. 782*75f6d617Schristos+ Deeper and more profound, 783*75f6d617Schristos+ The door of all subtleties! 784*75f6d617Schristos@end example 785*75f6d617Schristos 786*75f6d617Schristos@node Less Context 787*75f6d617Schristos@subsubsection An Example of Context Format with Less Context 788*75f6d617Schristos 789*75f6d617SchristosHere is the output of @samp{diff -C 1 lao tzu} (@pxref{Sample diff 790*75f6d617SchristosInput}, for the complete contents of the two files). Notice that at 791*75f6d617Schristosmost one context line is reported here. 792*75f6d617Schristos 793*75f6d617Schristos@example 794*75f6d617Schristos*** lao 2002-02-21 23:30:39.942229878 -0800 795*75f6d617Schristos--- tzu 2002-02-21 23:30:50.442260588 -0800 796*75f6d617Schristos*************** 797*75f6d617Schristos*** 1,5 **** 798*75f6d617Schristos- The Way that can be told of is not the eternal Way; 799*75f6d617Schristos- The name that can be named is not the eternal name. 800*75f6d617Schristos The Nameless is the origin of Heaven and Earth; 801*75f6d617Schristos! The Named is the mother of all things. 802*75f6d617Schristos Therefore let there always be non-being, 803*75f6d617Schristos--- 1,4 ---- 804*75f6d617Schristos The Nameless is the origin of Heaven and Earth; 805*75f6d617Schristos! The named is the mother of all things. 806*75f6d617Schristos! 807*75f6d617Schristos Therefore let there always be non-being, 808*75f6d617Schristos*************** 809*75f6d617Schristos*** 11 **** 810*75f6d617Schristos--- 10,13 ---- 811*75f6d617Schristos they have different names. 812*75f6d617Schristos+ They both may be called deep and profound. 813*75f6d617Schristos+ Deeper and more profound, 814*75f6d617Schristos+ The door of all subtleties! 815*75f6d617Schristos@end example 816*75f6d617Schristos 817*75f6d617Schristos@node Unified Format 818*75f6d617Schristos@subsection Unified Format 819*75f6d617Schristos@cindex unified output format 820*75f6d617Schristos@cindex @samp{+-} output format 821*75f6d617Schristos 822*75f6d617SchristosThe unified output format is a variation on the context format that is 823*75f6d617Schristosmore compact because it omits redundant context lines. To select this 824*75f6d617Schristosoutput format, use the @option{-U @var{lines}}, 825*75f6d617Schristos@option{--unified@r{[}=@var{lines}@r{]}}, or @option{-u} 826*75f6d617Schristosoption. The argument @var{lines} is the number of lines of context to 827*75f6d617Schristosshow. When it is not given, it defaults to three. 828*75f6d617Schristos 829*75f6d617SchristosAt present, only @sc{gnu} @command{diff} can produce this format and 830*75f6d617Schristosonly @sc{gnu} @command{patch} can automatically apply diffs in this 831*75f6d617Schristosformat. For proper operation, @command{patch} typically needs at 832*75f6d617Schristosleast three lines of context. 833*75f6d617Schristos 834*75f6d617Schristos@menu 835*75f6d617Schristos* Detailed Unified:: A detailed description of unified format. 836*75f6d617Schristos* Example Unified:: Sample output in unified format. 837*75f6d617Schristos@end menu 838*75f6d617Schristos 839*75f6d617Schristos@node Detailed Unified 840*75f6d617Schristos@subsubsection Detailed Description of Unified Format 841*75f6d617Schristos 842*75f6d617SchristosThe unified output format starts with a two-line header, which looks 843*75f6d617Schristoslike this: 844*75f6d617Schristos 845*75f6d617Schristos@example 846*75f6d617Schristos--- @var{from-file} @var{from-file-modification-time} 847*75f6d617Schristos+++ @var{to-file} @var{to-file-modification-time} 848*75f6d617Schristos@end example 849*75f6d617Schristos 850*75f6d617Schristos@noindent 851*75f6d617Schristos@cindex time stamp format, unified diffs 852*75f6d617SchristosThe time stamp looks like @samp{2002-02-21 23:30:39.942229878 -0800} 853*75f6d617Schristosto indicate the date, time with fractional seconds, and time zone. 854*75f6d617Schristos 855*75f6d617SchristosYou can change the header's content with the 856*75f6d617Schristos@option{--label=@var{label}} option; see @xref{Alternate Names}. 857*75f6d617Schristos 858*75f6d617SchristosNext come one or more hunks of differences; each hunk shows one area 859*75f6d617Schristoswhere the files differ. Unified format hunks look like this: 860*75f6d617Schristos 861*75f6d617Schristos@example 862*75f6d617Schristos@@@@ @var{from-file-range} @var{to-file-range} @@@@ 863*75f6d617Schristos @var{line-from-either-file} 864*75f6d617Schristos @var{line-from-either-file}@dots{} 865*75f6d617Schristos@end example 866*75f6d617Schristos 867*75f6d617SchristosThe lines common to both files begin with a space character. The lines 868*75f6d617Schristosthat actually differ between the two files have one of the following 869*75f6d617Schristosindicator characters in the left print column: 870*75f6d617Schristos 871*75f6d617Schristos@table @samp 872*75f6d617Schristos@item + 873*75f6d617SchristosA line was added here to the first file. 874*75f6d617Schristos 875*75f6d617Schristos@item - 876*75f6d617SchristosA line was removed here from the first file. 877*75f6d617Schristos@end table 878*75f6d617Schristos 879*75f6d617Schristos@node Example Unified 880*75f6d617Schristos@subsubsection An Example of Unified Format 881*75f6d617Schristos 882*75f6d617SchristosHere is the output of the command @samp{diff -u lao tzu} 883*75f6d617Schristos(@pxref{Sample diff Input}, for the complete contents of the two files): 884*75f6d617Schristos 885*75f6d617Schristos@example 886*75f6d617Schristos--- lao 2002-02-21 23:30:39.942229878 -0800 887*75f6d617Schristos+++ tzu 2002-02-21 23:30:50.442260588 -0800 888*75f6d617Schristos@@@@ -1,7 +1,6 @@@@ 889*75f6d617Schristos-The Way that can be told of is not the eternal Way; 890*75f6d617Schristos-The name that can be named is not the eternal name. 891*75f6d617Schristos The Nameless is the origin of Heaven and Earth; 892*75f6d617Schristos-The Named is the mother of all things. 893*75f6d617Schristos+The named is the mother of all things. 894*75f6d617Schristos+ 895*75f6d617Schristos Therefore let there always be non-being, 896*75f6d617Schristos so we may see their subtlety, 897*75f6d617Schristos And let there always be being, 898*75f6d617Schristos@@@@ -9,3 +8,6 @@@@ 899*75f6d617Schristos The two are the same, 900*75f6d617Schristos But after they are produced, 901*75f6d617Schristos they have different names. 902*75f6d617Schristos+They both may be called deep and profound. 903*75f6d617Schristos+Deeper and more profound, 904*75f6d617Schristos+The door of all subtleties! 905*75f6d617Schristos@end example 906*75f6d617Schristos 907*75f6d617Schristos@node Sections 908*75f6d617Schristos@subsection Showing Which Sections Differences Are in 909*75f6d617Schristos@cindex headings 910*75f6d617Schristos@cindex section headings 911*75f6d617Schristos 912*75f6d617SchristosSometimes you might want to know which part of the files each change 913*75f6d617Schristosfalls in. If the files are source code, this could mean which function 914*75f6d617Schristoswas changed. If the files are documents, it could mean which chapter or 915*75f6d617Schristosappendix was changed. @sc{gnu} @command{diff} can show this by displaying the 916*75f6d617Schristosnearest section heading line that precedes the differing lines. Which 917*75f6d617Schristoslines are ``section headings'' is determined by a regular expression. 918*75f6d617Schristos 919*75f6d617Schristos@menu 920*75f6d617Schristos* Specified Headings:: Showing headings that match regular expressions. 921*75f6d617Schristos* C Function Headings:: Showing headings of C functions. 922*75f6d617Schristos@end menu 923*75f6d617Schristos 924*75f6d617Schristos@node Specified Headings 925*75f6d617Schristos@subsubsection Showing Lines That Match Regular Expressions 926*75f6d617Schristos@cindex specified headings 927*75f6d617Schristos@cindex regular expression matching headings 928*75f6d617Schristos 929*75f6d617SchristosTo show in which sections differences occur for files that are not 930*75f6d617Schristossource code for C or similar languages, use the @option{-F @var{regexp}} 931*75f6d617Schristosor @option{--show-function-line=@var{regexp}} option. @command{diff} 932*75f6d617Schristosconsiders lines that match the @command{grep}-style regular expression 933*75f6d617Schristos@var{regexp} to be the beginning 934*75f6d617Schristosof a section of the file. Here are suggested regular expressions for 935*75f6d617Schristossome common languages: 936*75f6d617Schristos 937*75f6d617Schristos@c Please add to this list, e.g. Fortran, Pascal, Perl, Python. 938*75f6d617Schristos@table @samp 939*75f6d617Schristos@item ^[[:alpha:]$_] 940*75f6d617SchristosC, C++, Prolog 941*75f6d617Schristos@item ^( 942*75f6d617SchristosLisp 943*75f6d617Schristos@item ^@@node 944*75f6d617SchristosTexinfo 945*75f6d617Schristos@end table 946*75f6d617Schristos 947*75f6d617SchristosThis option does not automatically select an output format; in order to 948*75f6d617Schristosuse it, you must select the context format (@pxref{Context Format}) or 949*75f6d617Schristosunified format (@pxref{Unified Format}). In other output formats it 950*75f6d617Schristoshas no effect. 951*75f6d617Schristos 952*75f6d617SchristosThe @option{-F} and @option{--show-function-line} options find the nearest 953*75f6d617Schristosunchanged line that precedes each hunk of differences and matches the 954*75f6d617Schristosgiven regular expression. Then they add that line to the end of the 955*75f6d617Schristosline of asterisks in the context format, or to the @samp{@@@@} line in 956*75f6d617Schristosunified format. If no matching line exists, they leave the output for 957*75f6d617Schristosthat hunk unchanged. If that line is more than 40 characters long, they 958*75f6d617Schristosoutput only the first 40 characters. You can specify more than one 959*75f6d617Schristosregular expression for such lines; @command{diff} tries to match each line 960*75f6d617Schristosagainst each regular expression, starting with the last one given. This 961*75f6d617Schristosmeans that you can use @option{-p} and @option{-F} together, if you wish. 962*75f6d617Schristos 963*75f6d617Schristos@node C Function Headings 964*75f6d617Schristos@subsubsection Showing C Function Headings 965*75f6d617Schristos@cindex C function headings 966*75f6d617Schristos@cindex function headings, C 967*75f6d617Schristos 968*75f6d617SchristosTo show in which functions differences occur for C and similar 969*75f6d617Schristoslanguages, you can use the @option{-p} or @option{--show-c-function} option. 970*75f6d617SchristosThis option automatically defaults to the context output format 971*75f6d617Schristos(@pxref{Context Format}), with the default number of lines of context. 972*75f6d617SchristosYou can override that number with @option{-C @var{lines}} elsewhere in the 973*75f6d617Schristoscommand line. You can override both the format and the number with 974*75f6d617Schristos@option{-U @var{lines}} elsewhere in the command line. 975*75f6d617Schristos 976*75f6d617SchristosThe @option{-p} and @option{--show-c-function} options are equivalent to 977*75f6d617Schristos@option{-F '^[[:alpha:]$_]'} if the unified format is specified, otherwise 978*75f6d617Schristos@option{-c -F '^[[:alpha:]$_]'} (@pxref{Specified Headings}). @sc{gnu} 979*75f6d617Schristos@command{diff} provides them for the sake of convenience. 980*75f6d617Schristos 981*75f6d617Schristos@node Alternate Names 982*75f6d617Schristos@subsection Showing Alternate File Names 983*75f6d617Schristos@cindex alternate file names 984*75f6d617Schristos@cindex file name alternates 985*75f6d617Schristos 986*75f6d617SchristosIf you are comparing two files that have meaningless or uninformative 987*75f6d617Schristosnames, you might want @command{diff} to show alternate names in the header 988*75f6d617Schristosof the context and unified output formats. To do this, use the 989*75f6d617Schristos@option{--label=@var{label}} option. The first time 990*75f6d617Schristosyou give this option, its argument replaces the name and date of the 991*75f6d617Schristosfirst file in the header; the second time, its argument replaces the 992*75f6d617Schristosname and date of the second file. If you give this option more than 993*75f6d617Schristostwice, @command{diff} reports an error. The @option{--label} option does not 994*75f6d617Schristosaffect the file names in the @command{pr} header when the @option{-l} or 995*75f6d617Schristos@option{--paginate} option is used (@pxref{Pagination}). 996*75f6d617Schristos 997*75f6d617SchristosHere are the first two lines of the output from @samp{diff -C 2 998*75f6d617Schristos--label=original --label=modified lao tzu}: 999*75f6d617Schristos 1000*75f6d617Schristos@example 1001*75f6d617Schristos*** original 1002*75f6d617Schristos--- modified 1003*75f6d617Schristos@end example 1004*75f6d617Schristos 1005*75f6d617Schristos@node Side by Side 1006*75f6d617Schristos@section Showing Differences Side by Side 1007*75f6d617Schristos@cindex side by side 1008*75f6d617Schristos@cindex two-column output 1009*75f6d617Schristos@cindex columnar output 1010*75f6d617Schristos 1011*75f6d617Schristos@command{diff} can produce a side by side difference listing of two files. 1012*75f6d617SchristosThe files are listed in two columns with a gutter between them. The 1013*75f6d617Schristosgutter contains one of the following markers: 1014*75f6d617Schristos 1015*75f6d617Schristos@table @asis 1016*75f6d617Schristos@item white space 1017*75f6d617SchristosThe corresponding lines are in common. That is, either the lines are 1018*75f6d617Schristosidentical, or the difference is ignored because of one of the 1019*75f6d617Schristos@option{--ignore} options (@pxref{White Space}). 1020*75f6d617Schristos 1021*75f6d617Schristos@item @samp{|} 1022*75f6d617SchristosThe corresponding lines differ, and they are either both complete 1023*75f6d617Schristosor both incomplete. 1024*75f6d617Schristos 1025*75f6d617Schristos@item @samp{<} 1026*75f6d617SchristosThe files differ and only the first file contains the line. 1027*75f6d617Schristos 1028*75f6d617Schristos@item @samp{>} 1029*75f6d617SchristosThe files differ and only the second file contains the line. 1030*75f6d617Schristos 1031*75f6d617Schristos@item @samp{(} 1032*75f6d617SchristosOnly the first file contains the line, but the difference is ignored. 1033*75f6d617Schristos 1034*75f6d617Schristos@item @samp{)} 1035*75f6d617SchristosOnly the second file contains the line, but the difference is ignored. 1036*75f6d617Schristos 1037*75f6d617Schristos@item @samp{\} 1038*75f6d617SchristosThe corresponding lines differ, and only the first line is incomplete. 1039*75f6d617Schristos 1040*75f6d617Schristos@item @samp{/} 1041*75f6d617SchristosThe corresponding lines differ, and only the second line is incomplete. 1042*75f6d617Schristos@end table 1043*75f6d617Schristos 1044*75f6d617SchristosNormally, an output line is incomplete if and only if the lines that it 1045*75f6d617Schristoscontains are incomplete; @xref{Incomplete Lines}. However, when an 1046*75f6d617Schristosoutput line represents two differing lines, one might be incomplete 1047*75f6d617Schristoswhile the other is not. In this case, the output line is complete, 1048*75f6d617Schristosbut its the gutter is marked @samp{\} if the first line is incomplete, 1049*75f6d617Schristos@samp{/} if the second line is. 1050*75f6d617Schristos 1051*75f6d617SchristosSide by side format is sometimes easiest to read, but it has limitations. 1052*75f6d617SchristosIt generates much wider output than usual, and truncates lines that are 1053*75f6d617Schristostoo long to fit. Also, it relies on lining up output more heavily than 1054*75f6d617Schristosusual, so its output looks particularly bad if you use varying 1055*75f6d617Schristoswidth fonts, nonstandard tab stops, or nonprinting characters. 1056*75f6d617Schristos 1057*75f6d617SchristosYou can use the @command{sdiff} command to interactively merge side by side 1058*75f6d617Schristosdifferences. @xref{Interactive Merging}, for more information on merging files. 1059*75f6d617Schristos 1060*75f6d617Schristos@menu 1061*75f6d617Schristos* Side by Side Format:: Controlling side by side output format. 1062*75f6d617Schristos* Example Side by Side:: Sample side by side output. 1063*75f6d617Schristos@end menu 1064*75f6d617Schristos 1065*75f6d617Schristos@node Side by Side Format 1066*75f6d617Schristos@subsection Controlling Side by Side Format 1067*75f6d617Schristos@cindex side by side format 1068*75f6d617Schristos 1069*75f6d617SchristosThe @option{-y} or @option{--side-by-side} option selects side by side 1070*75f6d617Schristosformat. Because side by side output lines contain two input lines, the 1071*75f6d617Schristosoutput is wider than usual: normally 130 print columns, which can fit 1072*75f6d617Schristosonto a traditional printer line. You can set the width of the output 1073*75f6d617Schristoswith the @option{-W @var{columns}} or @option{--width=@var{columns}} 1074*75f6d617Schristosoption. The output is split into two halves of equal width, separated by a 1075*75f6d617Schristossmall gutter to mark differences; the right half is aligned to a tab 1076*75f6d617Schristosstop so that tabs line up. Input lines that are too long to fit in half 1077*75f6d617Schristosof an output line are truncated for output. 1078*75f6d617Schristos 1079*75f6d617SchristosThe @option{--left-column} option prints only the left column of two 1080*75f6d617Schristoscommon lines. The @option{--suppress-common-lines} option suppresses 1081*75f6d617Schristoscommon lines entirely. 1082*75f6d617Schristos 1083*75f6d617Schristos@node Example Side by Side 1084*75f6d617Schristos@subsection An Example of Side by Side Format 1085*75f6d617Schristos 1086*75f6d617SchristosHere is the output of the command @samp{diff -y -W 72 lao tzu} 1087*75f6d617Schristos(@pxref{Sample diff Input}, for the complete contents of the two files). 1088*75f6d617Schristos 1089*75f6d617Schristos@example 1090*75f6d617SchristosThe Way that can be told of is n < 1091*75f6d617SchristosThe name that can be named is no < 1092*75f6d617SchristosThe Nameless is the origin of He The Nameless is the origin of He 1093*75f6d617SchristosThe Named is the mother of all t | The named is the mother of all t 1094*75f6d617Schristos > 1095*75f6d617SchristosTherefore let there always be no Therefore let there always be no 1096*75f6d617Schristos so we may see their subtlety, so we may see their subtlety, 1097*75f6d617SchristosAnd let there always be being, And let there always be being, 1098*75f6d617Schristos so we may see their outcome. so we may see their outcome. 1099*75f6d617SchristosThe two are the same, The two are the same, 1100*75f6d617SchristosBut after they are produced, But after they are produced, 1101*75f6d617Schristos they have different names. they have different names. 1102*75f6d617Schristos > They both may be called deep and 1103*75f6d617Schristos > Deeper and more profound, 1104*75f6d617Schristos > The door of all subtleties! 1105*75f6d617Schristos@end example 1106*75f6d617Schristos 1107*75f6d617Schristos@node Scripts 1108*75f6d617Schristos@section Making Edit Scripts 1109*75f6d617Schristos@cindex script output formats 1110*75f6d617Schristos 1111*75f6d617SchristosSeveral output modes produce command scripts for editing @var{from-file} 1112*75f6d617Schristosto produce @var{to-file}. 1113*75f6d617Schristos 1114*75f6d617Schristos@menu 1115*75f6d617Schristos* ed Scripts:: Using @command{diff} to produce commands for @command{ed}. 1116*75f6d617Schristos* Forward ed:: Making forward @command{ed} scripts. 1117*75f6d617Schristos* RCS:: A special @command{diff} output format used by @sc{rcs}. 1118*75f6d617Schristos@end menu 1119*75f6d617Schristos 1120*75f6d617Schristos@node ed Scripts 1121*75f6d617Schristos@subsection @command{ed} Scripts 1122*75f6d617Schristos@cindex @command{ed} script output format 1123*75f6d617Schristos 1124*75f6d617Schristos@command{diff} can produce commands that direct the @command{ed} text editor 1125*75f6d617Schristosto change the first file into the second file. Long ago, this was the 1126*75f6d617Schristosonly output mode that was suitable for editing one file into another 1127*75f6d617Schristosautomatically; today, with @command{patch}, it is almost obsolete. Use the 1128*75f6d617Schristos@option{-e} or @option{--ed} option to select this output format. 1129*75f6d617Schristos 1130*75f6d617SchristosLike the normal format (@pxref{Normal}), this output format does not 1131*75f6d617Schristosshow any context; unlike the normal format, it does not include the 1132*75f6d617Schristosinformation necessary to apply the diff in reverse (to produce the first 1133*75f6d617Schristosfile if all you have is the second file and the diff). 1134*75f6d617Schristos 1135*75f6d617SchristosIf the file @file{d} contains the output of @samp{diff -e old new}, then 1136*75f6d617Schristosthe command @samp{(cat d && echo w) | ed - old} edits @file{old} to make 1137*75f6d617Schristosit a copy of @file{new}. More generally, if @file{d1}, @file{d2}, 1138*75f6d617Schristos@dots{}, @file{dN} contain the outputs of @samp{diff -e old new1}, 1139*75f6d617Schristos@samp{diff -e new1 new2}, @dots{}, @samp{diff -e newN-1 newN}, 1140*75f6d617Schristosrespectively, then the command @samp{(cat d1 d2 @dots{} dN && echo w) | 1141*75f6d617Schristosed - old} edits @file{old} to make it a copy of @file{newN}. 1142*75f6d617Schristos 1143*75f6d617Schristos@menu 1144*75f6d617Schristos* Detailed ed:: A detailed description of @command{ed} format. 1145*75f6d617Schristos* Example ed:: A sample @command{ed} script. 1146*75f6d617Schristos@end menu 1147*75f6d617Schristos 1148*75f6d617Schristos@node Detailed ed 1149*75f6d617Schristos@subsubsection Detailed Description of @command{ed} Format 1150*75f6d617Schristos 1151*75f6d617SchristosThe @command{ed} output format consists of one or more hunks of 1152*75f6d617Schristosdifferences. The changes closest to the ends of the files come first so 1153*75f6d617Schristosthat commands that change the number of lines do not affect how 1154*75f6d617Schristos@command{ed} interprets line numbers in succeeding commands. @command{ed} 1155*75f6d617Schristosformat hunks look like this: 1156*75f6d617Schristos 1157*75f6d617Schristos@example 1158*75f6d617Schristos@var{change-command} 1159*75f6d617Schristos@var{to-file-line} 1160*75f6d617Schristos@var{to-file-line}@dots{} 1161*75f6d617Schristos. 1162*75f6d617Schristos@end example 1163*75f6d617Schristos 1164*75f6d617SchristosBecause @command{ed} uses a single period on a line to indicate the end of 1165*75f6d617Schristosinput, @sc{gnu} @command{diff} protects lines of changes that contain a single 1166*75f6d617Schristosperiod on a line by writing two periods instead, then writing a 1167*75f6d617Schristossubsequent @command{ed} command to change the two periods into one. The 1168*75f6d617Schristos@command{ed} format cannot represent an incomplete line, so if the second 1169*75f6d617Schristosfile ends in a changed incomplete line, @command{diff} reports an error and 1170*75f6d617Schristosthen pretends that a newline was appended. 1171*75f6d617Schristos 1172*75f6d617SchristosThere are three types of change commands. Each consists of a line 1173*75f6d617Schristosnumber or comma-separated range of lines in the first file and a single 1174*75f6d617Schristoscharacter indicating the kind of change to make. All line numbers are 1175*75f6d617Schristosthe original line numbers in the file. The types of change commands 1176*75f6d617Schristosare: 1177*75f6d617Schristos 1178*75f6d617Schristos@table @samp 1179*75f6d617Schristos@item @var{l}a 1180*75f6d617SchristosAdd text from the second file after line @var{l} in the first file. For 1181*75f6d617Schristosexample, @samp{8a} means to add the following lines after line 8 of file 1182*75f6d617Schristos1. 1183*75f6d617Schristos 1184*75f6d617Schristos@item @var{r}c 1185*75f6d617SchristosReplace the lines in range @var{r} in the first file with the following 1186*75f6d617Schristoslines. Like a combined add and delete, but more compact. For example, 1187*75f6d617Schristos@samp{5,7c} means change lines 5--7 of file 1 to read as the text file 1188*75f6d617Schristos2. 1189*75f6d617Schristos 1190*75f6d617Schristos@item @var{r}d 1191*75f6d617SchristosDelete the lines in range @var{r} from the first file. For example, 1192*75f6d617Schristos@samp{5,7d} means delete lines 5--7 of file 1. 1193*75f6d617Schristos@end table 1194*75f6d617Schristos 1195*75f6d617Schristos@node Example ed 1196*75f6d617Schristos@subsubsection Example @command{ed} Script 1197*75f6d617Schristos 1198*75f6d617SchristosHere is the output of @samp{diff -e lao tzu} (@pxref{Sample 1199*75f6d617Schristosdiff Input}, for the complete contents of the two files): 1200*75f6d617Schristos 1201*75f6d617Schristos@example 1202*75f6d617Schristos11a 1203*75f6d617SchristosThey both may be called deep and profound. 1204*75f6d617SchristosDeeper and more profound, 1205*75f6d617SchristosThe door of all subtleties! 1206*75f6d617Schristos. 1207*75f6d617Schristos4c 1208*75f6d617SchristosThe named is the mother of all things. 1209*75f6d617Schristos 1210*75f6d617Schristos. 1211*75f6d617Schristos1,2d 1212*75f6d617Schristos@end example 1213*75f6d617Schristos 1214*75f6d617Schristos@node Forward ed 1215*75f6d617Schristos@subsection Forward @command{ed} Scripts 1216*75f6d617Schristos@cindex forward @command{ed} script output format 1217*75f6d617Schristos 1218*75f6d617Schristos@command{diff} can produce output that is like an @command{ed} script, but 1219*75f6d617Schristoswith hunks in forward (front to back) order. The format of the commands 1220*75f6d617Schristosis also changed slightly: command characters precede the lines they 1221*75f6d617Schristosmodify, spaces separate line numbers in ranges, and no attempt is made 1222*75f6d617Schristosto disambiguate hunk lines consisting of a single period. Like 1223*75f6d617Schristos@command{ed} format, forward @command{ed} format cannot represent incomplete 1224*75f6d617Schristoslines. 1225*75f6d617Schristos 1226*75f6d617SchristosForward @command{ed} format is not very useful, because neither @command{ed} 1227*75f6d617Schristosnor @command{patch} can apply diffs in this format. It exists mainly for 1228*75f6d617Schristoscompatibility with older versions of @command{diff}. Use the @option{-f} or 1229*75f6d617Schristos@option{--forward-ed} option to select it. 1230*75f6d617Schristos 1231*75f6d617Schristos@node RCS 1232*75f6d617Schristos@subsection @sc{rcs} Scripts 1233*75f6d617Schristos@cindex @sc{rcs} script output format 1234*75f6d617Schristos 1235*75f6d617SchristosThe @sc{rcs} output format is designed specifically for use by the Revision 1236*75f6d617SchristosControl System, which is a set of free programs used for organizing 1237*75f6d617Schristosdifferent versions and systems of files. Use the @option{-n} or 1238*75f6d617Schristos@option{--rcs} option to select this output format. It is like the 1239*75f6d617Schristosforward @command{ed} format (@pxref{Forward ed}), but it can represent 1240*75f6d617Schristosarbitrary changes to the contents of a file because it avoids the 1241*75f6d617Schristosforward @command{ed} format's problems with lines consisting of a single 1242*75f6d617Schristosperiod and with incomplete lines. Instead of ending text sections with 1243*75f6d617Schristosa line consisting of a single period, each command specifies the number 1244*75f6d617Schristosof lines it affects; a combination of the @samp{a} and @samp{d} 1245*75f6d617Schristoscommands are used instead of @samp{c}. Also, if the second file ends 1246*75f6d617Schristosin a changed incomplete line, then the output also ends in an 1247*75f6d617Schristosincomplete line. 1248*75f6d617Schristos 1249*75f6d617SchristosHere is the output of @samp{diff -n lao tzu} (@pxref{Sample 1250*75f6d617Schristosdiff Input}, for the complete contents of the two files): 1251*75f6d617Schristos 1252*75f6d617Schristos@example 1253*75f6d617Schristosd1 2 1254*75f6d617Schristosd4 1 1255*75f6d617Schristosa4 2 1256*75f6d617SchristosThe named is the mother of all things. 1257*75f6d617Schristos 1258*75f6d617Schristosa11 3 1259*75f6d617SchristosThey both may be called deep and profound. 1260*75f6d617SchristosDeeper and more profound, 1261*75f6d617SchristosThe door of all subtleties! 1262*75f6d617Schristos@end example 1263*75f6d617Schristos 1264*75f6d617Schristos@node If-then-else 1265*75f6d617Schristos@section Merging Files with If-then-else 1266*75f6d617Schristos@cindex merged output format 1267*75f6d617Schristos@cindex if-then-else output format 1268*75f6d617Schristos@cindex C if-then-else output format 1269*75f6d617Schristos@cindex @command{ifdef} output format 1270*75f6d617Schristos 1271*75f6d617SchristosYou can use @command{diff} to merge two files of C source code. The output 1272*75f6d617Schristosof @command{diff} in this format contains all the lines of both files. 1273*75f6d617SchristosLines common to both files are output just once; the differing parts are 1274*75f6d617Schristosseparated by the C preprocessor directives @code{#ifdef @var{name}} or 1275*75f6d617Schristos@code{#ifndef @var{name}}, @code{#else}, and @code{#endif}. When 1276*75f6d617Schristoscompiling the output, you select which version to use by either defining 1277*75f6d617Schristosor leaving undefined the macro @var{name}. 1278*75f6d617Schristos 1279*75f6d617SchristosTo merge two files, use @command{diff} with the @option{-D @var{name}} or 1280*75f6d617Schristos@option{--ifdef=@var{name}} option. The argument @var{name} is the C 1281*75f6d617Schristospreprocessor identifier to use in the @code{#ifdef} and @code{#ifndef} 1282*75f6d617Schristosdirectives. 1283*75f6d617Schristos 1284*75f6d617SchristosFor example, if you change an instance of @code{wait (&s)} to 1285*75f6d617Schristos@code{waitpid (-1, &s, 0)} and then merge the old and new files with 1286*75f6d617Schristosthe @option{--ifdef=HAVE_WAITPID} option, then the affected part of your code 1287*75f6d617Schristosmight look like this: 1288*75f6d617Schristos 1289*75f6d617Schristos@example 1290*75f6d617Schristos do @{ 1291*75f6d617Schristos#ifndef HAVE_WAITPID 1292*75f6d617Schristos if ((w = wait (&s)) < 0 && errno != EINTR) 1293*75f6d617Schristos#else /* HAVE_WAITPID */ 1294*75f6d617Schristos if ((w = waitpid (-1, &s, 0)) < 0 && errno != EINTR) 1295*75f6d617Schristos#endif /* HAVE_WAITPID */ 1296*75f6d617Schristos return w; 1297*75f6d617Schristos @} while (w != child); 1298*75f6d617Schristos@end example 1299*75f6d617Schristos 1300*75f6d617SchristosYou can specify formats for languages other than C by using line group 1301*75f6d617Schristosformats and line formats, as described in the next sections. 1302*75f6d617Schristos 1303*75f6d617Schristos@menu 1304*75f6d617Schristos* Line Group Formats:: Formats for general if-then-else line groups. 1305*75f6d617Schristos* Line Formats:: Formats for each line in a line group. 1306*75f6d617Schristos* Detailed If-then-else:: A detailed description of if-then-else format. 1307*75f6d617Schristos* Example If-then-else:: Sample if-then-else format output. 1308*75f6d617Schristos@end menu 1309*75f6d617Schristos 1310*75f6d617Schristos@node Line Group Formats 1311*75f6d617Schristos@subsection Line Group Formats 1312*75f6d617Schristos@cindex line group formats 1313*75f6d617Schristos@cindex formats for if-then-else line groups 1314*75f6d617Schristos 1315*75f6d617SchristosLine group formats let you specify formats suitable for many 1316*75f6d617Schristosapplications that allow if-then-else input, including programming 1317*75f6d617Schristoslanguages and text formatting languages. A line group format specifies 1318*75f6d617Schristosthe output format for a contiguous group of similar lines. 1319*75f6d617Schristos 1320*75f6d617SchristosFor example, the following command compares the TeX files @file{old} 1321*75f6d617Schristosand @file{new}, and outputs a merged file in which old regions are 1322*75f6d617Schristossurrounded by @samp{\begin@{em@}}-@samp{\end@{em@}} lines, and new 1323*75f6d617Schristosregions are surrounded by @samp{\begin@{bf@}}-@samp{\end@{bf@}} lines. 1324*75f6d617Schristos 1325*75f6d617Schristos@example 1326*75f6d617Schristosdiff \ 1327*75f6d617Schristos --old-group-format='\begin@{em@} 1328*75f6d617Schristos%<\end@{em@} 1329*75f6d617Schristos' \ 1330*75f6d617Schristos --new-group-format='\begin@{bf@} 1331*75f6d617Schristos%>\end@{bf@} 1332*75f6d617Schristos' \ 1333*75f6d617Schristos old new 1334*75f6d617Schristos@end example 1335*75f6d617Schristos 1336*75f6d617SchristosThe following command is equivalent to the above example, but it is a 1337*75f6d617Schristoslittle more verbose, because it spells out the default line group formats. 1338*75f6d617Schristos 1339*75f6d617Schristos@example 1340*75f6d617Schristosdiff \ 1341*75f6d617Schristos --old-group-format='\begin@{em@} 1342*75f6d617Schristos%<\end@{em@} 1343*75f6d617Schristos' \ 1344*75f6d617Schristos --new-group-format='\begin@{bf@} 1345*75f6d617Schristos%>\end@{bf@} 1346*75f6d617Schristos' \ 1347*75f6d617Schristos --unchanged-group-format='%=' \ 1348*75f6d617Schristos --changed-group-format='\begin@{em@} 1349*75f6d617Schristos%<\end@{em@} 1350*75f6d617Schristos\begin@{bf@} 1351*75f6d617Schristos%>\end@{bf@} 1352*75f6d617Schristos' \ 1353*75f6d617Schristos old new 1354*75f6d617Schristos@end example 1355*75f6d617Schristos 1356*75f6d617SchristosHere is a more advanced example, which outputs a diff listing with 1357*75f6d617Schristosheaders containing line numbers in a ``plain English'' style. 1358*75f6d617Schristos 1359*75f6d617Schristos@example 1360*75f6d617Schristosdiff \ 1361*75f6d617Schristos --unchanged-group-format='' \ 1362*75f6d617Schristos --old-group-format='-------- %dn line%(n=1?:s) deleted at %df: 1363*75f6d617Schristos%<' \ 1364*75f6d617Schristos --new-group-format='-------- %dN line%(N=1?:s) added after %de: 1365*75f6d617Schristos%>' \ 1366*75f6d617Schristos --changed-group-format='-------- %dn line%(n=1?:s) changed at %df: 1367*75f6d617Schristos%<-------- to: 1368*75f6d617Schristos%>' \ 1369*75f6d617Schristos old new 1370*75f6d617Schristos@end example 1371*75f6d617Schristos 1372*75f6d617SchristosTo specify a line group format, use @command{diff} with one of the options 1373*75f6d617Schristoslisted below. You can specify up to four line group formats, one for 1374*75f6d617Schristoseach kind of line group. You should quote @var{format}, because it 1375*75f6d617Schristostypically contains shell metacharacters. 1376*75f6d617Schristos 1377*75f6d617Schristos@table @option 1378*75f6d617Schristos@item --old-group-format=@var{format} 1379*75f6d617SchristosThese line groups are hunks containing only lines from the first file. 1380*75f6d617SchristosThe default old group format is the same as the changed group format if 1381*75f6d617Schristosit is specified; otherwise it is a format that outputs the line group as-is. 1382*75f6d617Schristos 1383*75f6d617Schristos@item --new-group-format=@var{format} 1384*75f6d617SchristosThese line groups are hunks containing only lines from the second 1385*75f6d617Schristosfile. The default new group format is same as the changed group 1386*75f6d617Schristosformat if it is specified; otherwise it is a format that outputs the 1387*75f6d617Schristosline group as-is. 1388*75f6d617Schristos 1389*75f6d617Schristos@item --changed-group-format=@var{format} 1390*75f6d617SchristosThese line groups are hunks containing lines from both files. The 1391*75f6d617Schristosdefault changed group format is the concatenation of the old and new 1392*75f6d617Schristosgroup formats. 1393*75f6d617Schristos 1394*75f6d617Schristos@item --unchanged-group-format=@var{format} 1395*75f6d617SchristosThese line groups contain lines common to both files. The default 1396*75f6d617Schristosunchanged group format is a format that outputs the line group as-is. 1397*75f6d617Schristos@end table 1398*75f6d617Schristos 1399*75f6d617SchristosIn a line group format, ordinary characters represent themselves; 1400*75f6d617Schristosconversion specifications start with @samp{%} and have one of the 1401*75f6d617Schristosfollowing forms. 1402*75f6d617Schristos 1403*75f6d617Schristos@table @samp 1404*75f6d617Schristos@item %< 1405*75f6d617Schristosstands for the lines from the first file, including the trailing newline. 1406*75f6d617SchristosEach line is formatted according to the old line format (@pxref{Line Formats}). 1407*75f6d617Schristos 1408*75f6d617Schristos@item %> 1409*75f6d617Schristosstands for the lines from the second file, including the trailing newline. 1410*75f6d617SchristosEach line is formatted according to the new line format. 1411*75f6d617Schristos 1412*75f6d617Schristos@item %= 1413*75f6d617Schristosstands for the lines common to both files, including the trailing newline. 1414*75f6d617SchristosEach line is formatted according to the unchanged line format. 1415*75f6d617Schristos 1416*75f6d617Schristos@item %% 1417*75f6d617Schristosstands for @samp{%}. 1418*75f6d617Schristos 1419*75f6d617Schristos@item %c'@var{C}' 1420*75f6d617Schristoswhere @var{C} is a single character, stands for @var{C}. 1421*75f6d617Schristos@var{C} may not be a backslash or an apostrophe. 1422*75f6d617SchristosFor example, @samp{%c':'} stands for a colon, even inside 1423*75f6d617Schristosthe then-part of an if-then-else format, which a colon would 1424*75f6d617Schristosnormally terminate. 1425*75f6d617Schristos 1426*75f6d617Schristos@item %c'\@var{O}' 1427*75f6d617Schristoswhere @var{O} is a string of 1, 2, or 3 octal digits, 1428*75f6d617Schristosstands for the character with octal code @var{O}. 1429*75f6d617SchristosFor example, @samp{%c'\0'} stands for a null character. 1430*75f6d617Schristos 1431*75f6d617Schristos@item @var{F}@var{n} 1432*75f6d617Schristoswhere @var{F} is a @code{printf} conversion specification and @var{n} is one 1433*75f6d617Schristosof the following letters, stands for @var{n}'s value formatted with @var{F}. 1434*75f6d617Schristos 1435*75f6d617Schristos@table @samp 1436*75f6d617Schristos@item e 1437*75f6d617SchristosThe line number of the line just before the group in the old file. 1438*75f6d617Schristos 1439*75f6d617Schristos@item f 1440*75f6d617SchristosThe line number of the first line in the group in the old file; 1441*75f6d617Schristosequals @var{e} + 1. 1442*75f6d617Schristos 1443*75f6d617Schristos@item l 1444*75f6d617SchristosThe line number of the last line in the group in the old file. 1445*75f6d617Schristos 1446*75f6d617Schristos@item m 1447*75f6d617SchristosThe line number of the line just after the group in the old file; 1448*75f6d617Schristosequals @var{l} + 1. 1449*75f6d617Schristos 1450*75f6d617Schristos@item n 1451*75f6d617SchristosThe number of lines in the group in the old file; equals @var{l} - @var{f} + 1. 1452*75f6d617Schristos 1453*75f6d617Schristos@item E, F, L, M, N 1454*75f6d617SchristosLikewise, for lines in the new file. 1455*75f6d617Schristos 1456*75f6d617Schristos@end table 1457*75f6d617Schristos 1458*75f6d617Schristos@vindex LC_NUMERIC 1459*75f6d617SchristosThe @code{printf} conversion specification can be @samp{%d}, 1460*75f6d617Schristos@samp{%o}, @samp{%x}, or @samp{%X}, specifying decimal, octal, 1461*75f6d617Schristoslower case hexadecimal, or upper case hexadecimal output 1462*75f6d617Schristosrespectively. After the @samp{%} the following options can appear in 1463*75f6d617Schristossequence: a series of zero or more flags; an integer 1464*75f6d617Schristosspecifying the minimum field width; and a period followed by an 1465*75f6d617Schristosoptional integer specifying the minimum number of digits. 1466*75f6d617SchristosThe flags are @samp{-} for left-justification, @samp{'} for separating 1467*75f6d617Schristosthe digit into groups as specified by the @env{LC_NUMERIC} locale category, 1468*75f6d617Schristosand @samp{0} for padding with zeros instead of spaces. 1469*75f6d617SchristosFor example, @samp{%5dN} prints the number of new lines in the group 1470*75f6d617Schristosin a field of width 5 characters, using the @code{printf} format @code{"%5d"}. 1471*75f6d617Schristos 1472*75f6d617Schristos@item (@var{A}=@var{B}?@var{T}:@var{E}) 1473*75f6d617SchristosIf @var{A} equals @var{B} then @var{T} else @var{E}. 1474*75f6d617Schristos@var{A} and @var{B} are each either a decimal constant 1475*75f6d617Schristosor a single letter interpreted as above. 1476*75f6d617SchristosThis format spec is equivalent to @var{T} if 1477*75f6d617Schristos@var{A}'s value equals @var{B}'s; otherwise it is equivalent to @var{E}. 1478*75f6d617Schristos 1479*75f6d617SchristosFor example, @samp{%(N=0?no:%dN) line%(N=1?:s)} is equivalent to 1480*75f6d617Schristos@samp{no lines} if @var{N} (the number of lines in the group in the the 1481*75f6d617Schristosnew file) is 0, to @samp{1 line} if @var{N} is 1, and to @samp{%dN lines} 1482*75f6d617Schristosotherwise. 1483*75f6d617Schristos@end table 1484*75f6d617Schristos 1485*75f6d617Schristos@node Line Formats 1486*75f6d617Schristos@subsection Line Formats 1487*75f6d617Schristos@cindex line formats 1488*75f6d617Schristos 1489*75f6d617SchristosLine formats control how each line taken from an input file is 1490*75f6d617Schristosoutput as part of a line group in if-then-else format. 1491*75f6d617Schristos 1492*75f6d617SchristosFor example, the following command outputs text with a one-character 1493*75f6d617Schristoschange indicator to the left of the text. The first character of output 1494*75f6d617Schristosis @samp{-} for deleted lines, @samp{|} for added lines, and a space for 1495*75f6d617Schristosunchanged lines. The formats contain newline characters where newlines 1496*75f6d617Schristosare desired on output. 1497*75f6d617Schristos 1498*75f6d617Schristos@example 1499*75f6d617Schristosdiff \ 1500*75f6d617Schristos --old-line-format='-%l 1501*75f6d617Schristos' \ 1502*75f6d617Schristos --new-line-format='|%l 1503*75f6d617Schristos' \ 1504*75f6d617Schristos --unchanged-line-format=' %l 1505*75f6d617Schristos' \ 1506*75f6d617Schristos old new 1507*75f6d617Schristos@end example 1508*75f6d617Schristos 1509*75f6d617SchristosTo specify a line format, use one of the following options. You should 1510*75f6d617Schristosquote @var{format}, since it often contains shell metacharacters. 1511*75f6d617Schristos 1512*75f6d617Schristos@table @option 1513*75f6d617Schristos@item --old-line-format=@var{format} 1514*75f6d617Schristosformats lines just from the first file. 1515*75f6d617Schristos 1516*75f6d617Schristos@item --new-line-format=@var{format} 1517*75f6d617Schristosformats lines just from the second file. 1518*75f6d617Schristos 1519*75f6d617Schristos@item --unchanged-line-format=@var{format} 1520*75f6d617Schristosformats lines common to both files. 1521*75f6d617Schristos 1522*75f6d617Schristos@item --line-format=@var{format} 1523*75f6d617Schristosformats all lines; in effect, it sets all three above options simultaneously. 1524*75f6d617Schristos@end table 1525*75f6d617Schristos 1526*75f6d617SchristosIn a line format, ordinary characters represent themselves; 1527*75f6d617Schristosconversion specifications start with @samp{%} and have one of the 1528*75f6d617Schristosfollowing forms. 1529*75f6d617Schristos 1530*75f6d617Schristos@table @samp 1531*75f6d617Schristos@item %l 1532*75f6d617Schristosstands for the contents of the line, not counting its trailing 1533*75f6d617Schristosnewline (if any). This format ignores whether the line is incomplete; 1534*75f6d617Schristos@xref{Incomplete Lines}. 1535*75f6d617Schristos 1536*75f6d617Schristos@item %L 1537*75f6d617Schristosstands for the contents of the line, including its trailing newline 1538*75f6d617Schristos(if any). If a line is incomplete, this format preserves its 1539*75f6d617Schristosincompleteness. 1540*75f6d617Schristos 1541*75f6d617Schristos@item %% 1542*75f6d617Schristosstands for @samp{%}. 1543*75f6d617Schristos 1544*75f6d617Schristos@item %c'@var{C}' 1545*75f6d617Schristoswhere @var{C} is a single character, stands for @var{C}. 1546*75f6d617Schristos@var{C} may not be a backslash or an apostrophe. 1547*75f6d617SchristosFor example, @samp{%c':'} stands for a colon. 1548*75f6d617Schristos 1549*75f6d617Schristos@item %c'\@var{O}' 1550*75f6d617Schristoswhere @var{O} is a string of 1, 2, or 3 octal digits, 1551*75f6d617Schristosstands for the character with octal code @var{O}. 1552*75f6d617SchristosFor example, @samp{%c'\0'} stands for a null character. 1553*75f6d617Schristos 1554*75f6d617Schristos@item @var{F}n 1555*75f6d617Schristoswhere @var{F} is a @code{printf} conversion specification, 1556*75f6d617Schristosstands for the line number formatted with @var{F}. 1557*75f6d617SchristosFor example, @samp{%.5dn} prints the line number using the 1558*75f6d617Schristos@code{printf} format @code{"%.5d"}. @xref{Line Group Formats}, for 1559*75f6d617Schristosmore about printf conversion specifications. 1560*75f6d617Schristos 1561*75f6d617Schristos@end table 1562*75f6d617Schristos 1563*75f6d617SchristosThe default line format is @samp{%l} followed by a newline character. 1564*75f6d617Schristos 1565*75f6d617SchristosIf the input contains tab characters and it is important that they line 1566*75f6d617Schristosup on output, you should ensure that @samp{%l} or @samp{%L} in a line 1567*75f6d617Schristosformat is just after a tab stop (e.g.@: by preceding @samp{%l} or 1568*75f6d617Schristos@samp{%L} with a tab character), or you should use the @option{-t} or 1569*75f6d617Schristos@option{--expand-tabs} option. 1570*75f6d617Schristos 1571*75f6d617SchristosTaken together, the line and line group formats let you specify many 1572*75f6d617Schristosdifferent formats. For example, the following command uses a format 1573*75f6d617Schristossimilar to normal @command{diff} format. You can tailor this command 1574*75f6d617Schristosto get fine control over @command{diff} output. 1575*75f6d617Schristos 1576*75f6d617Schristos@example 1577*75f6d617Schristosdiff \ 1578*75f6d617Schristos --old-line-format='< %l 1579*75f6d617Schristos' \ 1580*75f6d617Schristos --new-line-format='> %l 1581*75f6d617Schristos' \ 1582*75f6d617Schristos --old-group-format='%df%(f=l?:,%dl)d%dE 1583*75f6d617Schristos%<' \ 1584*75f6d617Schristos --new-group-format='%dea%dF%(F=L?:,%dL) 1585*75f6d617Schristos%>' \ 1586*75f6d617Schristos --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL) 1587*75f6d617Schristos%<--- 1588*75f6d617Schristos%>' \ 1589*75f6d617Schristos --unchanged-group-format='' \ 1590*75f6d617Schristos old new 1591*75f6d617Schristos@end example 1592*75f6d617Schristos 1593*75f6d617Schristos@node Detailed If-then-else 1594*75f6d617Schristos@subsection Detailed Description of If-then-else Format 1595*75f6d617Schristos 1596*75f6d617SchristosFor lines common to both files, @command{diff} uses the unchanged line 1597*75f6d617Schristosgroup format. For each hunk of differences in the merged output 1598*75f6d617Schristosformat, if the hunk contains only lines from the first file, 1599*75f6d617Schristos@command{diff} uses the old line group format; if the hunk contains only 1600*75f6d617Schristoslines from the second file, @command{diff} uses the new group format; 1601*75f6d617Schristosotherwise, @command{diff} uses the changed group format. 1602*75f6d617Schristos 1603*75f6d617SchristosThe old, new, and unchanged line formats specify the output format of 1604*75f6d617Schristoslines from the first file, lines from the second file, and lines common 1605*75f6d617Schristosto both files, respectively. 1606*75f6d617Schristos 1607*75f6d617SchristosThe option @option{--ifdef=@var{name}} is equivalent to 1608*75f6d617Schristosthe following sequence of options using shell syntax: 1609*75f6d617Schristos 1610*75f6d617Schristos@example 1611*75f6d617Schristos--old-group-format='#ifndef @var{name} 1612*75f6d617Schristos%<#endif /* ! @var{name} */ 1613*75f6d617Schristos' \ 1614*75f6d617Schristos--new-group-format='#ifdef @var{name} 1615*75f6d617Schristos%>#endif /* @var{name} */ 1616*75f6d617Schristos' \ 1617*75f6d617Schristos--unchanged-group-format='%=' \ 1618*75f6d617Schristos--changed-group-format='#ifndef @var{name} 1619*75f6d617Schristos%<#else /* @var{name} */ 1620*75f6d617Schristos%>#endif /* @var{name} */ 1621*75f6d617Schristos' 1622*75f6d617Schristos@end example 1623*75f6d617Schristos 1624*75f6d617SchristosYou should carefully check the @command{diff} output for proper nesting. 1625*75f6d617SchristosFor example, when using the @option{-D @var{name}} or 1626*75f6d617Schristos@option{--ifdef=@var{name}} option, you should check that if the 1627*75f6d617Schristosdiffering lines contain any of the C preprocessor directives 1628*75f6d617Schristos@samp{#ifdef}, @samp{#ifndef}, @samp{#else}, @samp{#elif}, or 1629*75f6d617Schristos@samp{#endif}, they are nested properly and match. If they don't, you 1630*75f6d617Schristosmust make corrections manually. It is a good idea to carefully check 1631*75f6d617Schristosthe resulting code anyway to make sure that it really does what you 1632*75f6d617Schristoswant it to; depending on how the input files were produced, the output 1633*75f6d617Schristosmight contain duplicate or otherwise incorrect code. 1634*75f6d617Schristos 1635*75f6d617SchristosThe @command{patch} @option{-D @var{name}} option behaves like 1636*75f6d617Schristosthe @command{diff} @option{-D @var{name}} option, except it operates on 1637*75f6d617Schristosa file and a diff to produce a merged file; @xref{patch Options}. 1638*75f6d617Schristos 1639*75f6d617Schristos@node Example If-then-else 1640*75f6d617Schristos@subsection An Example of If-then-else Format 1641*75f6d617Schristos 1642*75f6d617SchristosHere is the output of @samp{diff -DTWO lao tzu} (@pxref{Sample 1643*75f6d617Schristosdiff Input}, for the complete contents of the two files): 1644*75f6d617Schristos 1645*75f6d617Schristos@example 1646*75f6d617Schristos#ifndef TWO 1647*75f6d617SchristosThe Way that can be told of is not the eternal Way; 1648*75f6d617SchristosThe name that can be named is not the eternal name. 1649*75f6d617Schristos#endif /* ! TWO */ 1650*75f6d617SchristosThe Nameless is the origin of Heaven and Earth; 1651*75f6d617Schristos#ifndef TWO 1652*75f6d617SchristosThe Named is the mother of all things. 1653*75f6d617Schristos#else /* TWO */ 1654*75f6d617SchristosThe named is the mother of all things. 1655*75f6d617Schristos 1656*75f6d617Schristos#endif /* TWO */ 1657*75f6d617SchristosTherefore let there always be non-being, 1658*75f6d617Schristos so we may see their subtlety, 1659*75f6d617SchristosAnd let there always be being, 1660*75f6d617Schristos so we may see their outcome. 1661*75f6d617SchristosThe two are the same, 1662*75f6d617SchristosBut after they are produced, 1663*75f6d617Schristos they have different names. 1664*75f6d617Schristos#ifdef TWO 1665*75f6d617SchristosThey both may be called deep and profound. 1666*75f6d617SchristosDeeper and more profound, 1667*75f6d617SchristosThe door of all subtleties! 1668*75f6d617Schristos#endif /* TWO */ 1669*75f6d617Schristos@end example 1670*75f6d617Schristos 1671*75f6d617Schristos@node Incomplete Lines 1672*75f6d617Schristos@chapter Incomplete Lines 1673*75f6d617Schristos@cindex incomplete lines 1674*75f6d617Schristos@cindex full lines 1675*75f6d617Schristos@cindex newline treatment by @command{diff} 1676*75f6d617Schristos 1677*75f6d617SchristosWhen an input file ends in a non-newline character, its last line is 1678*75f6d617Schristoscalled an @dfn{incomplete line} because its last character is not a 1679*75f6d617Schristosnewline. All other lines are called @dfn{full lines} and end in a 1680*75f6d617Schristosnewline character. Incomplete lines do not match full lines unless 1681*75f6d617Schristosdifferences in white space are ignored (@pxref{White Space}). 1682*75f6d617Schristos 1683*75f6d617SchristosAn incomplete line is normally distinguished on output from a full line 1684*75f6d617Schristosby a following line that starts with @samp{\}. However, the @sc{rcs} format 1685*75f6d617Schristos(@pxref{RCS}) outputs the incomplete line as-is, without any trailing 1686*75f6d617Schristosnewline or following line. The side by side format normally represents 1687*75f6d617Schristosincomplete lines as-is, but in some cases uses a @samp{\} or @samp{/} 1688*75f6d617Schristosgutter marker; @xref{Side by Side}. The if-then-else line format 1689*75f6d617Schristospreserves a line's incompleteness with @samp{%L}, and discards the 1690*75f6d617Schristosnewline with @samp{%l}; @xref{Line Formats}. Finally, with the 1691*75f6d617Schristos@command{ed} and forward @command{ed} output formats (@pxref{Output Formats}) 1692*75f6d617Schristos@command{diff} cannot represent an incomplete line, so it pretends there 1693*75f6d617Schristoswas a newline and reports an error. 1694*75f6d617Schristos 1695*75f6d617SchristosFor example, suppose @file{F} and @file{G} are one-byte files that 1696*75f6d617Schristoscontain just @samp{f} and @samp{g}, respectively. Then @samp{diff F G} 1697*75f6d617Schristosoutputs 1698*75f6d617Schristos 1699*75f6d617Schristos@example 1700*75f6d617Schristos1c1 1701*75f6d617Schristos< f 1702*75f6d617Schristos\ No newline at end of file 1703*75f6d617Schristos--- 1704*75f6d617Schristos> g 1705*75f6d617Schristos\ No newline at end of file 1706*75f6d617Schristos@end example 1707*75f6d617Schristos 1708*75f6d617Schristos@noindent 1709*75f6d617Schristos(The exact message may differ in non-English locales.) 1710*75f6d617Schristos@samp{diff -n F G} outputs the following without a trailing newline: 1711*75f6d617Schristos 1712*75f6d617Schristos@example 1713*75f6d617Schristosd1 1 1714*75f6d617Schristosa1 1 1715*75f6d617Schristosg 1716*75f6d617Schristos@end example 1717*75f6d617Schristos 1718*75f6d617Schristos@noindent 1719*75f6d617Schristos@samp{diff -e F G} reports two errors and outputs the following: 1720*75f6d617Schristos 1721*75f6d617Schristos@example 1722*75f6d617Schristos1c 1723*75f6d617Schristosg 1724*75f6d617Schristos. 1725*75f6d617Schristos@end example 1726*75f6d617Schristos 1727*75f6d617Schristos@node Comparing Directories 1728*75f6d617Schristos@chapter Comparing Directories 1729*75f6d617Schristos 1730*75f6d617Schristos@vindex LC_COLLATE 1731*75f6d617SchristosYou can use @command{diff} to compare some or all of the files in two 1732*75f6d617Schristosdirectory trees. When both file name arguments to @command{diff} are 1733*75f6d617Schristosdirectories, it compares each file that is contained in both 1734*75f6d617Schristosdirectories, examining file names in alphabetical order as specified by 1735*75f6d617Schristosthe @env{LC_COLLATE} locale category. Normally 1736*75f6d617Schristos@command{diff} is silent about pairs of files that contain no differences, 1737*75f6d617Schristosbut if you use the @option{-s} or @option{--report-identical-files} option, 1738*75f6d617Schristosit reports pairs of identical files. Normally @command{diff} reports 1739*75f6d617Schristossubdirectories common to both directories without comparing 1740*75f6d617Schristossubdirectories' files, but if you use the @option{-r} or 1741*75f6d617Schristos@option{--recursive} option, it compares every corresponding pair of files 1742*75f6d617Schristosin the directory trees, as many levels deep as they go. 1743*75f6d617Schristos 1744*75f6d617SchristosFor file names that are in only one of the directories, @command{diff} 1745*75f6d617Schristosnormally does not show the contents of the file that exists; it reports 1746*75f6d617Schristosonly that the file exists in that directory and not in the other. You 1747*75f6d617Schristoscan make @command{diff} act as though the file existed but was empty in the 1748*75f6d617Schristosother directory, so that it outputs the entire contents of the file that 1749*75f6d617Schristosactually exists. (It is output as either an insertion or a 1750*75f6d617Schristosdeletion, depending on whether it is in the first or the second 1751*75f6d617Schristosdirectory given.) To do this, use the @option{-N} or @option{--new-file} 1752*75f6d617Schristosoption. 1753*75f6d617Schristos 1754*75f6d617SchristosIf the older directory contains one or more large files that are not in 1755*75f6d617Schristosthe newer directory, you can make the patch smaller by using the 1756*75f6d617Schristos@option{--unidirectional-new-file} option instead of @option{-N}. 1757*75f6d617SchristosThis option is like @option{-N} except that it only inserts the contents 1758*75f6d617Schristosof files that appear in the second directory but not the first (that is, 1759*75f6d617Schristosfiles that were added). At the top of the patch, write instructions for 1760*75f6d617Schristosthe user applying the patch to remove the files that were deleted before 1761*75f6d617Schristosapplying the patch. @xref{Making Patches}, for more discussion of 1762*75f6d617Schristosmaking patches for distribution. 1763*75f6d617Schristos 1764*75f6d617SchristosTo ignore some files while comparing directories, use the @option{-x 1765*75f6d617Schristos@var{pattern}} or @option{--exclude=@var{pattern}} option. This option 1766*75f6d617Schristosignores any files or subdirectories whose base names match the shell 1767*75f6d617Schristospattern @var{pattern}. Unlike in the shell, a period at the start of 1768*75f6d617Schristosthe base of a file name matches a wildcard at the start of a pattern. 1769*75f6d617SchristosYou should enclose @var{pattern} in quotes so that the shell does not 1770*75f6d617Schristosexpand it. For example, the option @option{-x '*.[ao]'} ignores any file 1771*75f6d617Schristoswhose name ends with @samp{.a} or @samp{.o}. 1772*75f6d617Schristos 1773*75f6d617SchristosThis option accumulates if you specify it more than once. For example, 1774*75f6d617Schristosusing the options @option{-x 'RCS' -x '*,v'} ignores any file or 1775*75f6d617Schristossubdirectory whose base name is @samp{RCS} or ends with @samp{,v}. 1776*75f6d617Schristos 1777*75f6d617SchristosIf you need to give this option many times, you can instead put the 1778*75f6d617Schristospatterns in a file, one pattern per line, and use the @option{-X 1779*75f6d617Schristos@var{file}} or @option{--exclude-from=@var{file}} option. 1780*75f6d617Schristos 1781*75f6d617SchristosIf you have been comparing two directories and stopped partway through, 1782*75f6d617Schristoslater you might want to continue where you left off. You can do this by 1783*75f6d617Schristosusing the @option{-S @var{file}} or @option{--starting-file=@var{file}} 1784*75f6d617Schristosoption. This compares only the file @var{file} and all alphabetically 1785*75f6d617Schristoslater files in the topmost directory level. 1786*75f6d617Schristos 1787*75f6d617SchristosIf two directories differ only in that file names are lower case in 1788*75f6d617Schristosone directory and upper case in the upper, @command{diff} normally 1789*75f6d617Schristosreports many differences because it compares file names in a 1790*75f6d617Schristoscase sensitive way. With the @option{--ignore-file-name-case} option, 1791*75f6d617Schristos@command{diff} ignores case differences in file names, so that for example 1792*75f6d617Schristosthe contents of the file @file{Tao} in one directory are compared to 1793*75f6d617Schristosthe contents of the file @file{TAO} in the other. The 1794*75f6d617Schristos@option{--no-ignore-file-name-case} option cancels the effect of the 1795*75f6d617Schristos@option{--ignore-file-name-case} option, reverting to the default 1796*75f6d617Schristosbehavior. 1797*75f6d617Schristos 1798*75f6d617SchristosIf an @option{-x @var{pattern}}, @option{--exclude=@var{pattern}}, 1799*75f6d617Schristos@option{-X @var{file}}, or @option{--exclude-from=@var{file}} option 1800*75f6d617Schristosis specified while the @option{--ignore-file-name-case} option is in 1801*75f6d617Schristoseffect, case is ignored when excluding file names matching the 1802*75f6d617Schristosspecified patterns. 1803*75f6d617Schristos 1804*75f6d617Schristos@node Adjusting Output 1805*75f6d617Schristos@chapter Making @command{diff} Output Prettier 1806*75f6d617Schristos 1807*75f6d617Schristos@command{diff} provides several ways to adjust the appearance of its output. 1808*75f6d617SchristosThese adjustments can be applied to any output format. 1809*75f6d617Schristos 1810*75f6d617Schristos@menu 1811*75f6d617Schristos* Tabs:: Preserving the alignment of tab stops. 1812*75f6d617Schristos* Pagination:: Page numbering and time-stamping @command{diff} output. 1813*75f6d617Schristos@end menu 1814*75f6d617Schristos 1815*75f6d617Schristos@node Tabs 1816*75f6d617Schristos@section Preserving Tab Stop Alignment 1817*75f6d617Schristos@cindex tab stop alignment 1818*75f6d617Schristos@cindex aligning tab stops 1819*75f6d617Schristos 1820*75f6d617SchristosThe lines of text in some of the @command{diff} output formats are preceded 1821*75f6d617Schristosby one or two characters that indicate whether the text is inserted, 1822*75f6d617Schristosdeleted, or changed. The addition of those characters can cause tabs to 1823*75f6d617Schristosmove to the next tab stop, throwing off the alignment of columns in the 1824*75f6d617Schristosline. @sc{gnu} @command{diff} provides two ways to make tab-aligned columns 1825*75f6d617Schristosline up correctly. 1826*75f6d617Schristos 1827*75f6d617SchristosThe first way is to have @command{diff} convert all tabs into the correct 1828*75f6d617Schristosnumber of spaces before outputting them; select this method with the 1829*75f6d617Schristos@option{-t} or @option{--expand-tabs} option. @command{diff} assumes that 1830*75f6d617Schristostab stops are set every 8 print columns. To use this form of output with 1831*75f6d617Schristos@command{patch}, you must give @command{patch} the @option{-l} or 1832*75f6d617Schristos@option{--ignore-white-space} option (@pxref{Changed White Space}, for more 1833*75f6d617Schristosinformation). 1834*75f6d617Schristos 1835*75f6d617SchristosThe other method for making tabs line up correctly is to add a tab 1836*75f6d617Schristoscharacter instead of a space after the indicator character at the 1837*75f6d617Schristosbeginning of the line. This ensures that all following tab characters 1838*75f6d617Schristosare in the same position relative to tab stops that they were in the 1839*75f6d617Schristosoriginal files, so that the output is aligned correctly. Its 1840*75f6d617Schristosdisadvantage is that it can make long lines too long to fit on one line 1841*75f6d617Schristosof the screen or the paper. It also does not work with the unified 1842*75f6d617Schristosoutput format, which does not have a space character after the change 1843*75f6d617Schristostype indicator character. Select this method with the @option{-T} or 1844*75f6d617Schristos@option{--initial-tab} option. 1845*75f6d617Schristos 1846*75f6d617Schristos@node Pagination 1847*75f6d617Schristos@section Paginating @command{diff} Output 1848*75f6d617Schristos@cindex paginating @command{diff} output 1849*75f6d617Schristos 1850*75f6d617SchristosIt can be convenient to have long output page-numbered and time-stamped. 1851*75f6d617SchristosThe @option{-l} and @option{--paginate} options do this by sending the 1852*75f6d617Schristos@command{diff} output through the @command{pr} program. Here is what the page 1853*75f6d617Schristosheader might look like for @samp{diff -lc lao tzu}: 1854*75f6d617Schristos 1855*75f6d617Schristos@example 1856*75f6d617Schristos2002-02-22 14:20 diff -lc lao tzu Page 1 1857*75f6d617Schristos@end example 1858*75f6d617Schristos 1859*75f6d617Schristos@node diff Performance 1860*75f6d617Schristos@chapter @command{diff} Performance Tradeoffs 1861*75f6d617Schristos@cindex performance of @command{diff} 1862*75f6d617Schristos 1863*75f6d617Schristos@sc{gnu} @command{diff} runs quite efficiently; however, in some circumstances 1864*75f6d617Schristosyou can cause it to run faster or produce a more compact set of changes. 1865*75f6d617Schristos 1866*75f6d617SchristosOne way to improve @command{diff} performance is to use hard or 1867*75f6d617Schristossymbolic links to files instead of copies. This improves performance 1868*75f6d617Schristosbecause @command{diff} normally does not need to read two hard or 1869*75f6d617Schristossymbolic links to the same file, since their contents must be 1870*75f6d617Schristosidentical. For example, suppose you copy a large directory hierarchy, 1871*75f6d617Schristosmake a few changes to the copy, and then often use @samp{diff -r} to 1872*75f6d617Schristoscompare the original to the copy. If the original files are 1873*75f6d617Schristosread-only, you can greatly improve performance by creating the copy 1874*75f6d617Schristosusing hard or symbolic links (e.g., with @sc{gnu} @samp{cp -lR} or 1875*75f6d617Schristos@samp{cp -sR}). Before editing a file in the copy for the first time, 1876*75f6d617Schristosyou should break the link and replace it with a regular copy. 1877*75f6d617Schristos 1878*75f6d617SchristosYou can also affect the performance of @sc{gnu} @command{diff} by 1879*75f6d617Schristosgiving it options that change the way it compares files. 1880*75f6d617SchristosPerformance has more than one dimension. These options improve one 1881*75f6d617Schristosaspect of performance at the cost of another, or they improve 1882*75f6d617Schristosperformance in some cases while hurting it in others. 1883*75f6d617Schristos 1884*75f6d617SchristosThe way that @sc{gnu} @command{diff} determines which lines have changed always 1885*75f6d617Schristoscomes up with a near-minimal set of differences. Usually it is good 1886*75f6d617Schristosenough for practical purposes. If the @command{diff} output is large, you 1887*75f6d617Schristosmight want @command{diff} to use a modified algorithm that sometimes 1888*75f6d617Schristosproduces a smaller set of differences. The @option{-d} or 1889*75f6d617Schristos@option{--minimal} option does this; however, it can also cause 1890*75f6d617Schristos@command{diff} to run more slowly than usual, so it is not the default 1891*75f6d617Schristosbehavior. 1892*75f6d617Schristos 1893*75f6d617SchristosWhen the files you are comparing are large and have small groups of 1894*75f6d617Schristoschanges scattered throughout them, you can use the 1895*75f6d617Schristos@option{--speed-large-files} option to make a different modification to 1896*75f6d617Schristosthe algorithm that @command{diff} uses. If the input files have a constant 1897*75f6d617Schristossmall density of changes, this option speeds up the comparisons without 1898*75f6d617Schristoschanging the output. If not, @command{diff} might produce a larger set of 1899*75f6d617Schristosdifferences; however, the output will still be correct. 1900*75f6d617Schristos 1901*75f6d617SchristosNormally @command{diff} discards the prefix and suffix that is common to 1902*75f6d617Schristosboth files before it attempts to find a minimal set of differences. 1903*75f6d617SchristosThis makes @command{diff} run faster, but occasionally it may produce 1904*75f6d617Schristosnon-minimal output. The @option{--horizon-lines=@var{lines}} option 1905*75f6d617Schristosprevents @command{diff} from discarding the last @var{lines} lines of the 1906*75f6d617Schristosprefix and the first @var{lines} lines of the suffix. This gives 1907*75f6d617Schristos@command{diff} further opportunities to find a minimal output. 1908*75f6d617Schristos 1909*75f6d617SchristosSuppose a run of changed lines includes a sequence of lines at one end 1910*75f6d617Schristosand there is an identical sequence of lines just outside the other end. 1911*75f6d617SchristosThe @command{diff} command is free to choose which identical sequence is 1912*75f6d617Schristosincluded in the hunk. In this case, @command{diff} normally shifts the 1913*75f6d617Schristoshunk's boundaries when this merges adjacent hunks, or shifts a hunk's 1914*75f6d617Schristoslines towards the end of the file. Merging hunks can make the output 1915*75f6d617Schristoslook nicer in some cases. 1916*75f6d617Schristos 1917*75f6d617Schristos@node Comparing Three Files 1918*75f6d617Schristos@chapter Comparing Three Files 1919*75f6d617Schristos@cindex comparing three files 1920*75f6d617Schristos@cindex format of @command{diff3} output 1921*75f6d617Schristos 1922*75f6d617SchristosUse the program @command{diff3} to compare three files and show any 1923*75f6d617Schristosdifferences among them. (@command{diff3} can also merge files; see 1924*75f6d617Schristos@ref{diff3 Merging}). 1925*75f6d617Schristos 1926*75f6d617SchristosThe ``normal'' @command{diff3} output format shows each hunk of 1927*75f6d617Schristosdifferences without surrounding context. Hunks are labeled depending 1928*75f6d617Schristoson whether they are two-way or three-way, and lines are annotated by 1929*75f6d617Schristostheir location in the input files. 1930*75f6d617Schristos 1931*75f6d617Schristos@xref{Invoking diff3}, for more information on how to run @command{diff3}. 1932*75f6d617Schristos 1933*75f6d617Schristos@menu 1934*75f6d617Schristos* Sample diff3 Input:: Sample @command{diff3} input for examples. 1935*75f6d617Schristos* Detailed diff3 Normal:: A detailed description of normal output format. 1936*75f6d617Schristos* diff3 Hunks:: The format of normal output format. 1937*75f6d617Schristos* Example diff3 Normal:: Sample output in the normal format. 1938*75f6d617Schristos@end menu 1939*75f6d617Schristos 1940*75f6d617Schristos@node Sample diff3 Input 1941*75f6d617Schristos@section A Third Sample Input File 1942*75f6d617Schristos@cindex @command{diff3} sample input 1943*75f6d617Schristos@cindex sample input for @command{diff3} 1944*75f6d617Schristos 1945*75f6d617SchristosHere is a third sample file that will be used in examples to illustrate 1946*75f6d617Schristosthe output of @command{diff3} and how various options can change it. The 1947*75f6d617Schristosfirst two files are the same that we used for @command{diff} (@pxref{Sample 1948*75f6d617Schristosdiff Input}). This is the third sample file, called @file{tao}: 1949*75f6d617Schristos 1950*75f6d617Schristos@example 1951*75f6d617SchristosThe Way that can be told of is not the eternal Way; 1952*75f6d617SchristosThe name that can be named is not the eternal name. 1953*75f6d617SchristosThe Nameless is the origin of Heaven and Earth; 1954*75f6d617SchristosThe named is the mother of all things. 1955*75f6d617Schristos 1956*75f6d617SchristosTherefore let there always be non-being, 1957*75f6d617Schristos so we may see their subtlety, 1958*75f6d617SchristosAnd let there always be being, 1959*75f6d617Schristos so we may see their result. 1960*75f6d617SchristosThe two are the same, 1961*75f6d617SchristosBut after they are produced, 1962*75f6d617Schristos they have different names. 1963*75f6d617Schristos 1964*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 1965*75f6d617Schristos@end example 1966*75f6d617Schristos 1967*75f6d617Schristos@node Detailed diff3 Normal 1968*75f6d617Schristos@section Detailed Description of @command{diff3} Normal Format 1969*75f6d617Schristos 1970*75f6d617SchristosEach hunk begins with a line marked @samp{====}. Three-way hunks have 1971*75f6d617Schristosplain @samp{====} lines, and two-way hunks have @samp{1}, @samp{2}, or 1972*75f6d617Schristos@samp{3} appended to specify which of the three input files differ in 1973*75f6d617Schristosthat hunk. The hunks contain copies of two or three sets of input 1974*75f6d617Schristoslines each preceded by one or two commands identifying where the lines 1975*75f6d617Schristoscame from. 1976*75f6d617Schristos 1977*75f6d617SchristosNormally, two spaces precede each copy of an input line to distinguish 1978*75f6d617Schristosit from the commands. But with the @option{-T} or @option{--initial-tab} 1979*75f6d617Schristosoption, @command{diff3} uses a tab instead of two spaces; this lines up 1980*75f6d617Schristostabs correctly. @xref{Tabs}, for more information. 1981*75f6d617Schristos 1982*75f6d617SchristosCommands take the following forms: 1983*75f6d617Schristos 1984*75f6d617Schristos@table @samp 1985*75f6d617Schristos@item @var{file}:@var{l}a 1986*75f6d617SchristosThis hunk appears after line @var{l} of file @var{file}, and 1987*75f6d617Schristoscontains no lines in that file. To edit this file to yield the other 1988*75f6d617Schristosfiles, one must append hunk lines taken from the other files. For 1989*75f6d617Schristosexample, @samp{1:11a} means that the hunk follows line 11 in the first 1990*75f6d617Schristosfile and contains no lines from that file. 1991*75f6d617Schristos 1992*75f6d617Schristos@item @var{file}:@var{r}c 1993*75f6d617SchristosThis hunk contains the lines in the range @var{r} of file @var{file}. 1994*75f6d617SchristosThe range @var{r} is a comma-separated pair of line numbers, or just one 1995*75f6d617Schristosnumber if the range is a singleton. To edit this file to yield the 1996*75f6d617Schristosother files, one must change the specified lines to be the lines taken 1997*75f6d617Schristosfrom the other files. For example, @samp{2:11,13c} means that the hunk 1998*75f6d617Schristoscontains lines 11 through 13 from the second file. 1999*75f6d617Schristos@end table 2000*75f6d617Schristos 2001*75f6d617SchristosIf the last line in a set of input lines is incomplete 2002*75f6d617Schristos(@pxref{Incomplete Lines}), it is distinguished on output from a full 2003*75f6d617Schristosline by a following line that starts with @samp{\}. 2004*75f6d617Schristos 2005*75f6d617Schristos@node diff3 Hunks 2006*75f6d617Schristos@section @command{diff3} Hunks 2007*75f6d617Schristos@cindex hunks for @command{diff3} 2008*75f6d617Schristos@cindex @command{diff3} hunks 2009*75f6d617Schristos 2010*75f6d617SchristosGroups of lines that differ in two or three of the input files are 2011*75f6d617Schristoscalled @dfn{diff3 hunks}, by analogy with @command{diff} hunks 2012*75f6d617Schristos(@pxref{Hunks}). If all three input files differ in a @command{diff3} 2013*75f6d617Schristoshunk, the hunk is called a @dfn{three-way hunk}; if just two input files 2014*75f6d617Schristosdiffer, it is a @dfn{two-way hunk}. 2015*75f6d617Schristos 2016*75f6d617SchristosAs with @command{diff}, several solutions are possible. When comparing the 2017*75f6d617Schristosfiles @samp{A}, @samp{B}, and @samp{C}, @command{diff3} normally finds 2018*75f6d617Schristos@command{diff3} hunks by merging the two-way hunks output by the two 2019*75f6d617Schristoscommands @samp{diff A B} and @samp{diff A C}. This does not necessarily 2020*75f6d617Schristosminimize the size of the output, but exceptions should be rare. 2021*75f6d617Schristos 2022*75f6d617SchristosFor example, suppose @file{F} contains the three lines @samp{a}, 2023*75f6d617Schristos@samp{b}, @samp{f}, @file{G} contains the lines @samp{g}, @samp{b}, 2024*75f6d617Schristos@samp{g}, and @file{H} contains the lines @samp{a}, @samp{b}, 2025*75f6d617Schristos@samp{h}. @samp{diff3 F G H} might output the following: 2026*75f6d617Schristos 2027*75f6d617Schristos@example 2028*75f6d617Schristos====2 2029*75f6d617Schristos1:1c 2030*75f6d617Schristos3:1c 2031*75f6d617Schristos a 2032*75f6d617Schristos2:1c 2033*75f6d617Schristos g 2034*75f6d617Schristos==== 2035*75f6d617Schristos1:3c 2036*75f6d617Schristos f 2037*75f6d617Schristos2:3c 2038*75f6d617Schristos g 2039*75f6d617Schristos3:3c 2040*75f6d617Schristos h 2041*75f6d617Schristos@end example 2042*75f6d617Schristos 2043*75f6d617Schristos@noindent 2044*75f6d617Schristosbecause it found a two-way hunk containing @samp{a} in the first and 2045*75f6d617Schristosthird files and @samp{g} in the second file, then the single line 2046*75f6d617Schristos@samp{b} common to all three files, then a three-way hunk containing 2047*75f6d617Schristosthe last line of each file. 2048*75f6d617Schristos 2049*75f6d617Schristos@node Example diff3 Normal 2050*75f6d617Schristos@section An Example of @command{diff3} Normal Format 2051*75f6d617Schristos 2052*75f6d617SchristosHere is the output of the command @samp{diff3 lao tzu tao} 2053*75f6d617Schristos(@pxref{Sample diff3 Input}, for the complete contents of the files). 2054*75f6d617SchristosNotice that it shows only the lines that are different among the three 2055*75f6d617Schristosfiles. 2056*75f6d617Schristos 2057*75f6d617Schristos@example 2058*75f6d617Schristos====2 2059*75f6d617Schristos1:1,2c 2060*75f6d617Schristos3:1,2c 2061*75f6d617Schristos The Way that can be told of is not the eternal Way; 2062*75f6d617Schristos The name that can be named is not the eternal name. 2063*75f6d617Schristos2:0a 2064*75f6d617Schristos====1 2065*75f6d617Schristos1:4c 2066*75f6d617Schristos The Named is the mother of all things. 2067*75f6d617Schristos2:2,3c 2068*75f6d617Schristos3:4,5c 2069*75f6d617Schristos The named is the mother of all things. 2070*75f6d617Schristos 2071*75f6d617Schristos====3 2072*75f6d617Schristos1:8c 2073*75f6d617Schristos2:7c 2074*75f6d617Schristos so we may see their outcome. 2075*75f6d617Schristos3:9c 2076*75f6d617Schristos so we may see their result. 2077*75f6d617Schristos==== 2078*75f6d617Schristos1:11a 2079*75f6d617Schristos2:11,13c 2080*75f6d617Schristos They both may be called deep and profound. 2081*75f6d617Schristos Deeper and more profound, 2082*75f6d617Schristos The door of all subtleties! 2083*75f6d617Schristos3:13,14c 2084*75f6d617Schristos 2085*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 2086*75f6d617Schristos@end example 2087*75f6d617Schristos 2088*75f6d617Schristos@node diff3 Merging 2089*75f6d617Schristos@chapter Merging From a Common Ancestor 2090*75f6d617Schristos@cindex merging from a common ancestor 2091*75f6d617Schristos 2092*75f6d617SchristosWhen two people have made changes to copies of the same file, 2093*75f6d617Schristos@command{diff3} can produce a merged output that contains both sets of 2094*75f6d617Schristoschanges together with warnings about conflicts. 2095*75f6d617Schristos 2096*75f6d617SchristosOne might imagine programs with names like @command{diff4} and @command{diff5} 2097*75f6d617Schristosto compare more than three files simultaneously, but in practice the 2098*75f6d617Schristosneed rarely arises. You can use @command{diff3} to merge three or more 2099*75f6d617Schristossets of changes to a file by merging two change sets at a time. 2100*75f6d617Schristos 2101*75f6d617Schristos@command{diff3} can incorporate changes from two modified versions into a 2102*75f6d617Schristoscommon preceding version. This lets you merge the sets of changes 2103*75f6d617Schristosrepresented by the two newer files. Specify the common ancestor version 2104*75f6d617Schristosas the second argument and the two newer versions as the first and third 2105*75f6d617Schristosarguments, like this: 2106*75f6d617Schristos 2107*75f6d617Schristos@example 2108*75f6d617Schristosdiff3 @var{mine} @var{older} @var{yours} 2109*75f6d617Schristos@end example 2110*75f6d617Schristos 2111*75f6d617Schristos@noindent 2112*75f6d617SchristosYou can remember the order of the arguments by noting that they are in 2113*75f6d617Schristosalphabetical order. 2114*75f6d617Schristos 2115*75f6d617Schristos@cindex conflict 2116*75f6d617Schristos@cindex overlap 2117*75f6d617SchristosYou can think of this as subtracting @var{older} from @var{yours} and 2118*75f6d617Schristosadding the result to @var{mine}, or as merging into @var{mine} the 2119*75f6d617Schristoschanges that would turn @var{older} into @var{yours}. This merging is 2120*75f6d617Schristoswell-defined as long as @var{mine} and @var{older} match in the 2121*75f6d617Schristosneighborhood of each such change. This fails to be true when all three 2122*75f6d617Schristosinput files differ or when only @var{older} differs; we call this 2123*75f6d617Schristosa @dfn{conflict}. When all three input files differ, we call the 2124*75f6d617Schristosconflict an @dfn{overlap}. 2125*75f6d617Schristos 2126*75f6d617Schristos@command{diff3} gives you several ways to handle overlaps and conflicts. 2127*75f6d617SchristosYou can omit overlaps or conflicts, or select only overlaps, 2128*75f6d617Schristosor mark conflicts with special @samp{<<<<<<<} and @samp{>>>>>>>} lines. 2129*75f6d617Schristos 2130*75f6d617Schristos@command{diff3} can output the merge results as an @command{ed} script that 2131*75f6d617Schristosthat can be applied to the first file to yield the merged output. 2132*75f6d617SchristosHowever, it is usually better to have @command{diff3} generate the merged 2133*75f6d617Schristosoutput directly; this bypasses some problems with @command{ed}. 2134*75f6d617Schristos 2135*75f6d617Schristos@menu 2136*75f6d617Schristos* Which Changes:: Selecting changes to incorporate. 2137*75f6d617Schristos* Marking Conflicts:: Marking conflicts. 2138*75f6d617Schristos* Bypassing ed:: Generating merged output directly. 2139*75f6d617Schristos* Merging Incomplete Lines:: How @command{diff3} merges incomplete lines. 2140*75f6d617Schristos* Saving the Changed File:: Emulating System V behavior. 2141*75f6d617Schristos@end menu 2142*75f6d617Schristos 2143*75f6d617Schristos@node Which Changes 2144*75f6d617Schristos@section Selecting Which Changes to Incorporate 2145*75f6d617Schristos@cindex overlapping change, selection of 2146*75f6d617Schristos@cindex unmerged change 2147*75f6d617Schristos 2148*75f6d617SchristosYou can select all unmerged changes from @var{older} to @var{yours} for merging 2149*75f6d617Schristosinto @var{mine} with the @option{-e} or @option{--ed} option. You can 2150*75f6d617Schristosselect only the nonoverlapping unmerged changes with @option{-3} or 2151*75f6d617Schristos@option{--easy-only}, and you can select only the overlapping changes with 2152*75f6d617Schristos@option{-x} or @option{--overlap-only}. 2153*75f6d617Schristos 2154*75f6d617SchristosThe @option{-e}, @option{-3} and @option{-x} options select only 2155*75f6d617Schristos@dfn{unmerged changes}, i.e.@: changes where @var{mine} and @var{yours} 2156*75f6d617Schristosdiffer; they ignore changes from @var{older} to @var{yours} where 2157*75f6d617Schristos@var{mine} and @var{yours} are identical, because they assume that such 2158*75f6d617Schristoschanges have already been merged. If this assumption is not a safe 2159*75f6d617Schristosone, you can use the @option{-A} or @option{--show-all} option 2160*75f6d617Schristos(@pxref{Marking Conflicts}). 2161*75f6d617Schristos 2162*75f6d617SchristosHere is the output of the command @command{diff3} with each of these three 2163*75f6d617Schristosoptions (@pxref{Sample diff3 Input}, for the complete contents of the files). 2164*75f6d617SchristosNotice that @option{-e} outputs the union of the disjoint sets of changes 2165*75f6d617Schristosoutput by @option{-3} and @option{-x}. 2166*75f6d617Schristos 2167*75f6d617SchristosOutput of @samp{diff3 -e lao tzu tao}: 2168*75f6d617Schristos@example 2169*75f6d617Schristos11a 2170*75f6d617Schristos 2171*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 2172*75f6d617Schristos. 2173*75f6d617Schristos8c 2174*75f6d617Schristos so we may see their result. 2175*75f6d617Schristos. 2176*75f6d617Schristos@end example 2177*75f6d617Schristos 2178*75f6d617SchristosOutput of @samp{diff3 -3 lao tzu tao}: 2179*75f6d617Schristos@example 2180*75f6d617Schristos8c 2181*75f6d617Schristos so we may see their result. 2182*75f6d617Schristos. 2183*75f6d617Schristos@end example 2184*75f6d617Schristos 2185*75f6d617SchristosOutput of @samp{diff3 -x lao tzu tao}: 2186*75f6d617Schristos@example 2187*75f6d617Schristos11a 2188*75f6d617Schristos 2189*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 2190*75f6d617Schristos. 2191*75f6d617Schristos@end example 2192*75f6d617Schristos 2193*75f6d617Schristos@node Marking Conflicts 2194*75f6d617Schristos@section Marking Conflicts 2195*75f6d617Schristos@cindex conflict marking 2196*75f6d617Schristos@cindex @samp{<<<<<<<} for marking conflicts 2197*75f6d617Schristos 2198*75f6d617Schristos@command{diff3} can mark conflicts in the merged output by 2199*75f6d617Schristosbracketing them with special marker lines. A conflict 2200*75f6d617Schristosthat comes from two files @var{A} and @var{B} is marked as follows: 2201*75f6d617Schristos 2202*75f6d617Schristos@example 2203*75f6d617Schristos<<<<<<< @var{A} 2204*75f6d617Schristos@r{lines from @var{A}} 2205*75f6d617Schristos======= 2206*75f6d617Schristos@r{lines from @var{B}} 2207*75f6d617Schristos>>>>>>> @var{B} 2208*75f6d617Schristos@end example 2209*75f6d617Schristos 2210*75f6d617SchristosA conflict that comes from three files @var{A}, @var{B} and @var{C} is 2211*75f6d617Schristosmarked as follows: 2212*75f6d617Schristos 2213*75f6d617Schristos@example 2214*75f6d617Schristos<<<<<<< @var{A} 2215*75f6d617Schristos@r{lines from @var{A}} 2216*75f6d617Schristos||||||| @var{B} 2217*75f6d617Schristos@r{lines from @var{B}} 2218*75f6d617Schristos======= 2219*75f6d617Schristos@r{lines from @var{C}} 2220*75f6d617Schristos>>>>>>> @var{C} 2221*75f6d617Schristos@end example 2222*75f6d617Schristos 2223*75f6d617SchristosThe @option{-A} or @option{--show-all} option acts like the @option{-e} 2224*75f6d617Schristosoption, except that it brackets conflicts, and it outputs all changes 2225*75f6d617Schristosfrom @var{older} to @var{yours}, not just the unmerged changes. Thus, 2226*75f6d617Schristosgiven the sample input files (@pxref{Sample diff3 Input}), @samp{diff3 2227*75f6d617Schristos-A lao tzu tao} puts brackets around the conflict where only @file{tzu} 2228*75f6d617Schristosdiffers: 2229*75f6d617Schristos 2230*75f6d617Schristos@example 2231*75f6d617Schristos<<<<<<< tzu 2232*75f6d617Schristos======= 2233*75f6d617SchristosThe Way that can be told of is not the eternal Way; 2234*75f6d617SchristosThe name that can be named is not the eternal name. 2235*75f6d617Schristos>>>>>>> tao 2236*75f6d617Schristos@end example 2237*75f6d617Schristos 2238*75f6d617SchristosAnd it outputs the three-way conflict as follows: 2239*75f6d617Schristos 2240*75f6d617Schristos@example 2241*75f6d617Schristos<<<<<<< lao 2242*75f6d617Schristos||||||| tzu 2243*75f6d617SchristosThey both may be called deep and profound. 2244*75f6d617SchristosDeeper and more profound, 2245*75f6d617SchristosThe door of all subtleties! 2246*75f6d617Schristos======= 2247*75f6d617Schristos 2248*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 2249*75f6d617Schristos>>>>>>> tao 2250*75f6d617Schristos@end example 2251*75f6d617Schristos 2252*75f6d617SchristosThe @option{-E} or @option{--show-overlap} option outputs less information 2253*75f6d617Schristosthan the @option{-A} or @option{--show-all} option, because it outputs only 2254*75f6d617Schristosunmerged changes, and it never outputs the contents of the second 2255*75f6d617Schristosfile. Thus the @option{-E} option acts like the @option{-e} option, 2256*75f6d617Schristosexcept that it brackets the first and third files from three-way 2257*75f6d617Schristosoverlapping changes. Similarly, @option{-X} acts like @option{-x}, except 2258*75f6d617Schristosit brackets all its (necessarily overlapping) changes. For example, 2259*75f6d617Schristosfor the three-way overlapping change above, the @option{-E} and @option{-X} 2260*75f6d617Schristosoptions output the following: 2261*75f6d617Schristos 2262*75f6d617Schristos@example 2263*75f6d617Schristos<<<<<<< lao 2264*75f6d617Schristos======= 2265*75f6d617Schristos 2266*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 2267*75f6d617Schristos>>>>>>> tao 2268*75f6d617Schristos@end example 2269*75f6d617Schristos 2270*75f6d617SchristosIf you are comparing files that have meaningless or uninformative names, 2271*75f6d617Schristosyou can use the @option{-L @var{label}} or @option{--label=@var{label}} 2272*75f6d617Schristosoption to show alternate names in the @samp{<<<<<<<}, @samp{|||||||} 2273*75f6d617Schristosand @samp{>>>>>>>} brackets. This option can be given up to three 2274*75f6d617Schristostimes, once for each input file. Thus @samp{diff3 -A -L X -L Y -L Z A 2275*75f6d617SchristosB C} acts like @samp{diff3 -A A B C}, except that the output looks like 2276*75f6d617Schristosit came from files named @samp{X}, @samp{Y} and @samp{Z} rather than 2277*75f6d617Schristosfrom files named @samp{A}, @samp{B} and @samp{C}. 2278*75f6d617Schristos 2279*75f6d617Schristos@node Bypassing ed 2280*75f6d617Schristos@section Generating the Merged Output Directly 2281*75f6d617Schristos@cindex merged @command{diff3} format 2282*75f6d617Schristos 2283*75f6d617SchristosWith the @option{-m} or @option{--merge} option, @command{diff3} outputs the 2284*75f6d617Schristosmerged file directly. This is more efficient than using @command{ed} to 2285*75f6d617Schristosgenerate it, and works even with non-text files that @command{ed} would 2286*75f6d617Schristosreject. If you specify @option{-m} without an @command{ed} script option, 2287*75f6d617Schristos@option{-A} (@option{--show-all}) is assumed. 2288*75f6d617Schristos 2289*75f6d617SchristosFor example, the command @samp{diff3 -m lao tzu tao} 2290*75f6d617Schristos(@pxref{Sample diff3 Input} for a copy of the input files) would output 2291*75f6d617Schristosthe following: 2292*75f6d617Schristos 2293*75f6d617Schristos@example 2294*75f6d617Schristos<<<<<<< tzu 2295*75f6d617Schristos======= 2296*75f6d617SchristosThe Way that can be told of is not the eternal Way; 2297*75f6d617SchristosThe name that can be named is not the eternal name. 2298*75f6d617Schristos>>>>>>> tao 2299*75f6d617SchristosThe Nameless is the origin of Heaven and Earth; 2300*75f6d617SchristosThe Named is the mother of all things. 2301*75f6d617SchristosTherefore let there always be non-being, 2302*75f6d617Schristos so we may see their subtlety, 2303*75f6d617SchristosAnd let there always be being, 2304*75f6d617Schristos so we may see their result. 2305*75f6d617SchristosThe two are the same, 2306*75f6d617SchristosBut after they are produced, 2307*75f6d617Schristos they have different names. 2308*75f6d617Schristos<<<<<<< lao 2309*75f6d617Schristos||||||| tzu 2310*75f6d617SchristosThey both may be called deep and profound. 2311*75f6d617SchristosDeeper and more profound, 2312*75f6d617SchristosThe door of all subtleties! 2313*75f6d617Schristos======= 2314*75f6d617Schristos 2315*75f6d617Schristos -- The Way of Lao-Tzu, tr. Wing-tsit Chan 2316*75f6d617Schristos>>>>>>> tao 2317*75f6d617Schristos@end example 2318*75f6d617Schristos 2319*75f6d617Schristos@node Merging Incomplete Lines 2320*75f6d617Schristos@section How @command{diff3} Merges Incomplete Lines 2321*75f6d617Schristos@cindex incomplete line merging 2322*75f6d617Schristos 2323*75f6d617SchristosWith @option{-m}, incomplete lines (@pxref{Incomplete Lines}) are simply 2324*75f6d617Schristoscopied to the output as they are found; if the merged output ends in an 2325*75f6d617Schristosconflict and one of the input files ends in an incomplete 2326*75f6d617Schristosline, succeeding @samp{|||||||}, @samp{=======} or @samp{>>>>>>>} 2327*75f6d617Schristosbrackets appear somewhere other than the start of a line because 2328*75f6d617Schristosthey are appended to the incomplete line. 2329*75f6d617Schristos 2330*75f6d617SchristosWithout @option{-m}, if an @command{ed} script option is specified and an 2331*75f6d617Schristosincomplete line is found, @command{diff3} generates a warning and acts as 2332*75f6d617Schristosif a newline had been present. 2333*75f6d617Schristos 2334*75f6d617Schristos@node Saving the Changed File 2335*75f6d617Schristos@section Saving the Changed File 2336*75f6d617Schristos@cindex System V @command{diff3} compatibility 2337*75f6d617Schristos 2338*75f6d617SchristosTraditional Unix @command{diff3} generates an @command{ed} script without the 2339*75f6d617Schristostrailing @samp{w} and @samp{q} commands that save the changes. 2340*75f6d617SchristosSystem V @command{diff3} generates these extra commands. @sc{gnu} 2341*75f6d617Schristos@command{diff3} normally behaves like traditional Unix 2342*75f6d617Schristos@command{diff3}, but with the @option{-i} option it behaves like 2343*75f6d617SchristosSystem V @command{diff3} and appends the @samp{w} and @samp{q} 2344*75f6d617Schristoscommands. 2345*75f6d617Schristos 2346*75f6d617SchristosThe @option{-i} option requires one of the @command{ed} script options 2347*75f6d617Schristos@option{-AeExX3}, and is incompatible with the merged output option 2348*75f6d617Schristos@option{-m}. 2349*75f6d617Schristos 2350*75f6d617Schristos@node Interactive Merging 2351*75f6d617Schristos@chapter Interactive Merging with @command{sdiff} 2352*75f6d617Schristos@cindex diff merging 2353*75f6d617Schristos@cindex interactive merging 2354*75f6d617Schristos 2355*75f6d617SchristosWith @command{sdiff}, you can merge two files interactively based on a 2356*75f6d617Schristosside-by-side @option{-y} format comparison (@pxref{Side by Side}). Use 2357*75f6d617Schristos@option{-o @var{file}} or @option{--output=@var{file}} to specify where to 2358*75f6d617Schristosput the merged text. @xref{Invoking sdiff}, for more details on the 2359*75f6d617Schristosoptions to @command{sdiff}. 2360*75f6d617Schristos 2361*75f6d617SchristosAnother way to merge files interactively is to use the Emacs Lisp 2362*75f6d617Schristospackage @command{emerge}. @xref{emerge, , emerge, emacs, The @sc{gnu} Emacs 2363*75f6d617SchristosManual}, for more information. 2364*75f6d617Schristos 2365*75f6d617Schristos@menu 2366*75f6d617Schristos* sdiff Option Summary:: Summary of @command{sdiff} options. 2367*75f6d617Schristos* Merge Commands:: Merging two files interactively. 2368*75f6d617Schristos@end menu 2369*75f6d617Schristos 2370*75f6d617Schristos@node sdiff Option Summary 2371*75f6d617Schristos@section Specifying @command{diff} Options to @command{sdiff} 2372*75f6d617Schristos@cindex @command{sdiff} output format 2373*75f6d617Schristos 2374*75f6d617SchristosThe following @command{sdiff} options have the same meaning as for 2375*75f6d617Schristos@command{diff}. @xref{diff Options}, for the use of these options. 2376*75f6d617Schristos 2377*75f6d617Schristos@example 2378*75f6d617Schristos-a -b -d -i -t -v 2379*75f6d617Schristos-B -E -I @var{regexp} 2380*75f6d617Schristos 2381*75f6d617Schristos--ignore-blank-lines --ignore-case 2382*75f6d617Schristos--ignore-matching-lines=@var{regexp} --ignore-space-change 2383*75f6d617Schristos--ignore-tab-expansion 2384*75f6d617Schristos--left-column --minimal --speed-large-files 2385*75f6d617Schristos--strip-trailing-cr --suppress-common-lines --expand-tabs 2386*75f6d617Schristos--text --version --width=@var{columns} 2387*75f6d617Schristos@end example 2388*75f6d617Schristos 2389*75f6d617SchristosFor historical reasons, @command{sdiff} has alternate names for some 2390*75f6d617Schristosoptions. The @option{-l} option is equivalent to the 2391*75f6d617Schristos@option{--left-column} option, and similarly @option{-s} is equivalent 2392*75f6d617Schristosto @option{--suppress-common-lines}. The meaning of the @command{sdiff} 2393*75f6d617Schristos@option{-w} and @option{-W} options is interchanged from that of 2394*75f6d617Schristos@command{diff}: with @command{sdiff}, @option{-w @var{columns}} is 2395*75f6d617Schristosequivalent to @option{--width=@var{columns}}, and @option{-W} is 2396*75f6d617Schristosequivalent to @option{--ignore-all-space}. @command{sdiff} without the 2397*75f6d617Schristos@option{-o} option is equivalent to @command{diff} with the @option{-y} 2398*75f6d617Schristosor @option{--side-by-side} option (@pxref{Side by Side}). 2399*75f6d617Schristos 2400*75f6d617Schristos@node Merge Commands 2401*75f6d617Schristos@section Merge Commands 2402*75f6d617Schristos@cindex merge commands 2403*75f6d617Schristos@cindex merging interactively 2404*75f6d617Schristos 2405*75f6d617SchristosGroups of common lines, with a blank gutter, are copied from the first 2406*75f6d617Schristosfile to the output. After each group of differing lines, @command{sdiff} 2407*75f6d617Schristosprompts with @samp{%} and pauses, waiting for one of the following 2408*75f6d617Schristoscommands. Follow each command with @key{RET}. 2409*75f6d617Schristos 2410*75f6d617Schristos@table @samp 2411*75f6d617Schristos@item e 2412*75f6d617SchristosDiscard both versions. 2413*75f6d617SchristosInvoke a text editor on an empty temporary file, 2414*75f6d617Schristosthen copy the resulting file to the output. 2415*75f6d617Schristos 2416*75f6d617Schristos@item eb 2417*75f6d617SchristosConcatenate the two versions, edit the result in a temporary file, 2418*75f6d617Schristosthen copy the edited result to the output. 2419*75f6d617Schristos 2420*75f6d617Schristos@item ed 2421*75f6d617SchristosLike @samp{eb}, except precede each version with a header that 2422*75f6d617Schristosshows what file and lines the version came from. 2423*75f6d617Schristos 2424*75f6d617Schristos@item el 2425*75f6d617SchristosEdit a copy of the left version, then copy the result to the output. 2426*75f6d617Schristos 2427*75f6d617Schristos@item er 2428*75f6d617SchristosEdit a copy of the right version, then copy the result to the output. 2429*75f6d617Schristos 2430*75f6d617Schristos@item l 2431*75f6d617SchristosCopy the left version to the output. 2432*75f6d617Schristos 2433*75f6d617Schristos@item q 2434*75f6d617SchristosQuit. 2435*75f6d617Schristos 2436*75f6d617Schristos@item r 2437*75f6d617SchristosCopy the right version to the output. 2438*75f6d617Schristos 2439*75f6d617Schristos@item s 2440*75f6d617SchristosSilently copy common lines. 2441*75f6d617Schristos 2442*75f6d617Schristos@item v 2443*75f6d617SchristosVerbosely copy common lines. This is the default. 2444*75f6d617Schristos@end table 2445*75f6d617Schristos 2446*75f6d617Schristos@vindex EDITOR 2447*75f6d617SchristosThe text editor invoked is specified by the @env{EDITOR} environment 2448*75f6d617Schristosvariable if it is set. The default is system-dependent. 2449*75f6d617Schristos 2450*75f6d617Schristos@node Merging with patch 2451*75f6d617Schristos@chapter Merging with @command{patch} 2452*75f6d617Schristos 2453*75f6d617Schristos@command{patch} takes comparison output produced by @command{diff} and applies 2454*75f6d617Schristosthe differences to a copy of the original file, producing a patched 2455*75f6d617Schristosversion. With @command{patch}, you can distribute just the changes to a 2456*75f6d617Schristosset of files instead of distributing the entire file set; your 2457*75f6d617Schristoscorrespondents can apply @command{patch} to update their copy of the files 2458*75f6d617Schristoswith your changes. @command{patch} automatically determines the diff 2459*75f6d617Schristosformat, skips any leading or trailing headers, and uses the headers to 2460*75f6d617Schristosdetermine which file to patch. This lets your correspondents feed a 2461*75f6d617Schristosmail message containing a difference listing directly to 2462*75f6d617Schristos@command{patch}. 2463*75f6d617Schristos 2464*75f6d617Schristos@command{patch} detects and warns about common problems like forward 2465*75f6d617Schristospatches. It saves any patches that it could not apply. It can also maintain a 2466*75f6d617Schristos@code{patchlevel.h} file to ensure that your correspondents apply 2467*75f6d617Schristosdiffs in the proper order. 2468*75f6d617Schristos 2469*75f6d617Schristos@command{patch} accepts a series of diffs in its standard input, usually 2470*75f6d617Schristosseparated by headers that specify which file to patch. It applies 2471*75f6d617Schristos@command{diff} hunks (@pxref{Hunks}) one by one. If a hunk does not 2472*75f6d617Schristosexactly match the original file, @command{patch} uses heuristics to try to 2473*75f6d617Schristospatch the file as well as it can. If no approximate match can be found, 2474*75f6d617Schristos@command{patch} rejects the hunk and skips to the next hunk. @command{patch} 2475*75f6d617Schristosnormally replaces each file @var{f} with its new version, putting reject 2476*75f6d617Schristoshunks (if any) into @samp{@var{f}.rej}. 2477*75f6d617Schristos 2478*75f6d617Schristos@xref{Invoking patch}, for detailed information on the options to 2479*75f6d617Schristos@command{patch}. 2480*75f6d617Schristos 2481*75f6d617Schristos@menu 2482*75f6d617Schristos* patch Input:: Selecting the type of @command{patch} input. 2483*75f6d617Schristos* Revision Control:: Getting files from @sc{rcs}, @sc{sccs}, etc. 2484*75f6d617Schristos* Imperfect:: Dealing with imperfect patches. 2485*75f6d617Schristos* Creating and Removing:: Creating and removing files with a patch. 2486*75f6d617Schristos* Patching Time Stamps:: Updating time stamps on patched files. 2487*75f6d617Schristos* Multiple Patches:: Handling multiple patches in a file. 2488*75f6d617Schristos* patch Directories:: Changing directory and stripping directories. 2489*75f6d617Schristos* Backups:: Whether backup files are made. 2490*75f6d617Schristos* Backup Names:: Backup file names. 2491*75f6d617Schristos* Reject Names:: Reject file names. 2492*75f6d617Schristos* patch Messages:: Messages and questions @command{patch} can produce. 2493*75f6d617Schristos* patch and POSIX:: Conformance to the @sc{posix} standard. 2494*75f6d617Schristos* patch and Tradition:: @sc{gnu} versus traditional @command{patch}. 2495*75f6d617Schristos@end menu 2496*75f6d617Schristos 2497*75f6d617Schristos@node patch Input 2498*75f6d617Schristos@section Selecting the @command{patch} Input Format 2499*75f6d617Schristos@cindex @command{patch} input format 2500*75f6d617Schristos 2501*75f6d617Schristos@command{patch} normally determines which @command{diff} format the patch 2502*75f6d617Schristosfile uses by examining its contents. For patch files that contain 2503*75f6d617Schristosparticularly confusing leading text, you might need to use one of the 2504*75f6d617Schristosfollowing options to force @command{patch} to interpret the patch file as a 2505*75f6d617Schristoscertain format of diff. The output formats listed here are the only 2506*75f6d617Schristosones that @command{patch} can understand. 2507*75f6d617Schristos 2508*75f6d617Schristos@table @option 2509*75f6d617Schristos@item -c 2510*75f6d617Schristos@itemx --context 2511*75f6d617Schristoscontext diff. 2512*75f6d617Schristos 2513*75f6d617Schristos@item -e 2514*75f6d617Schristos@itemx --ed 2515*75f6d617Schristos@command{ed} script. 2516*75f6d617Schristos 2517*75f6d617Schristos@item -n 2518*75f6d617Schristos@itemx --normal 2519*75f6d617Schristosnormal diff. 2520*75f6d617Schristos 2521*75f6d617Schristos@item -u 2522*75f6d617Schristos@itemx --unified 2523*75f6d617Schristosunified diff. 2524*75f6d617Schristos@end table 2525*75f6d617Schristos 2526*75f6d617Schristos@node Revision Control 2527*75f6d617Schristos@section Revision Control 2528*75f6d617Schristos@cindex revision control 2529*75f6d617Schristos@cindex version control 2530*75f6d617Schristos@cindex @sc{rcs} 2531*75f6d617Schristos@cindex ClearCase 2532*75f6d617Schristos@cindex @sc{sccs} 2533*75f6d617Schristos 2534*75f6d617SchristosIf a nonexistent input file is under a revision control system 2535*75f6d617Schristossupported by @command{patch}, @command{patch} normally asks the user 2536*75f6d617Schristoswhether to get (or check out) the file from the revision control 2537*75f6d617Schristossystem. Patch currently supports @sc{rcs}, ClearCase and @sc{sccs}. 2538*75f6d617SchristosUnder @sc{rcs} and @sc{sccs}, @command{patch} also asks when the input 2539*75f6d617Schristosfile is read-only and matches the default version in the revision 2540*75f6d617Schristoscontrol system. 2541*75f6d617Schristos 2542*75f6d617Schristos@vindex PATCH_GET 2543*75f6d617SchristosThe @option{-g @var{num}} or @option{--get=@var{num}} affects access 2544*75f6d617Schristosto files under supported revision control systems. If @var{num} is 2545*75f6d617Schristospositive, @command{patch} gets the file without asking the user; if 2546*75f6d617Schristoszero, @command{patch} neither asks the user nor gets the file; and if 2547*75f6d617Schristosnegative, @command{patch} asks the user before getting the file. The 2548*75f6d617Schristosdefault value of @var{num} is given by the value of the 2549*75f6d617Schristos@env{PATCH_GET} environment variable if it is set; if not, the default 2550*75f6d617Schristosvalue is zero if @command{patch} is conforming to @sc{posix}, negative 2551*75f6d617Schristosotherwise. @xref{patch and POSIX}. 2552*75f6d617Schristos 2553*75f6d617Schristos@vindex VERSION_CONTROL 2554*75f6d617SchristosThe choice of revision control system is unaffected by the 2555*75f6d617Schristos@env{VERSION_CONTROL} environment variable (@pxref{Backup Names}). 2556*75f6d617Schristos 2557*75f6d617Schristos@node Imperfect 2558*75f6d617Schristos@section Applying Imperfect Patches 2559*75f6d617Schristos@cindex imperfect patch application 2560*75f6d617Schristos 2561*75f6d617Schristos@command{patch} tries to skip any leading text in the patch file, 2562*75f6d617Schristosapply the diff, and then skip any trailing text. Thus you can feed a 2563*75f6d617Schristosmail message directly to @command{patch}, and it should work. If the 2564*75f6d617Schristosentire diff is indented by a constant amount of white space, 2565*75f6d617Schristos@command{patch} automatically ignores the indentation. If a context 2566*75f6d617Schristosdiff contains trailing carriage return on each line, @command{patch} 2567*75f6d617Schristosautomatically ignores the carriage return. If a context diff has been 2568*75f6d617Schristosencapsulated by prepending @w{@samp{- }} to lines beginning with @samp{-} 2569*75f6d617Schristosas per @uref{ftp://ftp.isi.edu/in-notes/rfc934.txt, Internet RFC 934}, 2570*75f6d617Schristos@command{patch} automatically unencapsulates the input. 2571*75f6d617Schristos 2572*75f6d617SchristosHowever, certain other types of imperfect input require user 2573*75f6d617Schristosintervention or testing. 2574*75f6d617Schristos 2575*75f6d617Schristos@menu 2576*75f6d617Schristos* Changed White Space:: When tabs and spaces don't match exactly. 2577*75f6d617Schristos* Reversed Patches:: Applying reversed patches correctly. 2578*75f6d617Schristos* Inexact:: Helping @command{patch} find close matches. 2579*75f6d617Schristos* Dry Runs:: Predicting what @command{patch} will do. 2580*75f6d617Schristos@end menu 2581*75f6d617Schristos 2582*75f6d617Schristos@node Changed White Space 2583*75f6d617Schristos@subsection Applying Patches with Changed White Space 2584*75f6d617Schristos@cindex white space in patches 2585*75f6d617Schristos 2586*75f6d617SchristosSometimes mailers, editors, or other programs change spaces into tabs, 2587*75f6d617Schristosor vice versa. If this happens to a patch file or an input file, the 2588*75f6d617Schristosfiles might look the same, but @command{patch} will not be able to match 2589*75f6d617Schristosthem properly. If this problem occurs, use the @option{-l} or 2590*75f6d617Schristos@option{--ignore-white-space} option, which makes @command{patch} compare 2591*75f6d617Schristosblank characters (i.e.@: spaces and tabs) loosely so that any nonempty 2592*75f6d617Schristossequence of blanks in the patch file matches any nonempty sequence of 2593*75f6d617Schristosblanks in the input files. Non-blank 2594*75f6d617Schristoscharacters must still match exactly. Each line of the context must 2595*75f6d617Schristosstill match a line in the input file. 2596*75f6d617Schristos 2597*75f6d617Schristos@node Reversed Patches 2598*75f6d617Schristos@subsection Applying Reversed Patches 2599*75f6d617Schristos@cindex reversed patches 2600*75f6d617Schristos 2601*75f6d617SchristosSometimes people run @command{diff} with the new file first instead of 2602*75f6d617Schristossecond. This creates a diff that is ``reversed''. To apply such 2603*75f6d617Schristospatches, give @command{patch} the @option{-R} or @option{--reverse} option. 2604*75f6d617Schristos@command{patch} then attempts to swap each hunk around before applying it. 2605*75f6d617SchristosRejects come out in the swapped format. 2606*75f6d617Schristos 2607*75f6d617SchristosOften @command{patch} can guess that the patch is reversed. If the first 2608*75f6d617Schristoshunk of a patch fails, @command{patch} reverses the hunk to see if it can 2609*75f6d617Schristosapply it that way. If it can, @command{patch} asks you if you want to have 2610*75f6d617Schristosthe @option{-R} option set; if it can't, @command{patch} continues to apply 2611*75f6d617Schristosthe patch normally. This method cannot detect a reversed patch if it is 2612*75f6d617Schristosa normal diff and the first command is an append (which should have been 2613*75f6d617Schristosa delete) since appends always succeed, because a null context matches 2614*75f6d617Schristosanywhere. But most patches add or change lines rather than delete them, 2615*75f6d617Schristosso most reversed normal diffs begin with a delete, which fails, and 2616*75f6d617Schristos@command{patch} notices. 2617*75f6d617Schristos 2618*75f6d617SchristosIf you apply a patch that you have already applied, @command{patch} thinks 2619*75f6d617Schristosit is a reversed patch and offers to un-apply the patch. This could be 2620*75f6d617Schristosconstrued as a feature. If you did this inadvertently and you don't 2621*75f6d617Schristoswant to un-apply the patch, just answer @samp{n} to this offer and to 2622*75f6d617Schristosthe subsequent ``apply anyway'' question---or type @kbd{C-c} to kill the 2623*75f6d617Schristos@command{patch} process. 2624*75f6d617Schristos 2625*75f6d617Schristos@node Inexact 2626*75f6d617Schristos@subsection Helping @command{patch} Find Inexact Matches 2627*75f6d617Schristos@cindex inexact patches 2628*75f6d617Schristos@cindex fuzz factor when patching 2629*75f6d617Schristos 2630*75f6d617SchristosFor context diffs, and to a lesser extent normal diffs, @command{patch} can 2631*75f6d617Schristosdetect when the line numbers mentioned in the patch are incorrect, and 2632*75f6d617Schristosit attempts to find the correct place to apply each hunk of the patch. 2633*75f6d617SchristosAs a first guess, it takes the line number mentioned in the hunk, plus 2634*75f6d617Schristosor minus any offset used in applying the previous hunk. If that is not 2635*75f6d617Schristosthe correct place, @command{patch} scans both forward and backward for a 2636*75f6d617Schristosset of lines matching the context given in the hunk. 2637*75f6d617Schristos 2638*75f6d617SchristosFirst @command{patch} looks for a place where all lines of the context 2639*75f6d617Schristosmatch. If it cannot find such a place, and it is reading a context or 2640*75f6d617Schristosunified diff, and the maximum fuzz factor is set to 1 or more, then 2641*75f6d617Schristos@command{patch} makes another scan, ignoring the first and last line of 2642*75f6d617Schristoscontext. If that fails, and the maximum fuzz factor is set to 2 or 2643*75f6d617Schristosmore, it makes another scan, ignoring the first two and last two lines 2644*75f6d617Schristosof context are ignored. It continues similarly if the maximum fuzz 2645*75f6d617Schristosfactor is larger. 2646*75f6d617Schristos 2647*75f6d617SchristosThe @option{-F @var{lines}} or @option{--fuzz=@var{lines}} option sets the 2648*75f6d617Schristosmaximum fuzz factor to @var{lines}. This option only applies to context 2649*75f6d617Schristosand unified diffs; it ignores up to @var{lines} lines while looking for 2650*75f6d617Schristosthe place to install a hunk. Note that a larger fuzz factor increases 2651*75f6d617Schristosthe odds of making a faulty patch. The default fuzz factor is 2; there 2652*75f6d617Schristosis no point to setting it to more than the number of lines of context 2653*75f6d617Schristosin the diff, ordinarily 3. 2654*75f6d617Schristos 2655*75f6d617SchristosIf @command{patch} cannot find a place to install a hunk of the patch, it 2656*75f6d617Schristoswrites the hunk out to a reject file (@pxref{Reject Names}, for information 2657*75f6d617Schristoson how reject files are named). It writes out rejected hunks in context 2658*75f6d617Schristosformat no matter what form the input patch is in. If the input is a 2659*75f6d617Schristosnormal or @command{ed} diff, many of the contexts are simply null. The 2660*75f6d617Schristosline numbers on the hunks in the reject file may be different from those 2661*75f6d617Schristosin the patch file: they show the approximate location where @command{patch} 2662*75f6d617Schristosthinks the failed hunks belong in the new file rather than in the old 2663*75f6d617Schristosone. 2664*75f6d617Schristos 2665*75f6d617SchristosIf the @option{--verbose} option is given, then 2666*75f6d617Schristosas it completes each hunk @command{patch} tells you whether the hunk 2667*75f6d617Schristossucceeded or failed, and if it failed, on which line (in the new file) 2668*75f6d617Schristos@command{patch} thinks the hunk should go. If this is different from the 2669*75f6d617Schristosline number specified in the diff, it tells you the offset. A single 2670*75f6d617Schristoslarge offset @emph{may} indicate that @command{patch} installed a hunk in 2671*75f6d617Schristosthe wrong place. @command{patch} also tells you if it used a fuzz factor 2672*75f6d617Schristosto make the match, in which case you should also be slightly suspicious. 2673*75f6d617Schristos 2674*75f6d617Schristos@command{patch} cannot tell if the line numbers are off in an @command{ed} 2675*75f6d617Schristosscript, and can only detect wrong line numbers in a normal diff when it 2676*75f6d617Schristosfinds a change or delete command. It may have the same problem with a 2677*75f6d617Schristoscontext diff using a fuzz factor equal to or greater than the number of 2678*75f6d617Schristoslines of context shown in the diff (typically 3). In these cases, you 2679*75f6d617Schristosshould probably look at a context diff between your original and patched 2680*75f6d617Schristosinput files to see if the changes make sense. Compiling without errors 2681*75f6d617Schristosis a pretty good indication that the patch worked, but not a guarantee. 2682*75f6d617Schristos 2683*75f6d617SchristosA patch against an empty file applies to a nonexistent file, and vice 2684*75f6d617Schristosversa. @xref{Creating and Removing}. 2685*75f6d617Schristos 2686*75f6d617Schristos@command{patch} usually produces the correct results, even when it must 2687*75f6d617Schristosmake many guesses. However, the results are guaranteed only when 2688*75f6d617Schristosthe patch is applied to an exact copy of the file that the patch was 2689*75f6d617Schristosgenerated from. 2690*75f6d617Schristos 2691*75f6d617Schristos@node Dry Runs 2692*75f6d617Schristos@subsection Predicting what @command{patch} will do 2693*75f6d617Schristos@cindex testing @command{patch} 2694*75f6d617Schristos@cindex dry runs for @command{patch} 2695*75f6d617Schristos 2696*75f6d617SchristosIt may not be obvious in advance what @command{patch} will do with a 2697*75f6d617Schristoscomplicated or poorly formatted patch. If you are concerned that the 2698*75f6d617Schristosinput might cause @command{patch} to modify the wrong files, you can 2699*75f6d617Schristosuse the @option{--dry-run} option, which causes @command{patch} to 2700*75f6d617Schristosprint the results of applying patches without actually changing any 2701*75f6d617Schristosfiles. You can then inspect the diagnostics generated by the dry run 2702*75f6d617Schristosto see whether @command{patch} will modify the files that you expect. 2703*75f6d617SchristosIf the patch does not do what you want, you can modify the patch (or 2704*75f6d617Schristosthe other options to @command{patch}) and try another dry run. Once 2705*75f6d617Schristosyou are satisfied with the proposed patch you can apply it by invoking 2706*75f6d617Schristos@command{patch} as before, but this time without the 2707*75f6d617Schristos@option{--dry-run} option. 2708*75f6d617Schristos 2709*75f6d617Schristos@node Creating and Removing 2710*75f6d617Schristos@section Creating and Removing Files 2711*75f6d617Schristos@cindex creating files 2712*75f6d617Schristos@cindex empty files, removing 2713*75f6d617Schristos@cindex removing empty files 2714*75f6d617Schristos 2715*75f6d617SchristosSometimes when comparing two directories, a file may exist in one 2716*75f6d617Schristosdirectory but not the other. If you give @command{diff} the 2717*75f6d617Schristos@option{-N} or @option{--new-file} option, or if you supply an old or 2718*75f6d617Schristosnew file that is named @file{/dev/null} or is empty and is dated the 2719*75f6d617SchristosEpoch (1970-01-01 00:00:00 UTC), @command{diff} outputs a patch that 2720*75f6d617Schristosadds or deletes the contents of this file. When given such a patch, 2721*75f6d617Schristos@command{patch} normally creates a new file or removes the old file. 2722*75f6d617SchristosHowever, when conforming to @sc{posix} (@pxref{patch and POSIX}), 2723*75f6d617Schristos@command{patch} does not remove the old file, but leaves it empty. 2724*75f6d617SchristosThe @option{-E} or @option{--remove-empty-files} option causes 2725*75f6d617Schristos@command{patch} to remove output files that are empty after applying a 2726*75f6d617Schristospatch, even if the patch does not appear to be one that removed the 2727*75f6d617Schristosfile. 2728*75f6d617Schristos 2729*75f6d617SchristosIf the patch appears to create a file that already exists, 2730*75f6d617Schristos@command{patch} asks for confirmation before applying the patch. 2731*75f6d617Schristos 2732*75f6d617Schristos@node Patching Time Stamps 2733*75f6d617Schristos@section Updating Time Stamps on Patched Files 2734*75f6d617Schristos@cindex time stamps on patched files 2735*75f6d617Schristos 2736*75f6d617SchristosWhen @command{patch} updates a file, it normally sets the file's 2737*75f6d617Schristoslast-modified time stamp to the current time of day. If you are using 2738*75f6d617Schristos@command{patch} to track a software distribution, this can cause 2739*75f6d617Schristos@command{make} to incorrectly conclude that a patched file is out of 2740*75f6d617Schristosdate. For example, if @file{syntax.c} depends on @file{syntax.y}, and 2741*75f6d617Schristos@command{patch} updates @file{syntax.c} and then @file{syntax.y}, then 2742*75f6d617Schristos@file{syntax.c} will normally appear to be out of date with respect to 2743*75f6d617Schristos@file{syntax.y} even though its contents are actually up to date. 2744*75f6d617Schristos 2745*75f6d617SchristosThe @option{-Z} or @option{--set-utc} option causes @command{patch} to 2746*75f6d617Schristosset a patched file's modification and access times to the time stamps 2747*75f6d617Schristosgiven in context diff headers. If the context diff headers do not 2748*75f6d617Schristosspecify a time zone, they are assumed to use Coordinated Universal 2749*75f6d617SchristosTime (@sc{utc}, often known as @sc{gmt}). 2750*75f6d617Schristos 2751*75f6d617SchristosThe @option{-T} or @option{--set-time} option acts like @option{-Z} or 2752*75f6d617Schristos@option{--set-utc}, except that it assumes that the context diff 2753*75f6d617Schristosheaders' time stamps use local time instead of @sc{utc}. This option 2754*75f6d617Schristosis not recommended, because patches using local time cannot easily be 2755*75f6d617Schristosused by people in other time zones, and because local time stamps are 2756*75f6d617Schristosambiguous when local clocks move backwards during daylight-saving time 2757*75f6d617Schristosadjustments. If the context diff headers specify a time zone, this 2758*75f6d617Schristosoption is equivalent to @option{-Z} or @option{--set-utc}. 2759*75f6d617Schristos 2760*75f6d617Schristos@command{patch} normally refrains from setting a file's time stamps if 2761*75f6d617Schristosthe file's original last-modified time stamp does not match the time 2762*75f6d617Schristosgiven in the diff header, of if the file's contents do not exactly 2763*75f6d617Schristosmatch the patch. However, if the @option{-f} or @option{--force} 2764*75f6d617Schristosoption is given, the file's time stamps are set regardless. 2765*75f6d617Schristos 2766*75f6d617SchristosDue to the limitations of the current @command{diff} format, 2767*75f6d617Schristos@command{patch} cannot update the times of files whose contents have 2768*75f6d617Schristosnot changed. Also, if you set file time stamps to values other than 2769*75f6d617Schristosthe current time of day, you should also remove (e.g., with @samp{make 2770*75f6d617Schristosclean}) all files that depend on the patched files, so that later 2771*75f6d617Schristosinvocations of @command{make} do not get confused by the patched 2772*75f6d617Schristosfiles' times. 2773*75f6d617Schristos 2774*75f6d617Schristos@node Multiple Patches 2775*75f6d617Schristos@section Multiple Patches in a File 2776*75f6d617Schristos@cindex multiple patches 2777*75f6d617Schristos@cindex intuiting file names from patches 2778*75f6d617Schristos 2779*75f6d617SchristosIf the patch file contains more than one patch, and if you do not 2780*75f6d617Schristosspecify an input file on the command line, @command{patch} tries to 2781*75f6d617Schristosapply each patch as if they came from separate patch files. This 2782*75f6d617Schristosmeans that it determines the name of the file to patch for each patch, 2783*75f6d617Schristosand that it examines the leading text before each patch for file names 2784*75f6d617Schristosand prerequisite revision level (@pxref{Making Patches}, for more on 2785*75f6d617Schristosthat topic). 2786*75f6d617Schristos 2787*75f6d617Schristos@command{patch} uses the following rules to intuit a file name from 2788*75f6d617Schristosthe leading text before a patch. First, @command{patch} takes an 2789*75f6d617Schristosordered list of candidate file names as follows: 2790*75f6d617Schristos 2791*75f6d617Schristos@itemize @bullet 2792*75f6d617Schristos@item 2793*75f6d617SchristosIf the header is that of a context diff, @command{patch} takes the old 2794*75f6d617Schristosand new file names in the header. A name is ignored if it does not 2795*75f6d617Schristoshave enough slashes to satisfy the @option{-p@var{num}} or 2796*75f6d617Schristos@option{--strip=@var{num}} option. The name @file{/dev/null} is also 2797*75f6d617Schristosignored. 2798*75f6d617Schristos 2799*75f6d617Schristos@item 2800*75f6d617SchristosIf there is an @samp{Index:} line in the leading garbage and if either 2801*75f6d617Schristosthe old and new names are both absent or if @command{patch} is 2802*75f6d617Schristosconforming to @sc{posix}, @command{patch} takes the name in the 2803*75f6d617Schristos@samp{Index:} line. 2804*75f6d617Schristos 2805*75f6d617Schristos@item 2806*75f6d617SchristosFor the purpose of the following rules, the candidate file names are 2807*75f6d617Schristosconsidered to be in the order (old, new, index), regardless of the 2808*75f6d617Schristosorder that they appear in the header. 2809*75f6d617Schristos@end itemize 2810*75f6d617Schristos 2811*75f6d617Schristos@noindent 2812*75f6d617SchristosThen @command{patch} selects a file name from the candidate list as 2813*75f6d617Schristosfollows: 2814*75f6d617Schristos 2815*75f6d617Schristos@itemize @bullet 2816*75f6d617Schristos@item 2817*75f6d617SchristosIf some of the named files exist, @command{patch} selects the first 2818*75f6d617Schristosname if conforming to @sc{posix}, and the best name otherwise. 2819*75f6d617Schristos 2820*75f6d617Schristos@item 2821*75f6d617SchristosIf @command{patch} is not ignoring @sc{rcs}, ClearCase, and @sc{sccs} 2822*75f6d617Schristos(@pxref{Revision Control}), and no named files exist but an @sc{rcs}, 2823*75f6d617SchristosClearCase, or @sc{sccs} master is found, @command{patch} selects the 2824*75f6d617Schristosfirst named file with an @sc{rcs}, ClearCase, or @sc{sccs} master. 2825*75f6d617Schristos 2826*75f6d617Schristos@item 2827*75f6d617SchristosIf no named files exist, no @sc{rcs}, ClearCase, or @sc{sccs} master 2828*75f6d617Schristoswas found, some names are given, @command{patch} is not conforming to 2829*75f6d617Schristos@sc{posix}, and the patch appears to create a file, @command{patch} 2830*75f6d617Schristosselects the best name requiring the creation of the fewest 2831*75f6d617Schristosdirectories. 2832*75f6d617Schristos 2833*75f6d617Schristos@item 2834*75f6d617SchristosIf no file name results from the above heuristics, you are asked for 2835*75f6d617Schristosthe name of the file to patch, and @command{patch} selects that name. 2836*75f6d617Schristos@end itemize 2837*75f6d617Schristos 2838*75f6d617SchristosTo determine the @dfn{best} of a nonempty list of file names, 2839*75f6d617Schristos@command{patch} first takes all the names with the fewest path name 2840*75f6d617Schristoscomponents; of those, it then takes all the names with the shortest 2841*75f6d617Schristosbasename; of those, it then takes all the shortest names; finally, it 2842*75f6d617Schristostakes the first remaining name. 2843*75f6d617Schristos 2844*75f6d617Schristos@xref{patch and POSIX}, to see whether @command{patch} is conforming 2845*75f6d617Schristosto @sc{posix}. 2846*75f6d617Schristos 2847*75f6d617Schristos@node patch Directories 2848*75f6d617Schristos@section Applying Patches in Other Directories 2849*75f6d617Schristos@cindex directories and patch 2850*75f6d617Schristos@cindex patching directories 2851*75f6d617Schristos 2852*75f6d617SchristosThe @option{-d @var{directory}} or @option{--directory=@var{directory}} 2853*75f6d617Schristosoption to @command{patch} makes directory @var{directory} the current 2854*75f6d617Schristosdirectory for interpreting both file names in the patch file, and file 2855*75f6d617Schristosnames given as arguments to other options (such as @option{-B} and 2856*75f6d617Schristos@option{-o}). For example, while in a mail reading program, you can patch 2857*75f6d617Schristosa file in the @file{/usr/src/emacs} directory directly from a message 2858*75f6d617Schristoscontaining the patch like this: 2859*75f6d617Schristos 2860*75f6d617Schristos@example 2861*75f6d617Schristos| patch -d /usr/src/emacs 2862*75f6d617Schristos@end example 2863*75f6d617Schristos 2864*75f6d617SchristosSometimes the file names given in a patch contain leading directories, 2865*75f6d617Schristosbut you keep your files in a directory different from the one given in 2866*75f6d617Schristosthe patch. In those cases, you can use the 2867*75f6d617Schristos@option{-p@var{number}} or @option{--strip=@var{number}} 2868*75f6d617Schristosoption to set the file name strip count to @var{number}. The strip 2869*75f6d617Schristoscount tells @command{patch} how many slashes, along with the directory 2870*75f6d617Schristosnames between them, to strip from the front of file names. A sequence 2871*75f6d617Schristosof one or more adjacent slashes is counted as a single slash. By 2872*75f6d617Schristosdefault, @command{patch} strips off all leading directories, leaving 2873*75f6d617Schristosjust the base file names. 2874*75f6d617Schristos 2875*75f6d617SchristosFor example, suppose the file name in the patch file is 2876*75f6d617Schristos@file{/gnu/src/emacs/etc/NEWS}. Using @option{-p0} gives the 2877*75f6d617Schristosentire file name unmodified, @option{-p1} gives 2878*75f6d617Schristos@file{gnu/src/emacs/etc/NEWS} (no leading slash), @option{-p4} gives 2879*75f6d617Schristos@file{etc/NEWS}, and not specifying @option{-p} at all gives @file{NEWS}. 2880*75f6d617Schristos 2881*75f6d617Schristos@command{patch} looks for each file (after any slashes have been stripped) 2882*75f6d617Schristosin the current directory, or if you used the @option{-d @var{directory}} 2883*75f6d617Schristosoption, in that directory. 2884*75f6d617Schristos 2885*75f6d617Schristos@node Backups 2886*75f6d617Schristos@section Backup Files 2887*75f6d617Schristos@cindex backup file strategy 2888*75f6d617Schristos 2889*75f6d617SchristosNormally, @command{patch} creates a backup file if the patch does not 2890*75f6d617Schristosexactly match the original input file, because in that case the 2891*75f6d617Schristosoriginal data might not be recovered if you undo the patch with 2892*75f6d617Schristos@samp{patch -R} (@pxref{Reversed Patches}). However, when conforming 2893*75f6d617Schristosto @sc{posix}, @command{patch} does not create backup files by 2894*75f6d617Schristosdefault. @xref{patch and POSIX}. 2895*75f6d617Schristos 2896*75f6d617SchristosThe @option{-b} or @option{--backup} option causes @command{patch} to 2897*75f6d617Schristosmake a backup file regardless of whether the patch matches the 2898*75f6d617Schristosoriginal input. The @option{--backup-if-mismatch} option causes 2899*75f6d617Schristos@command{patch} to create backup files for mismatches files; this is 2900*75f6d617Schristosthe default when not conforming to @sc{posix}. The 2901*75f6d617Schristos@option{--no-backup-if-mismatch} option causes @command{patch} to not 2902*75f6d617Schristoscreate backup files, even for mismatched patches; this is the default 2903*75f6d617Schristoswhen conforming to @sc{posix}. 2904*75f6d617Schristos 2905*75f6d617SchristosWhen backing up a file that does not exist, an empty, unreadable 2906*75f6d617Schristosbackup file is created as a placeholder to represent the nonexistent 2907*75f6d617Schristosfile. 2908*75f6d617Schristos 2909*75f6d617Schristos@node Backup Names 2910*75f6d617Schristos@section Backup File Names 2911*75f6d617Schristos@cindex backup file names 2912*75f6d617Schristos 2913*75f6d617SchristosNormally, @command{patch} renames an original input file into a backup 2914*75f6d617Schristosfile by appending to its name the extension @samp{.orig}, or @samp{~} 2915*75f6d617Schristosif using @samp{.orig} would make the backup file name too 2916*75f6d617Schristoslong.@footnote{A coding error in @sc{gnu} @command{patch} version 2917*75f6d617Schristos2.5.4 causes it to always use @samp{~}, but this should be fixed in 2918*75f6d617Schristosthe next release.} The @option{-z @var{backup-suffix}} or 2919*75f6d617Schristos@option{--suffix=@var{backup-suffix}} option causes @command{patch} to 2920*75f6d617Schristosuse @var{backup-suffix} as the backup extension instead. 2921*75f6d617Schristos 2922*75f6d617Schristos@vindex SIMPLE_BACKUP_SUFFIX 2923*75f6d617SchristosAlternately, you can specify the extension for backup files with the 2924*75f6d617Schristos@env{SIMPLE_BACKUP_SUFFIX} environment variable, which the options 2925*75f6d617Schristosoverride. 2926*75f6d617Schristos 2927*75f6d617Schristos@command{patch} can also create numbered backup files the way @sc{gnu} Emacs 2928*75f6d617Schristosdoes. With this method, instead of having a single backup of each file, 2929*75f6d617Schristos@command{patch} makes a new backup file name each time it patches a file. 2930*75f6d617SchristosFor example, the backups of a file named @file{sink} would be called, 2931*75f6d617Schristossuccessively, @file{sink.~1~}, @file{sink.~2~}, @file{sink.~3~}, etc. 2932*75f6d617Schristos 2933*75f6d617Schristos@vindex PATCH_VERSION_CONTROL 2934*75f6d617Schristos@vindex VERSION_CONTROL 2935*75f6d617SchristosThe @option{-V @var{backup-style}} or 2936*75f6d617Schristos@option{--version-control=@var{backup-style}} option takes as an 2937*75f6d617Schristosargument a method for creating backup file names. You can alternately 2938*75f6d617Schristoscontrol the type of backups that @command{patch} makes with the 2939*75f6d617Schristos@env{PATCH_VERSION_CONTROL} environment variable, which the 2940*75f6d617Schristos@option{-V} option overrides. If @env{PATCH_VERSION_CONTROL} is not 2941*75f6d617Schristosset, the @env{VERSION_CONTROL} environment variable is used instead. 2942*75f6d617SchristosPlease note that these options and variables control backup file 2943*75f6d617Schristosnames; they do not affect the choice of revision control system 2944*75f6d617Schristos(@pxref{Revision Control}). 2945*75f6d617Schristos 2946*75f6d617SchristosThe values of these environment variables and the argument to the 2947*75f6d617Schristos@option{-V} option are like the @sc{gnu} Emacs @code{version-control} 2948*75f6d617Schristosvariable (@pxref{Backup Names, , , emacs, The @sc{gnu} Emacs Manual}, 2949*75f6d617Schristosfor more information on backup versions in Emacs). They also 2950*75f6d617Schristosrecognize synonyms that are more descriptive. The valid values are 2951*75f6d617Schristoslisted below; unique abbreviations are acceptable. 2952*75f6d617Schristos 2953*75f6d617Schristos@table @option 2954*75f6d617Schristos@item t 2955*75f6d617Schristos@itemx numbered 2956*75f6d617SchristosAlways make numbered backups. 2957*75f6d617Schristos 2958*75f6d617Schristos@item nil 2959*75f6d617Schristos@itemx existing 2960*75f6d617SchristosMake numbered backups of files that already have them, simple backups of 2961*75f6d617Schristosthe others. This is the default. 2962*75f6d617Schristos 2963*75f6d617Schristos@item never 2964*75f6d617Schristos@itemx simple 2965*75f6d617SchristosAlways make simple backups. 2966*75f6d617Schristos@end table 2967*75f6d617Schristos 2968*75f6d617SchristosYou can also tell @command{patch} to prepend a prefix, such as a 2969*75f6d617Schristosdirectory name, to produce backup file names. The @option{-B 2970*75f6d617Schristos@var{prefix}} or @option{--prefix=@var{prefix}} option makes backup 2971*75f6d617Schristosfiles by prepending @var{prefix} to them. The @option{-Y 2972*75f6d617Schristos@var{prefix}} or @option{--basename-prefix=@var{prefix}} prepends 2973*75f6d617Schristos@var{prefix} to the last file name component of backup file names 2974*75f6d617Schristosinstead; for example, @option{-Y ~} causes the backup name for 2975*75f6d617Schristos@file{dir/file.c} to be @file{dir/~file.c}. If you use either of 2976*75f6d617Schristosthese prefix options, the suffix-based options are ignored. 2977*75f6d617Schristos 2978*75f6d617SchristosIf you specify the output file with the @option{-o} option, that file is 2979*75f6d617Schristosthe one that is backed up, not the input file. 2980*75f6d617Schristos 2981*75f6d617SchristosOptions that affect the names of backup files do not affect whether 2982*75f6d617Schristosbackups are made. For example, if you specify the 2983*75f6d617Schristos@option{--no-backup-if-mismatch} option, none of the options described 2984*75f6d617Schristosin this section have any affect, because no backups are made. 2985*75f6d617Schristos 2986*75f6d617Schristos@node Reject Names 2987*75f6d617Schristos@section Reject File Names 2988*75f6d617Schristos@cindex reject file names 2989*75f6d617Schristos 2990*75f6d617SchristosThe names for reject files (files containing patches that 2991*75f6d617Schristos@command{patch} could not find a place to apply) are normally the name 2992*75f6d617Schristosof the output file with @samp{.rej} appended (or @samp{#} if if using 2993*75f6d617Schristos@samp{.rej} would make the backup file name too long). 2994*75f6d617Schristos 2995*75f6d617SchristosAlternatively, you can tell @command{patch} to place all of the rejected 2996*75f6d617Schristospatches in a single file. The @option{-r @var{reject-file}} or 2997*75f6d617Schristos@option{--reject-file=@var{reject-file}} option uses @var{reject-file} as 2998*75f6d617Schristosthe reject file name. 2999*75f6d617Schristos 3000*75f6d617Schristos@node patch Messages 3001*75f6d617Schristos@section Messages and Questions from @command{patch} 3002*75f6d617Schristos@cindex @command{patch} messages and questions 3003*75f6d617Schristos@cindex diagnostics from @command{patch} 3004*75f6d617Schristos@cindex messages from @command{patch} 3005*75f6d617Schristos 3006*75f6d617Schristos@command{patch} can produce a variety of messages, especially if it 3007*75f6d617Schristoshas trouble decoding its input. In a few situations where it's not 3008*75f6d617Schristossure how to proceed, @command{patch} normally prompts you for more 3009*75f6d617Schristosinformation from the keyboard. There are options to produce more or 3010*75f6d617Schristosfewer messages, to have it not ask for keyboard input, and to 3011*75f6d617Schristosaffect the way that file names are quoted in messages. 3012*75f6d617Schristos 3013*75f6d617Schristos@menu 3014*75f6d617Schristos* More or Fewer Messages:: Controlling the verbosity of @command{patch}. 3015*75f6d617Schristos* patch and Keyboard Input:: Inhibiting keyboard input. 3016*75f6d617Schristos* patch Quoting Style:: Quoting file names in diagnostics. 3017*75f6d617Schristos@end menu 3018*75f6d617Schristos 3019*75f6d617Schristos@command{patch} exits with status 0 if all hunks are applied successfully, 3020*75f6d617Schristos1 if some hunks cannot be applied, and 2 if there is more serious trouble. 3021*75f6d617SchristosWhen applying a set of patches in a loop, you should check the 3022*75f6d617Schristosexit status, so you don't apply a later patch to a partially patched 3023*75f6d617Schristosfile. 3024*75f6d617Schristos 3025*75f6d617Schristos@node More or Fewer Messages 3026*75f6d617Schristos@subsection Controlling the Verbosity of @command{patch} 3027*75f6d617Schristos@cindex verbose messages from @command{patch} 3028*75f6d617Schristos@cindex inhibit messages from @command{patch} 3029*75f6d617Schristos 3030*75f6d617SchristosYou can cause @command{patch} to produce more messages by using the 3031*75f6d617Schristos@option{--verbose} option. For example, when you give this option, 3032*75f6d617Schristosthe message @samp{Hmm...} indicates that @command{patch} is reading text in 3033*75f6d617Schristosthe patch file, attempting to determine whether there is a patch in that 3034*75f6d617Schristostext, and if so, what kind of patch it is. 3035*75f6d617Schristos 3036*75f6d617SchristosYou can inhibit all terminal output from @command{patch}, unless an error 3037*75f6d617Schristosoccurs, by using the @option{-s}, @option{--quiet}, or @option{--silent} 3038*75f6d617Schristosoption. 3039*75f6d617Schristos 3040*75f6d617Schristos@node patch and Keyboard Input 3041*75f6d617Schristos@subsection Inhibiting Keyboard Input 3042*75f6d617Schristos@cindex keyboard input to @command{patch} 3043*75f6d617Schristos 3044*75f6d617SchristosThere are two ways you can prevent @command{patch} from asking you any 3045*75f6d617Schristosquestions. The @option{-f} or @option{--force} option assumes that you know 3046*75f6d617Schristoswhat you are doing. It causes @command{patch} to do the following: 3047*75f6d617Schristos 3048*75f6d617Schristos@itemize @bullet 3049*75f6d617Schristos@item 3050*75f6d617SchristosSkip patches that do not contain file names in their headers. 3051*75f6d617Schristos 3052*75f6d617Schristos@item 3053*75f6d617SchristosPatch files even though they have the wrong version for the 3054*75f6d617Schristos@samp{Prereq:} line in the patch; 3055*75f6d617Schristos 3056*75f6d617Schristos@item 3057*75f6d617SchristosAssume that patches are not reversed even if they look like they are. 3058*75f6d617Schristos@end itemize 3059*75f6d617Schristos 3060*75f6d617Schristos@noindent 3061*75f6d617SchristosThe @option{-t} or @option{--batch} option is similar to @option{-f}, in that 3062*75f6d617Schristosit suppresses questions, but it makes somewhat different assumptions: 3063*75f6d617Schristos 3064*75f6d617Schristos@itemize @bullet 3065*75f6d617Schristos@item 3066*75f6d617SchristosSkip patches that do not contain file names in their headers 3067*75f6d617Schristos(the same as @option{-f}). 3068*75f6d617Schristos 3069*75f6d617Schristos@item 3070*75f6d617SchristosSkip patches for which the file has the wrong version for the 3071*75f6d617Schristos@samp{Prereq:} line in the patch; 3072*75f6d617Schristos 3073*75f6d617Schristos@item 3074*75f6d617SchristosAssume that patches are reversed if they look like they are. 3075*75f6d617Schristos@end itemize 3076*75f6d617Schristos 3077*75f6d617Schristos@node patch Quoting Style 3078*75f6d617Schristos@subsection @command{patch} Quoting Style 3079*75f6d617Schristos@cindex quoting style 3080*75f6d617Schristos 3081*75f6d617SchristosWhen @command{patch} outputs a file name in a diagnostic message, it 3082*75f6d617Schristoscan format the name in any of several ways. This can be useful to 3083*75f6d617Schristosoutput file names unambiguously, even if they contain punctuation or 3084*75f6d617Schristosspecial characters like newlines. The 3085*75f6d617Schristos@option{--quoting-style=@var{word}} option controls how names are 3086*75f6d617Schristosoutput. The @var{word} should be one of the following: 3087*75f6d617Schristos 3088*75f6d617Schristos@table @samp 3089*75f6d617Schristos@item literal 3090*75f6d617SchristosOutput names as-is. 3091*75f6d617Schristos@item shell 3092*75f6d617SchristosQuote names for the shell if they contain shell metacharacters or would 3093*75f6d617Schristoscause ambiguous output. 3094*75f6d617Schristos@item shell-always 3095*75f6d617SchristosQuote names for the shell, even if they would normally not require quoting. 3096*75f6d617Schristos@item c 3097*75f6d617SchristosQuote names as for a C language string. 3098*75f6d617Schristos@item escape 3099*75f6d617SchristosQuote as with @samp{c} except omit the surrounding double-quote 3100*75f6d617Schristoscharacters. 3101*75f6d617Schristos@c The following are not yet implemented in patch 2.5.4. 3102*75f6d617Schristos@c @item clocale 3103*75f6d617Schristos@c Quote as with @samp{c} except use quotation marks appropriate for the 3104*75f6d617Schristos@c locale. 3105*75f6d617Schristos@c @item locale 3106*75f6d617Schristos@c @c Use @t instead of @samp to avoid duplicate quoting in some output styles. 3107*75f6d617Schristos@c Like @samp{clocale}, but quote @t{`like this'} instead of @t{"like 3108*75f6d617Schristos@c this"} in the default C locale. This looks nicer on many displays. 3109*75f6d617Schristos@end table 3110*75f6d617Schristos 3111*75f6d617Schristos@vindex QUOTING_STYLE 3112*75f6d617SchristosYou can specify the default value of the @option{--quoting-style} 3113*75f6d617Schristosoption with the environment variable @env{QUOTING_STYLE}. If that 3114*75f6d617Schristosenvironment variable is not set, the default value is @samp{shell}, 3115*75f6d617Schristosbut this default may change in a future version of @command{patch}. 3116*75f6d617Schristos 3117*75f6d617Schristos@node patch and POSIX 3118*75f6d617Schristos@section @command{patch} and the @sc{posix} Standard 3119*75f6d617Schristos@cindex @sc{posix} 3120*75f6d617Schristos 3121*75f6d617Schristos@vindex POSIXLY_CORRECT 3122*75f6d617SchristosIf you specify the @option{--posix} option, or set the 3123*75f6d617Schristos@env{POSIXLY_CORRECT} environment variable, @command{patch} conforms 3124*75f6d617Schristosmore strictly to the @sc{posix} standard, as follows: 3125*75f6d617Schristos 3126*75f6d617Schristos@itemize @bullet 3127*75f6d617Schristos@item 3128*75f6d617SchristosTake the first existing file from the list (old, new, index) 3129*75f6d617Schristoswhen intuiting file names from diff headers. @xref{Multiple Patches}. 3130*75f6d617Schristos 3131*75f6d617Schristos@item 3132*75f6d617SchristosDo not remove files that are removed by a diff. 3133*75f6d617Schristos@xref{Creating and Removing}. 3134*75f6d617Schristos 3135*75f6d617Schristos@item 3136*75f6d617SchristosDo not ask whether to get files from @sc{rcs}, ClearCase, or 3137*75f6d617Schristos@sc{sccs}. @xref{Revision Control}. 3138*75f6d617Schristos 3139*75f6d617Schristos@item 3140*75f6d617SchristosRequire that all options precede the files in the command line. 3141*75f6d617Schristos 3142*75f6d617Schristos@item 3143*75f6d617SchristosDo not backup files, even when there is a mismatch. @xref{Backups}. 3144*75f6d617Schristos 3145*75f6d617Schristos@end itemize 3146*75f6d617Schristos 3147*75f6d617Schristos@node patch and Tradition 3148*75f6d617Schristos@section @sc{gnu} @command{patch} and Traditional @command{patch} 3149*75f6d617Schristos@cindex traditional @command{patch} 3150*75f6d617Schristos 3151*75f6d617SchristosThe current version of @sc{gnu} @command{patch} normally follows the 3152*75f6d617Schristos@sc{posix} standard. @xref{patch and POSIX}, for the few exceptions 3153*75f6d617Schristosto this general rule. 3154*75f6d617Schristos 3155*75f6d617SchristosUnfortunately, @sc{posix} redefined the behavior of @command{patch} in 3156*75f6d617Schristosseveral important ways. You should be aware of the following 3157*75f6d617Schristosdifferences if you must interoperate with traditional @command{patch}, 3158*75f6d617Schristosor with @sc{gnu} @command{patch} version 2.1 and earlier. 3159*75f6d617Schristos 3160*75f6d617Schristos@itemize @bullet 3161*75f6d617Schristos@item 3162*75f6d617SchristosIn traditional @command{patch}, the @option{-p} option's operand was 3163*75f6d617Schristosoptional, and a bare @option{-p} was equivalent to @option{-p0}. The 3164*75f6d617Schristos@option{-p} option now requires an operand, and @option{-p@ 0} is now 3165*75f6d617Schristosequivalent to @option{-p0}. For maximum compatibility, use options 3166*75f6d617Schristoslike @option{-p0} and @option{-p1}. 3167*75f6d617Schristos 3168*75f6d617SchristosAlso, traditional @command{patch} simply counted slashes when 3169*75f6d617Schristosstripping path prefixes; @command{patch} now counts pathname 3170*75f6d617Schristoscomponents. That is, a sequence of one or more adjacent slashes now 3171*75f6d617Schristoscounts as a single slash. For maximum portability, avoid sending 3172*75f6d617Schristospatches containing @file{//} in file names. 3173*75f6d617Schristos 3174*75f6d617Schristos@item 3175*75f6d617SchristosIn traditional @command{patch}, backups were enabled by default. This 3176*75f6d617Schristosbehavior is now enabled with the @option{-b} or @option{--backup} 3177*75f6d617Schristosoption. 3178*75f6d617Schristos 3179*75f6d617SchristosConversely, in @sc{posix} @command{patch}, backups are never made, 3180*75f6d617Schristoseven when there is a mismatch. In @sc{gnu} @command{patch}, this 3181*75f6d617Schristosbehavior is enabled with the @option{--no-backup-if-mismatch} option, 3182*75f6d617Schristosor by conforming to @sc{posix}. 3183*75f6d617Schristos 3184*75f6d617SchristosThe @option{-b@ @var{suffix}} option of traditional @command{patch} is 3185*75f6d617Schristosequivalent to the @samp{-b -z@ @var{suffix}} options of @sc{gnu} 3186*75f6d617Schristos@command{patch}. 3187*75f6d617Schristos 3188*75f6d617Schristos@item 3189*75f6d617SchristosTraditional @command{patch} used a complicated (and incompletely 3190*75f6d617Schristosdocumented) method to intuit the name of the file to be patched from 3191*75f6d617Schristosthe patch header. This method did not conform to @sc{posix}, and had 3192*75f6d617Schristosa few gotchas. Now @command{patch} uses a different, equally 3193*75f6d617Schristoscomplicated (but better documented) method that is optionally 3194*75f6d617Schristos@sc{posix}-conforming; we hope it has fewer gotchas. The two methods 3195*75f6d617Schristosare compatible if the file names in the context diff header and the 3196*75f6d617Schristos@samp{Index:} line are all identical after prefix-stripping. Your 3197*75f6d617Schristospatch is normally compatible if each header's file names all contain 3198*75f6d617Schristosthe same number of slashes. 3199*75f6d617Schristos 3200*75f6d617Schristos@item 3201*75f6d617SchristosWhen traditional @command{patch} asked the user a question, it sent 3202*75f6d617Schristosthe question to standard error and looked for an answer from the first 3203*75f6d617Schristosfile in the following list that was a terminal: standard error, 3204*75f6d617Schristosstandard output, @file{/dev/tty}, and standard input. Now 3205*75f6d617Schristos@command{patch} sends questions to standard output and gets answers 3206*75f6d617Schristosfrom @file{/dev/tty}. Defaults for some answers have been changed so 3207*75f6d617Schristosthat @command{patch} never goes into an infinite loop when using 3208*75f6d617Schristosdefault answers. 3209*75f6d617Schristos 3210*75f6d617Schristos@item 3211*75f6d617SchristosTraditional @command{patch} exited with a status value that counted 3212*75f6d617Schristosthe number of bad hunks, or with status 1 if there was real trouble. 3213*75f6d617SchristosNow @command{patch} exits with status 1 if some hunks failed, or with 3214*75f6d617Schristos2 if there was real trouble. 3215*75f6d617Schristos 3216*75f6d617Schristos@item 3217*75f6d617SchristosLimit yourself to the following options when sending instructions 3218*75f6d617Schristosmeant to be executed by anyone running @sc{gnu} @command{patch}, 3219*75f6d617Schristostraditional @command{patch}, or a @command{patch} that conforms to 3220*75f6d617Schristos@sc{posix}. Spaces are significant in the following list, and 3221*75f6d617Schristosoperands are required. 3222*75f6d617Schristos 3223*75f6d617Schristos@example 3224*75f6d617Schristos@option{-c} 3225*75f6d617Schristos@option{-d @var{dir}} 3226*75f6d617Schristos@option{-D @var{define}} 3227*75f6d617Schristos@option{-e} 3228*75f6d617Schristos@option{-l} 3229*75f6d617Schristos@option{-n} 3230*75f6d617Schristos@option{-N} 3231*75f6d617Schristos@option{-o @var{outfile}} 3232*75f6d617Schristos@option{-p@var{num}} 3233*75f6d617Schristos@option{-R} 3234*75f6d617Schristos@option{-r @var{rejectfile}} 3235*75f6d617Schristos@end example 3236*75f6d617Schristos 3237*75f6d617Schristos@end itemize 3238*75f6d617Schristos 3239*75f6d617Schristos@node Making Patches 3240*75f6d617Schristos@chapter Tips for Making and Using Patches 3241*75f6d617Schristos 3242*75f6d617SchristosUse some common sense when making and using patches. For example, 3243*75f6d617Schristoswhen sending bug fixes to a program's maintainer, send several small 3244*75f6d617Schristospatches, one per independent subject, instead of one large, 3245*75f6d617Schristosharder-to-digest patch that covers all the subjects. 3246*75f6d617Schristos 3247*75f6d617SchristosHere are some other things you should keep in mind if you are going to 3248*75f6d617Schristosdistribute patches for updating a software package. 3249*75f6d617Schristos 3250*75f6d617Schristos@menu 3251*75f6d617Schristos* Tips for Patch Producers:: Advice for making patches. 3252*75f6d617Schristos* Tips for Patch Consumers:: Advice for using patches. 3253*75f6d617Schristos* Avoiding Common Mistakes:: Avoiding common mistakes when using @command{patch}. 3254*75f6d617Schristos* Generating Smaller Patches:: How to generate smaller patches. 3255*75f6d617Schristos@end menu 3256*75f6d617Schristos 3257*75f6d617Schristos@node Tips for Patch Producers 3258*75f6d617Schristos@section Tips for Patch Producers 3259*75f6d617Schristos@cindex patch producer tips 3260*75f6d617Schristos 3261*75f6d617SchristosTo create a patch that changes an older version of a package into a 3262*75f6d617Schristosnewer version, first make a copy of the older and newer versions in 3263*75f6d617Schristosadjacent subdirectories. It is common to do that by unpacking 3264*75f6d617Schristos@command{tar} archives of the two versions. 3265*75f6d617Schristos 3266*75f6d617SchristosTo generate the patch, use the command @samp{diff -Naur @var{old} 3267*75f6d617Schristos@var{new}} where @var{old} and @var{new} identify the old and new 3268*75f6d617Schristosdirectories. The names @var{old} and @var{new} should not contain any 3269*75f6d617Schristosslashes. The @option{-N} option lets the patch create and remove 3270*75f6d617Schristosfiles; @option{-a} lets the patch update non-text files; @option{-u} 3271*75f6d617Schristosgenerates useful time stamps and enough context; and @option{-r} lets 3272*75f6d617Schristosthe patch update subdirectories. Here is an example command, using 3273*75f6d617SchristosBourne shell syntax: 3274*75f6d617Schristos 3275*75f6d617Schristos@example 3276*75f6d617Schristosdiff -Naur gcc-3.0.3 gcc-3.0.4 3277*75f6d617Schristos@end example 3278*75f6d617Schristos 3279*75f6d617SchristosTell your recipients how to apply the patches. This should include 3280*75f6d617Schristoswhich working directory to use, and which @command{patch} options to 3281*75f6d617Schristosuse; the option @samp{-p1} is recommended. Test your procedure by 3282*75f6d617Schristospretending to be a recipient and applying your patches to a copy of 3283*75f6d617Schristosthe original files. 3284*75f6d617Schristos 3285*75f6d617Schristos@xref{Avoiding Common Mistakes}, for how to avoid common mistakes when 3286*75f6d617Schristosgenerating a patch. 3287*75f6d617Schristos 3288*75f6d617Schristos@node Tips for Patch Consumers 3289*75f6d617Schristos@section Tips for Patch Consumers 3290*75f6d617Schristos@cindex patch consumer tips 3291*75f6d617Schristos 3292*75f6d617SchristosA patch producer should tell recipients how to apply the patches, so 3293*75f6d617Schristosthe first rule of thumb for a patch consumer is to follow the 3294*75f6d617Schristosinstructions supplied with the patch. 3295*75f6d617Schristos 3296*75f6d617Schristos@sc{gnu} @command{diff} can analyze files with arbitrarily long lines 3297*75f6d617Schristosand files that end in incomplete lines. However, older versions of 3298*75f6d617Schristos@command{patch} cannot patch such files. If you are having trouble 3299*75f6d617Schristosapplying such patches, try upgrading to a recent version of @sc{gnu} 3300*75f6d617Schristos@command{patch}. 3301*75f6d617Schristos 3302*75f6d617Schristos@node Avoiding Common Mistakes 3303*75f6d617Schristos@section Avoiding Common Mistakes 3304*75f6d617Schristos@cindex common mistakes with patches 3305*75f6d617Schristos@cindex patch, common mistakes 3306*75f6d617Schristos 3307*75f6d617SchristosWhen producing a patch for multiple files, apply @command{diff} to 3308*75f6d617Schristosdirectories whose names do not have slashes. This reduces confusion 3309*75f6d617Schristoswhen the patch consumer specifies the @option{-p@var{number}} option, 3310*75f6d617Schristossince this option can have surprising results when the old and new 3311*75f6d617Schristosfile names have different numbers of slashes. For example, do not 3312*75f6d617Schristossend a patch with a header that looks like this: 3313*75f6d617Schristos 3314*75f6d617Schristos@example 3315*75f6d617Schristosdiff -Naur v2.0.29/prog/README prog/README 3316*75f6d617Schristos--- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800 3317*75f6d617Schristos+++ prog/README 2002-03-17 20:49:32.442260588 -0800 3318*75f6d617Schristos@end example 3319*75f6d617Schristos 3320*75f6d617Schristos@noindent 3321*75f6d617Schristosbecause the two file names have different numbers of slashes, and 3322*75f6d617Schristosdifferent versions of @command{patch} interpret the file names 3323*75f6d617Schristosdifferently. To avoid confusion, send output that looks like this 3324*75f6d617Schristosinstead: 3325*75f6d617Schristos 3326*75f6d617Schristos@example 3327*75f6d617Schristosdiff -Naur v2.0.29/prog/README v2.0.30/prog/README 3328*75f6d617Schristos--- v2.0.29/prog/README 2002-03-10 23:30:39.942229878 -0800 3329*75f6d617Schristos+++ v2.0.30/prog/README 2002-03-17 20:49:32.442260588 -0800 3330*75f6d617Schristos@end example 3331*75f6d617Schristos 3332*75f6d617SchristosMake sure you have specified the file names correctly, either in a 3333*75f6d617Schristoscontext diff header or with an @samp{Index:} line. Take care to not send out 3334*75f6d617Schristosreversed patches, since these make people wonder whether they have 3335*75f6d617Schristosalready applied the patch. 3336*75f6d617Schristos 3337*75f6d617SchristosAvoid sending patches that compare backup file names like 3338*75f6d617Schristos@file{README.orig} or @file{README~}, since this might confuse 3339*75f6d617Schristos@command{patch} into patching a backup file instead of the real file. 3340*75f6d617SchristosInstead, send patches that compare the same base file names in 3341*75f6d617Schristosdifferent directories, e.g.@: @file{old/README} and @file{new/README}. 3342*75f6d617Schristos 3343*75f6d617SchristosTo save people from partially applying a patch before other patches that 3344*75f6d617Schristosshould have gone before it, you can make the first patch in the patch 3345*75f6d617Schristosfile update a file with a name like @file{patchlevel.h} or 3346*75f6d617Schristos@file{version.c}, which contains a patch level or version number. If 3347*75f6d617Schristosthe input file contains the wrong version number, @command{patch} will 3348*75f6d617Schristoscomplain immediately. 3349*75f6d617Schristos 3350*75f6d617SchristosAn even clearer way to prevent this problem is to put a @samp{Prereq:} 3351*75f6d617Schristosline before the patch. If the leading text in the patch file contains a 3352*75f6d617Schristosline that starts with @samp{Prereq:}, @command{patch} takes the next word 3353*75f6d617Schristosfrom that line (normally a version number) and checks whether the next 3354*75f6d617Schristosinput file contains that word, preceded and followed by either 3355*75f6d617Schristoswhite space or a newline. If not, @command{patch} prompts you for 3356*75f6d617Schristosconfirmation before proceeding. This makes it difficult to accidentally 3357*75f6d617Schristosapply patches in the wrong order. 3358*75f6d617Schristos 3359*75f6d617Schristos@node Generating Smaller Patches 3360*75f6d617Schristos@section Generating Smaller Patches 3361*75f6d617Schristos@cindex patches, shrinking 3362*75f6d617Schristos 3363*75f6d617SchristosThe simplest way to generate a patch is to use @samp{diff -Naur} 3364*75f6d617Schristos(@pxref{Tips for Patch Producers}), but you might be able to reduce 3365*75f6d617Schristosthe size of the patch by renaming or removing some files before making 3366*75f6d617Schristosthe patch. If the older version of the package contains any files 3367*75f6d617Schristosthat the newer version does not, or if any files have been renamed 3368*75f6d617Schristosbetween the two versions, make a list of @command{rm} and @command{mv} 3369*75f6d617Schristoscommands for the user to execute in the old version directory before 3370*75f6d617Schristosapplying the patch. Then run those commands yourself in the scratch 3371*75f6d617Schristosdirectory. 3372*75f6d617Schristos 3373*75f6d617SchristosIf there are any files that you don't need to include in the patch 3374*75f6d617Schristosbecause they can easily be rebuilt from other files (for example, 3375*75f6d617Schristos@file{TAGS} and output from @command{yacc} and @command{makeinfo}), 3376*75f6d617Schristosexclude them from the patch by giving @command{diff} the @option{-x 3377*75f6d617Schristos@var{pattern}} option (@pxref{Comparing Directories}). If you want 3378*75f6d617Schristosyour patch to modify a derived file because your recipients lack tools 3379*75f6d617Schristosto build it, make sure that the patch for the derived file follows any 3380*75f6d617Schristospatches for files that it depends on, so that the recipients' time 3381*75f6d617Schristosstamps will not confuse @command{make}. 3382*75f6d617Schristos 3383*75f6d617SchristosNow you can create the patch using @samp{diff -Naur}. Make sure to 3384*75f6d617Schristosspecify the scratch directory first and the newer directory second. 3385*75f6d617Schristos 3386*75f6d617SchristosAdd to the top of the patch a note telling the user any @command{rm} and 3387*75f6d617Schristos@command{mv} commands to run before applying the patch. Then you can 3388*75f6d617Schristosremove the scratch directory. 3389*75f6d617Schristos 3390*75f6d617SchristosYou can also shrink the patch size by using fewer lines of context, 3391*75f6d617Schristosbut bear in mind that @command{patch} typically needs at least two 3392*75f6d617Schristoslines for proper operation when patches do not exactly match the input 3393*75f6d617Schristosfiles. 3394*75f6d617Schristos 3395*75f6d617Schristos@node Invoking cmp 3396*75f6d617Schristos@chapter Invoking @command{cmp} 3397*75f6d617Schristos@cindex invoking @command{cmp} 3398*75f6d617Schristos@cindex @command{cmp} invocation 3399*75f6d617Schristos 3400*75f6d617SchristosThe @command{cmp} command compares two files, and if they differ, 3401*75f6d617Schristostells the first byte and line number where they differ. Bytes and 3402*75f6d617Schristoslines are numbered starting with 1. The arguments of @command{cmp} 3403*75f6d617Schristosare as follows: 3404*75f6d617Schristos 3405*75f6d617Schristos@example 3406*75f6d617Schristoscmp @var{options}@dots{} @var{from-file} @r{[}@var{to-file} @r{[}@var{from-skip} @r{[}@var{to-skip}@r{]}@r{]}@r{]} 3407*75f6d617Schristos@end example 3408*75f6d617Schristos 3409*75f6d617SchristosThe file name @file{-} is always the standard input. @command{cmp} 3410*75f6d617Schristosalso uses the standard input if one file name is omitted. The 3411*75f6d617Schristos@var{from-skip} and @var{to-skip} operands specify how many bytes to 3412*75f6d617Schristosignore at the start of each file; they are equivalent to the 3413*75f6d617Schristos@option{--ignore-initial=@var{from-skip}:@var{to-skip}} option. 3414*75f6d617Schristos 3415*75f6d617SchristosAn exit status of 0 means no differences were found, 1 means some 3416*75f6d617Schristosdifferences were found, and 2 means trouble. 3417*75f6d617Schristos 3418*75f6d617Schristos@menu 3419*75f6d617Schristos* cmp Options:: Summary of options to @command{cmp}. 3420*75f6d617Schristos@end menu 3421*75f6d617Schristos 3422*75f6d617Schristos@node cmp Options 3423*75f6d617Schristos@section Options to @command{cmp} 3424*75f6d617Schristos@cindex @command{cmp} options 3425*75f6d617Schristos@cindex options for @command{cmp} 3426*75f6d617Schristos 3427*75f6d617SchristosBelow is a summary of all of the options that @sc{gnu} @command{cmp} accepts. 3428*75f6d617SchristosMost options have two equivalent names, one of which is a single letter 3429*75f6d617Schristospreceded by @samp{-}, and the other of which is a long name preceded by 3430*75f6d617Schristos@samp{--}. Multiple single letter options (unless they take an 3431*75f6d617Schristosargument) can be combined into a single command line word: @option{-bl} is 3432*75f6d617Schristosequivalent to @option{-b -l}. 3433*75f6d617Schristos 3434*75f6d617Schristos@table @option 3435*75f6d617Schristos@item -b 3436*75f6d617Schristos@itemx --print-bytes 3437*75f6d617SchristosPrint the differing bytes. Display control bytes as a 3438*75f6d617Schristos@samp{^} followed by a letter of the alphabet and precede bytes 3439*75f6d617Schristosthat have the high bit set with @samp{M-} (which stands for ``meta''). 3440*75f6d617Schristos 3441*75f6d617Schristos@item --help 3442*75f6d617SchristosOutput a summary of usage and then exit. 3443*75f6d617Schristos 3444*75f6d617Schristos@item -i @var{skip} 3445*75f6d617Schristos@itemx --ignore-initial=@var{skip} 3446*75f6d617SchristosIgnore any differences in the first @var{skip} bytes of the input 3447*75f6d617Schristosfiles. Treat files with fewer than @var{skip} bytes as if they are 3448*75f6d617Schristosempty. If @var{skip} is of the form 3449*75f6d617Schristos@option{@var{from-skip}:@var{to-skip}}, skip the first @var{from-skip} 3450*75f6d617Schristosbytes of the first input file and the first @var{to-skip} bytes of the 3451*75f6d617Schristossecond. 3452*75f6d617Schristos 3453*75f6d617Schristos@item -l 3454*75f6d617Schristos@itemx --verbose 3455*75f6d617SchristosPrint the (decimal) byte numbers and (octal) values of all differing bytes. 3456*75f6d617Schristos 3457*75f6d617Schristos@item -n @var{count} 3458*75f6d617Schristos@itemx --bytes=@var{count} 3459*75f6d617SchristosCompare at most @var{count} input bytes. 3460*75f6d617Schristos 3461*75f6d617Schristos@item -s 3462*75f6d617Schristos@itemx --quiet 3463*75f6d617Schristos@itemx --silent 3464*75f6d617SchristosDo not print anything; only return an exit status indicating whether 3465*75f6d617Schristosthe files differ. 3466*75f6d617Schristos 3467*75f6d617Schristos@item -v 3468*75f6d617Schristos@itemx --version 3469*75f6d617SchristosOutput version information and then exit. 3470*75f6d617Schristos@end table 3471*75f6d617Schristos 3472*75f6d617SchristosIn the above table, operands that are byte counts are normally 3473*75f6d617Schristosdecimal, but may be preceded by @samp{0} for octal and @samp{0x} for 3474*75f6d617Schristoshexadecimal. 3475*75f6d617Schristos 3476*75f6d617SchristosA byte count can be followed by a suffix to specify a multiple of that 3477*75f6d617Schristoscount; in this case an omitted integer is understood to be 1. A bare 3478*75f6d617Schristossize letter, or one followed by @samp{iB}, specifies a multiple using 3479*75f6d617Schristospowers of 1024. A size letter followed by @samp{B} specifies powers 3480*75f6d617Schristosof 1000 instead. For example, @option{-n 4M} and @option{-n 4MiB} are 3481*75f6d617Schristosequivalent to @option{-n 4194304}, whereas @option{-n 4MB} is 3482*75f6d617Schristosequivalent to @option{-n 4000000}. This notation is upward compatible 3483*75f6d617Schristoswith the @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI 3484*75f6d617Schristosprefixes} for decimal multiples and with the 3485*75f6d617Schristos@uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2 3486*75f6d617Schristosprefixes for binary multiples}. 3487*75f6d617Schristos 3488*75f6d617SchristosThe following suffixes are defined. Large sizes like @code{1Y} may be 3489*75f6d617Schristosrejected by your computer due to limitations of its arithmetic. 3490*75f6d617Schristos 3491*75f6d617Schristos@table @samp 3492*75f6d617Schristos@item kB 3493*75f6d617Schristos@cindex kilobyte, definition of 3494*75f6d617Schristoskilobyte: @math{10^3 = 1000}. 3495*75f6d617Schristos@item k 3496*75f6d617Schristos@itemx K 3497*75f6d617Schristos@itemx KiB 3498*75f6d617Schristos@cindex kibibyte, definition of 3499*75f6d617Schristoskibibyte: @math{2^10 = 1024}. @samp{K} is special: the SI prefix is 3500*75f6d617Schristos@samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and 3501*75f6d617Schristos@sc{posix} use @samp{k} to mean @samp{KiB}. 3502*75f6d617Schristos@item MB 3503*75f6d617Schristos@cindex megabyte, definition of 3504*75f6d617Schristosmegabyte: @math{10^6 = 1,000,000}. 3505*75f6d617Schristos@item M 3506*75f6d617Schristos@itemx MiB 3507*75f6d617Schristos@cindex mebibyte, definition of 3508*75f6d617Schristosmebibyte: @math{2^20 = 1,048,576}. 3509*75f6d617Schristos@item GB 3510*75f6d617Schristos@cindex gigabyte, definition of 3511*75f6d617Schristosgigabyte: @math{10^9 = 1,000,000,000}. 3512*75f6d617Schristos@item G 3513*75f6d617Schristos@itemx GiB 3514*75f6d617Schristos@cindex gibibyte, definition of 3515*75f6d617Schristosgibibyte: @math{2^30 = 1,073,741,824}. 3516*75f6d617Schristos@item TB 3517*75f6d617Schristos@cindex terabyte, definition of 3518*75f6d617Schristosterabyte: @math{10^12 = 1,000,000,000,000}. 3519*75f6d617Schristos@item T 3520*75f6d617Schristos@itemx TiB 3521*75f6d617Schristos@cindex tebibyte, definition of 3522*75f6d617Schristostebibyte: @math{2^40 = 1,099,511,627,776}. 3523*75f6d617Schristos@item PB 3524*75f6d617Schristos@cindex petabyte, definition of 3525*75f6d617Schristospetabyte: @math{10^15 = 1,000,000,000,000,000}. 3526*75f6d617Schristos@item P 3527*75f6d617Schristos@itemx PiB 3528*75f6d617Schristos@cindex pebibyte, definition of 3529*75f6d617Schristospebibyte: @math{2^50 = 1,125,899,906,842,624}. 3530*75f6d617Schristos@item EB 3531*75f6d617Schristos@cindex exabyte, definition of 3532*75f6d617Schristosexabyte: @math{10^18 = 1,000,000,000,000,000,000}. 3533*75f6d617Schristos@item E 3534*75f6d617Schristos@itemx EiB 3535*75f6d617Schristos@cindex exbibyte, definition of 3536*75f6d617Schristosexbibyte: @math{2^60 = 1,152,921,504,606,846,976}. 3537*75f6d617Schristos@item ZB 3538*75f6d617Schristos@cindex zettabyte, definition of 3539*75f6d617Schristoszettabyte: @math{10^21 = 1,000,000,000,000,000,000,000} 3540*75f6d617Schristos@item Z 3541*75f6d617Schristos@itemx ZiB 3542*75f6d617Schristos@math{2^70 = 1,180,591,620,717,411,303,424}. 3543*75f6d617Schristos(@samp{Zi} is a GNU extension to IEC 60027-2.) 3544*75f6d617Schristos@item YB 3545*75f6d617Schristos@cindex yottabyte, definition of 3546*75f6d617Schristosyottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}. 3547*75f6d617Schristos@item Y 3548*75f6d617Schristos@itemx YiB 3549*75f6d617Schristos@math{2^80 = 1,208,925,819,614,629,174,706,176}. 3550*75f6d617Schristos(@samp{Yi} is a GNU extension to IEC 60027-2.) 3551*75f6d617Schristos@end table 3552*75f6d617Schristos 3553*75f6d617Schristos@node Invoking diff 3554*75f6d617Schristos@chapter Invoking @command{diff} 3555*75f6d617Schristos@cindex invoking @command{diff} 3556*75f6d617Schristos@cindex @command{diff} invocation 3557*75f6d617Schristos 3558*75f6d617SchristosThe format for running the @command{diff} command is: 3559*75f6d617Schristos 3560*75f6d617Schristos@example 3561*75f6d617Schristosdiff @var{options}@dots{} @var{files}@dots{} 3562*75f6d617Schristos@end example 3563*75f6d617Schristos 3564*75f6d617SchristosIn the simplest case, two file names @var{from-file} and 3565*75f6d617Schristos@var{to-file} are given, and @command{diff} compares the contents of 3566*75f6d617Schristos@var{from-file} and @var{to-file}. A file name of @file{-} stands for 3567*75f6d617Schristostext read from the standard input. As a special case, @samp{diff - -} 3568*75f6d617Schristoscompares a copy of standard input to itself. 3569*75f6d617Schristos 3570*75f6d617SchristosIf one file is a directory and the other is not, @command{diff} compares 3571*75f6d617Schristosthe file in the directory whose name is that of the non-directory. 3572*75f6d617SchristosThe non-directory file must not be @file{-}. 3573*75f6d617Schristos 3574*75f6d617SchristosIf two file names are given and both are directories, 3575*75f6d617Schristos@command{diff} compares corresponding files in both directories, in 3576*75f6d617Schristosalphabetical order; this comparison is not recursive unless the 3577*75f6d617Schristos@option{-r} or @option{--recursive} option is given. @command{diff} never 3578*75f6d617Schristoscompares the actual contents of a directory as if it were a file. The 3579*75f6d617Schristosfile that is fully specified may not be standard input, because standard 3580*75f6d617Schristosinput is nameless and the notion of ``file with the same name'' does not 3581*75f6d617Schristosapply. 3582*75f6d617Schristos 3583*75f6d617SchristosIf the @option{--from-file=@var{file}} option is given, the number of 3584*75f6d617Schristosfile names is arbitrary, and @var{file} is compared to each named file. 3585*75f6d617SchristosSimilarly, if the @option{--to-file=@var{file}} option is given, each 3586*75f6d617Schristosnamed file is compared to @var{file}. 3587*75f6d617Schristos 3588*75f6d617Schristos@command{diff} options begin with @samp{-}, so normally file names 3589*75f6d617Schristosmay not begin with @samp{-}. However, @option{--} as an 3590*75f6d617Schristosargument by itself treats the remaining arguments as file names even if 3591*75f6d617Schristosthey begin with @samp{-}. 3592*75f6d617Schristos 3593*75f6d617SchristosAn exit status of 0 means no differences were found, 1 means some 3594*75f6d617Schristosdifferences were found, and 2 means trouble. 3595*75f6d617Schristos 3596*75f6d617Schristos@menu 3597*75f6d617Schristos* diff Options:: Summary of options to @command{diff}. 3598*75f6d617Schristos@end menu 3599*75f6d617Schristos 3600*75f6d617Schristos@node diff Options 3601*75f6d617Schristos@section Options to @command{diff} 3602*75f6d617Schristos@cindex @command{diff} options 3603*75f6d617Schristos@cindex options for @command{diff} 3604*75f6d617Schristos 3605*75f6d617SchristosBelow is a summary of all of the options that @sc{gnu} @command{diff} accepts. 3606*75f6d617SchristosMost options have two equivalent names, one of which is a single letter 3607*75f6d617Schristospreceded by @samp{-}, and the other of which is a long name preceded by 3608*75f6d617Schristos@samp{--}. Multiple single letter options (unless they take an 3609*75f6d617Schristosargument) can be combined into a single command line word: @option{-ac} is 3610*75f6d617Schristosequivalent to @option{-a -c}. Long named options can be abbreviated to 3611*75f6d617Schristosany unique prefix of their name. Brackets ([ and ]) indicate that an 3612*75f6d617Schristosoption takes an optional argument. 3613*75f6d617Schristos 3614*75f6d617Schristos@table @option 3615*75f6d617Schristos@item -a 3616*75f6d617Schristos@itemx --text 3617*75f6d617SchristosTreat all files as text and compare them line-by-line, even if they 3618*75f6d617Schristosdo not seem to be text. @xref{Binary}. 3619*75f6d617Schristos 3620*75f6d617Schristos@item -b 3621*75f6d617Schristos@itemx --ignore-space-change 3622*75f6d617SchristosIgnore changes in amount of white space. @xref{White Space}. 3623*75f6d617Schristos 3624*75f6d617Schristos@item -B 3625*75f6d617Schristos@itemx --ignore-blank-lines 3626*75f6d617SchristosIgnore changes that just insert or delete blank lines. @xref{Blank 3627*75f6d617SchristosLines}. 3628*75f6d617Schristos 3629*75f6d617Schristos@item --binary 3630*75f6d617SchristosRead and write data in binary mode. @xref{Binary}. 3631*75f6d617Schristos 3632*75f6d617Schristos@item -c 3633*75f6d617SchristosUse the context output format, showing three lines of context. 3634*75f6d617Schristos@xref{Context Format}. 3635*75f6d617Schristos 3636*75f6d617Schristos@item -C @var{lines} 3637*75f6d617Schristos@itemx --context@r{[}=@var{lines}@r{]} 3638*75f6d617SchristosUse the context output format, showing @var{lines} (an integer) lines of 3639*75f6d617Schristoscontext, or three if @var{lines} is not given. @xref{Context Format}. 3640*75f6d617SchristosFor proper operation, @command{patch} typically needs at least two lines of 3641*75f6d617Schristoscontext. 3642*75f6d617Schristos 3643*75f6d617SchristosOn older systems, @command{diff} supports an obsolete option 3644*75f6d617Schristos@option{-@var{lines}} that has effect when combined with @option{-c} 3645*75f6d617Schristosor @option{-p}. @sc{posix} 1003.1-2001 (@pxref{Standards 3646*75f6d617Schristosconformance}) does not allow this; use @option{-C @var{lines}} 3647*75f6d617Schristosinstead. 3648*75f6d617Schristos 3649*75f6d617Schristos@item --changed-group-format=@var{format} 3650*75f6d617SchristosUse @var{format} to output a line group containing differing lines from 3651*75f6d617Schristosboth files in if-then-else format. @xref{Line Group Formats}. 3652*75f6d617Schristos 3653*75f6d617Schristos@item -d 3654*75f6d617Schristos@itemx --minimal 3655*75f6d617SchristosChange the algorithm perhaps find a smaller set of changes. This makes 3656*75f6d617Schristos@command{diff} slower (sometimes much slower). @xref{diff Performance}. 3657*75f6d617Schristos 3658*75f6d617Schristos@item -D @var{name} 3659*75f6d617Schristos@itemx --ifdef=@var{name} 3660*75f6d617SchristosMake merged @samp{#ifdef} format output, conditional on the preprocessor 3661*75f6d617Schristosmacro @var{name}. @xref{If-then-else}. 3662*75f6d617Schristos 3663*75f6d617Schristos@item -e 3664*75f6d617Schristos@itemx --ed 3665*75f6d617SchristosMake output that is a valid @command{ed} script. @xref{ed Scripts}. 3666*75f6d617Schristos 3667*75f6d617Schristos@item -E 3668*75f6d617Schristos@itemx --ignore-tab-expansion 3669*75f6d617SchristosIgnore changes due to tab expansion. 3670*75f6d617Schristos@xref{White Space}. 3671*75f6d617Schristos 3672*75f6d617Schristos@item -f 3673*75f6d617Schristos@itemx --forward-ed 3674*75f6d617SchristosMake output that looks vaguely like an @command{ed} script but has changes 3675*75f6d617Schristosin the order they appear in the file. @xref{Forward ed}. 3676*75f6d617Schristos 3677*75f6d617Schristos@item -F @var{regexp} 3678*75f6d617Schristos@itemx --show-function-line=@var{regexp} 3679*75f6d617SchristosIn context and unified format, for each hunk of differences, show some 3680*75f6d617Schristosof the last preceding line that matches @var{regexp}. @xref{Specified 3681*75f6d617SchristosHeadings}. 3682*75f6d617Schristos 3683*75f6d617Schristos@item --from-file=@var{file} 3684*75f6d617SchristosCompare @var{file} to each operand; @var{file} may be a directory. 3685*75f6d617Schristos 3686*75f6d617Schristos@item --help 3687*75f6d617SchristosOutput a summary of usage and then exit. 3688*75f6d617Schristos 3689*75f6d617Schristos@item --horizon-lines=@var{lines} 3690*75f6d617SchristosDo not discard the last @var{lines} lines of the common prefix 3691*75f6d617Schristosand the first @var{lines} lines of the common suffix. 3692*75f6d617Schristos@xref{diff Performance}. 3693*75f6d617Schristos 3694*75f6d617Schristos@item -i 3695*75f6d617Schristos@itemx --ignore-case 3696*75f6d617SchristosIgnore changes in case; consider upper- and lower-case letters 3697*75f6d617Schristosequivalent. @xref{Case Folding}. 3698*75f6d617Schristos 3699*75f6d617Schristos@item -I @var{regexp} 3700*75f6d617Schristos@itemx --ignore-matching-lines=@var{regexp} 3701*75f6d617SchristosIgnore changes that just insert or delete lines that match @var{regexp}. 3702*75f6d617Schristos@xref{Specified Folding}. 3703*75f6d617Schristos 3704*75f6d617Schristos@item --ignore-file-name-case 3705*75f6d617SchristosIgnore case when comparing file names during recursive comparison. 3706*75f6d617Schristos@xref{Comparing Directories}. 3707*75f6d617Schristos 3708*75f6d617Schristos@item -l 3709*75f6d617Schristos@itemx --paginate 3710*75f6d617SchristosPass the output through @command{pr} to paginate it. @xref{Pagination}. 3711*75f6d617Schristos 3712*75f6d617Schristos@item --label=@var{label} 3713*75f6d617SchristosUse @var{label} instead of the file name in the context format 3714*75f6d617Schristos(@pxref{Context Format}) and unified format (@pxref{Unified Format}) 3715*75f6d617Schristosheaders. @xref{RCS}. 3716*75f6d617Schristos 3717*75f6d617Schristos@item --left-column 3718*75f6d617SchristosPrint only the left column of two common lines in side by side format. 3719*75f6d617Schristos@xref{Side by Side Format}. 3720*75f6d617Schristos 3721*75f6d617Schristos@item --line-format=@var{format} 3722*75f6d617SchristosUse @var{format} to output all input lines in if-then-else format. 3723*75f6d617Schristos@xref{Line Formats}. 3724*75f6d617Schristos 3725*75f6d617Schristos@item -n 3726*75f6d617Schristos@itemx --rcs 3727*75f6d617SchristosOutput @sc{rcs}-format diffs; like @option{-f} except that each command 3728*75f6d617Schristosspecifies the number of lines affected. @xref{RCS}. 3729*75f6d617Schristos 3730*75f6d617Schristos@item -N 3731*75f6d617Schristos@itemx --new-file 3732*75f6d617SchristosIn directory comparison, if a file is found in only one directory, 3733*75f6d617Schristostreat it as present but empty in the other directory. @xref{Comparing 3734*75f6d617SchristosDirectories}. 3735*75f6d617Schristos 3736*75f6d617Schristos@item --new-group-format=@var{format} 3737*75f6d617SchristosUse @var{format} to output a group of lines taken from just the second 3738*75f6d617Schristosfile in if-then-else format. @xref{Line Group Formats}. 3739*75f6d617Schristos 3740*75f6d617Schristos@item --new-line-format=@var{format} 3741*75f6d617SchristosUse @var{format} to output a line taken from just the second file in 3742*75f6d617Schristosif-then-else format. @xref{Line Formats}. 3743*75f6d617Schristos 3744*75f6d617Schristos@item --old-group-format=@var{format} 3745*75f6d617SchristosUse @var{format} to output a group of lines taken from just the first 3746*75f6d617Schristosfile in if-then-else format. @xref{Line Group Formats}. 3747*75f6d617Schristos 3748*75f6d617Schristos@item --old-line-format=@var{format} 3749*75f6d617SchristosUse @var{format} to output a line taken from just the first file in 3750*75f6d617Schristosif-then-else format. @xref{Line Formats}. 3751*75f6d617Schristos 3752*75f6d617Schristos@item -p 3753*75f6d617Schristos@itemx --show-c-function 3754*75f6d617SchristosShow which C function each change is in. @xref{C Function Headings}. 3755*75f6d617Schristos 3756*75f6d617Schristos@item -q 3757*75f6d617Schristos@itemx --brief 3758*75f6d617SchristosReport only whether the files differ, not the details of the 3759*75f6d617Schristosdifferences. @xref{Brief}. 3760*75f6d617Schristos 3761*75f6d617Schristos@item -r 3762*75f6d617Schristos@itemx --recursive 3763*75f6d617SchristosWhen comparing directories, recursively compare any subdirectories 3764*75f6d617Schristosfound. @xref{Comparing Directories}. 3765*75f6d617Schristos 3766*75f6d617Schristos@item -s 3767*75f6d617Schristos@itemx --report-identical-files 3768*75f6d617SchristosReport when two files are the same. @xref{Comparing Directories}. 3769*75f6d617Schristos 3770*75f6d617Schristos@item -S @var{file} 3771*75f6d617Schristos@itemx --starting-file=@var{file} 3772*75f6d617SchristosWhen comparing directories, start with the file @var{file}. This is 3773*75f6d617Schristosused for resuming an aborted comparison. @xref{Comparing Directories}. 3774*75f6d617Schristos 3775*75f6d617Schristos@item --speed-large-files 3776*75f6d617SchristosUse heuristics to speed handling of large files that have numerous 3777*75f6d617Schristosscattered small changes. @xref{diff Performance}. 3778*75f6d617Schristos 3779*75f6d617Schristos@item --strip-trailing-cr 3780*75f6d617SchristosStrip any trailing carriage return at the end of an input line. 3781*75f6d617Schristos@xref{Binary}. 3782*75f6d617Schristos 3783*75f6d617Schristos@item --suppress-common-lines 3784*75f6d617SchristosDo not print common lines in side by side format. 3785*75f6d617Schristos@xref{Side by Side Format}. 3786*75f6d617Schristos 3787*75f6d617Schristos@item -t 3788*75f6d617Schristos@itemx --expand-tabs 3789*75f6d617SchristosExpand tabs to spaces in the output, to preserve the alignment of tabs 3790*75f6d617Schristosin the input files. @xref{Tabs}. 3791*75f6d617Schristos 3792*75f6d617Schristos@item -T 3793*75f6d617Schristos@itemx --initial-tab 3794*75f6d617SchristosOutput a tab rather than a space before the text of a line in normal or 3795*75f6d617Schristoscontext format. This causes the alignment of tabs in the line to look 3796*75f6d617Schristosnormal. @xref{Tabs}. 3797*75f6d617Schristos 3798*75f6d617Schristos@item --to-file=@var{file} 3799*75f6d617SchristosCompare each operand to @var{file}; @var{file} may be a directory. 3800*75f6d617Schristos 3801*75f6d617Schristos@item -u 3802*75f6d617SchristosUse the unified output format, showing three lines of context. 3803*75f6d617Schristos@xref{Unified Format}. 3804*75f6d617Schristos 3805*75f6d617Schristos@item --unchanged-group-format=@var{format} 3806*75f6d617SchristosUse @var{format} to output a group of common lines taken from both files 3807*75f6d617Schristosin if-then-else format. @xref{Line Group Formats}. 3808*75f6d617Schristos 3809*75f6d617Schristos@item --unchanged-line-format=@var{format} 3810*75f6d617SchristosUse @var{format} to output a line common to both files in if-then-else 3811*75f6d617Schristosformat. @xref{Line Formats}. 3812*75f6d617Schristos 3813*75f6d617Schristos@item --unidirectional-new-file 3814*75f6d617SchristosWhen comparing directories, if a file appears only in the second 3815*75f6d617Schristosdirectory of the two, treat it as present but empty in the other. 3816*75f6d617Schristos@xref{Comparing Directories}. 3817*75f6d617Schristos 3818*75f6d617Schristos@item -U @var{lines} 3819*75f6d617Schristos@itemx --unified@r{[}=@var{lines}@r{]} 3820*75f6d617SchristosUse the unified output format, showing @var{lines} (an integer) lines of 3821*75f6d617Schristoscontext, or three if @var{lines} is not given. @xref{Unified Format}. 3822*75f6d617SchristosFor proper operation, @command{patch} typically needs at least two lines of 3823*75f6d617Schristoscontext. 3824*75f6d617Schristos 3825*75f6d617SchristosOn older systems, @command{diff} supports an obsolete option 3826*75f6d617Schristos@option{-@var{lines}} that has effect when combined with @option{-u}. 3827*75f6d617Schristos@sc{posix} 1003.1-2001 (@pxref{Standards conformance}) does not allow 3828*75f6d617Schristosthis; use @option{-U @var{lines}} instead. 3829*75f6d617Schristos 3830*75f6d617Schristos@item -v 3831*75f6d617Schristos@itemx --version 3832*75f6d617SchristosOutput version information and then exit. 3833*75f6d617Schristos 3834*75f6d617Schristos@item -w 3835*75f6d617Schristos@itemx --ignore-all-space 3836*75f6d617SchristosIgnore white space when comparing lines. @xref{White Space}. 3837*75f6d617Schristos 3838*75f6d617Schristos@item -W @var{columns} 3839*75f6d617Schristos@itemx --width=@var{columns} 3840*75f6d617SchristosOutput at most @var{columns} (default 130) print columns per line in 3841*75f6d617Schristosside by side format. @xref{Side by Side Format}. 3842*75f6d617Schristos 3843*75f6d617Schristos@item -x @var{pattern} 3844*75f6d617Schristos@itemx --exclude=@var{pattern} 3845*75f6d617SchristosWhen comparing directories, ignore files and subdirectories whose basenames 3846*75f6d617Schristosmatch @var{pattern}. @xref{Comparing Directories}. 3847*75f6d617Schristos 3848*75f6d617Schristos@item -X @var{file} 3849*75f6d617Schristos@itemx --exclude-from=@var{file} 3850*75f6d617SchristosWhen comparing directories, ignore files and subdirectories whose basenames 3851*75f6d617Schristosmatch any pattern contained in @var{file}. @xref{Comparing Directories}. 3852*75f6d617Schristos 3853*75f6d617Schristos@item -y 3854*75f6d617Schristos@itemx --side-by-side 3855*75f6d617SchristosUse the side by side output format. @xref{Side by Side Format}. 3856*75f6d617Schristos@end table 3857*75f6d617Schristos 3858*75f6d617Schristos@node Invoking diff3 3859*75f6d617Schristos@chapter Invoking @command{diff3} 3860*75f6d617Schristos@cindex invoking @command{diff3} 3861*75f6d617Schristos@cindex @command{diff3} invocation 3862*75f6d617Schristos 3863*75f6d617SchristosThe @command{diff3} command compares three files and outputs descriptions 3864*75f6d617Schristosof their differences. Its arguments are as follows: 3865*75f6d617Schristos 3866*75f6d617Schristos@example 3867*75f6d617Schristosdiff3 @var{options}@dots{} @var{mine} @var{older} @var{yours} 3868*75f6d617Schristos@end example 3869*75f6d617Schristos 3870*75f6d617SchristosThe files to compare are @var{mine}, @var{older}, and @var{yours}. 3871*75f6d617SchristosAt most one of these three file names may be @file{-}, 3872*75f6d617Schristoswhich tells @command{diff3} to read the standard input for that file. 3873*75f6d617Schristos 3874*75f6d617SchristosAn exit status of 0 means @command{diff3} was successful, 1 means some 3875*75f6d617Schristosconflicts were found, and 2 means trouble. 3876*75f6d617Schristos 3877*75f6d617Schristos@menu 3878*75f6d617Schristos* diff3 Options:: Summary of options to @command{diff3}. 3879*75f6d617Schristos@end menu 3880*75f6d617Schristos 3881*75f6d617Schristos@node diff3 Options 3882*75f6d617Schristos@section Options to @command{diff3} 3883*75f6d617Schristos@cindex @command{diff3} options 3884*75f6d617Schristos@cindex options for @command{diff3} 3885*75f6d617Schristos 3886*75f6d617SchristosBelow is a summary of all of the options that @sc{gnu} @command{diff3} 3887*75f6d617Schristosaccepts. Multiple single letter options (unless they take an argument) 3888*75f6d617Schristoscan be combined into a single command line argument. 3889*75f6d617Schristos 3890*75f6d617Schristos@table @option 3891*75f6d617Schristos@item -a 3892*75f6d617Schristos@itemx --text 3893*75f6d617SchristosTreat all files as text and compare them line-by-line, even if they 3894*75f6d617Schristosdo not appear to be text. @xref{Binary}. 3895*75f6d617Schristos 3896*75f6d617Schristos@item -A 3897*75f6d617Schristos@itemx --show-all 3898*75f6d617SchristosIncorporate all unmerged changes from @var{older} to @var{yours} into 3899*75f6d617Schristos@var{mine}, surrounding conflicts with bracket lines. 3900*75f6d617Schristos@xref{Marking Conflicts}. 3901*75f6d617Schristos 3902*75f6d617Schristos@item --diff-program=@var{program} 3903*75f6d617SchristosUse the compatible comparison program @var{program} to compare files 3904*75f6d617Schristosinstead of @command{diff}. 3905*75f6d617Schristos 3906*75f6d617Schristos@item -e 3907*75f6d617Schristos@itemx --ed 3908*75f6d617SchristosGenerate an @command{ed} script that incorporates all the changes from 3909*75f6d617Schristos@var{older} to @var{yours} into @var{mine}. @xref{Which Changes}. 3910*75f6d617Schristos 3911*75f6d617Schristos@item -E 3912*75f6d617Schristos@itemx --show-overlap 3913*75f6d617SchristosLike @option{-e}, except bracket lines from overlapping changes' first 3914*75f6d617Schristosand third files. 3915*75f6d617Schristos@xref{Marking Conflicts}. 3916*75f6d617SchristosWith @option{-E}, an overlapping change looks like this: 3917*75f6d617Schristos 3918*75f6d617Schristos@example 3919*75f6d617Schristos<<<<<<< @var{mine} 3920*75f6d617Schristos@r{lines from @var{mine}} 3921*75f6d617Schristos======= 3922*75f6d617Schristos@r{lines from @var{yours}} 3923*75f6d617Schristos>>>>>>> @var{yours} 3924*75f6d617Schristos@end example 3925*75f6d617Schristos 3926*75f6d617Schristos@item --help 3927*75f6d617SchristosOutput a summary of usage and then exit. 3928*75f6d617Schristos 3929*75f6d617Schristos@item -i 3930*75f6d617SchristosGenerate @samp{w} and @samp{q} commands at the end of the @command{ed} 3931*75f6d617Schristosscript for System V compatibility. This option must be combined with 3932*75f6d617Schristosone of the @option{-AeExX3} options, and may not be combined with @option{-m}. 3933*75f6d617Schristos@xref{Saving the Changed File}. 3934*75f6d617Schristos 3935*75f6d617Schristos@item -L @var{label} 3936*75f6d617Schristos@itemx --label=@var{label} 3937*75f6d617SchristosUse the label @var{label} for the brackets output by the @option{-A}, 3938*75f6d617Schristos@option{-E} and @option{-X} options. This option may be given up to three 3939*75f6d617Schristostimes, one for each input file. The default labels are the names of 3940*75f6d617Schristosthe input files. Thus @samp{diff3 -L X -L Y -L Z -m A B C} acts like 3941*75f6d617Schristos@samp{diff3 -m A B C}, except that the output looks like it came from 3942*75f6d617Schristosfiles named @samp{X}, @samp{Y} and @samp{Z} rather than from files 3943*75f6d617Schristosnamed @samp{A}, @samp{B} and @samp{C}. @xref{Marking Conflicts}. 3944*75f6d617Schristos 3945*75f6d617Schristos@item -m 3946*75f6d617Schristos@itemx --merge 3947*75f6d617SchristosApply the edit script to the first file and send the result to standard 3948*75f6d617Schristosoutput. Unlike piping the output from @command{diff3} to @command{ed}, this 3949*75f6d617Schristosworks even for binary files and incomplete lines. @option{-A} is assumed 3950*75f6d617Schristosif no edit script option is specified. @xref{Bypassing ed}. 3951*75f6d617Schristos 3952*75f6d617Schristos@item -T 3953*75f6d617Schristos@itemx --initial-tab 3954*75f6d617SchristosOutput a tab rather than two spaces before the text of a line in normal format. 3955*75f6d617SchristosThis causes the alignment of tabs in the line to look normal. @xref{Tabs}. 3956*75f6d617Schristos 3957*75f6d617Schristos@item -v 3958*75f6d617Schristos@itemx --version 3959*75f6d617SchristosOutput version information and then exit. 3960*75f6d617Schristos 3961*75f6d617Schristos@item -x 3962*75f6d617Schristos@itemx --overlap-only 3963*75f6d617SchristosLike @option{-e}, except output only the overlapping changes. 3964*75f6d617Schristos@xref{Which Changes}. 3965*75f6d617Schristos 3966*75f6d617Schristos@item -X 3967*75f6d617SchristosLike @option{-E}, except output only the overlapping changes. 3968*75f6d617SchristosIn other words, like @option{-x}, except bracket changes as in @option{-E}. 3969*75f6d617Schristos@xref{Marking Conflicts}. 3970*75f6d617Schristos 3971*75f6d617Schristos@item -3 3972*75f6d617Schristos@itemx --easy-only 3973*75f6d617SchristosLike @option{-e}, except output only the nonoverlapping changes. 3974*75f6d617Schristos@xref{Which Changes}. 3975*75f6d617Schristos@end table 3976*75f6d617Schristos 3977*75f6d617Schristos@node Invoking patch 3978*75f6d617Schristos@chapter Invoking @command{patch} 3979*75f6d617Schristos@cindex invoking @command{patch} 3980*75f6d617Schristos@cindex @command{patch} invocation 3981*75f6d617Schristos 3982*75f6d617SchristosNormally @command{patch} is invoked like this: 3983*75f6d617Schristos 3984*75f6d617Schristos@example 3985*75f6d617Schristospatch <@var{patchfile} 3986*75f6d617Schristos@end example 3987*75f6d617Schristos 3988*75f6d617SchristosThe full format for invoking @command{patch} is: 3989*75f6d617Schristos 3990*75f6d617Schristos@example 3991*75f6d617Schristospatch @var{options}@dots{} @r{[}@var{origfile} @r{[}@var{patchfile}@r{]}@r{]} 3992*75f6d617Schristos@end example 3993*75f6d617Schristos 3994*75f6d617SchristosYou can also specify where to read the patch from with the @option{-i 3995*75f6d617Schristos@var{patchfile}} or @option{--input=@var{patchfile}} option. 3996*75f6d617SchristosIf you do not specify @var{patchfile}, or if @var{patchfile} is 3997*75f6d617Schristos@file{-}, @command{patch} reads the patch (that is, the @command{diff} output) 3998*75f6d617Schristosfrom the standard input. 3999*75f6d617Schristos 4000*75f6d617SchristosIf you do not specify an input file on the command line, @command{patch} 4001*75f6d617Schristostries to intuit from the @dfn{leading text} (any text in the patch 4002*75f6d617Schristosthat comes before the @command{diff} output) which file to edit. 4003*75f6d617Schristos@xref{Multiple Patches}. 4004*75f6d617Schristos 4005*75f6d617SchristosBy default, @command{patch} replaces the original input file with the 4006*75f6d617Schristospatched version, possibly after renaming the original file into a 4007*75f6d617Schristosbackup file (@pxref{Backup Names}, for a description of how 4008*75f6d617Schristos@command{patch} names backup files). You can also specify where to 4009*75f6d617Schristosput the output with the @option{-o @var{file}} or 4010*75f6d617Schristos@option{--output=@var{file}} option; however, do not use this option 4011*75f6d617Schristosif @var{file} is one of the input files. 4012*75f6d617Schristos 4013*75f6d617Schristos@menu 4014*75f6d617Schristos* patch Options:: Summary table of options to @command{patch}. 4015*75f6d617Schristos@end menu 4016*75f6d617Schristos 4017*75f6d617Schristos@node patch Options 4018*75f6d617Schristos@section Options to @command{patch} 4019*75f6d617Schristos@cindex @command{patch} options 4020*75f6d617Schristos@cindex options for @command{patch} 4021*75f6d617Schristos 4022*75f6d617SchristosHere is a summary of all of the options that @sc{gnu} @command{patch} 4023*75f6d617Schristosaccepts. @xref{patch and Tradition}, for which of these options are 4024*75f6d617Schristossafe to use in older versions of @command{patch}. 4025*75f6d617Schristos 4026*75f6d617SchristosMultiple single-letter options that do not take an argument can be 4027*75f6d617Schristoscombined into a single command line argument with only one dash. 4028*75f6d617Schristos 4029*75f6d617Schristos@table @option 4030*75f6d617Schristos@item -b 4031*75f6d617Schristos@itemx --backup 4032*75f6d617SchristosBack up the original contents of each file, even if backups would 4033*75f6d617Schristosnormally not be made. @xref{Backups}. 4034*75f6d617Schristos 4035*75f6d617Schristos@item -B @var{prefix} 4036*75f6d617Schristos@itemx --prefix=@var{prefix} 4037*75f6d617SchristosPrepend @var{prefix} to backup file names. @xref{Backup Names}. 4038*75f6d617Schristos 4039*75f6d617Schristos@item --backup-if-mismatch 4040*75f6d617SchristosBack up the original contents of each file if the patch does not 4041*75f6d617Schristosexactly match the file. This is the default behavior when not 4042*75f6d617Schristosconforming to @sc{posix}. @xref{Backups}. 4043*75f6d617Schristos 4044*75f6d617Schristos@item --binary 4045*75f6d617SchristosRead and write all files in binary mode, except for standard output 4046*75f6d617Schristosand @file{/dev/tty}. This option has no effect on 4047*75f6d617Schristos@sc{posix}-conforming systems like @sc{gnu}/Linux. On systems where 4048*75f6d617Schristosthis option makes a difference, the patch should be generated by 4049*75f6d617Schristos@samp{diff -a --binary}. @xref{Binary}. 4050*75f6d617Schristos 4051*75f6d617Schristos@item -c 4052*75f6d617Schristos@itemx --context 4053*75f6d617SchristosInterpret the patch file as a context diff. @xref{patch Input}. 4054*75f6d617Schristos 4055*75f6d617Schristos@item -d @var{directory} 4056*75f6d617Schristos@itemx --directory=@var{directory} 4057*75f6d617SchristosMake directory @var{directory} the current directory for interpreting 4058*75f6d617Schristosboth file names in the patch file, and file names given as arguments to 4059*75f6d617Schristosother options. @xref{patch Directories}. 4060*75f6d617Schristos 4061*75f6d617Schristos@item -D @var{name} 4062*75f6d617Schristos@itemx --ifdef=@var{name} 4063*75f6d617SchristosMake merged if-then-else output using @var{name}. @xref{If-then-else}. 4064*75f6d617Schristos 4065*75f6d617Schristos@item --dry-run 4066*75f6d617SchristosPrint the results of applying the patches without actually changing 4067*75f6d617Schristosany files. @xref{Dry Runs}. 4068*75f6d617Schristos 4069*75f6d617Schristos@item -e 4070*75f6d617Schristos@itemx --ed 4071*75f6d617SchristosInterpret the patch file as an @command{ed} script. @xref{patch Input}. 4072*75f6d617Schristos 4073*75f6d617Schristos@item -E 4074*75f6d617Schristos@itemx --remove-empty-files 4075*75f6d617SchristosRemove output files that are empty after the patches have been applied. 4076*75f6d617Schristos@xref{Creating and Removing}. 4077*75f6d617Schristos 4078*75f6d617Schristos@item -f 4079*75f6d617Schristos@itemx --force 4080*75f6d617SchristosAssume that the user knows exactly what he or she is doing, and do not 4081*75f6d617Schristosask any questions. @xref{patch Messages}. 4082*75f6d617Schristos 4083*75f6d617Schristos@item -F @var{lines} 4084*75f6d617Schristos@itemx --fuzz=@var{lines} 4085*75f6d617SchristosSet the maximum fuzz factor to @var{lines}. @xref{Inexact}. 4086*75f6d617Schristos 4087*75f6d617Schristos@item -g @var{num} 4088*75f6d617Schristos@itemx --get=@var{num} 4089*75f6d617SchristosIf @var{num} is positive, get input files from a revision control 4090*75f6d617Schristossystem as necessary; if zero, do not get the files; if negative, ask 4091*75f6d617Schristosthe user whether to get the files. @xref{Revision Control}. 4092*75f6d617Schristos 4093*75f6d617Schristos@item --help 4094*75f6d617SchristosOutput a summary of usage and then exit. 4095*75f6d617Schristos 4096*75f6d617Schristos@item -i @var{patchfile} 4097*75f6d617Schristos@itemx --input=@var{patchfile} 4098*75f6d617SchristosRead the patch from @var{patchfile} rather than from standard input. 4099*75f6d617Schristos@xref{patch Options}. 4100*75f6d617Schristos 4101*75f6d617Schristos@item -l 4102*75f6d617Schristos@itemx --ignore-white-space 4103*75f6d617SchristosLet any sequence of blanks (spaces or tabs) in the patch file match 4104*75f6d617Schristosany sequence of blanks in the input file. @xref{Changed White Space}. 4105*75f6d617Schristos 4106*75f6d617Schristos@item -n 4107*75f6d617Schristos@itemx --normal 4108*75f6d617SchristosInterpret the patch file as a normal diff. @xref{patch Input}. 4109*75f6d617Schristos 4110*75f6d617Schristos@item -N 4111*75f6d617Schristos@itemx --forward 4112*75f6d617SchristosIgnore patches that @command{patch} thinks are reversed or already applied. 4113*75f6d617SchristosSee also @option{-R}. @xref{Reversed Patches}. 4114*75f6d617Schristos 4115*75f6d617Schristos@item --no-backup-if-mismatch 4116*75f6d617SchristosDo not back up the original contents of files. This is the default 4117*75f6d617Schristosbehavior when conforming to @sc{posix}. @xref{Backups}. 4118*75f6d617Schristos 4119*75f6d617Schristos@item -o @var{file} 4120*75f6d617Schristos@itemx --output=@var{file} 4121*75f6d617SchristosUse @var{file} as the output file name. @xref{patch Options}. 4122*75f6d617Schristos 4123*75f6d617Schristos@item -p@var{number} 4124*75f6d617Schristos@itemx --strip=@var{number} 4125*75f6d617SchristosSet the file name strip count to @var{number}. @xref{patch Directories}. 4126*75f6d617Schristos 4127*75f6d617Schristos@item --posix 4128*75f6d617SchristosConform to @sc{posix}, as if the @env{POSIXLY_CORRECT} environment 4129*75f6d617Schristosvariable had been set. @xref{patch and POSIX}. 4130*75f6d617Schristos 4131*75f6d617Schristos@item --quoting-style=@var{word} 4132*75f6d617SchristosUse style @var{word} to quote names in diagnostics, as if the 4133*75f6d617Schristos@env{QUOTING_STYLE} environment variable had been set to @var{word}. 4134*75f6d617Schristos@xref{patch Quoting Style}. 4135*75f6d617Schristos 4136*75f6d617Schristos@item -r @var{reject-file} 4137*75f6d617Schristos@itemx --reject-file=@var{reject-file} 4138*75f6d617SchristosUse @var{reject-file} as the reject file name. @xref{Reject Names}. 4139*75f6d617Schristos 4140*75f6d617Schristos@item -R 4141*75f6d617Schristos@itemx --reverse 4142*75f6d617SchristosAssume that this patch was created with the old and new files swapped. 4143*75f6d617Schristos@xref{Reversed Patches}. 4144*75f6d617Schristos 4145*75f6d617Schristos@item -s 4146*75f6d617Schristos@itemx --quiet 4147*75f6d617Schristos@itemx --silent 4148*75f6d617SchristosWork silently unless an error occurs. @xref{patch Messages}. 4149*75f6d617Schristos 4150*75f6d617Schristos@item -t 4151*75f6d617Schristos@itemx --batch 4152*75f6d617SchristosDo not ask any questions. @xref{patch Messages}. 4153*75f6d617Schristos 4154*75f6d617Schristos@item -T 4155*75f6d617Schristos@itemx --set-time 4156*75f6d617SchristosSet the modification and access times of patched files from time 4157*75f6d617Schristosstamps given in context diff headers, assuming that the context diff 4158*75f6d617Schristosheaders use local time. @xref{Patching Time Stamps}. 4159*75f6d617Schristos 4160*75f6d617Schristos@item -u 4161*75f6d617Schristos@itemx --unified 4162*75f6d617SchristosInterpret the patch file as a unified diff. @xref{patch Input}. 4163*75f6d617Schristos 4164*75f6d617Schristos@item -v 4165*75f6d617Schristos@itemx --version 4166*75f6d617SchristosOutput version information and then exit. 4167*75f6d617Schristos 4168*75f6d617Schristos@item -V @var{backup-style} 4169*75f6d617Schristos@itemx --version=control=@var{backup-style} 4170*75f6d617SchristosSelect the naming convention for backup file names. @xref{Backup Names}. 4171*75f6d617Schristos 4172*75f6d617Schristos@item --verbose 4173*75f6d617SchristosPrint more diagnostics than usual. @xref{patch Messages}. 4174*75f6d617Schristos 4175*75f6d617Schristos@item -x @var{number} 4176*75f6d617Schristos@itemx --debug=@var{number} 4177*75f6d617SchristosSet internal debugging flags. Of interest only to @command{patch} 4178*75f6d617Schristospatchers. 4179*75f6d617Schristos 4180*75f6d617Schristos@item -Y @var{prefix} 4181*75f6d617Schristos@itemx --basename-prefix=@var{prefix} 4182*75f6d617SchristosPrepend @var{prefix} to base names of backup files. @xref{Backup Names}. 4183*75f6d617Schristos 4184*75f6d617Schristos@item -z @var{suffix} 4185*75f6d617Schristos@itemx --suffix=@var{suffix} 4186*75f6d617SchristosUse @var{suffix} as the backup extension instead of @samp{.orig} or 4187*75f6d617Schristos@samp{~}. @xref{Backup Names}. 4188*75f6d617Schristos 4189*75f6d617Schristos@item -Z 4190*75f6d617Schristos@itemx --set-utc 4191*75f6d617SchristosSet the modification and access times of patched files from time 4192*75f6d617Schristosstamps given in context diff headers, assuming that the context diff 4193*75f6d617Schristosheaders use @sc{utc}. @xref{Patching Time Stamps}. 4194*75f6d617Schristos 4195*75f6d617Schristos@end table 4196*75f6d617Schristos 4197*75f6d617Schristos@node Invoking sdiff 4198*75f6d617Schristos@chapter Invoking @command{sdiff} 4199*75f6d617Schristos@cindex invoking @command{sdiff} 4200*75f6d617Schristos@cindex @command{sdiff} invocation 4201*75f6d617Schristos 4202*75f6d617SchristosThe @command{sdiff} command merges two files and interactively outputs the 4203*75f6d617Schristosresults. Its arguments are as follows: 4204*75f6d617Schristos 4205*75f6d617Schristos@example 4206*75f6d617Schristossdiff -o @var{outfile} @var{options}@dots{} @var{from-file} @var{to-file} 4207*75f6d617Schristos@end example 4208*75f6d617Schristos 4209*75f6d617SchristosThis merges @var{from-file} with @var{to-file}, with output to @var{outfile}. 4210*75f6d617SchristosIf @var{from-file} is a directory and @var{to-file} is not, @command{sdiff} 4211*75f6d617Schristoscompares the file in @var{from-file} whose file name is that of @var{to-file}, 4212*75f6d617Schristosand vice versa. @var{from-file} and @var{to-file} may not both be 4213*75f6d617Schristosdirectories. 4214*75f6d617Schristos 4215*75f6d617Schristos@command{sdiff} options begin with @samp{-}, so normally @var{from-file} 4216*75f6d617Schristosand @var{to-file} may not begin with @samp{-}. However, @option{--} as an 4217*75f6d617Schristosargument by itself treats the remaining arguments as file names even if 4218*75f6d617Schristosthey begin with @samp{-}. You may not use @file{-} as an input file. 4219*75f6d617Schristos 4220*75f6d617Schristos@command{sdiff} without @option{-o} (or @option{--output}) produces a 4221*75f6d617Schristosside-by-side difference. This usage is obsolete; use the @option{-y} 4222*75f6d617Schristosor @option{--side-by-side} option of @command{diff} instead. 4223*75f6d617Schristos 4224*75f6d617SchristosAn exit status of 0 means no differences were found, 1 means some 4225*75f6d617Schristosdifferences were found, and 2 means trouble. 4226*75f6d617Schristos 4227*75f6d617Schristos@menu 4228*75f6d617Schristos* sdiff Options:: Summary of options to @command{diff}. 4229*75f6d617Schristos@end menu 4230*75f6d617Schristos 4231*75f6d617Schristos@node sdiff Options 4232*75f6d617Schristos@section Options to @command{sdiff} 4233*75f6d617Schristos@cindex @command{sdiff} options 4234*75f6d617Schristos@cindex options for @command{sdiff} 4235*75f6d617Schristos 4236*75f6d617SchristosBelow is a summary of all of the options that @sc{gnu} @command{sdiff} accepts. 4237*75f6d617SchristosEach option has two equivalent names, one of which is a single 4238*75f6d617Schristosletter preceded by @samp{-}, and the other of which is a long name 4239*75f6d617Schristospreceded by @samp{--}. Multiple single letter options (unless they take 4240*75f6d617Schristosan argument) can be combined into a single command line argument. Long 4241*75f6d617Schristosnamed options can be abbreviated to any unique prefix of their name. 4242*75f6d617Schristos 4243*75f6d617Schristos@table @option 4244*75f6d617Schristos@item -a 4245*75f6d617Schristos@itemx --text 4246*75f6d617SchristosTreat all files as text and compare them line-by-line, even if they 4247*75f6d617Schristosdo not appear to be text. @xref{Binary}. 4248*75f6d617Schristos 4249*75f6d617Schristos@item -b 4250*75f6d617Schristos@itemx --ignore-space-change 4251*75f6d617SchristosIgnore changes in amount of white space. @xref{White Space}. 4252*75f6d617Schristos 4253*75f6d617Schristos@item -B 4254*75f6d617Schristos@itemx --ignore-blank-lines 4255*75f6d617SchristosIgnore changes that just insert or delete blank lines. @xref{Blank 4256*75f6d617SchristosLines}. 4257*75f6d617Schristos 4258*75f6d617Schristos@item -d 4259*75f6d617Schristos@itemx --minimal 4260*75f6d617SchristosChange the algorithm to perhaps find a smaller set of changes. This 4261*75f6d617Schristosmakes @command{sdiff} slower (sometimes much slower). @xref{diff 4262*75f6d617SchristosPerformance}. 4263*75f6d617Schristos 4264*75f6d617Schristos@item --diff-program=@var{program} 4265*75f6d617SchristosUse the compatible comparison program @var{program} to compare files 4266*75f6d617Schristosinstead of @command{diff}. 4267*75f6d617Schristos 4268*75f6d617Schristos@item -E 4269*75f6d617Schristos@itemx --ignore-tab-expansion 4270*75f6d617SchristosIgnore changes due to tab expansion. 4271*75f6d617Schristos@xref{White Space}. 4272*75f6d617Schristos 4273*75f6d617Schristos@item --help 4274*75f6d617SchristosOutput a summary of usage and then exit. 4275*75f6d617Schristos 4276*75f6d617Schristos@item -i 4277*75f6d617Schristos@itemx --ignore-case 4278*75f6d617SchristosIgnore changes in case; consider upper- and lower-case to be the same. 4279*75f6d617Schristos@xref{Case Folding}. 4280*75f6d617Schristos 4281*75f6d617Schristos@item -I @var{regexp} 4282*75f6d617Schristos@itemx --ignore-matching-lines=@var{regexp} 4283*75f6d617SchristosIgnore changes that just insert or delete lines that match @var{regexp}. 4284*75f6d617Schristos@xref{Specified Folding}. 4285*75f6d617Schristos 4286*75f6d617Schristos@item -l 4287*75f6d617Schristos@itemx --left-column 4288*75f6d617SchristosPrint only the left column of two common lines. 4289*75f6d617Schristos@xref{Side by Side Format}. 4290*75f6d617Schristos 4291*75f6d617Schristos@item -o @var{file} 4292*75f6d617Schristos@itemx --output=@var{file} 4293*75f6d617SchristosPut merged output into @var{file}. This option is required for merging. 4294*75f6d617Schristos 4295*75f6d617Schristos@item -s 4296*75f6d617Schristos@itemx --suppress-common-lines 4297*75f6d617SchristosDo not print common lines. @xref{Side by Side Format}. 4298*75f6d617Schristos 4299*75f6d617Schristos@item --speed-large-files 4300*75f6d617SchristosUse heuristics to speed handling of large files that have numerous 4301*75f6d617Schristosscattered small changes. @xref{diff Performance}. 4302*75f6d617Schristos 4303*75f6d617Schristos@item --strip-trailing-cr 4304*75f6d617SchristosStrip any trailing carriage return at the end of an input line. 4305*75f6d617Schristos@xref{Binary}. 4306*75f6d617Schristos 4307*75f6d617Schristos@item -t 4308*75f6d617Schristos@itemx --expand-tabs 4309*75f6d617SchristosExpand tabs to spaces in the output, to preserve the alignment of tabs 4310*75f6d617Schristosin the input files. @xref{Tabs}. 4311*75f6d617Schristos 4312*75f6d617Schristos@item -v 4313*75f6d617Schristos@itemx --version 4314*75f6d617SchristosOutput version information and then exit. 4315*75f6d617Schristos 4316*75f6d617Schristos@item -w @var{columns} 4317*75f6d617Schristos@itemx --width=@var{columns} 4318*75f6d617SchristosOutput at most @var{columns} (default 130) print columns per line. 4319*75f6d617Schristos@xref{Side by Side Format}. Note that for historical reasons, this 4320*75f6d617Schristosoption is @option{-W} in @command{diff}, @option{-w} in @command{sdiff}. 4321*75f6d617Schristos 4322*75f6d617Schristos@item -W 4323*75f6d617Schristos@itemx --ignore-all-space 4324*75f6d617SchristosIgnore white space when comparing lines. @xref{White Space}. 4325*75f6d617SchristosNote that for historical reasons, this option is @option{-w} in @command{diff}, 4326*75f6d617Schristos@option{-W} in @command{sdiff}. 4327*75f6d617Schristos@end table 4328*75f6d617Schristos 4329*75f6d617Schristos@node Standards conformance 4330*75f6d617Schristos@chapter Standards conformance 4331*75f6d617Schristos@cindex @sc{posix} 4332*75f6d617Schristos 4333*75f6d617Schristos@vindex POSIXLY_CORRECT 4334*75f6d617SchristosIn a few cases, the @sc{gnu} utilities' default behavior is 4335*75f6d617Schristosincompatible with the @sc{posix} standard. To suppress these 4336*75f6d617Schristosincompatibilities, define the @env{POSIXLY_CORRECT} environment 4337*75f6d617Schristosvariable. Unless you are checking for @sc{posix} conformance, you 4338*75f6d617Schristosprobably do not need to define @env{POSIXLY_CORRECT}. 4339*75f6d617Schristos 4340*75f6d617SchristosNormally options and operands can appear in any order, and programs act 4341*75f6d617Schristosas if all the options appear before any operands. For example, 4342*75f6d617Schristos@samp{diff lao tzu -C 2} acts like @samp{diff -C 2 lao tzu}, since 4343*75f6d617Schristos@samp{2} is an option-argument of @option{-C}. However, if the 4344*75f6d617Schristos@env{POSIXLY_CORRECT} environment variable is set, options must appear 4345*75f6d617Schristosbefore operands, unless otherwise specified for a particular command. 4346*75f6d617Schristos 4347*75f6d617SchristosNewer versions of @sc{posix} are occasionally incompatible with older 4348*75f6d617Schristosversions. For example, older versions of @sc{posix} allowed the 4349*75f6d617Schristoscommand @samp{diff -c -10} to have the same meaning as @samp{diff -C 4350*75f6d617Schristos10}, but @sc{posix} 1003.1-2001 @samp{diff} no longer allows 4351*75f6d617Schristosdigit-string options like @option{-10}. 4352*75f6d617Schristos 4353*75f6d617Schristos@vindex _POSIX2_VERSION 4354*75f6d617SchristosThe @sc{gnu} utilities normally conform to the version of @sc{posix} 4355*75f6d617Schristosthat is standard for your system. To cause them to conform to a 4356*75f6d617Schristosdifferent version of @sc{posix}, define the @env{_POSIX2_VERSION} 4357*75f6d617Schristosenvironment variable to a value of the form @var{yyyymm} specifying 4358*75f6d617Schristosthe year and month the standard was adopted. Two values are currently 4359*75f6d617Schristossupported for @env{_POSIX2_VERSION}: @samp{199209} stands for 4360*75f6d617Schristos@sc{posix} 1003.2-1992, and @samp{200112} stands for @sc{posix} 4361*75f6d617Schristos1003.1-2001. For example, if you are running older software that 4362*75f6d617Schristosassumes an older version of @sc{posix} and uses @samp{diff -c -10}, 4363*75f6d617Schristosyou can work around the compatibility problems by setting 4364*75f6d617Schristos@samp{_POSIX2_VERSION=199209} in your environment. 4365*75f6d617Schristos 4366*75f6d617Schristos@node Projects 4367*75f6d617Schristos@chapter Future Projects 4368*75f6d617Schristos 4369*75f6d617SchristosHere are some ideas for improving @sc{gnu} @command{diff} and 4370*75f6d617Schristos@command{patch}. The @sc{gnu} project has identified some 4371*75f6d617Schristosimprovements as potential programming projects for volunteers. You 4372*75f6d617Schristoscan also help by reporting any bugs that you find. 4373*75f6d617Schristos 4374*75f6d617SchristosIf you are a programmer and would like to contribute something to the 4375*75f6d617Schristos@sc{gnu} project, please consider volunteering for one of these 4376*75f6d617Schristosprojects. If you are seriously contemplating work, please write to 4377*75f6d617Schristos@email{gnu@@gnu.org} to coordinate with other volunteers. 4378*75f6d617Schristos 4379*75f6d617Schristos@menu 4380*75f6d617Schristos* Shortcomings:: Suggested projects for improvements. 4381*75f6d617Schristos* Bugs:: Reporting bugs. 4382*75f6d617Schristos@end menu 4383*75f6d617Schristos 4384*75f6d617Schristos@node Shortcomings 4385*75f6d617Schristos@section Suggested Projects for Improving @sc{gnu} @command{diff} and @command{patch} 4386*75f6d617Schristos@cindex projects for directories 4387*75f6d617Schristos 4388*75f6d617SchristosOne should be able to use @sc{gnu} @command{diff} to generate a patch from any 4389*75f6d617Schristospair of directory trees, and given the patch and a copy of one such 4390*75f6d617Schristostree, use @command{patch} to generate a faithful copy of the other. 4391*75f6d617SchristosUnfortunately, some changes to directory trees cannot be expressed using 4392*75f6d617Schristoscurrent patch formats; also, @command{patch} does not handle some of the 4393*75f6d617Schristosexisting formats. These shortcomings motivate the following suggested 4394*75f6d617Schristosprojects. 4395*75f6d617Schristos 4396*75f6d617Schristos@menu 4397*75f6d617Schristos* Internationalization:: Handling multibyte and varying-width characters. 4398*75f6d617Schristos* Changing Structure:: Handling changes to the directory structure. 4399*75f6d617Schristos* Special Files:: Handling symbolic links, device special files, etc. 4400*75f6d617Schristos* Unusual File Names:: Handling file names that contain unusual characters. 4401*75f6d617Schristos* Time Stamp Order:: Outputting diffs in time stamp order. 4402*75f6d617Schristos* Ignoring Changes:: Ignoring certain changes while showing others. 4403*75f6d617Schristos* Speedups:: Improving performance. 4404*75f6d617Schristos@end menu 4405*75f6d617Schristos 4406*75f6d617Schristos@node Internationalization 4407*75f6d617Schristos@subsection Handling Multibyte and Varying-Width Characters 4408*75f6d617Schristos@cindex multibyte characters 4409*75f6d617Schristos@cindex varying-width characters 4410*75f6d617Schristos 4411*75f6d617Schristos@command{diff}, @command{diff3} and @command{sdiff} treat each line of 4412*75f6d617Schristosinput as a string of unibyte characters. This can mishandle multibyte 4413*75f6d617Schristoscharacters in some cases. For example, when asked to ignore spaces, 4414*75f6d617Schristos@command{diff} does not properly ignore a multibyte space character. 4415*75f6d617Schristos 4416*75f6d617SchristosAlso, @command{diff} currently assumes that each byte is one column 4417*75f6d617Schristoswide, and this assumption is incorrect in some locales, e.g., locales 4418*75f6d617Schristosthat use UTF-8 encoding. This causes problems with the @option{-y} or 4419*75f6d617Schristos@option{--side-by-side} option of @command{diff}. 4420*75f6d617Schristos 4421*75f6d617SchristosThese problems need to be fixed without unduly affecting the 4422*75f6d617Schristosperformance of the utilities in unibyte environments. 4423*75f6d617Schristos 4424*75f6d617SchristosThe IBM GNU/Linux Technology Center Internationalization Team has 4425*75f6d617Schristosproposed some patches to support internationalized @command{diff} 4426*75f6d617Schristos@uref{http://oss.software.ibm.com/developer/opensource/linux/patches/i18n/diffutils-2.7.2-i18n-0.1.patch.gz}. 4427*75f6d617SchristosUnfortunately, these patches are incomplete and are to an older 4428*75f6d617Schristosversion of @command{diff}, so more work needs to be done in this area. 4429*75f6d617Schristos 4430*75f6d617Schristos@node Changing Structure 4431*75f6d617Schristos@subsection Handling Changes to the Directory Structure 4432*75f6d617Schristos@cindex directory structure changes 4433*75f6d617Schristos 4434*75f6d617Schristos@command{diff} and @command{patch} do not handle some changes to directory 4435*75f6d617Schristosstructure. For example, suppose one directory tree contains a directory 4436*75f6d617Schristosnamed @samp{D} with some subsidiary files, and another contains a file 4437*75f6d617Schristoswith the same name @samp{D}. @samp{diff -r} does not output enough 4438*75f6d617Schristosinformation for @command{patch} to transform the directory subtree into 4439*75f6d617Schristosthe file. 4440*75f6d617Schristos 4441*75f6d617SchristosThere should be a way to specify that a file has been removed without 4442*75f6d617Schristoshaving to include its entire contents in the patch file. There should 4443*75f6d617Schristosalso be a way to tell @command{patch} that a file was renamed, even if 4444*75f6d617Schristosthere is no way for @command{diff} to generate such information. 4445*75f6d617SchristosThere should be a way to tell @command{patch} that a file's time stamp 4446*75f6d617Schristoshas changed, even if its contents have not changed. 4447*75f6d617Schristos 4448*75f6d617SchristosThese problems can be fixed by extending the @command{diff} output format 4449*75f6d617Schristosto represent changes in directory structure, and extending @command{patch} 4450*75f6d617Schristosto understand these extensions. 4451*75f6d617Schristos 4452*75f6d617Schristos@node Special Files 4453*75f6d617Schristos@subsection Files that are Neither Directories Nor Regular Files 4454*75f6d617Schristos@cindex special files 4455*75f6d617Schristos 4456*75f6d617SchristosSome files are neither directories nor regular files: they are unusual 4457*75f6d617Schristosfiles like symbolic links, device special files, named pipes, and 4458*75f6d617Schristossockets. Currently, @command{diff} treats symbolic links like regular files; 4459*75f6d617Schristosit treats other special files like regular files if they are specified 4460*75f6d617Schristosat the top level, but simply reports their presence when comparing 4461*75f6d617Schristosdirectories. This means that @command{patch} cannot represent changes 4462*75f6d617Schristosto such files. For example, if you change which file a symbolic link 4463*75f6d617Schristospoints to, @command{diff} outputs the difference between the two files, 4464*75f6d617Schristosinstead of the change to the symbolic link. 4465*75f6d617Schristos 4466*75f6d617Schristos@c This might not be a good idea; is it wise for root to install devices 4467*75f6d617Schristos@c this way? 4468*75f6d617Schristos@command{diff} should optionally report changes to special files specially, 4469*75f6d617Schristosand @command{patch} should be extended to understand these extensions. 4470*75f6d617Schristos 4471*75f6d617Schristos@node Unusual File Names 4472*75f6d617Schristos@subsection File Names that Contain Unusual Characters 4473*75f6d617Schristos@cindex file names with unusual characters 4474*75f6d617Schristos 4475*75f6d617SchristosWhen a file name contains an unusual character like a newline or 4476*75f6d617Schristoswhite space, @samp{diff -r} generates a patch that @command{patch} cannot 4477*75f6d617Schristosparse. The problem is with format of @command{diff} output, not just with 4478*75f6d617Schristos@command{patch}, because with odd enough file names one can cause 4479*75f6d617Schristos@command{diff} to generate a patch that is syntactically correct but 4480*75f6d617Schristospatches the wrong files. The format of @command{diff} output should be 4481*75f6d617Schristosextended to handle all possible file names. 4482*75f6d617Schristos 4483*75f6d617Schristos@node Time Stamp Order 4484*75f6d617Schristos@subsection Outputting Diffs in Time Stamp Order 4485*75f6d617Schristos 4486*75f6d617SchristosApplying @command{patch} to a multiple-file diff can result in files 4487*75f6d617Schristoswhose time stamps are out of order. @sc{gnu} @command{patch} has 4488*75f6d617Schristosoptions to restore the time stamps of the updated files 4489*75f6d617Schristos(@pxref{Patching Time Stamps}), but sometimes it is useful to generate 4490*75f6d617Schristosa patch that works even if the recipient does not have @sc{gnu} patch, 4491*75f6d617Schristosor does not use these options. One way to do this would be to 4492*75f6d617Schristosimplement a @command{diff} option to output diffs in time stamp order. 4493*75f6d617Schristos 4494*75f6d617Schristos@node Ignoring Changes 4495*75f6d617Schristos@subsection Ignoring Certain Changes 4496*75f6d617Schristos 4497*75f6d617SchristosIt would be nice to have a feature for specifying two strings, one in 4498*75f6d617Schristos@var{from-file} and one in @var{to-file}, which should be considered to 4499*75f6d617Schristosmatch. Thus, if the two strings are @samp{foo} and @samp{bar}, then if 4500*75f6d617Schristostwo lines differ only in that @samp{foo} in file 1 corresponds to 4501*75f6d617Schristos@samp{bar} in file 2, the lines are treated as identical. 4502*75f6d617Schristos 4503*75f6d617SchristosIt is not clear how general this feature can or should be, or 4504*75f6d617Schristoswhat syntax should be used for it. 4505*75f6d617Schristos 4506*75f6d617SchristosA partial substitute is to filter one or both files before comparing, 4507*75f6d617Schristose.g.: 4508*75f6d617Schristos 4509*75f6d617Schristos@example 4510*75f6d617Schristossed 's/foo/bar/g' file1 | diff - file2 4511*75f6d617Schristos@end example 4512*75f6d617Schristos 4513*75f6d617SchristosHowever, this outputs the filtered text, not the original. 4514*75f6d617Schristos 4515*75f6d617Schristos@node Speedups 4516*75f6d617Schristos@subsection Improving Performance 4517*75f6d617Schristos 4518*75f6d617SchristosWhen comparing two large directory structures, one of which was 4519*75f6d617Schristosoriginally copied from the other with time stamps preserved (e.g., 4520*75f6d617Schristoswith @samp{cp -pR}), it would greatly improve performance if an option 4521*75f6d617Schristostold @command{diff} to assume that two files with the same size and 4522*75f6d617Schristostime stamps have the same content. @xref{diff Performance}. 4523*75f6d617Schristos 4524*75f6d617Schristos@node Bugs 4525*75f6d617Schristos@section Reporting Bugs 4526*75f6d617Schristos@cindex bug reports 4527*75f6d617Schristos@cindex reporting bugs 4528*75f6d617Schristos 4529*75f6d617SchristosIf you think you have found a bug in @sc{gnu} @command{cmp}, 4530*75f6d617Schristos@command{diff}, @command{diff3}, or @command{sdiff}, please report it 4531*75f6d617Schristosby electronic mail to the 4532*75f6d617Schristos@uref{http://mail.gnu.org/mailman/listinfo/bug-gnu-utils,GNU utilities 4533*75f6d617Schristosbug report mailing list} @email{bug-gnu-utils@@gnu.org}. Please send 4534*75f6d617Schristosbug reports for @sc{gnu} @command{patch} to 4535*75f6d617Schristos@email{bug-patch@@gnu.org}. Send as precise a description of the 4536*75f6d617Schristosproblem as you can, including the output of the @option{--version} 4537*75f6d617Schristosoption and sample input files that produce the bug, if applicable. If 4538*75f6d617Schristosyou have a nontrivial fix for the bug, please send it as well. If you 4539*75f6d617Schristoshave a patch, please send it too. It may simplify the maintainer's 4540*75f6d617Schristosjob if the patch is relative to a recent test release, which you can 4541*75f6d617Schristosfind in the directory @uref{ftp://alpha.gnu.org/gnu/diffutils/}. 4542*75f6d617Schristos 4543*75f6d617Schristos@node Copying This Manual 4544*75f6d617Schristos@appendix Copying This Manual 4545*75f6d617Schristos 4546*75f6d617Schristos@menu 4547*75f6d617Schristos* GNU Free Documentation License:: License for copying this manual. 4548*75f6d617Schristos@end menu 4549*75f6d617Schristos 4550*75f6d617Schristos@include fdl.texi 4551*75f6d617Schristos 4552*75f6d617Schristos@node Index 4553*75f6d617Schristos@appendix Index 4554*75f6d617Schristos 4555*75f6d617Schristos@printindex cp 4556*75f6d617Schristos 4557*75f6d617Schristos@bye 4558