1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/*
* diff.c - diff specific util functions
* Copyright (C) 2001, 2002 Tim Waugh <twaugh@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
int num_pathname_components (const char *x)
{
int num = 0;
while ((x = strchr (x, '/')) != NULL) {
while (*x == '/')
x++;
num++;
}
return num;
}
/*
* Find the best name from a list.
*
* Of the names with the fewest path name components, select the
* one with the shortest base name. Of any remaining candidates,
* select the one with the shortest name.
*
*/
char *best_name (int n, char **names)
{
int *pathname_components;
int *basename_length;
int best_pn, best_bn, best_n, best = 0; /* shut gcc up */
int i;
pathname_components = xmalloc (sizeof (int *) * n);
basename_length = xmalloc (sizeof (int *) * n);
best_pn = -1;
for (i = 0; i < n; i++) {
if (!strcmp (names[i], "/dev/null"))
continue;
pathname_components[i] = num_pathname_components (names[i]);
if ((best_pn == -1) ||
(pathname_components[i] < best_pn))
best_pn = pathname_components[i];
}
best_bn = -1;
for (i = 0; i < n; i++) {
char *p;
if (!strcmp (names[i], "/dev/null"))
continue;
if (pathname_components[i] != best_pn)
continue;
p = strrchr (names[i], '/');
if (p)
p++;
else
p = names[i];
basename_length[i] = strlen (p);
if ((best_bn == -1) ||
(basename_length[i] < best_bn))
best_bn = basename_length[i];
}
best_n = -1;
for (i = 0; i < n; i++) {
int len;
if (basename_length[i] != best_bn)
continue;
len = strlen (names[i]);
if ((best_n == -1) ||
(len < best_n)) {
best_n = len;
best = i;
}
}
free (pathname_components);
free (basename_length);
return names[best];
}
unsigned long calculate_num_lines (const char *atatline, char which)
{
char *p = strchr (atatline, which);
if (!p)
return 1;
while (*p && *p != ',' && *p != ' ') p++;
if (!*p || *p == ' ')
return 1;
return strtoul (p + 1, NULL, 10);
}
unsigned long orig_num_lines (const char *atatline)
{
return calculate_num_lines (atatline, '-');
}
unsigned long new_num_lines (const char *atatline)
{
return calculate_num_lines (atatline, '+');
}
/* Parse an @@ line. */
int read_atatline (const char *atatline,
unsigned long *orig_offset,
unsigned long *orig_count,
unsigned long *new_offset,
unsigned long *new_count)
{
char *endptr;
unsigned long res;
char *p;
if (orig_offset) {
p = strchr (atatline, '-');
if (!p)
return 1;
p++;
res = strtoul (p, &endptr, 0);
if (p == endptr)
return 1;
*orig_offset = res;
}
if (orig_count)
*orig_count = orig_num_lines (atatline);
if (new_offset) {
p = strchr (atatline, '+');
if (!p)
return 1;
p++;
res = strtoul (p, &endptr, 0);
if (p == endptr)
return 1;
*new_offset = res;
}
if (new_count)
*new_count = new_num_lines (atatline);
return 0;
}
|