summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2013-07-11 12:39:31 +0000
committerotto <otto@openbsd.org>2013-07-11 12:39:31 +0000
commitc9392928ee3a335addf272969ff193de96b1a1c6 (patch)
treea9701eedf3884ed0e85e2741a4937550ed019951
parentMerge fuse_opt code with stsp@ fuse_opt code. (diff)
downloadwireguard-openbsd-c9392928ee3a335addf272969ff193de96b1a1c6.tar.xz
wireguard-openbsd-c9392928ee3a335addf272969ff193de96b1a1c6.zip
Bring file selection in accordance with the man page; from Stefan Esser
ok millert@
-rw-r--r--usr.bin/patch/pch.c68
1 files changed, 33 insertions, 35 deletions
diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c
index 16576f9f92a..4538787172e 100644
--- a/usr.bin/patch/pch.c
+++ b/usr.bin/patch/pch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pch.c,v 1.39 2012/04/11 08:07:13 ajacoutot Exp $ */
+/* $OpenBSD: pch.c,v 1.40 2013/07/11 12:39:31 otto Exp $ */
/*
* patch - a program to apply diffs to original files
@@ -1465,15 +1465,12 @@ posix_name(const struct file_name *names, bool assume_exists)
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)
+compare_names(const struct file_name *names, bool assume_exists, int phase)
{
size_t min_components, min_baselen, min_len, tmp;
char *best = NULL;
+ char *path;
int i;
/*
@@ -1485,41 +1482,43 @@ best_name(const struct file_name *names, bool assume_exists)
*/
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))
+ path = names[i].path;
+ if (path == NULL ||
+ (phase == 1 && !names[i].exists && !assume_exists) ||
+ (phase == 2 && checked_in(path) == NULL))
continue;
- if ((tmp = num_components(names[i].path)) > min_components)
+ if ((tmp = num_components(path)) > min_components)
continue;
- min_components = tmp;
- if ((tmp = strlen(basename(names[i].path))) > min_baselen)
+ if (tmp < min_components) {
+ min_components = tmp;
+ best = path;
+ }
+ if ((tmp = strlen(basename(path))) > min_baselen)
continue;
- min_baselen = tmp;
- if ((tmp = strlen(names[i].path)) > min_len)
+ if (tmp < min_baselen) {
+ min_baselen = tmp;
+ best = path;
+ }
+ if ((tmp = strlen(path)) > min_len)
continue;
min_len = tmp;
- best = names[i].path;
+ best = path;
}
+ return best;
+}
+
+/*
+ * 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)
+{
+ char *best;
+
+ best = compare_names(names, assume_exists, 1);
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;
- }
+ best = compare_names(names, assume_exists, 2);
/*
* Still no match? Check to see if the diff could be creating
* a new file.
@@ -1528,7 +1527,6 @@ best_name(const struct file_name *names, bool assume_exists)
names[NEW_FILE].path != NULL)
best = names[NEW_FILE].path;
}
-
return best ? savestr(best) : NULL;
}