1PATCH(P)                   POSIX Programmer's Manual                  PATCH(P)
2
3
4

NAME

6       patch - apply changes to files
7

SYNOPSIS

9       patch [-blNR][ -c| -e| -n][-d dir][-D define][-i patchfile]
10               [-o outfile][-p num][-r rejectfile][file]
11

DESCRIPTION

13       The  patch  utility  shall read a source (patch) file containing any of
14       the three forms of difference (diff)  listings  produced  by  the  diff
15       utility  (normal,  context, or in the style of ed) and apply those dif‐
16       ferences to a file. By default, patch  shall  read  from  the  standard
17       input.
18
19       The patch utility shall attempt to determine the type of the diff list‐
20       ing, unless overruled by a -c, -e, or -n option.
21
22       If the patch file contains more than one patch, patch shall attempt  to
23       apply  each of them as if they came from separate patch files. (In this
24       case, the application shall ensure that the name of the patch  file  is
25       determinable for each diff listing.)
26

OPTIONS

28       The  patch  utility  shall  conform  to  the Base Definitions volume of
29       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
30
31       The following options shall be supported:
32
33       -b     Save a copy of the original  contents  of  each  modified  file,
34              before  the  differences are applied, in a file of the same name
35              with the suffix .orig  appended  to  it.  If  the  file  already
36              exists, it shall be overwritten; if multiple patches are applied
37              to the same file, the .orig file shall be written only  for  the
38              first  patch. When the -o outfile option is also specified, file
39              .orig shall not be created but, if outfile already exists,  out‐
40              file .orig shall be created.
41
42       -c     Interpret  the patch file as a context difference (the output of
43              the utility diff when the -c or -C options are specified).
44
45       -d  dir
46              Change  the  current  directory  to  dir  before  processing  as
47              described in the EXTENDED DESCRIPTION section.
48
49       -D  define
50              Mark  changes  with  one  of  the  following C preprocessor con‐
51              structs:
52
53
54              #ifdef define
55              ...
56              #endif
57
58
59              #ifndef define
60              ...
61              #endif
62
63       optionally combined with the C preprocessor construct  #else.   If  the
64       patched  file  is  processed  with  the C preprocessor, where the macro
65       define is defined, the output shall contain the changes from the  patch
66       file;  otherwise, the output shall not contain the patches specified in
67       the patch file.
68
69       -e     Interpret the patch file as an ed script,  rather  than  a  diff
70              script.
71
72       -i  patchfile
73              Read  the  patch information from the file named by the pathname
74              patchfile, rather than the standard input.
75
76       -l     (The letter ell.) Cause any sequence of <blank>s in the  differ‐
77              ence script to match any sequence of <blank>s in the input file.
78              Other characters shall be matched exactly.
79
80       -n     Interpret the script as a normal difference.
81
82       -N     Ignore patches where the differences have already  been  applied
83              to  the  file;  by  default,  already-applied  patches  shall be
84              rejected.
85
86       -o  outfile
87              Instead of modifying the files (specified by the file operand or
88              the difference listings) directly, write a copy of the file ref‐
89              erenced by each patch, with the appropriate differences applied,
90              to  outfile. Multiple patches for a single file shall be applied
91              to the intermediate versions of the file created by any previous
92              patches,  and shall result in multiple, concatenated versions of
93              the file being written to outfile.
94
95       -p  num
96              For all pathnames in the patch file that indicate the  names  of
97              files  to  be  patched,  delete num pathname components from the
98              beginning of each pathname. If the pathname in the patch file is
99              absolute, any leading slashes shall be considered the first com‐
100              ponent (that is, -p 1 shall remove the leading slashes).  Speci‐
101              fying  -p 0  shall  cause the full pathname to be used. If -p is
102              not specified, only the basename (the final pathname  component)
103              shall be used.
104
105       -R     Reverse  the sense of the patch script; that is, assume that the
106              difference script was created from the new version  to  the  old
107              version.  The  -R  option  cannot  be used with ed scripts.  The
108              patch utility shall attempt  to  reverse  each  portion  of  the
109              script  before  applying it. Rejected differences shall be saved
110              in swapped format. If this option is not specified, and until  a
111              portion  of  the  patch  file  is  successfully  applied,  patch
112              attempts to apply each portion in its reversed sense as well  as
113              in  its  normal  sense.  If  the attempt is successful, the user
114              shall be prompted to determine whether the -R option  should  be
115              set.
116
117       -r  rejectfile
118              Override  the  default reject filename. In the default case, the
119              reject file shall have the same name as the  output  file,  with
120              the suffix .rej appended to it; see Patch Application .
121
122

OPERANDS

124       The following operand shall be supported:
125
126       file   A pathname of a file to patch.
127
128

STDIN

130       See the INPUT FILES section.
131

INPUT FILES

133       Input files shall be text files.
134

ENVIRONMENT VARIABLES

136       The  following  environment  variables  shall  affect  the execution of
137       patch:
138
139       LANG   Provide a default value for the  internationalization  variables
140              that  are  unset  or  null.  (See the Base Definitions volume of
141              IEEE Std 1003.1-2001, Section  8.2,  Internationalization  Vari‐
142              ables  for the precedence of internationalization variables used
143              to determine the values of locale categories.)
144
145       LC_ALL If set to a non-empty string value, override the values  of  all
146              the other internationalization variables.
147
148       LC_CTYPE
149              Determine  the  locale  for  the  interpretation of sequences of
150              bytes of text data as characters (for  example,  single-byte  as
151              opposed to multi-byte characters in arguments and input files).
152
153       LC_MESSAGES
154              Determine  the  locale  that should be used to affect the format
155              and contents of diagnostic messages written  to  standard  error
156              and informative messages written to standard output.
157
158       NLSPATH
159              Determine the location of message catalogs for the processing of
160              LC_MESSAGES .
161
162       LC_TIME
163              Determine the locale for recognizing the format  of  file  time‐
164              stamps written by the diff utility in a context-difference input
165              file.
166
167

ASYNCHRONOUS EVENTS

169       Default.
170

STDOUT

172       Not used.
173

STDERR

175       The standard error shall be used for diagnostic and informational  mes‐
176       sages.
177

OUTPUT FILES

179       The  output of the patch utility, the save files ( .orig suffixes), and
180       the reject files ( .rej suffixes) shall be text files.
181

EXTENDED DESCRIPTION

183       A patch file may contain patching instructions for more than one  file;
184       filenames  shall be determined as specified in Filename Determination .
185       When the -b option is specified, for each patched  file,  the  original
186       shall  be  saved  in  a  file  of  the  same name with the suffix .orig
187       appended to it.
188
189       For each patched file, a reject file may also be created  as  noted  in
190       Patch  Application  .  In  the absence of a -r option, the name of this
191       file shall be formed by appending the suffix .rej to the original file‐
192       name.
193
194   Patch File Format
195       The  patch  file shall contain zero or more lines of header information
196       followed by one or more patches. Each patch shall contain zero or  more
197       lines of filename identification in the format produced by diff -c, and
198       one or more sets of diff output, which are customarily called hunks.
199
200       The patch utility shall  recognize  the  following  expression  in  the
201       header information:
202
203       Index:  pathname
204
205              The file to be patched is named pathname.
206
207
208       If  all  lines  (including  headers) within a patch begin with the same
209       leading sequence of <blank>s,  the  patch  utility  shall  remove  this
210       sequence  before  proceeding. Within each patch, if the type of differ‐
211       ence is context,  the  patch  utility  shall  recognize  the  following
212       expressions:
213
214       *** filename timestamp
215
216              The patches arose from filename.
217
218       --- filename timestamp
219
220              The patches should be applied to filename.
221
222
223       Each  hunk  within  a  patch  shall be the diff output to change a line
224       range within the original file. The line numbers for  successive  hunks
225       within a patch shall occur in ascending order.
226
227   Filename Determination
228       If  no  file  operand  is  specified, patch shall perform the following
229       steps to determine the filename to use:
230
231        1. If the type of diff is context,  the  patch  utility  shall  delete
232           pathname  components (as specified by the -p option) from the file‐
233           name on the line beginning with "***" , then test for the existence
234           of  this  file  relative to the current directory (or the directory
235           specified with the -d option). If the file exists, the patch  util‐
236           ity shall use this filename.
237
238        2. If  the type of diff is context, the patch utility shall delete the
239           pathname components (as specified by the -p option) from the  file‐
240           name on the line beginning with "---" , then test for the existence
241           of this file relative to the current directory  (or  the  directory
242           specified  with the -d option). If the file exists, the patch util‐
243           ity shall use this filename.
244
245        3. If the header information contains a line beginning with the string
246           Index:,  the  patch  utility  shall  delete pathname components (as
247           specified by the -p option) from this line, then test for the exis‐
248           tence of this file relative to the current directory (or the direc‐
249           tory specified with the -d option).  If the file exists, the  patch
250           utility shall use this filename.
251
252        4. If  an  SCCS directory exists in the current directory, patch shall
253           attempt to perform a get -e SCCS/s. filename command to retrieve an
254           editable version of the file. If the file exists, the patch utility
255           shall use this filename.
256
257        5. The patch utility shall write  a  prompt  to  standard  output  and
258           request a filename interactively from the controlling terminal (for
259           example, /dev/tty).
260
261   Patch Application
262       If the -c, -e, or -n option is present, the patch utility shall  inter‐
263       pret  information  within each hunk as a context difference, an ed dif‐
264       ference, or a normal difference, respectively. In the absence of any of
265       these options, the patch utility shall determine the type of difference
266       based on the format of information within the hunk.
267
268       For each hunk, the patch utility shall begin to search for the place to
269       apply  the  patch at the line number at the beginning of the hunk, plus
270       or minus any offset used in applying the previous hunk. If lines match‐
271       ing  the hunk context are not found, patch shall scan both forwards and
272       backwards at least 1000 bytes for a set of lines that  match  the  hunk
273       context.
274
275       If  no such place is found and it is a context difference, then another
276       scan shall take place, ignoring the first and last line of context.  If
277       that  fails,  the  first  two  and  last  two lines of context shall be
278       ignored and another scan shall be  made.   Implementations  may  search
279       more extensively for installation locations.
280
281       If no location can be found, the patch utility shall append the hunk to
282       the reject file. The rejected hunk shall be written in  context-differ‐
283       ence  format  regardless  of the format of the patch file. If the input
284       was a normal or ed-style difference, the reject file may  contain  dif‐
285       ferences  with zero lines of context.  The line numbers on the hunks in
286       the reject file may be different from the line  numbers  in  the  patch
287       file  since they shall reflect the approximate locations for the failed
288       hunks in the new file rather than the old one.
289
290       If the type of patch is an ed diff, the implementation  may  accomplish
291       the patching by invoking the ed utility.
292

EXIT STATUS

294       The following exit values shall be returned:
295
296        0     Successful completion.
297
298        1     One or more lines were written to a reject file.
299
300       >1     An error occurred.
301
302

CONSEQUENCES OF ERRORS

304       Patches that cannot be correctly placed in the file shall be written to
305       a reject file.
306
307       The following sections are informative.
308

APPLICATION USAGE

310       The -R option does not work with ed scripts because there is too little
311       information to reconstruct the reverse operation.
312
313       The -p option makes it possible to customize a patch file to local user
314       directory structures without manually editing the patch file. For exam‐
315       ple, if the filename in the patch file was:
316
317
318              /curds/whey/src/blurfl/blurfl.c
319
320       Setting -p 0 gives the entire pathname unmodified; -p 1 gives:
321
322
323              curds/whey/src/blurfl/blurfl.c
324
325       without the leading slash, -p 4 gives:
326
327
328              blurfl/blurfl.c
329
330       and not specifying -p at all gives:
331
332
333              blurfl.c .
334

EXAMPLES

336       None.
337

RATIONALE

339       Some  of  the functionality in historical patch implementations was not
340       specified. The following documents those features present in historical
341       implementations that have not been specified.
342
343       A  deleted piece of functionality was the '+' pseudo-option allowing an
344       additional set of options and a patch file operand to  be  given.  This
345       was seen as being insufficiently useful to standardize.
346
347       In  historical implementations, if the string "Prereq:" appeared in the
348       header, the patch utility would search for  the  corresponding  version
349       information  (the string specified in the header, delimited by <blank>s
350       or the beginning or end of a line or the file) anywhere in the original
351       file. This was deleted as too simplistic and insufficiently trustworthy
352       a mechanism to standardize. For example, if:
353
354
355              Prereq: 1.2
356
357       were in the header, the presence of a delimited  1.2  anywhere  in  the
358       file would satisfy the prerequisite.
359
360       The  following  options were dropped from historical implementations of
361       patch as insufficiently useful to standardize:
362
363       -b     The -b option historically provided a method  for  changing  the
364              name  extension  of the backup file from the default .orig. This
365              option  has  been  modified  and  retained  in  this  volume  of
366              IEEE Std 1003.1-2001.
367
368       -F     The -F option specified the number of lines of a context diff to
369              ignore when searching for a place to install a patch.
370
371       -f     The -f option historically caused patch  not  to  request  addi‐
372              tional information from the user.
373
374       -r     The  -r  option historically provided a method of overriding the
375              extension of the reject file from the default .rej.
376
377       -s     The -s option historically caused patch to work silently  unless
378              an error occurred.
379
380       -x     The -x option historically set internal debugging flags.
381
382
383       In  some  file  system  implementations, the saving of a .orig file may
384       produce unwanted results. In the case of 12, 13, or 14-character  file‐
385       names  (on file systems supporting 14-character maximum filenames), the
386       .orig file overwrites the new file.  The reject file  may  also  exceed
387       this filename limit. It was suggested, due to some historical practice,
388       that a tilde ( '~' ) suffix be used instead of  .orig  and  some  other
389       character  instead  of the .rej suffix. This was rejected because it is
390       not obvious to the user which file is which.  The  suffixes  .orig  and
391       .rej are clearer and more understandable.
392
393       The  -b  option  has  the opposite sense in some historical implementa‐
394       tions-do not save the .orig file. The default case here is not to  save
395       the  files,  making patch behave more consistently with the other stan‐
396       dard utilities.
397
398       The -w option in early proposals was changed to -l to match  historical
399       practice.
400
401       The -N option was included because without it, a non-interactive appli‐
402       cation cannot reject previously applied patches.   For  example,  if  a
403       user  is piping the output of diff into the patch utility, and the user
404       only wants to patch a file to a newer version non-interactively, the -N
405       option is required.
406
407       Changes  to  the  -l option description were proposed to allow matching
408       across <newline>s in addition to just <blank>s. Since this is not  his‐
409       torical  practice,  and since some ambiguities could result, it is sug‐
410       gested that future developments in this  area  utilize  another  option
411       letter, such as -L.
412

FUTURE DIRECTIONS

414       None.
415

SEE ALSO

417       ed , diff
418
420       Portions  of  this text are reprinted and reproduced in electronic form
421       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
422       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
423       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
424       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
425       event of any discrepancy between this version and the original IEEE and
426       The  Open Group Standard, the original IEEE and The Open Group Standard
427       is the referee document. The original Standard can be  obtained  online
428       at http://www.opengroup.org/unix/online.html .
429
430
431
432IEEE/The Open Group                  2003                             PATCH(P)
Impressum