Files
oldlinux-files/Ref-docs/POSIX/susv3/utilities/patch.html
2024-02-19 00:21:47 -05:00

543 lines
22 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>patch</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="patch"></a> <a name="tag_04_98"></a><!-- patch -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_04_98_01"></a>NAME</h4>
<blockquote>patch - apply changes to files</blockquote>
<h4><a name="tag_04_98_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('UP')">UP</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> patch</tt> <b>[</b><tt>-blNR</tt><b>][</b> <tt>-c| -e| -n</tt><b>][</b><tt>-d</tt>
<i>dir</i><b>][</b><tt>-D</tt> <i>define</i><b>][</b><tt>-i</tt> <i>patchfile</i><b>]<br>
</b> <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt> <b>[</b><tt>-o</tt> <i>outfile</i><b>][</b><tt>-p</tt>
<i>num</i><b>][</b><tt>-r</tt> <i>rejectfile</i><b>][</b><i>file</i><b>]</b><tt><img src="../images/opt-end.gif" alt="[Option End]"
border="0"></tt></code></div>
</blockquote>
<h4><a name="tag_04_98_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>patch</i> utility shall read a source (patch) file containing any of the three forms of difference (diff) listings
produced by the <a href="../utilities/diff.html"><i>diff</i></a> utility (normal, context, or in the style of <a href=
"../utilities/ed.html"><i>ed</i></a>) and apply those differences to a file. By default, <i>patch</i> shall read from the standard
input.</p>
<p>The <i>patch</i> utility shall attempt to determine the type of the <a href="../utilities/diff.html"><i>diff</i></a> listing,
unless overruled by a <b>-c</b>, <b>-e</b>, or <b>-n</b> option.</p>
<p>If the patch file contains more than one patch, <i>patch</i> 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 <a href=
"../utilities/diff.html"><i>diff</i></a> listing.)</p>
</blockquote>
<h4><a name="tag_04_98_04"></a>OPTIONS</h4>
<blockquote>
<p>The <i>patch</i> utility shall conform to the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p>
<p>The following options shall be supported:</p>
<dl compact>
<dt><b>-b</b></dt>
<dd>Save a copy of the original contents of each modified file, before the differences are applied, in a file of the same name with
the suffix <b>.orig</b> appended to it. If the file already exists, it shall be overwritten; if multiple patches are applied to the
same file, the <b>.orig</b> file shall be written only for the first patch. When the <b>-o</b> <i>outfile</i> option is also
specified, <i>file</i> <b>.orig</b> shall not be created but, if <i>outfile</i> already exists, <i>outfile</i> <b>.orig</b> shall
be created.</dd>
<dt><b>-c</b></dt>
<dd>Interpret the patch file as a context difference (the output of the utility <a href="../utilities/diff.html"><i>diff</i></a>
when the <b>-c</b> or <b>-C</b> options are specified).</dd>
<dt><b>-d&nbsp;</b> <i>dir</i></dt>
<dd>Change the current directory to <i>dir</i> before processing as described in the EXTENDED DESCRIPTION section.</dd>
<dt><b>-D&nbsp;</b> <i>define</i></dt>
<dd>Mark changes with one of the following C preprocessor constructs:
<pre>
<tt>#ifdef define
...
#endif
<br>
#ifndef define
...
#endif
</tt>
</pre>
<p>optionally combined with the C preprocessor construct <b>#else</b>.</p>
</dd>
<dt><b>-e</b></dt>
<dd>Interpret the patch file as an <a href="../utilities/ed.html"><i>ed</i></a> script, rather than a <a href=
"../utilities/diff.html"><i>diff</i></a> script.</dd>
<dt><b>-i&nbsp;</b> <i>patchfile</i></dt>
<dd>Read the patch information from the file named by the pathname <i>patchfile</i>, rather than the standard input.</dd>
<dt><b>-l</b></dt>
<dd>(The letter ell.) Cause any sequence of &lt;blank&gt;s in the difference script to match any sequence of &lt;blank&gt;s in the
input file. Other characters shall be matched exactly.</dd>
<dt><b>-n</b></dt>
<dd>Interpret the script as a normal difference.</dd>
<dt><b>-N</b></dt>
<dd>Ignore patches where the differences have already been applied to the file; by default, already-applied patches shall be
rejected.</dd>
<dt><b>-o&nbsp;</b> <i>outfile</i></dt>
<dd>Instead of modifying the files (specified by the <i>file</i> operand or the difference listings) directly, write a copy of the
file referenced by each patch, with the appropriate differences applied, to <i>outfile</i>. Multiple patches for a single file
shall be applied to the intermediate versions of the file created by any previous patches, and shall result in multiple,
concatenated versions of the file being written to <i>outfile</i>.</dd>
<dt><b>-p&nbsp;</b> <i>num</i></dt>
<dd>For all pathnames in the patch file that indicate the names of files to be patched, delete <i>num</i> pathname components from
the beginning of each pathname. If the pathname in the patch file is absolute, any leading slashes shall be considered the first
component (that is, <b>-p&nbsp;1</b> shall remove the leading slashes). Specifying <b>-p&nbsp;0</b> shall cause the full pathname
to be used. If <b>-p</b> is not specified, only the basename (the final pathname component) shall be used.</dd>
<dt><b>-R</b></dt>
<dd>Reverse the sense of the patch script; that is, assume that the difference script was created from the new version to the old
version. The <b>-R</b> option cannot be used with <a href="../utilities/ed.html"><i>ed</i></a> scripts. The <i>patch</i> utility
shall attempt to reverse each portion of the script before applying it. Rejected differences shall be saved in swapped format. If
this option is not specified, and until a portion of the patch file is successfully applied, <i>patch</i> attempts to apply each
portion in its reversed sense as well as in its normal sense. If the attempt is successful, the user shall be prompted to determine
whether the <b>-R</b> option should be set.</dd>
<dt><b>-r&nbsp;</b> <i>rejectfile</i></dt>
<dd>Override the default reject filename. In the default case, the reject file shall have the same name as the output file, with
the suffix <b>.rej</b> appended to it; see <a href="#tag_04_98_13_03">Patch Application</a> .</dd>
</dl>
</blockquote>
<h4><a name="tag_04_98_05"></a>OPERANDS</h4>
<blockquote>
<p>The following operand shall be supported:</p>
<dl compact>
<dt><i>file</i></dt>
<dd>A pathname of a file to patch.</dd>
</dl>
</blockquote>
<h4><a name="tag_04_98_06"></a>STDIN</h4>
<blockquote>
<p>See the INPUT FILES section.</p>
</blockquote>
<h4><a name="tag_04_98_07"></a>INPUT FILES</h4>
<blockquote>
<p>Input files shall be text files.</p>
</blockquote>
<h4><a name="tag_04_98_08"></a>ENVIRONMENT VARIABLES</h4>
<blockquote>
<p>The following environment variables shall affect the execution of <i>patch</i>:</p>
<dl compact>
<dt><i>LANG</i></dt>
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
<dt><i>LC_ALL</i></dt>
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
<dt><i>LC_CTYPE</i></dt>
<dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
opposed to multi-byte characters in arguments and input files).</dd>
<dt><i>LC_MESSAGES</i></dt>
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and
informative messages written to standard output.</dd>
<dt><i>NLSPATH</i></dt>
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
<dt><i>LC_TIME</i></dt>
<dd>Determine the locale for recognizing the format of file timestamps written by the <a href=
"../utilities/diff.html"><i>diff</i></a> utility in a context-difference input file.</dd>
</dl>
</blockquote>
<h4><a name="tag_04_98_09"></a>ASYNCHRONOUS EVENTS</h4>
<blockquote>
<p>Default.</p>
</blockquote>
<h4><a name="tag_04_98_10"></a>STDOUT</h4>
<blockquote>
<p>Not used.</p>
</blockquote>
<h4><a name="tag_04_98_11"></a>STDERR</h4>
<blockquote>
<p>The standard error shall be used for diagnostic and informational messages.</p>
</blockquote>
<h4><a name="tag_04_98_12"></a>OUTPUT FILES</h4>
<blockquote>
<p>The output of the <i>patch</i> utility, the save files ( <b>.orig</b> suffixes), and the reject files ( <b>.rej</b> suffixes)
shall be text files.</p>
</blockquote>
<h4><a name="tag_04_98_13"></a>EXTENDED DESCRIPTION</h4>
<blockquote>
<p>A patch file may contain patching instructions for more than one file; filenames shall be determined as specified in <a href=
"#tag_04_98_13_02">Filename Determination</a> . When the <b>-b</b> option is specified, for each patched file, the original shall
be saved in a file of the same name with the suffix <b>.orig</b> appended to it.</p>
<p>For each patched file, a reject file may also be created as noted in <a href="#tag_04_98_13_03">Patch Application</a> . In the
absence of a <b>-r</b> option, the name of this file shall be formed by appending the suffix <b>.rej</b> to the original
filename.</p>
<h5><a name="tag_04_98_13_01"></a>Patch File Format</h5>
<p>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 <a href="../utilities/diff.html"><i>diff</i></a> <b>-c</b>,
and one or more sets of <a href="../utilities/diff.html"><i>diff</i></a> output, which are customarily called <i>hunks</i>.</p>
<p>The <i>patch</i> utility shall recognize the following expression in the header information:</p>
<dl compact>
<dt><b>Index:&nbsp;</b> <i>pathname</i></dt>
<dd><br>
The file to be patched is named <i>pathname</i>.</dd>
</dl>
<p>If all lines (including headers) within a patch begin with the same leading sequence of &lt;blank&gt;s, the <i>patch</i> utility
shall remove this sequence before proceeding. Within each patch, if the type of difference is context, the <i>patch</i> utility
shall recognize the following expressions:</p>
<dl compact>
<dt>***&nbsp;<i>filename&nbsp;timestamp</i></dt>
<dd><br>
The patches arose from <i>filename</i>.</dd>
<dt>---&nbsp;<i>filename&nbsp;timestamp</i></dt>
<dd><br>
The patches should be applied to <i>filename</i>.</dd>
</dl>
<p>Each hunk within a patch shall be the <a href="../utilities/diff.html"><i>diff</i></a> output to change a line range within the
original file. The line numbers for successive hunks within a patch shall occur in ascending order.</p>
<h5><a name="tag_04_98_13_02"></a>Filename Determination</h5>
<p>If no <i>file</i> operand is specified, <i>patch</i> shall perform the following steps to determine the filename to use:</p>
<ol>
<li>
<p>If the type of <a href="../utilities/diff.html"><i>diff</i></a> is context, the <i>patch</i> utility shall delete pathname
components (as specified by the <b>-p</b> option) from the filename on the line beginning with <tt>"***"</tt> , then test for the
existence of this file relative to the current directory (or the directory specified with the <b>-d</b> option). If the file
exists, the <i>patch</i> utility shall use this filename.</p>
</li>
<li>
<p>If the type of <a href="../utilities/diff.html"><i>diff</i></a> is context, the <i>patch</i> utility shall delete the pathname
components (as specified by the <b>-p</b> option) from the filename on the line beginning with <tt>"---"</tt> , then test for the
existence of this file relative to the current directory (or the directory specified with the <b>-d</b> option). If the file
exists, the <i>patch</i> utility shall use this filename.</p>
</li>
<li>
<p>If the header information contains a line beginning with the string <b>Index:</b>, the <i>patch</i> utility shall delete
pathname components (as specified by the <b>-p</b> option) from this line, then test for the existence of this file relative to the
current directory (or the directory specified with the <b>-d</b> option). If the file exists, the <i>patch</i> utility shall use
this filename.</p>
</li>
<li>
<p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
If an <b>SCCS</b> directory exists in the current directory, <i>patch</i> shall attempt to perform a <a href=
"../utilities/get.html"><i>get</i></a> <b>-e</b> <b>SCCS/s.</b> <i>filename</i> command to retrieve an editable version of the
file. If the file exists, the <i>patch</i> utility shall use this filename. <img src="../images/opt-end.gif" alt="[Option End]"
border="0"></p>
</li>
<li>
<p>The <i>patch</i> utility shall write a prompt to standard output and request a filename interactively from the controlling
terminal (for example, <b>/dev/tty</b>).</p>
</li>
</ol>
<h5><a name="tag_04_98_13_03"></a>Patch Application</h5>
<p>If the <b>-c</b>, <b>-e</b>, or <b>-n</b> option is present, the <i>patch</i> utility shall interpret information within each
hunk as a context difference, an <a href="../utilities/ed.html"><i>ed</i></a> difference, or a normal difference, respectively. In
the absence of any of these options, the <i>patch</i> utility shall determine the type of difference based on the format of
information within the hunk.</p>
<p>For each hunk, the <i>patch</i> 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, <i>patch</i> shall scan both forwards and backwards at least 1000 bytes for a set of lines that match the hunk context.</p>
<p>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.</p>
<p>If no location can be found, the <i>patch</i> 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 <a href=
"../utilities/ed.html"><i>ed</i></a>-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.</p>
<p>If the type of patch is an <a href="../utilities/ed.html"><i>ed</i></a> diff, the implementation may accomplish the patching by
invoking the <a href="../utilities/ed.html"><i>ed</i></a> utility.</p>
</blockquote>
<h4><a name="tag_04_98_14"></a>EXIT STATUS</h4>
<blockquote>
<p>The following exit values shall be returned:</p>
<dl compact>
<dt>&nbsp;0</dt>
<dd>Successful completion.</dd>
<dt>&nbsp;1</dt>
<dd>One or more lines were written to a reject file.</dd>
<dt>&gt;1</dt>
<dd>An error occurred.</dd>
</dl>
</blockquote>
<h4><a name="tag_04_98_15"></a>CONSEQUENCES OF ERRORS</h4>
<blockquote>
<p>Patches that cannot be correctly placed in the file shall be written to a reject file.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_04_98_16"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The <b>-R</b> option does not work with <a href="../utilities/ed.html"><i>ed</i></a> scripts because there is too little
information to reconstruct the reverse operation.</p>
<p>The <b>-p</b> 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:</p>
<pre>
<tt>/curds/whey/src/blurfl/blurfl.c
</tt>
</pre>
<p>Setting <b>-p&nbsp;0</b> gives the entire pathname unmodified; <b>-p&nbsp;1</b> gives:</p>
<pre>
<tt>curds/whey/src/blurfl/blurfl.c
</tt>
</pre>
<p>without the leading slash, <b>-p&nbsp;4</b> gives:</p>
<pre>
<tt>blurfl/blurfl.c
</tt>
</pre>
<p>and not specifying <b>-p</b> at all gives:</p>
<pre>
<tt>blurfl.c .
</tt>
</pre>
</blockquote>
<h4><a name="tag_04_98_17"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_98_18"></a>RATIONALE</h4>
<blockquote>
<p>Some of the functionality in historical <i>patch</i> implementations was not specified. The following documents those features
present in historical implementations that have not been specified.</p>
<p>A deleted piece of functionality was the <tt>'+'</tt> 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.</p>
<p>In historical implementations, if the string <tt>"Prereq:"</tt> appeared in the header, the <i>patch</i> utility would search
for the corresponding version information (the string specified in the header, delimited by &lt;blank&gt;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:</p>
<pre>
<tt>Prereq: 1.2
</tt>
</pre>
<p>were in the header, the presence of a delimited 1.2 anywhere in the file would satisfy the prerequisite.</p>
<p>The following options were dropped from historical implementations of <i>patch</i> as insufficiently useful to standardize:</p>
<dl compact>
<dt><b>-b</b></dt>
<dd>The <b>-b</b> option historically provided a method for changing the name extension of the backup file from the default
<b>.orig</b>. This option has been modified and retained in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001.</dd>
<dt><b>-F</b></dt>
<dd>The <b>-F</b> option specified the number of lines of a context diff to ignore when searching for a place to install a
patch.</dd>
<dt><b>-f</b></dt>
<dd>The <b>-f</b> option historically caused <i>patch</i> not to request additional information from the user.</dd>
<dt><b>-r</b></dt>
<dd>The <b>-r</b> option historically provided a method of overriding the extension of the reject file from the default
<b>.rej</b>.</dd>
<dt><b>-s</b></dt>
<dd>The <b>-s</b> option historically caused <i>patch</i> to work silently unless an error occurred.</dd>
<dt><b>-x</b></dt>
<dd>The <b>-x</b> option historically set internal debugging flags.</dd>
</dl>
<p>In some file system implementations, the saving of a <b>.orig</b> file may produce unwanted results. In the case of 12, 13, or
14-character filenames (on file systems supporting 14-character maximum filenames), the <b>.orig</b> 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 (
<tt>'&#152;'</tt> ) suffix be used instead of <b>.orig</b> and some other character instead of the <b>.rej</b> suffix. This was
rejected because it is not obvious to the user which file is which. The suffixes <b>.orig</b> and <b>.rej</b> are clearer and more
understandable.</p>
<p>The <b>-b</b> option has the opposite sense in some historical implementations-do not save the <b>.orig</b> file. The default
case here is not to save the files, making <i>patch</i> behave more consistently with the other standard utilities.</p>
<p>The <b>-w</b> option in early proposals was changed to <b>-l</b> to match historical practice.</p>
<p>The <b>-N</b> 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 <a href="../utilities/diff.html"><i>diff</i></a> into the <i>patch</i> utility, and
the user only wants to patch a file to a newer version non-interactively, the <b>-N</b> option is required.</p>
<p>Changes to the <b>-l</b> option description were proposed to allow matching across &lt;newline&gt;s in addition to just
&lt;blank&gt;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 <b>-L</b>.</p>
</blockquote>
<h4><a name="tag_04_98_19"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_98_20"></a>SEE ALSO</h4>
<blockquote>
<p><a href="ed.html"><i>ed</i></a> , <a href="diff.html"><i>diff</i></a></p>
</blockquote>
<h4><a name="tag_04_98_21"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 4.</p>
</blockquote>
<h4><a name="tag_04_98_22"></a>Issue 5</h4>
<blockquote>
<p>The FUTURE DIRECTIONS section is added.</p>
</blockquote>
<h4><a name="tag_04_98_23"></a>Issue 6</h4>
<blockquote>
<p>This utility is marked as part of the User Portability Utilities option.</p>
<p>The description of the <b>-D</b> option and the steps in <a href="#tag_04_98_13_02">Filename Determination</a> are changed to
match historical practice as defined in the IEEE&nbsp;P1003.2b draft standard.</p>
<p>The normative text is reworded to avoid use of the term &quot;must&quot; for application requirements.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>