diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2003-10-31 20:20:46 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2003-10-31 20:20:46 +0000 |
commit | d9a96b2321d13ba3cae279a85bdfb1d0ad7690fe (patch) | |
tree | 70ffd476551e5c88bb8506940fe6ae9b551e4f8f /usr.bin/patch | |
parent | 927e2e8868b02d49448255f5a876e1dff8acb5b8 (diff) |
o fairly major man page reorganization
o add --posix option for strict POSIX conformance
o change filename determination to match GNU patch
Tested by various people, including a full ports build by naddy@
Diffstat (limited to 'usr.bin/patch')
-rw-r--r-- | usr.bin/patch/common.h | 3 | ||||
-rw-r--r-- | usr.bin/patch/patch.1 | 413 | ||||
-rw-r--r-- | usr.bin/patch/patch.c | 21 | ||||
-rw-r--r-- | usr.bin/patch/pch.c | 246 | ||||
-rw-r--r-- | usr.bin/patch/pch.h | 12 | ||||
-rw-r--r-- | usr.bin/patch/util.c | 51 | ||||
-rw-r--r-- | usr.bin/patch/util.h | 5 |
7 files changed, 495 insertions, 256 deletions
diff --git a/usr.bin/patch/common.h b/usr.bin/patch/common.h index 7c5fa439a12..4101a575747 100644 --- a/usr.bin/patch/common.h +++ b/usr.bin/patch/common.h @@ -1,4 +1,4 @@ -/* $OpenBSD: common.h,v 1.24 2003/08/15 08:00:51 otto Exp $ */ +/* $OpenBSD: common.h,v 1.25 2003/10/31 20:20:45 millert Exp $ */ /* * patch - a program to apply diffs to original files @@ -110,4 +110,5 @@ extern int diff_type; extern char *revision; /* prerequisite revision, if any */ extern LINENUM input_lines; /* how long is input file in lines */ +extern int posix; diff --git a/usr.bin/patch/patch.1 b/usr.bin/patch/patch.1 index 082443d87a9..9d12b8b12bd 100644 --- a/usr.bin/patch/patch.1 +++ b/usr.bin/patch/patch.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: patch.1,v 1.16 2003/07/31 21:07:35 millert Exp $ +.\" $OpenBSD: patch.1,v 1.17 2003/10/31 20:20:45 millert Exp $ .\" Copyright 1986, Larry Wall .\" .\" Redistribution and use in source and binary forms, with or without @@ -38,40 +38,12 @@ listing produced by the .Xr diff 1 program and apply those differences to an original file, producing a patched version. -By default, the patched version is put in place of the original, with -the original file backed up to the same name with the extension -.Qq .orig , -or as specified by the -.Fl B , -.Fl V , -or -.Fl z -options. -The extension used for making backup files may also be specified in the -.Ev SIMPLE_BACKUP_SUFFIX -environment variable, which is overridden by the options above. -.Pp -If the backup file is linked to the original file, -.Nm -creates a new backup file name by changing the first lowercase letter -in the last component of the file's name into uppercase. -If there are no more lowercase letters in the name, -it removes the first character from the name. -It repeats this process until it comes up with a -backup file that does not already exist or is not linked to the original file. -.Pp -You may also specify where you want the output to go with the -.Fl o -option; if that file already exists, it is backed up first. -.Pp If .Ar patchfile -is omitted, or is a hyphen, the patch will be read from standard input. +is omitted, or is a hyphen, the patch will be read from the standard input. .Pp -Upon startup, .Nm -will attempt to determine the type of the diff listing, -unless over-ruled by a +will attempt to determine the type of the diff listing, unless over-ruled by a .Fl c , .Fl e , .Fl n , @@ -79,116 +51,23 @@ or .Fl u option. Context diffs (old-style, new-style, and unified) and -normal diffs are applied by the +normal diffs are applied directly by the .Nm -program itself, while ed diffs are simply fed to the +program itself, whereas ed diffs are simply fed to the .Xr ed 1 editor via a pipe. .Pp -.Nm -will try to skip any leading garbage, apply the diff, -and then skip any trailing garbage. -Thus you could feed an article or message containing a -diff listing to -.Nm patch , -and it should work. -If the entire diff is indented by a consistent amount, -this will be taken into account. -.Pp -With context diffs, and to a lesser extent with normal diffs, -.Nm -can detect when the line numbers mentioned in the patch are incorrect, -and will attempt to find the correct place to apply each hunk of the patch. -As a first guess, it takes the line number mentioned for the hunk, plus or -minus any offset used in applying the previous hunk. -If that is not the correct place, -.Nm -will scan both forwards and backwards for a set of lines matching the context -given in the hunk. -First -.Nm -looks for a place where all lines of the context match. -If no such place is found, and it's a context diff, and the maximum fuzz factor -is set to 1 or more, then another scan takes place ignoring the first and last -line of context. -If that fails, and the maximum fuzz factor is set to 2 or more, -the first two and last two lines of context are ignored, -and another scan is made. -.Pq The default maximum fuzz factor is 2. -.Pp -If -.Nm -cannot find a place to install that hunk of the patch, it will put the hunk -out to a reject file, which normally is the name of the output file plus -.Qq .rej . -(Note that the rejected hunk will come out in context diff form whether the -input patch was a context diff or a normal diff. -If the input was a normal diff, many of the contexts will simply be null.) -The line numbers on the hunks in the reject file may be different than -in the patch file: they reflect the approximate location patch thinks the -failed hunks belong in the new file rather than the old one. -.Pp -As each hunk is completed, you will be told whether the hunk succeeded or -failed, and which line (in the new file) -.Nm -thought the hunk should go on. -If this is different from the line number specified in the diff, -you will be told the offset. -A single large offset MAY be an indication that a hunk was installed in the -wrong place. -You will also be told if a fuzz factor was used to make the match, in which -case you should also be slightly suspicious. -.Pp -If no original file is specified on the command line, -.Nm -will try to figure out from the leading garbage what the name of the file -to edit is. -In the header of a context diff, the filename is found from lines beginning -with -.Qq *** -or -.Qq --- , -with the shortest name of an existing file winning. -Only context diffs have lines like that, but if there is an -.Qq Index: -line in the leading garbage, -.Nm -will try to use the filename from that line. -The context diff header takes precedence over an Index line. -If no filename can be intuited from the leading garbage, you will be asked -for the name of the file to patch. -.Pp -If the original file cannot be found or is read-only, but a suitable -SCCS or RCS file is handy, -.Nm -will attempt to get or check out the file. -.Pp -Additionally, if the leading garbage contains a -.Qq Prereq:\ \& -line, -.Nm -will take the first word from the prerequisites line (normally a version -number) and check the input file to see if that word can be found. -If not, -.Nm -will ask for confirmation before proceeding. -.Pp -The upshot of all this is that you should be able to say, while in a news -interface, the following: -.Pp -.Dl | patch -d /usr/src/local/blurfl -.Pp -and patch a file in the blurfl directory directly from the article containing -the patch. -.Pp -If the patch file contains more than one patch, +If the +.Ar patchfile +contains more than one patch, .Nm will try to apply each of them as if they came from separate patch files. This means, among other things, that it is assumed that the name of the file -to patch must be determined for each diff listing, -and that the garbage before each diff listing will -be examined for interesting things such as filenames and revision level, as -mentioned previously. +to patch must be determined for each diff listing, and that the garbage before +each diff listing will be examined for interesting things such as file names +and revision level (see the section on +.Sx Filename Determination +below). .Pp The options are as follows: .Bl -tag -width Ds @@ -204,7 +83,7 @@ This option is currently the default but that will change in a future release. .It Fl B , Fl Fl prefix Causes the next argument to be interpreted as a prefix to the backup file name. -If this argument is specified, any argument from +If this argument is specified, any argument to .Fl z will be ignored. .It Fl c , Fl Fl context @@ -239,6 +118,7 @@ script. Causes .Nm to remove output files that are empty after the patches have been applied. +This option is useful when applying patches that create or remove files. .It Fl f , Fl Fl force Forces .Nm @@ -253,16 +133,6 @@ and assume that patches are not reversed even if they look like they are. This option does not suppress commentary; use .Fl s for that. -.It Fl t , Fl Fl batch -Similar to -.Fl f , -in that it suppresses questions, but makes some different assumptions: -skip patches for which a file to patch can't be found (the same as -.Fl f ) ; -skip patches for which the file has the wrong version for the -.Qq Prereq: -line in the patch; -and assume that patches are reversed if they look like they are. .It Xo .Fl F Ns Aq Ar number , .Fl Fl fuzz Aq Ar number @@ -308,7 +178,7 @@ out the patch. The strip count specifies how many slashes are to be stripped from the front of the pathname. (Any intervening directory names also go away.) -For example, supposing the filename in the patch file was +For example, supposing the file name in the patch file was .Pa /u/howard/src/blurfl/blurfl.c : .Pp Setting @@ -376,6 +246,16 @@ the heuristic.) Makes .Nm do its work silently, unless an error occurs. +.It Fl t , Fl Fl batch +Similar to +.Fl f , +in that it suppresses questions, but makes some different assumptions: +skip patches for which a file to patch can't be found (the same as +.Fl f ) ; +skip patches for which the file has the wrong version for the +.Qq Prereq: +line in the patch; +and assume that patches are reversed if they look like they are. .It Fl u , Fl Fl unified Forces .Nm @@ -388,15 +268,19 @@ to print out its revision header and patch level. Causes the next argument to be interpreted as a method for creating backup file names. The type of backups made can also be given in the +.Ev PATCH_VERSION_CONTROL +or .Ev VERSION_CONTROL -environment variable, which is overridden by this option. +environment variables, which are overridden by this option. The .Fl B option overrides this option, causing the prefix to always be used for making backup file names. -The value of the +The values of the +.Ev PATCH_VERSION_CONTROL +and .Ev VERSION_CONTROL -environment variable and the argument to the +environment variables and the argument to the .Fl V option are like the GNU Emacs .Dq version-control @@ -422,8 +306,202 @@ patchers. Causes the next argument to be interpreted as the backup extension, to be used in place of .Qq .orig . +.It Fl Fl posix +Enables strict +.St -p1003.2 +conformance, specifically: +.Bl -enum +.It +Backup files are not created unless the +.Fl b +option is specified. +.It +If unspecified, the file name used is the first of the old, new and +index files that exists. +.El +.El +.Ss Patch Application +.Nm +will try to skip any leading garbage, apply the diff, +and then skip any trailing garbage. +Thus you could feed an article or message containing a +diff listing to +.Nm patch , +and it should work. +If the entire diff is indented by a consistent amount, +this will be taken into account. +.Pp +With context diffs, and to a lesser extent with normal diffs, +.Nm +can detect when the line numbers mentioned in the patch are incorrect, +and will attempt to find the correct place to apply each hunk of the patch. +As a first guess, it takes the line number mentioned for the hunk, plus or +minus any offset used in applying the previous hunk. +If that is not the correct place, +.Nm +will scan both forwards and backwards for a set of lines matching the context +given in the hunk. +First +.Nm +looks for a place where all lines of the context match. +If no such place is found, and it's a context diff, and the maximum fuzz factor +is set to 1 or more, then another scan takes place ignoring the first and last +line of context. +If that fails, and the maximum fuzz factor is set to 2 or more, +the first two and last two lines of context are ignored, +and another scan is made. +.Pq The default maximum fuzz factor is 2. +.Pp +If +.Nm +cannot find a place to install that hunk of the patch, it will put the hunk +out to a reject file, which normally is the name of the output file plus +.Qq .rej . +(Note that the rejected hunk will come out in context diff form whether the +input patch was a context diff or a normal diff. +If the input was a normal diff, many of the contexts will simply be null.) +The line numbers on the hunks in the reject file may be different than +in the patch file: they reflect the approximate location patch thinks the +failed hunks belong in the new file rather than the old one. +.Pp +As each hunk is completed, you will be told whether the hunk succeeded or +failed, and which line (in the new file) +.Nm +thought the hunk should go on. +If this is different from the line number specified in the diff, +you will be told the offset. +A single large offset MAY be an indication that a hunk was installed in the +wrong place. +You will also be told if a fuzz factor was used to make the match, in which +case you should also be slightly suspicious. +.Ss Filename Determination +If no original file is specified on the command line, +.Nm +will try to figure out from the leading garbage what the name of the file +to edit is. +When checking a prospective file name, pathname components are stripped +as specified by the +.Fl p +option and the file's existence and writability are checked relative +to the current working directory (or the directory specified by the +.Fl d +option). +.Pp +If the diff is a context or unified diff, +.Nm +is able to determine the old and new file names from the diff header. +For context diffs, the +.Dq old +file is specified in the line beginning with +.Qq *** +and the +.Dq new +file is specified in the line beginning with +.Qq --- . +For a unified diff, the +.Dq old +file is specified in the line beginning with +.Qq --- +and the +.Dq new +file is specified in the line beginning with +.Qq +++ . +If there is an +.Qq Index: +line in the leading garbage (regardless of the diff type), +.Nm +will use the file name from that line as the +.Dq index +file. +.Pp +.Nm +will choose the file name by performing the following steps, with the first +match used: +.Bl -enum +.It +If +.Nm +is operating in strict +.St -p1003.2 +mode, the first of the +.Dq old , +.Dq new +and +.Dq index +file names that exist is used. +Otherwise, +.Nm +will examine either the +.Dq old +and +.Dq new +file names or, for a non-context diff, the +.Dq index +file name, and choose the file name with the fewest path components, +the shortest basename, and the shortest total file name length (in that order). +.It +If no file exists, +.Nm +checks for the existence of the files in an SCCS or RCS directory +(using the appropriate prefix or suffix) using the criteria specified +above. +If found, +.Nm +will attempt to get or check out the file. +.It +If no suitable file was found to patch, the patch file is a context or +unified diff, and the old file was zero length, the new file name is +created and used. +.It +If the file name still cannot be determined, +.Nm +will prompt the user for the file name to use. .El -.Sh NOTES FOR PATCH SENDERS +.Pp +Additionally, if the leading garbage contains a +.Qq Prereq:\ \& +line, +.Nm +will take the first word from the prerequisites line (normally a version +number) and check the input file to see if that word can be found. +If not, +.Nm +will ask for confirmation before proceeding. +.Pp +The upshot of all this is that you should be able to say, while in a news +interface, the following: +.Pp +.Dl | patch -d /usr/src/local/blurfl +.Pp +and patch a file in the blurfl directory directly from the article containing +the patch. +.Ss Backup Files +By default, the patched version is put in place of the original, with +the original file backed up to the same name with the extension +.Qq .orig , +or as specified by the +.Fl B , +.Fl V , +or +.Fl z +options. +The extension used for making backup files may also be specified in the +.Ev SIMPLE_BACKUP_SUFFIX +environment variable, which is overridden by the options above. +.Pp +If the backup file is a symbolic or hard link to the original file, +.Nm +creates a new backup file name by changing the first lowercase letter +in the last component of the file's name into uppercase. +If there are no more lowercase letters in the name, +it removes the first character from the name. +It repeats this process until it comes up with a +backup file that does not already exist or is not linked to the original file. +.Pp +You may also specify where you want the output to go with the +.Fl o +option; if that file already exists, it is backed up first. +.Ss Notes For Patch Senders There are several things you should bear in mind if you are going to be sending out patches: .Pp @@ -436,7 +514,7 @@ If you put a line in with the patch, it won't let them apply patches out of order without some warning. .Pp -Second, make sure you've specified the filenames right, either in a +Second, make sure you've specified the file names right, either in a context diff header, or with an .Qq Index: line. @@ -457,19 +535,34 @@ Fifth, while you may be able to get away with putting 582 diff listings into one file, it is probably wiser to group related patches into separate files in case something goes haywire. .Sh ENVIRONMENT -.Bl -tag -width "SIMPLE_BACKUP_SUFFIX" -compact -.It Ev TMPDIR -Directory to put temporary files in; default is -.Pa /tmp . +.Bl -tag -width "PATCH_VERSION_CONTROL" -compact +.It Ev POSIXLY_CORRECT +When set, +.Nm +behaves as if the +.Fl Fl posix +option has been specified. .It Ev SIMPLE_BACKUP_SUFFIX Extension to use for backup file names instead of .Qq .orig . -.It Ev VERSION_CONTROL +.It Ev TMPDIR +Directory to put temporary files in; default is +.Pa /tmp . +.It Ev PATCH_VERSION_CONTROL Selects when numbered backup files are made. +.It Ev VERSION_CONTROL +Same as +.Ev PATCH_VERSION_CONTROL . .El .Sh FILES -.Bl -tag -width Ds +.Bl -tag -width "$TMPDIR/patch*" -compact .It Pa $TMPDIR/patch* +.Nm +temporary files +.It Pa /dev/tty +used to read input when +.Nm +prompts the user .El .Sh DIAGNOSTICS Too many to list here, but generally indicative that @@ -531,10 +624,10 @@ Check patch mode .Pq Fl C will fail if you try to check several patches in succession that build on each other. -The whole code of +The entire .Nm -would have to be restructured to keep temporary files around so that it can -handle this situation. +code would have to be restructured to keep temporary files around so that it +can handle this situation. .Pp If code has been duplicated (for instance with #ifdef OLDCODE ... #else ... #endif), diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c index 97e461f75c3..a7bfa36960f 100644 --- a/usr.bin/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: patch.c,v 1.37 2003/08/10 21:28:48 otto Exp $ */ +/* $OpenBSD: patch.c,v 1.38 2003/10/31 20:20:45 millert Exp $ */ /* * patch - a program to apply diffs to original files @@ -27,7 +27,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: patch.c,v 1.37 2003/08/10 21:28:48 otto Exp $"; +static const char rcsid[] = "$OpenBSD: patch.c,v 1.38 2003/10/31 20:20:45 millert Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -86,6 +86,7 @@ bool check_only = false; int diff_type = 0; char *revision = NULL; /* prerequisite revision, if any */ LINENUM input_lines = 0; /* how long is input file in lines */ +int posix = 0; /* strict POSIX mode? */ static void reinitialize_almost_everything(void); static void get_some_switches(void); @@ -196,10 +197,9 @@ main(int argc, char *argv[]) get_some_switches(); if (backup_type == none) { - v = getenv("VERSION_CONTROL"); -#ifdef notyet - if (v != NULL) -#endif + if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL) + v = getenv("VERSION_CONTROL"); + if (v != NULL || !posix) backup_type = get_version(v); /* OK to pass NULL. */ } @@ -465,6 +465,7 @@ get_some_switches(void) {"unified", no_argument, 0, 'u'}, {"version", no_argument, 0, 'v'}, {"version-control", required_argument, 0, 'V'}, + {"posix", no_argument, &posix, 1}, {NULL, 0, 0, 0} }; int ch; @@ -478,10 +479,8 @@ get_some_switches(void) while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) { switch (ch) { case 'b': -#ifdef notyet if (backup_type == none) backup_type = numbered_existing; -#endif if (optarg == NULL) break; if (verbose) @@ -578,7 +577,8 @@ get_some_switches(void) break; #endif default: - usage(); + if (ch != '\0') + usage(); break; } } @@ -595,6 +595,9 @@ get_some_switches(void) Argc--; } } + + if (getenv("POSIXLY_CORRECT") != NULL) + posix = 1; } static __dead void diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 1da80fa07f8..7227d466499 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pch.c,v 1.31 2003/09/28 07:55:19 otto Exp $ */ +/* $OpenBSD: pch.c,v 1.32 2003/10/31 20:20:45 millert Exp $ */ /* * patch - a program to apply diffs to original files @@ -27,13 +27,15 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: pch.c,v 1.31 2003/09/28 07:55:19 otto Exp $"; +static const char rcsid[] = "$OpenBSD: pch.c,v 1.32 2003/10/31 20:20:45 millert Exp $"; #endif /* not lint */ #include <sys/types.h> #include <sys/stat.h> #include <ctype.h> +#include <libgen.h> +#include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -75,7 +77,9 @@ static int intuit_diff_type(void); static void next_intuit_at(LINENUM, LINENUM); static void skip_to(LINENUM, LINENUM); static char *pgets(char *, int, FILE *); - +static char *best_name(const struct file_name *, bool); +static char *posix_name(const struct file_name *, bool); +static size_t num_components(const char *); /* * Prepare to look for the next patch in the patch file. @@ -180,6 +184,8 @@ grow_hunkmax(void) bool there_is_another_patch(void) { + bool exists = false; + if (p_base != 0L && p_base >= p_filesize) { if (verbose) say("done\n"); @@ -219,15 +225,16 @@ there_is_another_patch(void) if (*buf != '\n') { free(bestguess); bestguess = savestr(buf); - filearg[0] = fetchname(buf, 0, false); + filearg[0] = fetchname(buf, &exists, 0); } - if (filearg[0] == NULL) { + if (!exists) { + free(filearg[0]); ask("No file found--skip this patch? [n] "); if (*buf != 'y') continue; if (verbose) say("Skipping patch...\n"); - filearg[0] = fetchname(bestguess, 0, true); + filearg[0] = fetchname(bestguess, &exists, 0); skip_rest_of_patch = true; return true; } @@ -246,15 +253,10 @@ intuit_diff_type(void) bool last_line_was_command = false, this_is_a_command = false; bool stars_last_line = false, stars_this_line = false; char *s, *t; - char *indtmp = NULL; - char *oldtmp = NULL; - char *newtmp = NULL; - char *indname = NULL; - char *oldname = NULL; - char *newname = NULL; int indent, retval; - bool no_filearg = (filearg[0] == NULL); + struct file_name names[MAX_FILE]; + memset(names, 0, sizeof(names)); ok_to_create_file = false; fseek(pfp, p_base, SEEK_SET); p_input_line = p_bline - 1; @@ -295,13 +297,18 @@ intuit_diff_type(void) p_indent = indent; /* assume this for now */ } if (!stars_last_line && strnEQ(s, "*** ", 4)) - oldtmp = savestr(s + 4); + names[OLD_FILE].path = fetchname(s + 4, + &names[OLD_FILE].exists, strippath); else if (strnEQ(s, "--- ", 4)) - newtmp = savestr(s + 4); + names[NEW_FILE].path = fetchname(s + 4, + &names[NEW_FILE].exists, strippath); else if (strnEQ(s, "+++ ", 4)) - oldtmp = savestr(s + 4); /* pretend it is the old name */ + /* pretend it is the old name */ + names[OLD_FILE].path = fetchname(s + 4, + &names[OLD_FILE].exists, strippath); else if (strnEQ(s, "Index:", 6)) - indtmp = savestr(s + 6); + names[INDEX_FILE].path = fetchname(s + 6, + &names[INDEX_FILE].exists, strippath); else if (strnEQ(s, "Prereq:", 7)) { for (t = s + 7; isspace(*t); t++) ; @@ -324,7 +331,7 @@ intuit_diff_type(void) goto scan_exit; } if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) { - if (!atol(s + 3)) + if (strnEQ(s + 4, "0,0", 3)) ok_to_create_file = true; p_indent = indent; p_start = this_line; @@ -335,13 +342,12 @@ intuit_diff_type(void) stars_this_line = strnEQ(s, "********", 8); if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && strnEQ(s, "*** ", 4)) { - if (!atol(s + 4)) + if (atol(s + 4) == 0) ok_to_create_file = true; /* - * if this is a new context diff the character just - * before + * If this is a new context diff the character just + * before the newline is a '*'. */ - /* the newline is a '*'. */ while (*s != '\n') s++; p_indent = indent; @@ -361,54 +367,44 @@ intuit_diff_type(void) } } scan_exit: - if (no_filearg) { - if (indtmp != NULL) - indname = fetchname(indtmp, strippath, ok_to_create_file); - if (oldtmp != NULL) - oldname = fetchname(oldtmp, strippath, ok_to_create_file); - if (newtmp != NULL) - newname = fetchname(newtmp, strippath, ok_to_create_file); - if (indname) - filearg[0] = savestr(indname); - else if (oldname && newname) { - if (strlen(oldname) < strlen(newname)) - filearg[0] = savestr(oldname); - else - filearg[0] = savestr(newname); - } else if (oldname) - filearg[0] = savestr(oldname); - else if (newname) - filearg[0] = savestr(newname); + if (retval == UNI_DIFF) { + /* unswap old and new */ + struct file_name tmp = names[OLD_FILE]; + names[OLD_FILE] = names[NEW_FILE]; + names[NEW_FILE] = tmp; + } + if (filearg[0] == NULL) { + if (posix) + filearg[0] = posix_name(names, ok_to_create_file); + else { + /* Ignore the Index: name for context diffs, like GNU */ + if (names[OLD_FILE].path != NULL || + names[NEW_FILE].path != NULL) { + free(names[INDEX_FILE].path); + names[INDEX_FILE].path = NULL; + } + filearg[0] = best_name(names, ok_to_create_file); + } } free(bestguess); bestguess = NULL; - if (filearg[0] != NULL) bestguess = savestr(filearg[0]); - else if (indtmp != NULL) - bestguess = fetchname(indtmp, strippath, true); - else { - if (oldtmp != NULL) - oldname = fetchname(oldtmp, strippath, true); - if (newtmp != NULL) - newname = fetchname(newtmp, strippath, true); - if (oldname && newname) { - if (strlen(oldname) < strlen(newname)) - bestguess = savestr(oldname); - else - bestguess = savestr(newname); - } else if (oldname) - bestguess = savestr(oldname); - else if (newname) - bestguess = savestr(newname); + else if (!ok_to_create_file) { + /* + * We don't want to create a new file but we need a + * filename to set bestguess. Avoid setting filearg[0] + * so the file is not created automatically. + */ + if (posix) + bestguess = posix_name(names, true); + else + bestguess = best_name(names, true); } - free(indtmp); - free(oldtmp); - free(newtmp); - free(indname); - free(oldname); - free(newname); + free(names[OLD_FILE].path); + free(names[NEW_FILE].path); + free(names[INDEX_FILE].path); return retval; } @@ -1421,3 +1417,127 @@ do_ed_script(void) } set_signals(1); } + +/* + * Choose the name of the file to be patched based on POSIX rules. + * NOTE: the POSIX rules are amazingly stupid and we only follow them + * if the user specified --posix or set POSIXLY_CORRECT. + */ +static char * +posix_name(const struct file_name *names, bool assume_exists) +{ + char *path = NULL; + int i; + + /* + * POSIX states that the filename will be chosen from one + * of the old, new and index names (in that order) if + * the file exists relative to CWD after -p stripping. + */ + for (i = 0; i < MAX_FILE; i++) { + if (names[i].path != NULL && names[i].exists) { + path = names[i].path; + break; + } + } + if (path == NULL && !assume_exists) { + /* + * No files found, look for something we can checkout from + * RCS/SCCS dirs. Same order as above. + */ + for (i = 0; i < MAX_FILE; i++) { + if (names[i].path != NULL && + (path = checked_in(names[i].path)) != NULL) + break; + } + /* + * Still no match? Check to see if the diff could be creating + * a new file. + */ + if (path == NULL && ok_to_create_file && + names[NEW_FILE].path != NULL) + path = names[NEW_FILE].path; + } + + return path ? savestr(path) : NULL; +} + +/* + * Choose the name of the file to be patched based the "best" one + * available. + */ +static char * +best_name(const struct file_name *names, bool assume_exists) +{ + size_t min_components, min_baselen, min_len, tmp; + char *best = NULL; + int i; + + /* + * The "best" name is the one with the fewest number of path + * components, the shortest basename length, and the shortest + * overall length (in that order). We only use the Index: file + * if neither of the old or new files could be intuited from + * the diff header. + */ + min_components = min_baselen = min_len = SIZE_MAX; + for (i = INDEX_FILE; i >= OLD_FILE; i--) { + if (names[i].path == NULL || + (!names[i].exists && !assume_exists)) + continue; + if ((tmp = num_components(names[i].path)) > min_components) + continue; + min_components = tmp; + if ((tmp = strlen(basename(names[i].path))) > min_baselen) + continue; + min_baselen = tmp; + if ((tmp = strlen(names[i].path)) > min_len) + continue; + min_len = tmp; + best = names[i].path; + } + if (best == NULL) { + /* + * No files found, look for something we can checkout from + * RCS/SCCS dirs. Logic is identical to that above... + */ + min_components = min_baselen = min_len = SIZE_MAX; + for (i = INDEX_FILE; i >= OLD_FILE; i--) { + if (names[i].path == NULL || + checked_in(names[i].path) == NULL) + continue; + if ((tmp = num_components(names[i].path)) > min_components) + continue; + min_components = tmp; + if ((tmp = strlen(basename(names[i].path))) > min_baselen) + continue; + min_baselen = tmp; + if ((tmp = strlen(names[i].path)) > min_len) + continue; + min_len = tmp; + best = names[i].path; + } + /* + * Still no match? Check to see if the diff could be creating + * a new file. + */ + if (best == NULL && ok_to_create_file && + names[NEW_FILE].path != NULL) + best = names[NEW_FILE].path; + } + + return best ? savestr(best) : NULL; +} + +static size_t +num_components(const char *path) +{ + size_t n; + const char *cp; + + for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) { + while (*cp == '/') + cp++; /* skip consecutive slashes */ + } + return n; +} diff --git a/usr.bin/patch/pch.h b/usr.bin/patch/pch.h index d8b9fa73c3d..b7bf8e8a224 100644 --- a/usr.bin/patch/pch.h +++ b/usr.bin/patch/pch.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pch.h,v 1.8 2003/08/15 08:00:51 otto Exp $ */ +/* $OpenBSD: pch.h,v 1.9 2003/10/31 20:20:45 millert Exp $ */ /* * patch - a program to apply diffs to original files @@ -26,6 +26,16 @@ * behaviour */ +#define OLD_FILE 0 +#define NEW_FILE 1 +#define INDEX_FILE 2 +#define MAX_FILE 3 + +struct file_name { + char *path; + bool exists; +}; + void re_patch(void); void open_patch_file(const char *); void set_hunkmax(void); diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c index 31b18635394..23a22b17078 100644 --- a/usr.bin/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.26 2003/08/15 08:00:51 otto Exp $ */ +/* $OpenBSD: util.c,v 1.27 2003/10/31 20:20:45 millert Exp $ */ /* * patch - a program to apply diffs to original files @@ -27,7 +27,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: util.c,v 1.26 2003/08/15 08:00:51 otto Exp $"; +static const char rcsid[] = "$OpenBSD: util.c,v 1.27 2003/10/31 20:20:45 millert Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -330,9 +330,9 @@ makedirs(const char *filename, bool striplast) * Make filenames more reasonable. */ char * -fetchname(const char *at, int strip_leading, int assume_exists) +fetchname(const char *at, bool *exists, int strip_leading) { - char *fullname, *name, *t, tmpbuf[200]; + char *fullname, *name, *t; int sleading; struct stat filestat; @@ -342,7 +342,7 @@ fetchname(const char *at, int strip_leading, int assume_exists) at++; #ifdef DEBUGGING if (debug & 128) - say("fetchname %s %d %d\n", at, strip_leading, assume_exists); + say("fetchname %s %d\n", at, strip_leading); #endif /* So files can be created by diffing against /dev/null. */ if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1)) @@ -372,23 +372,34 @@ fetchname(const char *at, int strip_leading, int assume_exists) name = savestr(name); free(fullname); - if (stat(name, &filestat) && !assume_exists) { - char *filebase = basename(name); - char *filedir = dirname(name); + *exists = stat(name, &filestat) == 0; + return name; +} + +/* + * Takes the name returned by fetchname and looks in RCS/SCCS directories + * for a checked in version. + */ +char * +checked_in(char *file) +{ + char *filebase, *filedir, tmpbuf[MAXPATHLEN]; + struct stat filestat; + + filebase = basename(file); + filedir = dirname(file); #define try(f, a1, a2, a3) \ - (snprintf(tmpbuf, sizeof tmpbuf, f, a1, a2, a3), stat(tmpbuf, &filestat) == 0) - - if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) || - try("%s/RCS/%s%s", filedir, filebase, "") || - try("%s/%s%s", filedir, filebase, RCSSUFFIX) || - try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) || - try("%s/%s%s", filedir, SCCSPREFIX, filebase)) - return name; - free(name); - name = NULL; - } - return name; +(snprintf(tmpbuf, sizeof tmpbuf, f, a1, a2, a3), stat(tmpbuf, &filestat) == 0) + + if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) || + try("%s/RCS/%s%s", filedir, filebase, "") || + try("%s/%s%s", filedir, filebase, RCSSUFFIX) || + try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) || + try("%s/%s%s", filedir, SCCSPREFIX, filebase)) + return file; + + return NULL; } void diff --git a/usr.bin/patch/util.h b/usr.bin/patch/util.h index e2f480f6c49..0ec187028b9 100644 --- a/usr.bin/patch/util.h +++ b/usr.bin/patch/util.h @@ -1,4 +1,4 @@ -/* $OpenBSD: util.h,v 1.11 2003/08/15 08:00:51 otto Exp $ */ +/* $OpenBSD: util.h,v 1.12 2003/10/31 20:20:45 millert Exp $ */ /* * patch - a program to apply diffs to original files @@ -26,7 +26,8 @@ * behaviour */ -char *fetchname(const char *, int, int); +char *fetchname(const char *, bool *, int); +char *checked_in(char *); int backup_file(const char *); int move_file(const char *, const char *); int copy_file(const char *, const char *); |