PATCH(1)                   POSIX Programmer's Manual                  PATCH(1)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       patch -- apply changes to files

SSYYNNOOPPSSIISS
       patch [[-blNR]] [[-c|-e|-n|-u]] [[-d _d_i_r]] [[-D _d_e_f_i_n_e]] [[-i _p_a_t_c_h_f_i_l_e]]
           [[-o _o_u_t_f_i_l_e]] [[-p _n_u_m]] [[-r _r_e_j_e_c_t_f_i_l_e]] [[_f_i_l_e]]

DDEESSCCRRIIPPTTIIOONN
       The _p_a_t_c_h utility shall read a source (patch) file containing any of
       four forms of difference (diff) listings produced by the _d_i_f_f utility
       (normal, copied context, unified context, or in the style of _e_d) and
       apply those differences to a file. By default, _p_a_t_c_h shall read from
       the standard input.

       The _p_a_t_c_h utility shall attempt to determine the type of the _d_i_f_f
       listing, unless overruled by a --cc, --ee, --nn, or --uu option.

       If the patch file contains more than one patch, _p_a_t_c_h 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 _d_i_f_f listing.)

OOPPTTIIOONNSS
       The _p_a_t_c_h utility shall conform to the Base Definitions volume of
       POSIX.1-2017, _S_e_c_t_i_o_n _1_2_._2, _U_t_i_l_i_t_y _S_y_n_t_a_x _G_u_i_d_e_l_i_n_e_s.

       The following options shall be supported:

       --bb        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 ..oorriigg appended to it. If the file
                 already exists, it shall be overwritten; if multiple patches
                 are applied to the same file, the ..oorriigg file shall be written
                 only for the first patch. When the --oo _o_u_t_f_i_l_e option is also
                 specified, _f_i_l_e..oorriigg shall not be created but, if _o_u_t_f_i_l_e
                 already exists, _o_u_t_f_i_l_e..oorriigg shall be created.

       --cc        Interpret the patch file as a copied context difference (the
                 output of the utility _d_i_f_f when the --cc or --CC options are
                 specified).

       --dd _d_i_r    Change the current directory to _d_i_r before processing as
                 described in the EXTENDED DESCRIPTION section.

       --DD _d_e_f_i_n_e Mark changes with one of the following C preprocessor
                 constructs:


                     #ifdef define
                     ...
                     #endif

                     #ifndef define
                     ...
                     #endif

                 optionally combined with the C preprocessor construct ##eellssee.
                 If the patched file is processed with the C preprocessor,
                 where the macro _d_e_f_i_n_e 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.

       --ee        Interpret the patch file as an _e_d script, rather than a _d_i_f_f
                 script.

       --ii _p_a_t_c_h_f_i_l_e
                 Read the patch information from the file named by the
                 pathname _p_a_t_c_h_f_i_l_e, rather than the standard input.

       --ll        (The letter ell.) Cause any sequence of <blank> characters in
                 the difference script to match any sequence of <blank>
                 characters in the input file. Other characters shall be
                 matched exactly.

       --nn        Interpret the script as a normal difference.

       --NN        Ignore patches where the differences have already been
                 applied to the file; by default, already-applied patches
                 shall be rejected.

       --oo _o_u_t_f_i_l_e
                 Instead of modifying the files (specified by the _f_i_l_e operand
                 or the difference listings) directly, write a copy of the
                 file referenced by each patch, with the appropriate
                 differences applied, to _o_u_t_f_i_l_e.  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
                 _o_u_t_f_i_l_e.

       --pp _n_u_m    For all pathnames in the patch file that indicate the names
                 of files to be patched, delete _n_u_m pathname components from
                 the beginning of each pathname. If the pathname in the patch
                 file is absolute, any leading <slash> characters shall be
                 considered the first component (that is, --pp 11 shall remove
                 the leading <slash> characters). Specifying --pp 00 shall cause
                 the full pathname to be used. If --pp is not specified, only
                 the basename (the final pathname component) shall be used.

       --RR        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 --RR option cannot be used with _e_d scripts.
                 The _p_a_t_c_h 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,
                 _p_a_t_c_h 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 --RR option
                 should be set.

       --rr _r_e_j_e_c_t_f_i_l_e
                 Override the default reject filename. In the default case,
                 the reject file shall have the same name as the output file,
                 with the suffix ..rreejj appended to it; see _P_a_t_c_h _A_p_p_l_i_c_a_t_i_o_n.

       --uu        Interpret the patch file as a unified context difference (the
                 output of the _d_i_f_f utility when the --uu or --UU options are
                 specified).

OOPPEERRAANNDDSS
       The following operand shall be supported:

       _f_i_l_e      A pathname of a file to patch.

SSTTDDIINN
       See the INPUT FILES section.

IINNPPUUTT FFIILLEESS
       Input files shall be text files.

EENNVVIIRROONNMMEENNTT VVAARRIIAABBLLEESS
       The following environment variables shall affect the execution of
       _p_a_t_c_h:

       _L_A_N_G      Provide a default value for the internationalization
                 variables that are unset or null. (See the Base Definitions
                 volume of POSIX.1-2017, _S_e_c_t_i_o_n _8_._2, _I_n_t_e_r_n_a_t_i_o_n_a_l_i_z_a_t_i_o_n
                 _V_a_r_i_a_b_l_e_s the precedence of internationalization variables
                 used to determine the values of locale categories.)

       _L_C___A_L_L    If set to a non-empty string value, override the values of
                 all the other internationalization variables.

       _L_C___C_O_L_L_A_T_E
                 Determine the locale for the behavior of ranges, equivalence
                 classes, and multi-character collating elements used in the
                 extended regular expression defined for the yyeesseexxpprr locale
                 keyword in the _L_C___M_E_S_S_A_G_E_S category.

       _L_C___C_T_Y_P_E  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), and the behavior of character classes used in the
                 extended regular expression defined for the yyeesseexxpprr locale
                 keyword in the _L_C___M_E_S_S_A_G_E_S category.

       _L_C___M_E_S_S_A_G_E_S
                 Determine the locale used to process affirmative responses,
                 and the locale used to affect the format and contents of
                 diagnostic messages and prompts written to standard error.

       _N_L_S_P_A_T_H   Determine the location of message catalogs for the processing
                 of _L_C___M_E_S_S_A_G_E_S.

       _L_C___T_I_M_E   Determine the locale for recognizing the format of file
                 timestamps written by the _d_i_f_f utility in a context-
                 difference input file.

AASSYYNNCCHHRROONNOOUUSS EEVVEENNTTSS
       Default.

SSTTDDOOUUTT
       Not used.

SSTTDDEERRRR
       The standard error shall be used for diagnostic and informational
       messages.

OOUUTTPPUUTT FFIILLEESS
       The output of the _p_a_t_c_h utility, the save files (..oorriigg suffixes), and
       the reject files (..rreejj suffixes) shall be text files.

EEXXTTEENNDDEEDD DDEESSCCRRIIPPTTIIOONN
       A patch file may contain patching instructions for more than one file;
       filenames shall be determined as specified in _F_i_l_e_n_a_m_e _D_e_t_e_r_m_i_n_a_t_i_o_n.
       When the --bb option is specified, for each patched file, the original
       shall be saved in a file of the same name with the suffix ..oorriigg
       appended to it.

       For each patched file, a reject file may also be created as noted in
       _P_a_t_c_h _A_p_p_l_i_c_a_t_i_o_n.  In the absence of a --rr option, the name of this
       file shall be formed by appending the suffix ..rreejj to the original
       filename.

   PPaattcchh FFiillee FFoorrmmaatt
       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 --cc, --CC,
       --uu, or --UU options of the _d_i_f_f utility, and one or more sets of _d_i_f_f
       output, which are customarily called _h_u_n_k_s.

       The _p_a_t_c_h utility shall recognize the following expression in the
       header information:

       IInnddeexx:: _p_a_t_h_n_a_m_e
             The file to be patched is named _p_a_t_h_n_a_m_e.

       If all lines (including headers) within a patch begin with the same
       leading sequence of <blank> characters, the _p_a_t_c_h utility shall remove
       this sequence before proceeding. Within each patch, if the type of
       difference is common context, the _p_a_t_c_h utility shall recognize the
       following expressions:

       *** _f_i_l_e_n_a_m_e _t_i_m_e_s_t_a_m_p
             The patches arose from _f_i_l_e_n_a_m_e.

       --- _f_i_l_e_n_a_m_e _t_i_m_e_s_t_a_m_p
             The patches should be applied to _f_i_l_e_n_a_m_e.

       If the type of difference is unified context, the _p_a_t_c_h utility shall
       recognize the following expressions:

       --- _f_i_l_e_n_a_m_e _t_i_m_e_s_t_a_m_p
             The patches arose from _f_i_l_e_n_a_m_e.

       +++ _f_i_l_e_n_a_m_e _t_i_m_e_s_t_a_m_p
             The patches should be applied to _f_i_l_e_n_a_m_e.

       Each hunk within a patch shall be the _d_i_f_f output to change a line
       range within the original file. The line numbers for successive hunks
       within a patch shall occur in ascending order.

   FFiilleennaammee DDeetteerrmmiinnaattiioonn
       If no _f_i_l_e operand is specified, _p_a_t_c_h shall perform the following
       steps to determine the filename to use:

        1. If the type of _d_i_f_f is context, the _p_a_t_c_h utility shall delete
           pathname components (as specified by the --pp 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 --dd option). If the file exists, the _p_a_t_c_h utility shall
           use this filename.

        2. If the type of _d_i_f_f is context, the _p_a_t_c_h utility shall delete the
           pathname components (as specified by the --pp 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 --dd option). If the file exists, the _p_a_t_c_h utility shall
           use this filename.

        3. If the header information contains a line beginning with the string
           IInnddeexx::, the _p_a_t_c_h utility shall delete pathname components (as
           specified by the --pp option) from this line, then test for the
           existence of this file relative to the current directory (or the
           directory specified with the --dd option). If the file exists, the
           _p_a_t_c_h utility shall use this filename.

        4. If an SSCCCCSS directory exists in the current directory, _p_a_t_c_h shall
           attempt to perform a _g_e_t --ee SSCCCCSS//ss.._f_i_l_e_n_a_m_e command to retrieve an
           editable version of the file. If the file exists, the _p_a_t_c_h utility
           shall use this filename.

        5. The _p_a_t_c_h utility shall write a prompt to standard output and
           request a filename interactively from the controlling terminal (for
           example, //ddeevv//ttttyy).

   PPaattcchh AApppplliiccaattiioonn
       If the --cc, --ee, --nn, or --uu option is present, the _p_a_t_c_h utility shall
       interpret information within each hunk as a copied context difference,
       an _e_d difference, a normal difference, or a unified context difference,
       respectively. In the absence of any of these options, the _p_a_t_c_h utility
       shall determine the type of difference based on the format of
       information within the hunk.

       For each hunk, the _p_a_t_c_h 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, _p_a_t_c_h 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 _p_a_t_c_h utility shall append the hunk to
       the reject file. A rejected hunk that is a copied context difference,
       an _e_d 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 _e_d-
       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 _e_d diff, the implementation may accomplish
       the patching by invoking the _e_d utility.

EEXXIITT SSTTAATTUUSS
       The following exit values shall be returned:

        0    Successful completion.

        1    One or more lines were written to a reject file.

       >1    An error occurred.

CCOONNSSEEQQUUEENNCCEESS OOFF EERRRROORRSS
       Patches that cannot be correctly placed in the file shall be written to
       a reject file.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The --RR option does not work with _e_d scripts because there is too little
       information to reconstruct the reverse operation.

       The --pp 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 --pp 00 gives the entire pathname unmodified; --pp 11 gives:


           curds/whey/src/blurfl/blurfl.c

       without the leading <slash>, --pp 44 gives:


           blurfl/blurfl.c

       and not specifying --pp at all gives:


           blurfl.c .

EEXXAAMMPPLLEESS
       None.

RRAATTIIOONNAALLEE
       Some of the functionality in historical _p_a_t_c_h 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 ""PPrreerreeqq::"" appeared in the
       header, the _p_a_t_c_h utility would search for the corresponding version
       information (the string specified in the header, delimited by <blank>
       characters 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
       _p_a_t_c_h as insufficiently useful to standardize:

       --bb        The --bb option historically provided a method for changing the
                 name extension of the backup file from the default ..oorriigg.
                 This option has been modified and retained in this volume of
                 POSIX.1-2017.

       --FF        The --FF option specified the number of lines of a context diff
                 to ignore when searching for a place to install a patch.

       --ff        The --ff option historically caused _p_a_t_c_h not to request
                 additional information from the user.

       --rr        The --rr option historically provided a method of overriding
                 the extension of the reject file from the default ..rreejj.

       --ss        The --ss option historically caused _p_a_t_c_h to work silently
                 unless an error occurred.

       --xx        The --xx option historically set internal debugging flags.

       In some file system implementations, the saving of a ..oorriigg file may
       produce unwanted results. In the case of 12, 13, or 14-character
       filenames (on file systems supporting 14-character maximum filenames),
       the ..oorriigg 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 ..oorriigg and some other
       character instead of the ..rreejj suffix. This was rejected because it is
       not obvious to the user which file is which. The suffixes ..oorriigg and
       ..rreejj are clearer and more understandable.

       The --bb option has the opposite sense in some historical
       implementations--do not save the ..oorriigg file. The default case here is
       not to save the files, making _p_a_t_c_h behave more consistently with the
       other standard utilities.

       The --ww option in early proposals was changed to --ll to match historical
       practice.

       The --NN 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 _d_i_f_f into the _p_a_t_c_h utility, and the user
       only wants to patch a file to a newer version non-interactively, the --NN
       option is required.

       Changes to the --ll option description were proposed to allow matching
       across <newline> characters in addition to just <blank> characters.
       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 --LL.

       The --uu option of GNU _p_a_t_c_h has been added, along with support for
       unified context formats.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _d_i_f_f, _e_d

       The Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t
       _V_a_r_i_a_b_l_e_s, _S_e_c_t_i_o_n _1_2_._2, _U_t_i_l_i_t_y _S_y_n_t_a_x _G_u_i_d_e_l_i_n_e_s

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                             PATCH(1)
