patch - apply changes to files
patch [-blNR][ -c| -e| -n][-d
dir][-D define][-i patchfile]
[-o outfile][-p
num][-r rejectfile][file]
The patch utility shall read a source (patch) file containing any of the three forms of difference (diff) listings produced by the diff utility (normal, context, or in the style of ed) and apply those differences to a file. By default, patch shall read from the standard input.
The patch utility shall attempt to determine the type of the diff listing, unless overruled by a -c, -e, or -n option.
If the patch file contains more than one patch, patch shall attempt to apply each of them as if they came from separate patch files. (In this case, the application shall ensure that the name of the patch file is determinable for each diff listing.)
The patch utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
The following options shall be supported:
#ifdef define ... #endif #ifndef define ... #endif
optionally combined with the C preprocessor construct #else. If the patched file is processed with the C preprocessor, where the macro define is defined, the output shall contain the changes from the patch file; otherwise, the output shall not contain the patches specified in the patch file.
The following operand shall be supported:
Input files shall be text files.
The following environment variables shall affect the execution of patch:
The standard error shall be used for diagnostic and informational messages.
The output of the patch utility, the save files ( .orig suffixes), and the reject files ( .rej suffixes) shall be text files.
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.
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 diff -c, 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:
The file to be patched is named pathname.
If all lines (including headers) within a patch begin with the same leading sequence of <blank>s, the patch utility shall remove this sequence before proceeding. Within each patch, if the type of difference is context, the patch utility shall recognize the following expressions:
The patches arose from filename.
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.
If no file operand is specified, patch shall perform the following steps to determine the filename to use:
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 "***" , 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.
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 "---" , 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.
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.
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.
The patch utility shall write a prompt to standard output and request a filename interactively from the controlling terminal (for example, /dev/tty).
If the -c, -e, or -n option is present, the patch utility shall interpret information within each hunk as a context difference, an ed difference, or a normal 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. The rejected hunk shall be written in context-difference format regardless of the format of the patch file. 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.
The following exit values shall be returned:
Patches that cannot be correctly placed in the file shall be written to a reject file.
The following sections are informative.
The -R option does not work with ed scripts because there is too little information to reconstruct the reverse operation.
The -p option makes it possible to customize a patch file to local user directory structures without manually editing the patch file. For example, if the filename in the patch file was:
/curds/whey/src/blurfl/blurfl.c
Setting -p 0 gives the entire pathname unmodified; -p 1 gives:
curds/whey/src/blurfl/blurfl.c
without the leading slash, -p 4 gives:
blurfl/blurfl.c
and not specifying -p at all gives:
blurfl.c .
Some of the functionality in historical patch implementations was not specified. The following documents those features present in historical implementations that have not been specified.
A deleted piece of functionality was the '+' pseudo-option allowing an additional set of options and a patch file operand to be given. This was seen as being insufficiently useful to standardize.
In historical implementations, if the string "Prereq:" appeared in the header, the patch utility would search for the corresponding version information (the string specified in the header, delimited by <blank>s or the beginning or end of a line or the file) anywhere in the original file. This was deleted as too simplistic and insufficiently trustworthy a mechanism to standardize. For example, if:
Prereq: 1.2
were in the header, the presence of a delimited 1.2 anywhere in the file would satisfy the prerequisite.
The following options were dropped from historical implementations of patch as insufficiently useful to standardize:
In some file system implementations, the saving of a .orig file may produce unwanted results. In the case of 12, 13, or 14-character filenames (on file systems supporting 14-character maximum filenames), the .orig file overwrites the new file. The reject file may also exceed this filename limit. It was suggested, due to some historical practice, that a tilde ( '~' ) suffix be used instead of .orig and some other character instead of the .rej suffix. This was rejected because it is not obvious to the user which file is which. The suffixes .orig and .rej are clearer and more understandable.
The -b option has the opposite sense in some historical implementations-do not save the .orig file. The default case here is not to save the files, making patch behave more consistently with the other standard utilities.
The -w option in early proposals was changed to -l to match historical practice.
The -N option was included because without it, a non-interactive application cannot reject previously applied patches. For example, if a user is piping the output of diff into the patch utility, and the user only wants to patch a file to a newer version non-interactively, the -N option is required.
Changes to the -l option description were proposed to allow matching across <newline>s in addition to just <blank>s. Since this is not historical practice, and since some ambiguities could result, it is suggested that future developments in this area utilize another option letter, such as -L.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |