A patch file may contain patching instructions for more than one
file; filenames shall be determined as specified in Filename
Determination. When the -b
option is specified, for each patched
file, the original shall be saved in a file of the same name with
the suffix .orig
appended to it.
For each patched file, a reject file may also be created as noted
in Patch Application. In the absence of a -r
option, the name of
this file shall be formed by appending the suffix .rej
to the
original filename.
Patch File Format
The patch file shall contain zero or more lines of header
information followed by one or more patches. Each patch shall
contain zero or more lines of filename identification in the
format produced by the -c
, -C
, -u
, or -U
options of the diff
utility, and one or more sets of diff output, which are
customarily called hunks.
The patch utility shall recognize the following expression in the
header information:
Index:
pathname
The file to be patched is named pathname.
If all lines (including headers) within a patch begin with the
same leading sequence of <blank> characters, the patch utility
shall remove this sequence before proceeding. Within each patch,
if the type of difference is common context, the patch utility
shall recognize the following expressions:
*** filename timestamp
The patches arose from filename.
--- filename timestamp
The patches should be applied to filename.
If the type of difference is unified context, the patch utility
shall recognize the following expressions:
--- filename timestamp
The patches arose from filename.
+++ filename timestamp
The patches should be applied to filename.
Each hunk within a patch shall be the diff output to change a
line range within the original file. The line numbers for
successive hunks within a patch shall occur in ascending order.
Filename Determination
If no file operand is specified, patch shall perform the
following steps to determine the filename to use:
1. If the type of diff is context, the patch utility shall
delete pathname components (as specified by the -p
option)
from the filename on the line beginning with "***"
(if copied
context) or "---"
(if unified context), then test for the
existence of this file relative to the current directory (or
the directory specified with the -d
option). If the file
exists, the patch utility shall use this filename.
2. If the type of diff is context, the patch utility shall
delete the pathname components (as specified by the -p
option) from the filename on the line beginning with "---"
(if copied context) or "+++"
(if unified context), then test
for the existence of this file relative to the current
directory (or the directory specified with the -d
option). If
the file exists, the patch utility shall use this filename.
3. If the header information contains a line beginning with the
string Index:
, the patch utility shall delete pathname
components (as specified by the -p
option) from this line,
then test for the existence of this file relative to the
current directory (or the directory specified with the -d
option). If the file exists, the patch utility shall use this
filename.
4. If an SCCS
directory exists in the current directory, patch
shall attempt to perform a get -e SCCS/s.
filename command to
retrieve an editable version of the file. If the file exists,
the patch utility shall use this filename.
5. The patch utility shall write a prompt to standard output and
request a filename interactively from the controlling
terminal (for example, /dev/tty
).
Patch Application
If the -c
, -e
, -n
, or -u
option is present, the patch utility
shall interpret information within each hunk as a copied context
difference, an ed difference, a normal difference, or a unified
context difference, respectively. In the absence of any of these
options, the patch utility shall determine the type of difference
based on the format of information within the hunk.
For each hunk, the patch utility shall begin to search for the
place to apply the patch at the line number at the beginning of
the hunk, plus or minus any offset used in applying the previous
hunk. If lines matching the hunk context are not found, patch
shall scan both forwards and backwards at least 1000 bytes for a
set of lines that match the hunk context.
If no such place is found and it is a context difference, then
another scan shall take place, ignoring the first and last line
of context. If that fails, the first two and last two lines of
context shall be ignored and another scan shall be made.
Implementations may search more extensively for installation
locations.
If no location can be found, the patch utility shall append the
hunk to the reject file. A rejected hunk that is a copied context
difference, an ed difference, or a normal difference shall be
written in copied-context-difference format regardless of the
format of the patch file. It is implementation-defined whether a
rejected hunk that is a unified context difference is written in
copied-context-difference format or in unified-context-difference
format. If the input was a normal or ed-style difference, the
reject file may contain differences with zero lines of context.
The line numbers on the hunks in the reject file may be different
from the line numbers in the patch file since they shall reflect
the approximate locations for the failed hunks in the new file
rather than the old one.
If the type of patch is an ed diff, the implementation may
accomplish the patching by invoking the ed utility.