Deleted Added
full compact
pch.c (286346) pch.c (286795)
1/*-
2 * Copyright 1986, Larry Wall
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
8 *

--- 10 unchanged lines hidden (view full) ---

19 * SUCH DAMAGE.
20 *
21 * patch - a program to apply diffs to original files
22 *
23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24 * behaviour
25 *
26 * $OpenBSD: pch.c,v 1.43 2014/11/18 17:03:35 tobias Exp $
1/*-
2 * Copyright 1986, Larry Wall
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
8 *

--- 10 unchanged lines hidden (view full) ---

19 * SUCH DAMAGE.
20 *
21 * patch - a program to apply diffs to original files
22 *
23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24 * behaviour
25 *
26 * $OpenBSD: pch.c,v 1.43 2014/11/18 17:03:35 tobias Exp $
27 * $FreeBSD: head/usr.bin/patch/pch.c 286346 2015-08-05 22:04:54Z delphij $
27 * $FreeBSD: head/usr.bin/patch/pch.c 286795 2015-08-15 00:42:33Z delphij $
28 */
29
30#include <sys/types.h>
31#include <sys/stat.h>
32
33#include <ctype.h>
34#include <libgen.h>
35#include <limits.h>

--- 1460 unchanged lines hidden (view full) ---

1496 for (i = 0; i < MAX_FILE; i++) {
1497 if (names[i].path != NULL && names[i].exists) {
1498 path = names[i].path;
1499 break;
1500 }
1501 }
1502 if (path == NULL && !assume_exists) {
1503 /*
28 */
29
30#include <sys/types.h>
31#include <sys/stat.h>
32
33#include <ctype.h>
34#include <libgen.h>
35#include <limits.h>

--- 1460 unchanged lines hidden (view full) ---

1496 for (i = 0; i < MAX_FILE; i++) {
1497 if (names[i].path != NULL && names[i].exists) {
1498 path = names[i].path;
1499 break;
1500 }
1501 }
1502 if (path == NULL && !assume_exists) {
1503 /*
1504 * No files found, look for something we can checkout from
1505 * RCS/SCCS dirs. Same order as above.
1504 * No files found, check to see if the diff could be
1505 * creating a new file.
1506 */
1506 */
1507 for (i = 0; i < MAX_FILE; i++) {
1508 if (names[i].path != NULL &&
1509 (path = checked_in(names[i].path)) != NULL)
1510 break;
1511 }
1512 /*
1513 * Still no match? Check to see if the diff could be creating
1514 * a new file.
1515 */
1516 if (path == NULL && ok_to_create_file &&
1517 names[NEW_FILE].path != NULL)
1518 path = names[NEW_FILE].path;
1519 }
1520
1521 return path ? xstrdup(path) : NULL;
1522}
1523
1524static char *
1507 if (path == NULL && ok_to_create_file &&
1508 names[NEW_FILE].path != NULL)
1509 path = names[NEW_FILE].path;
1510 }
1511
1512 return path ? xstrdup(path) : NULL;
1513}
1514
1515static char *
1525compare_names(const struct file_name *names, bool assume_exists, int phase)
1516compare_names(const struct file_name *names, bool assume_exists)
1526{
1527 size_t min_components, min_baselen, min_len, tmp;
1528 char *best = NULL;
1529 char *path;
1530 int i;
1531
1532 /*
1533 * The "best" name is the one with the fewest number of path
1534 * components, the shortest basename length, and the shortest
1535 * overall length (in that order). We only use the Index: file
1536 * if neither of the old or new files could be intuited from
1537 * the diff header.
1538 */
1539 min_components = min_baselen = min_len = SIZE_MAX;
1540 for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1541 path = names[i].path;
1517{
1518 size_t min_components, min_baselen, min_len, tmp;
1519 char *best = NULL;
1520 char *path;
1521 int i;
1522
1523 /*
1524 * The "best" name is the one with the fewest number of path
1525 * components, the shortest basename length, and the shortest
1526 * overall length (in that order). We only use the Index: file
1527 * if neither of the old or new files could be intuited from
1528 * the diff header.
1529 */
1530 min_components = min_baselen = min_len = SIZE_MAX;
1531 for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1532 path = names[i].path;
1542 if (path == NULL ||
1543 (phase == 1 && !names[i].exists && !assume_exists) ||
1544 (phase == 2 && checked_in(path) == NULL))
1533 if (path == NULL || (!names[i].exists && !assume_exists))
1545 continue;
1546 if ((tmp = num_components(path)) > min_components)
1547 continue;
1548 if (tmp < min_components) {
1549 min_components = tmp;
1550 best = path;
1551 }
1552 if ((tmp = strlen(basename(path))) > min_baselen)

--- 14 unchanged lines hidden (view full) ---

1567 * Choose the name of the file to be patched based the "best" one
1568 * available.
1569 */
1570static char *
1571best_name(const struct file_name *names, bool assume_exists)
1572{
1573 char *best;
1574
1534 continue;
1535 if ((tmp = num_components(path)) > min_components)
1536 continue;
1537 if (tmp < min_components) {
1538 min_components = tmp;
1539 best = path;
1540 }
1541 if ((tmp = strlen(basename(path))) > min_baselen)

--- 14 unchanged lines hidden (view full) ---

1556 * Choose the name of the file to be patched based the "best" one
1557 * available.
1558 */
1559static char *
1560best_name(const struct file_name *names, bool assume_exists)
1561{
1562 char *best;
1563
1575 best = compare_names(names, assume_exists, 1);
1576 if (best == NULL) {
1577 best = compare_names(names, assume_exists, 2);
1578 /*
1579 * Still no match? Check to see if the diff could be creating
1580 * a new file.
1581 */
1582 if (best == NULL && ok_to_create_file &&
1583 names[NEW_FILE].path != NULL)
1584 best = names[NEW_FILE].path;
1585 }
1564 best = compare_names(names, assume_exists);
1586
1565
1566 /* No match? Check to see if the diff could be creating a new file. */
1567 if (best == NULL && ok_to_create_file)
1568 best = names[NEW_FILE].path;
1569
1587 return best ? xstrdup(best) : NULL;
1588}
1589
1590static size_t
1591num_components(const char *path)
1592{
1593 size_t n;
1594 const char *cp;

--- 40 unchanged lines hidden ---
1570 return best ? xstrdup(best) : NULL;
1571}
1572
1573static size_t
1574num_components(const char *path)
1575{
1576 size_t n;
1577 const char *cp;

--- 40 unchanged lines hidden ---