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 flag, which specify context diffs and normal diffs, respectively.
The patch file must contain zero or more lines of header information followed by one or more
patches. Each patch must contain zero or more lines of filename identification 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_file is not specified, patch tries to determine the name of the file to edit from the lead-
ing text. In the header of a context diff, patch searches for filenames in lines beginning with ***
(the name of the file from which the patches arose) or --- (the name of the file to which the
patches should be applied), and selects the shortest name of an existing file. If there is an Index:
line in the leading text, patch tries to use the filename from that line. The context diff header
takes precedence over an Index: line. If no filename can be determined from the leading text,
patch asks you for the name of the file to patch.
If the original file cannot be found but a suitable SCCS or RCS file is available, patch attempts to
get or check out the SCCS or RCS file.
Additionally, if the leading text contains a Prereq: line, patch takes the first word from the
prerequisites line (normally a version number) and searches the input file for that word. If the
word is not found, patch asks for confirmation before proceeding.
If the patch file contains more than one patch, patch tries to apply each patch as if it came from a
separate patch file. In this case, the name of the file to patch must be determined for each diff
listing, and the header text before each diff listing is examined for information such as filenames
and revision level. You can give flags (and another original filename) 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 find a place to install that hunk of the patch, it places the hunk in a reject file
(normally, a file with the same name as the output file plus the suffix .rej). The rejected hunk is
written in the form of a context diff script, regardless of the format of the patch file. If the input
was a normal diff or ed-style script, the reject file might contain diffs with zero lines of context.
The line numbers on the hunks in the reject file might be different from the line numbers in the
patch file; the reject file line numbers reflect the approximate locations for the failed hunks in the
new file rather than the old file.
As each hunk is completed, patch tells you whether the hunk succeeded or failed, and on which
line in the new file patch assumed the hunk should go. If this line does not have the line number
specified 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 file that is patched to increment the patch
level as the first diff in the patch file 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 7−9