DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

patch(C)


patch -- apply changes to a file

Syntax

patch [-blNR] [-c|-e|-n] [-d dir] [-D symbol] [-i patch] [-o out] [-p n]
[-r reject] [file]

Description

patch applies changes in the style of diff(C) to a file. The file to be patched may be specified on the command line or within the list of changes in a patch file. By default, patch reads the changes to be made from the standard input.

patch attempts to determine the type of diff listing (normal, context, or ed-style). You can overrule this behavior using the -c, -e, or -n options.

patch tries to apply several patches in one patch file as though they come from different patch files; patch must be able to find out the name of the patch file from each diff listing.

patch accepts the following options:


-b
Saves a (backup) copy of the original contents of each modified file in a file with the same name as the original plus the suffix .orig. If multiple patches are applied to the same file, the backup file is written for the first patch only. patch creates a backup file out.orig if an output file out that already exists is also specified using the -o option.

-c
Interprets a patch as a context diff (the output from diff(C) when the -c or -C option is specified).

-d dir
Changes directory to dir before processing.

-D symbol
Marks changes within the C conditional compilation construct:
#ifdef symbol
#endif

-e
Interprets a patch as an ed(C) script.

-i patch
Reads patch information from the file patch rather than from the standard input.

-l
Matches any sequence of whitespace characters in the diff file with any sequence in the input file.

-n
Interprets the patch as normal diff output.

-N
Ignores patches where the differences have already been applied; the default behavior is to reject such patches.

-o out
Directs the output from each applied patch to the file out. Multiple patches are concatenated within the file. Does not modify the output file specified on the command line or in the patch file.

-p n
Deletes n leading components from the names of all files to be patched (leading slashes are counted as one component; subsequent slashes are not). For example, -p 1 removes the leading / from absolute pathnames. -p 0 implies the full pathname is to be used. The default is to use the basename (the final pathname) component only.

-r reject
Defines a new name for the reject file (see the -R option).

-R
Reverses the sense of the patch script; this may be used in cases when a diff script was created that shows the changes to generate an older version of a file from a newer version. patch attempts to reverse the sense of each portion of the script before using it. Those differences that it rejects are saved in swapped format in the reject file; this file has the same name as each output file with the suffix .rej.

If this option is not specified, patch first tries to apply the script in both the reverse and normal sense. If the normal sense succeeds, only this sense is applied subsequently. If it fails, and the reverse sense succeeds, patch prompts you to ask if you wish it to continue applying changes in the reverse sense.

This option does not work with ed style patch scripts.

Exit values

patch returns the following values:

0
Successful completion.

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

>1
An error occurred.

Examples

The usual way to invoke patch is to redirect the standard input of patch to read a patch file:

patch < patchfile

The -p option allows you to customize a patch to your local directory structure without editing the patch file. If the pathname in the patch file plaster is /usr/local/example/file.c and you wished to patch file.c in the current directory:

patch -p 4 < plaster

or more simply:

patch < plaster

To patch /u/local/example/file.c:

cd /u
patch -p 2 < plaster

Limitations

The -R option cannot be used with ed scripts since they do not contain enough information to perform the reverse operation.

The -b option may cause the patched file to be overwritten for filesystems where the maximum filename length is not long enough to append the .orig suffix.

Copyright © 1986 Larry Wall.
All rights reserved.

THE SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

lwall@netlabs.com

See also

diff(C), ed(C)

Standards conformance

patch is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005