1patch(1)                         User Commands                        patch(1)
2
3
4

NAME

6       patch - apply changes to files
7

SYNOPSIS

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

DESCRIPTION

15       The  patch  command  reads  a source (patch) file containing any of the
16       three forms of difference (diff) listings produced by the diff(1)  com‐
17       mand (normal, context or in the style of ed(1)) and apply those differ‐
18       ences to a file.  By default, patch reads from the standard input.
19
20
21       patch attempts to determine the type of the diff listing, unless  over‐
22       ruled by a -c, -e, or -n option.
23
24
25       If the patch file contains more than one patch, patch attempts to apply
26       each of them as if they came from separate patch files. (In  this  case
27       the name of the patch file must be determinable for each diff listing.)
28

OPTIONS

30       The following options are supported:
31
32       -b           Saves  a  copy  of  the original contents of each modified
33                    file, before the differences are applied, in a file of the
34                    same  name  with  the  suffix .orig appended to it. If the
35                    file  already  exists,  it  is  overwritten.  If  multiple
36                    patches  are  applied  to the same file, the .orig file is
37                    written only for the first  patch.  When  the  -o  outfile
38                    option is also specified, file.orig is not created but, if
39                    outfile already exists, outfile.orig is created.
40
41
42       -c           Interprets the patch file as  a  context  difference  (the
43                    output  of  the command diff when the -c or -C options are
44                    specified).
45
46
47       -d dir       Changes the current directory to dir before processing  as
48                    described in EXTENDED DESCRIPTION.
49
50
51       -D define    Marks changes with the C preprocessor construct:
52
53                      #ifdef define
54                      ...
55                      #endif
56
57
58
59
60
61       The option-argument define is used as the differentiating symbol.
62
63       -e               Interprets the patch file as an ed script, rather than
64                        a diff script.
65
66
67       -i patchfile     Reads the patch information from the file named by the
68                        path name patchfile, rather than the standard input.
69
70
71       -l               (The letter ell.) Causes any sequence of blank charac‐
72                        ters in the difference script to match any sequence of
73                        blank  characters  in the input file. Other characters
74                        is matched exactly.
75
76
77       -n               Interprets the script as a normal difference.
78
79
80       -N               Ignores patches where  the  differences  have  already
81                        been  applied to the file; by default, already-applied
82                        patches are rejected.
83
84
85       -o outfile       Instead of modifying the files (specified by the  file
86                        operand or the difference listings) directly, writes a
87                        copy of the file referenced by each  patch,  with  the
88                        appropriate  differences applied, to outfile. Multiple
89                        patches for a single file is applied to the intermedi‐
90                        ate  versions  of  the  file  created  by any previous
91                        patches, and results  in multiple,  concatenated  ver‐
92                        sions of the file being written to outfile.
93
94
95       -p num           For all path names in the patch file that indicate the
96                        names of files to be patched, deletes  num  path  name
97                        components  from  the  beginning of each path name. If
98                        the path name in the patch file is absolute, any lead‐
99                        ing  slashes  are considered the first component (that
100                        is, -p 1 removes the leading slashes). Specifying -p 0
101                        causes  the  full  path  name to be used. If -p is not
102                        specified, only the basename (the final path name com‐
103                        ponent) is used.
104
105
106       -R               Reverses  the  sense  of  the  patch  script. That is,
107                        assumes that the difference script  was  created  from
108                        the new version to the old version. The -R option can‐
109                        not be used with ed scripts. patch attempts to reverse
110                        each   portion  of  the  script  before  applying  it.
111                        Rejected differences is saved in  swapped  format.  If
112                        this  option  is not specified, and until a portion of
113                        the patch file is successfully applied, patch attempts
114                        to apply each portion in its reversed sense as well as
115                        in its normal sense. If the attempt is successful, the
116                        user  is prompted to determine if the -R option should
117                        be set.
118
119
120       -r rejectfile    Overrides the default reject file name. In the default
121                        case,  the reject file has the same name as the output
122                        file, with the suffix .rej appended to it.  See  Patch
123                        Application.
124
125
126       -u               Interprets the patch file as a unified context differ‐
127                        ence, that is, the output of the command diff when the
128                        -u or -U options are specified.
129
130

OPERANDS

132       The following operand is supported:
133
134       file    A path name of a file to patch.
135
136

USAGE

138       The -R option does not work with ed scripts because there is too little
139       information to reconstruct the reverse operation.
140
141
142       The -p option makes it possible to customize a patch file to local user
143       directory structures without manually editing the patch file. For exam‐
144       ple,    if    the    file    name    in    the    patch    file     was
145       /curds/whey/src/blurfl/blurfl.c:
146
147           o      Setting -p 0 gives the entire path name unmodified.
148
149           o      Setting -p 1 gives:
150
151                    curds/whey/src/blurfl/blurfl.c
152
153
154
155           o      Without the leading slash, -p 4 gives:
156
157                    blurfl/blurfl.c
158
159
160
161           o      Not specifying -p at all gives:
162
163                    blurfl.c
164
165
166
167
168       When  using  -b  in  some  file system implementations, the saving of a
169       .orig file might produce unwanted results. In the case of 12-, 13-,  or
170       14-character  file names, on file systems supporting 14-character maxi‐
171       mum file names, the .orig file overwrites the new file.
172

ENVIRONMENT VARIABLES

174       See environ(5) for descriptions of the following environment  variables
175       that affect the execution of patch: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
176       LC_MESSAGES, LC_TIME, and NLSPATH.
177
178
179       Affirmative responses are processed using the extended regular  expres‐
180       sion defined for the yesexpr keyword in the LC_MESSAGES category of the
181       user's locale. The locale specified in the LC_COLLATE category  defines
182       the  behavior  of ranges, equivalence classes, and multi-character col‐
183       lating elements used in the expression defined for yesexpr. The  locale
184       specified  in  LC_CTYPE  determines  the  locale  for interpretation of
185       sequences of bytes of text data a characters, the behavior of character
186       classes used in the expression defined for the yesexpr. See locale(5).
187

OUTPUT FILES

189       The  output  of  patch  the  save files (.orig suffixes) and the reject
190       files (.rej suffixes) are text files.
191

EXTENDED DESCRIPTION

193       A patch file can contain patching instructions for more than one  file.
194       File names are determined as specified in Patch Determination. When the
195       -b option is specified, for each patched file, the original is saved in
196       a file of the same name with the suffix .orig appended to it.
197
198
199       For  each  patched  file, a reject file can also be created as noted in
200       Patch Application. In the absence of an -r option,  the  name  of  this
201       file is formed by appending the suffix .rej to the original file name.
202
203   Patch File Format
204       The  patch  file  must contain zero or more lines of header information
205       followed by one or more patches. Each patch must contain zero  or  more
206       lines  of  file  name identification in the format produced by diff -c,
207       and one or more sets of  diff  output,  which  are  customarily  called
208       hunks.
209
210
211       patch recognizes the following expression in the header information:
212
213       Index:pathname    The file to be patched is named pathname.
214
215
216
217       If  all  lines  (including  headers) within a patch begin with the same
218       leading sequence of  blank  characters,  patch  removes  this  sequence
219       before proceeding. Within each patch, if the type of difference is con‐
220       text, patch recognizes the following expressions:
221
222       *** filename timestamp
223
224           The patches arose from filename.
225
226
227       −−− filename timestamp
228
229           The patches should be applied to filename.
230
231
232
233       Each hunk within a patch must be the diff output to change a line range
234       within  the original file. The line numbers for successive hunks within
235       a patch must occur in ascending order.
236
237   File Name Determination
238       If no file operand is specified, patch performs the following steps  to
239       obtain a path name:
240
241           1.     If  the patch contains the strings *** and −−−, patch strips
242                  components from the beginning of each path  name  (depending
243                  on  the  presence or value of the -p option), then tests for
244                  the existence of both files in  the  current  directory  (or
245                  directory specified with the -d option).
246
247           2.     If  both files exist, patch assumes that no path name can be
248                  obtained from this step. If the header information  contains
249                  a  line with the string Index:, patch strips components from
250                  the beginning of the path name (depending on -p), then tests
251                  for  the existence of this file in the current directory (or
252                  directory specified with the -d option).
253
254           3.     If an SCCS directory exists in the current directory,  patch
255                  attempts  to  perform  a  get  -e SCCS/s.filename command to
256                  retrieve an editable version of the file.
257
258           4.     If no path name can be obtained  by  applying  the  previous
259                  steps,  or  if  the  path names obtained do not exist, patch
260                  writes a prompt to standard output and request a  file  name
261                  interactively from standard input.
262
263   Patch Application
264       If  the  -c, -e, -n, or -u option is present, patch interprets informa‐
265       tion within each hunk as a context difference, an ed difference, a nor‐
266       mal  difference,  or a unified context difference, respectively. In the
267       absence of any of these options, patch determines the type  of  differ‐
268       ence based on the format of information within the hunk.
269
270
271       For  each hunk, patch begins to search for the place to apply the patch
272       at the line number at the beginning of the hunk, plus or minus any off‐
273       set used in applying the previous hunk. If lines matching the hunk con‐
274       text are not found, patch scans both forwards and  backwards  at  least
275       1000 bytes for a set of lines that match the hunk context.
276
277
278       If  no such place is found and it is a context difference, then another
279       scan takes place, ignoring the first and last line of context. If  that
280       fails,  the  first  two  and  last  two lines of context is ignored and
281       another scan is made. Implementations can search more  extensively  for
282       installation locations.
283
284
285       If no location can be found, patch appends the hunk to the reject file.
286       The rejected hunk is written in context-difference format regardless of
287       the  format  of  the patch file. If the input was a normal or ed -style
288       difference, the reject file can contain differences with zero lines  of
289       context.  The  line numbers on the hunks in the reject file can be dif‐
290       ferent from the line numbers in the patch file since they  reflect  the
291       approximate  locations for the failed hunks in the new file rather than
292       the old one.
293
294
295       If the type of patch is an ed diff, the implementation  can  accomplish
296       the patching by invoking the ed command.
297

EXIT STATUS

299       The following exit values are returned:
300
301       0     Successful completion.
302
303
304       1     One or more lines were written to a reject file.
305
306
307       >1    An error occurred.
308
309

ATTRIBUTES

311       See attributes(5) for descriptions of the following attributes:
312
313
314
315
316       ┌─────────────────────────────┬─────────────────────────────┐
317       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
318       ├─────────────────────────────┼─────────────────────────────┤
319       │Availability                 │SUNWcsu                      │
320       ├─────────────────────────────┼─────────────────────────────┤
321       │Interface Stability          │Standard                     │
322       └─────────────────────────────┴─────────────────────────────┘
323

SEE ALSO

325       ed(1), diff(1), attributes(5), environ(5), standards(5)
326
327
328
329SunOS 5.11                        17 Jul 2007                         patch(1)
Impressum