1PATCH(1P) POSIX Programmer's Manual PATCH(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 patch - apply changes to files
13
15 patch [-blNR][ -c| -e| -n][-d dir][-D define][-i patchfile]
16 [-o outfile][-p num][-r rejectfile][file]
17
19 The patch utility shall read a source (patch) file containing any of
20 the three forms of difference (diff) listings produced by the diff
21 utility (normal, context, or in the style of ed) and apply those dif‐
22 ferences to a file. By default, patch shall read from the standard
23 input.
24
25 The patch utility shall attempt to determine the type of the diff list‐
26 ing, unless overruled by a -c, -e, or -n option.
27
28 If the patch file contains more than one patch, patch shall attempt to
29 apply each of them as if they came from separate patch files. (In this
30 case, the application shall ensure that the name of the patch file is
31 determinable for each diff listing.)
32
34 The patch utility shall conform to the Base Definitions volume of
35 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
36
37 The following options shall be supported:
38
39 -b Save a copy of the original contents of each modified file,
40 before the differences are applied, in a file of the same name
41 with the suffix .orig appended to it. If the file already
42 exists, it shall be overwritten; if multiple patches are applied
43 to the same file, the .orig file shall be written only for the
44 first patch. When the -o outfile option is also specified, file
45 .orig shall not be created but, if outfile already exists, out‐
46 file .orig shall be created.
47
48 -c Interpret the patch file as a context difference (the output of
49 the utility diff when the -c or -C options are specified).
50
51 -d dir
52 Change the current directory to dir before processing as
53 described in the EXTENDED DESCRIPTION section.
54
55 -D define
56 Mark changes with one of the following C preprocessor con‐
57 structs:
58
59
60 #ifdef define
61 ...
62 #endif
63
64
65 #ifndef define
66 ...
67 #endif
68
69 optionally combined with the C preprocessor construct #else. If the
70 patched file is processed with the C preprocessor, where the macro
71 define is defined, the output shall contain the changes from the patch
72 file; otherwise, the output shall not contain the patches specified in
73 the patch file.
74
75 -e Interpret the patch file as an ed script, rather than a diff
76 script.
77
78 -i patchfile
79 Read the patch information from the file named by the pathname
80 patchfile, rather than the standard input.
81
82 -l (The letter ell.) Cause any sequence of <blank>s in the differ‐
83 ence script to match any sequence of <blank>s in the input file.
84 Other characters shall be matched exactly.
85
86 -n Interpret the script as a normal difference.
87
88 -N Ignore patches where the differences have already been applied
89 to the file; by default, already-applied patches shall be
90 rejected.
91
92 -o outfile
93 Instead of modifying the files (specified by the file operand or
94 the difference listings) directly, write a copy of the file ref‐
95 erenced by each patch, with the appropriate differences applied,
96 to outfile. Multiple patches for a single file shall be applied
97 to the intermediate versions of the file created by any previous
98 patches, and shall result in multiple, concatenated versions of
99 the file being written to outfile.
100
101 -p num
102 For all pathnames in the patch file that indicate the names of
103 files to be patched, delete num pathname components from the
104 beginning of each pathname. If the pathname in the patch file is
105 absolute, any leading slashes shall be considered the first com‐
106 ponent (that is, -p 1 shall remove the leading slashes). Speci‐
107 fying -p 0 shall cause the full pathname to be used. If -p is
108 not specified, only the basename (the final pathname component)
109 shall be used.
110
111 -R Reverse the sense of the patch script; that is, assume that the
112 difference script was created from the new version to the old
113 version. The -R option cannot be used with ed scripts. The
114 patch utility shall attempt to reverse each portion of the
115 script before applying it. Rejected differences shall be saved
116 in swapped format. If this option is not specified, and until a
117 portion of the patch file is successfully applied, patch
118 attempts to apply each portion in its reversed sense as well as
119 in its normal sense. If the attempt is successful, the user
120 shall be prompted to determine whether the -R option should be
121 set.
122
123 -r rejectfile
124 Override the default reject filename. In the default case, the
125 reject file shall have the same name as the output file, with
126 the suffix .rej appended to it; see Patch Application .
127
128
130 The following operand shall be supported:
131
132 file A pathname of a file to patch.
133
134
136 See the INPUT FILES section.
137
139 Input files shall be text files.
140
142 The following environment variables shall affect the execution of
143 patch:
144
145 LANG Provide a default value for the internationalization variables
146 that are unset or null. (See the Base Definitions volume of
147 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
148 ables for the precedence of internationalization variables used
149 to determine the values of locale categories.)
150
151 LC_ALL If set to a non-empty string value, override the values of all
152 the other internationalization variables.
153
154 LC_CTYPE
155 Determine the locale for the interpretation of sequences of
156 bytes of text data as characters (for example, single-byte as
157 opposed to multi-byte characters in arguments and input files).
158
159 LC_MESSAGES
160 Determine the locale that should be used to affect the format
161 and contents of diagnostic messages written to standard error
162 and informative messages written to standard output.
163
164 NLSPATH
165 Determine the location of message catalogs for the processing of
166 LC_MESSAGES .
167
168 LC_TIME
169 Determine the locale for recognizing the format of file time‐
170 stamps written by the diff utility in a context-difference input
171 file.
172
173
175 Default.
176
178 Not used.
179
181 The standard error shall be used for diagnostic and informational mes‐
182 sages.
183
185 The output of the patch utility, the save files ( .orig suffixes), and
186 the reject files ( .rej suffixes) shall be text files.
187
189 A patch file may contain patching instructions for more than one file;
190 filenames shall be determined as specified in Filename Determination .
191 When the -b option is specified, for each patched file, the original
192 shall be saved in a file of the same name with the suffix .orig
193 appended to it.
194
195 For each patched file, a reject file may also be created as noted in
196 Patch Application . In the absence of a -r option, the name of this
197 file shall be formed by appending the suffix .rej to the original file‐
198 name.
199
200 Patch File Format
201 The patch file shall contain zero or more lines of header information
202 followed by one or more patches. Each patch shall contain zero or more
203 lines of filename identification in the format produced by diff -c, and
204 one or more sets of diff output, which are customarily called hunks.
205
206 The patch utility shall recognize the following expression in the
207 header information:
208
209 Index: pathname
210
211 The file to be patched is named pathname.
212
213
214 If all lines (including headers) within a patch begin with the same
215 leading sequence of <blank>s, the patch utility shall remove this
216 sequence before proceeding. Within each patch, if the type of differ‐
217 ence is context, the patch utility shall recognize the following
218 expressions:
219
220 *** filename timestamp
221
222 The patches arose from filename.
223
224 --- filename timestamp
225
226 The patches should be applied to filename.
227
228
229 Each hunk within a patch shall be the diff output to change a line
230 range within the original file. The line numbers for successive hunks
231 within a patch shall occur in ascending order.
232
233 Filename Determination
234 If no file operand is specified, patch shall perform the following
235 steps to determine the filename to use:
236
237 1. If the type of diff is context, the patch utility shall delete
238 pathname components (as specified by the -p option) from the file‐
239 name on the line beginning with "***", then test for the existence
240 of this file relative to the current directory (or the directory
241 specified with the -d option). If the file exists, the patch util‐
242 ity shall use this filename.
243
244 2. If the type of diff is context, the patch utility shall delete the
245 pathname components (as specified by the -p option) from the file‐
246 name on the line beginning with "---", then test for the existence
247 of this file relative to the current directory (or the directory
248 specified with the -d option). If the file exists, the patch util‐
249 ity shall use this filename.
250
251 3. If the header information contains a line beginning with the string
252 Index:, the patch utility shall delete pathname components (as
253 specified by the -p option) from this line, then test for the exis‐
254 tence of this file relative to the current directory (or the direc‐
255 tory specified with the -d option). If the file exists, the patch
256 utility shall use this filename.
257
258 4. If an SCCS directory exists in the current directory, patch shall
259 attempt to perform a get -e SCCS/s. filename command to retrieve an
260 editable version of the file. If the file exists, the patch utility
261 shall use this filename.
262
263 5. The patch utility shall write a prompt to standard output and
264 request a filename interactively from the controlling terminal (for
265 example, /dev/tty).
266
267 Patch Application
268 If the -c, -e, or -n option is present, the patch utility shall inter‐
269 pret information within each hunk as a context difference, an ed dif‐
270 ference, or a normal difference, respectively. In the absence of any of
271 these options, the patch utility shall determine the type of difference
272 based on the format of information within the hunk.
273
274 For each hunk, the patch utility shall begin to search for the place to
275 apply the patch at the line number at the beginning of the hunk, plus
276 or minus any offset used in applying the previous hunk. If lines match‐
277 ing the hunk context are not found, patch shall scan both forwards and
278 backwards at least 1000 bytes for a set of lines that match the hunk
279 context.
280
281 If no such place is found and it is a context difference, then another
282 scan shall take place, ignoring the first and last line of context. If
283 that fails, the first two and last two lines of context shall be
284 ignored and another scan shall be made. Implementations may search
285 more extensively for installation locations.
286
287 If no location can be found, the patch utility shall append the hunk to
288 the reject file. The rejected hunk shall be written in context-differ‐
289 ence format regardless of the format of the patch file. If the input
290 was a normal or ed-style difference, the reject file may contain dif‐
291 ferences with zero lines of context. The line numbers on the hunks in
292 the reject file may be different from the line numbers in the patch
293 file since they shall reflect the approximate locations for the failed
294 hunks in the new file rather than the old one.
295
296 If the type of patch is an ed diff, the implementation may accomplish
297 the patching by invoking the ed utility.
298
300 The following exit values shall be returned:
301
302 0 Successful completion.
303
304 1 One or more lines were written to a reject file.
305
306 >1 An error occurred.
307
308
310 Patches that cannot be correctly placed in the file shall be written to
311 a reject file.
312
313 The following sections are informative.
314
316 The -R option does not work with ed scripts because there is too little
317 information to reconstruct the reverse operation.
318
319 The -p option makes it possible to customize a patch file to local user
320 directory structures without manually editing the patch file. For exam‐
321 ple, if the filename in the patch file was:
322
323
324 /curds/whey/src/blurfl/blurfl.c
325
326 Setting -p 0 gives the entire pathname unmodified; -p 1 gives:
327
328
329 curds/whey/src/blurfl/blurfl.c
330
331 without the leading slash, -p 4 gives:
332
333
334 blurfl/blurfl.c
335
336 and not specifying -p at all gives:
337
338
339 blurfl.c .
340
342 None.
343
345 Some of the functionality in historical patch implementations was not
346 specified. The following documents those features present in historical
347 implementations that have not been specified.
348
349 A deleted piece of functionality was the '+' pseudo-option allowing an
350 additional set of options and a patch file operand to be given. This
351 was seen as being insufficiently useful to standardize.
352
353 In historical implementations, if the string "Prereq:" appeared in the
354 header, the patch utility would search for the corresponding version
355 information (the string specified in the header, delimited by <blank>s
356 or the beginning or end of a line or the file) anywhere in the original
357 file. This was deleted as too simplistic and insufficiently trustworthy
358 a mechanism to standardize. For example, if:
359
360
361 Prereq: 1.2
362
363 were in the header, the presence of a delimited 1.2 anywhere in the
364 file would satisfy the prerequisite.
365
366 The following options were dropped from historical implementations of
367 patch as insufficiently useful to standardize:
368
369 -b The -b option historically provided a method for changing the
370 name extension of the backup file from the default .orig. This
371 option has been modified and retained in this volume of
372 IEEE Std 1003.1-2001.
373
374 -F The -F option specified the number of lines of a context diff to
375 ignore when searching for a place to install a patch.
376
377 -f The -f option historically caused patch not to request addi‐
378 tional information from the user.
379
380 -r The -r option historically provided a method of overriding the
381 extension of the reject file from the default .rej.
382
383 -s The -s option historically caused patch to work silently unless
384 an error occurred.
385
386 -x The -x option historically set internal debugging flags.
387
388
389 In some file system implementations, the saving of a .orig file may
390 produce unwanted results. In the case of 12, 13, or 14-character file‐
391 names (on file systems supporting 14-character maximum filenames), the
392 .orig file overwrites the new file. The reject file may also exceed
393 this filename limit. It was suggested, due to some historical practice,
394 that a tilde ( '~' ) suffix be used instead of .orig and some other
395 character instead of the .rej suffix. This was rejected because it is
396 not obvious to the user which file is which. The suffixes .orig and
397 .rej are clearer and more understandable.
398
399 The -b option has the opposite sense in some historical implementa‐
400 tions-do not save the .orig file. The default case here is not to save
401 the files, making patch behave more consistently with the other stan‐
402 dard utilities.
403
404 The -w option in early proposals was changed to -l to match historical
405 practice.
406
407 The -N option was included because without it, a non-interactive appli‐
408 cation cannot reject previously applied patches. For example, if a
409 user is piping the output of diff into the patch utility, and the user
410 only wants to patch a file to a newer version non-interactively, the -N
411 option is required.
412
413 Changes to the -l option description were proposed to allow matching
414 across <newline>s in addition to just <blank>s. Since this is not his‐
415 torical practice, and since some ambiguities could result, it is sug‐
416 gested that future developments in this area utilize another option
417 letter, such as -L.
418
420 None.
421
423 ed, diff
424
426 Portions of this text are reprinted and reproduced in electronic form
427 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
428 -- Portable Operating System Interface (POSIX), The Open Group Base
429 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
430 Electrical and Electronics Engineers, Inc and The Open Group. In the
431 event of any discrepancy between this version and the original IEEE and
432 The Open Group Standard, the original IEEE and The Open Group Standard
433 is the referee document. The original Standard can be obtained online
434 at http://www.opengroup.org/unix/online.html .
435
436
437
438IEEE/The Open Group 2003 PATCH(1P)