Open System Services Shell and Utilities Reference Manual (G06.28+, H06.05+)

User Commands (p - r) patch(1)
The patch command attempts to determine the type of the diff script, unless it is overruled by a
-c or -n ag, which specify context diffs and normal diffs, respectively.
The patch le must contain zero or more lines of header information followed by one or more
patches. Each patch must contain zero or more lines of lename identication in the format pro-
duced by diff -c, and one or more sets of diff output, which are customarily called "hunks."
The patch command tries to skip any leading text, apply the diff, and then skip any trailing text.
Therefore, you could feed an article or message containing a diff listing to patch, and it will
work. If the entire diff is indented by a consistent amount, patch takes this into account.
If original_le is not specied, patch tries to determine the name of the le to edit from the lead-
ing text. In the header of a context diff, patch searches for lenames in lines beginning with ***
(the name of the le from which the patches arose) or --- (the name of the le to which the
patches should be applied), and selects the shortest name of an existing le. If there is an Index:
line in the leading text, patch tries to use the lename from that line. The context diff header
takes precedence over an Index: line. If no lename can be determined from the leading text,
patch asks you for the name of the le to patch.
If the original le cannot be found but a suitable SCCS or RCS le is available, patch attempts to
get or check out the SCCS or RCS le.
Additionally, if the leading text contains a Prereq: line, patch takes the rst word from the
prerequisites line (normally a version number) and searches the input le for that word. If the
word is not found, patch asks for conrmation before proceeding.
If the patch le contains more than one patch, patch tries to apply each patch as if it came from a
separate patch le. In this case, the name of the le to patch must be determined for each diff
listing, and the header text before each diff listing is examined for information such as lenames
and revision level. You can give ags (and another original lename) for the second and subse-
quent patches by separating the corresponding argument lists with a +.
For each hunk, patch calculates the location to apply the hunk using the line number mentioned
for the hunk, plus or minus any offset used in applying the previous hunk. If that calculation
does not give the correct place to apply the hunk, patch scans both forward and backward for a
set of lines matching the context given in the hunk. The patch command searches for a location
where all lines of the context match.
If patch cannot nd a place to install that hunk of the patch, it places the hunk in a reject le
(normally, a le with the same name as the output le plus the sufx .rej). The rejected hunk is
written in the form of a context diff script, regardless of the format of the patch le. If the input
was a normal diff or ed-style script, the reject le might contain diffs with zero lines of context.
The line numbers on the hunks in the reject le might be different from the line numbers in the
patch le; the reject le line numbers reect the approximate locations for the failed hunks in the
new le rather than the old le.
As each hunk is completed, patch tells you whether the hunk succeeded or failed, and on which
line in the new le patch assumed the hunk should go. If this line does not have the line number
specied in the diff, patch tells you the offset. A single large offset might be an indication that a
hunk was installed in the wrong place.
NOTES
Note the following if you are going to be sending out patches:
HP recommends that you keep a patchlevel.h le that is patched to increment the patch
level as the rst diff in the patch le you send out. If you put a Prereq: line in with the
patch, users will not be able to apply patches out of order without some warning.
527188-007 Hewlett-Packard Company 79