[9] | 1 | /* diff - print differences between 2 files Author: Erik Baalbergen */
|
---|
| 2 |
|
---|
| 3 | /* Poor man's implementation of diff(1) - no options available
|
---|
| 4 | * - may give more output than other diffs,
|
---|
| 5 | * due to the straight-forward algorithm
|
---|
| 6 | * - runs out of memory if the differing chunks become too large
|
---|
| 7 | * - input line length should not exceed LINELEN; longer lines are
|
---|
| 8 | * truncated, while only the first LINELEN characters are compared
|
---|
| 9 | *
|
---|
| 10 | * - Bug fixes by Rick Thomas Sept. 1989
|
---|
| 11 | *
|
---|
| 12 | * Please report bugs and suggestions to erikb@cs.vu.nl
|
---|
| 13 | *------------------------------------------------------------------------------
|
---|
| 14 | * Changed diff to conform to POSIX 1003.2 ( Draft 11) by Thomas Brupbacher
|
---|
| 15 | * ( tobr@mw.lpc.ethz.ch).
|
---|
| 16 | *
|
---|
| 17 | * To incorporate the context diff option -c in the program, the source code
|
---|
| 18 | * for the program cdiff has been copied to the end of this program. Only
|
---|
| 19 | * slight modifications for the cdiff code to work within the program diff
|
---|
| 20 | * were made( e.g. main() -> context_diff()).
|
---|
| 21 | *
|
---|
| 22 | * New options:
|
---|
| 23 | * -c, -C n where n=0,1,...:
|
---|
| 24 | * produces a context diff as the program cdiff. The default is to
|
---|
| 25 | * print 3 lines of context, this value can be changed with -C
|
---|
| 26 | * ( e.g. -C 5 prints five lines of context.)
|
---|
| 27 | * -e : Prints an ed script, so you can convert <file1> to <file2> with
|
---|
| 28 | * the command ed <file1> < `diff -e <file1> <file2>`.
|
---|
| 29 | * -b : Causes trailing blanks to be ignored and spaces of multiple blanks
|
---|
| 30 | * to be reduced to one blank before comparison.
|
---|
| 31 | *-----------------------------------------------------------------------------
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include <stdlib.h>
|
---|
| 36 | #include <limits.h> /* NAME_MAX for maximal filename length */
|
---|
| 37 | #include <string.h> /* string manipulation */
|
---|
| 38 | #include <sys/types.h>
|
---|
| 39 | #include <sys/stat.h>
|
---|
| 40 | #include <sys/wait.h>
|
---|
| 41 | #include <ctype.h>
|
---|
| 42 | #include <time.h>
|
---|
| 43 | #include <dirent.h>
|
---|
| 44 | #include <unistd.h>
|
---|
| 45 | #include <stdio.h>
|
---|
| 46 |
|
---|
| 47 | /* These definitions are needed only to suppress warning messages. */
|
---|
| 48 | #define Nullfp ((FILE*)0)
|
---|
| 49 | #define Nullch ((char*)0)
|
---|
| 50 | #define NullStructLine ((struct line *)0)
|
---|
| 51 |
|
---|
| 52 | #define LINELEN 128 /* max line length included in diff */
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | #define NOT_SET 0 /* Defines to characterise if a flag */
|
---|
| 56 | #define SET 1 /* is set */
|
---|
| 57 |
|
---|
| 58 | /* Indexes of the warning-message array */
|
---|
| 59 | #define EXCLUSIVE_OPTIONS 0
|
---|
| 60 | #define CANNOT_OPEN_FILE 1
|
---|
| 61 |
|
---|
| 62 | /* Used to define the mode */
|
---|
| 63 | typedef enum {
|
---|
| 64 | undefined, context, ed_mode
|
---|
| 65 | } MODE;
|
---|
| 66 |
|
---|
| 67 | /* Global variables for the 'normal' diff part */
|
---|
| 68 | char *progname; /* program name (on command line) */
|
---|
| 69 | int diffs = 0; /* number of differences */
|
---|
| 70 | MODE mode; /* which mode is used */
|
---|
| 71 | int severe_error; /* nonzero after severe, non-fatal error */
|
---|
| 72 |
|
---|
| 73 | /* The following global variables are used with the -r option:
|
---|
| 74 | * for every pair of files that are different, a "command line" of the
|
---|
| 75 | * form "diff <options> <oldfile> <newfile>" is printed before the real
|
---|
| 76 | * output starts. */
|
---|
| 77 | int firstoutput = 1; /* flag to print one time */
|
---|
| 78 | char options_string[10]; /* string to hold command line options */
|
---|
| 79 | char oldfile[PATH_MAX]; /* first file */
|
---|
| 80 | char newfile[PATH_MAX]; /* second file */
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | /* Global variables for the command-line options */
|
---|
| 84 | int trim_blanks = NOT_SET; /* SET if -b specified */
|
---|
| 85 | int recursive_dir = NOT_SET; /* SET if -r specified */
|
---|
| 86 | int context_lines = 3; /* numbers of lines in a context */
|
---|
| 87 | static int offset; /* offset of the actual line number for -e */
|
---|
| 88 |
|
---|
| 89 | /* Function prototypes for the functions in this file */
|
---|
| 90 | struct f;
|
---|
| 91 | _PROTOTYPE(int main, (int argc, char **argv ));
|
---|
| 92 | _PROTOTYPE(void process_command_line, (int argc, char **argv ));
|
---|
| 93 | _PROTOTYPE(void analyse_input_files, (char *arg1, char *arg2, char *input1,
|
---|
| 94 | char *input2 ));
|
---|
| 95 | _PROTOTYPE(void diff, (char *filename1, char *filename2 ));
|
---|
| 96 | _PROTOTYPE(FILE *check_file, (char *name ));
|
---|
| 97 | _PROTOTYPE(void build_option_string, (void ));
|
---|
| 98 | _PROTOTYPE(void fatal_error, (char *fmt, char *s ));
|
---|
| 99 | _PROTOTYPE(void warn, (int number, char *string ));
|
---|
| 100 | _PROTOTYPE(void trimming_blanks, (char *l_text ));
|
---|
| 101 | _PROTOTYPE(char *filename, (char *path_string));
|
---|
| 102 | _PROTOTYPE(struct line *new_line, (int size ));
|
---|
| 103 | _PROTOTYPE(void free_line, (struct line *l ));
|
---|
| 104 | _PROTOTYPE(int equal_line, (struct line *l1, struct line *l2 ));
|
---|
| 105 | _PROTOTYPE(int equal_3, (struct line *l1, struct line *l2 ));
|
---|
| 106 | _PROTOTYPE(struct line *read_line, (FILE *fp ));
|
---|
| 107 | _PROTOTYPE(void advance, (struct f *f ));
|
---|
| 108 | _PROTOTYPE(void aside, (struct f *f, struct line *l ));
|
---|
| 109 | _PROTOTYPE(struct line *next, (struct f *f ));
|
---|
| 110 | _PROTOTYPE(void init_f, (struct f *f, FILE *fp ));
|
---|
| 111 | _PROTOTYPE(void update, (struct f *f, char *s ));
|
---|
| 112 | _PROTOTYPE(void __diff, (FILE *fp1, FILE *fp2 ));
|
---|
| 113 | _PROTOTYPE(void differ, (struct f *f1, struct f *f2 ));
|
---|
| 114 | _PROTOTYPE(int wlen, (struct f *f ));
|
---|
| 115 | _PROTOTYPE(void range, (int a, int b ));
|
---|
| 116 | _PROTOTYPE(void cdiff, (char *old, char *new, FILE *file1, FILE *file2 ));
|
---|
| 117 | _PROTOTYPE(void dumphunk, (void ));
|
---|
| 118 | _PROTOTYPE(char *getold, (int targ ));
|
---|
| 119 | _PROTOTYPE(char *getnew, (int targ ));
|
---|
| 120 | _PROTOTYPE(int isdir, (char *path ));
|
---|
| 121 | _PROTOTYPE(void diff_recursive, (char *dir1, char *dir2 ));
|
---|
| 122 | _PROTOTYPE(void file_type_error, (char *filename1, char *filename2,
|
---|
| 123 | struct stat *statbuf1, struct stat *statbuf2 ));
|
---|
| 124 | _PROTOTYPE(void *xmalloc, (size_t size));
|
---|
| 125 | _PROTOTYPE(void *xrealloc, (void *ptr, size_t size));
|
---|
| 126 |
|
---|
| 127 | int main(argc, argv)
|
---|
| 128 | int argc;
|
---|
| 129 | char **argv;
|
---|
| 130 | {
|
---|
| 131 | char file1[PATH_MAX], file2[PATH_MAX];
|
---|
| 132 | extern int optind; /* index of the current string in argv */
|
---|
| 133 |
|
---|
| 134 | progname = argv[0];
|
---|
| 135 | process_command_line(argc, argv);
|
---|
| 136 |
|
---|
| 137 | analyse_input_files(argv[optind], argv[optind + 1], file1, file2);
|
---|
| 138 | optind++;
|
---|
| 139 |
|
---|
| 140 | if (recursive_dir == SET) {
|
---|
| 141 | build_option_string();
|
---|
| 142 | diff_recursive(file1, file2);
|
---|
| 143 | } else {
|
---|
| 144 | diff(file1, file2);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return(severe_error ? 2 : diffs > 0 ? 1 : 0);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /* Process the command line and set the flags for the different
|
---|
| 151 | * options. the processing of the command line is done with the
|
---|
| 152 | * getopt() library function. a minimal error processing is done
|
---|
| 153 | * for the number of command line arguments. */
|
---|
| 154 | void process_command_line(argc, argv)
|
---|
| 155 | int argc; /* number of arguments on command line */
|
---|
| 156 | char **argv; /* ** to arguments on command line */
|
---|
| 157 | {
|
---|
| 158 | int c;
|
---|
| 159 | extern char *optarg; /* points to string with options */
|
---|
| 160 | extern int optind; /* index of the current string in argv */
|
---|
| 161 |
|
---|
| 162 | /* Are there enough arguments? */
|
---|
| 163 | if (argc < 3) {
|
---|
| 164 | fatal_error("Usage: %s [-c|-e|-C n][-br] file1 file2\n", progname);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /* Process all options using getopt() */
|
---|
| 168 | while ((c = getopt(argc, argv, "ceC:br")) != -1) {
|
---|
| 169 | switch (c) {
|
---|
| 170 | case 'c':
|
---|
| 171 | if (mode != undefined) warn(EXCLUSIVE_OPTIONS, "c");
|
---|
| 172 | mode = context;
|
---|
| 173 | context_lines = 3;
|
---|
| 174 | break;
|
---|
| 175 | case 'e':
|
---|
| 176 | if (mode != undefined) warn(EXCLUSIVE_OPTIONS, "e");
|
---|
| 177 | mode = ed_mode;
|
---|
| 178 | break;
|
---|
| 179 | case 'C':
|
---|
| 180 | if (mode != undefined) warn(EXCLUSIVE_OPTIONS, "C");
|
---|
| 181 | mode = context;
|
---|
| 182 | context_lines = atoi(optarg);
|
---|
| 183 | break;
|
---|
| 184 | case 'b': trim_blanks = SET; break;
|
---|
| 185 | case 'r': recursive_dir = SET; break;
|
---|
| 186 | case '?':
|
---|
| 187 | exit(2);
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /* We should have two arguments left */
|
---|
| 192 | if ((argc - optind) != 2)
|
---|
| 193 | fatal_error("Need exactly two input file-names!\n", "");
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /* Analyse_input_files takes the two input files on the command line
|
---|
| 197 | * and decides what to do. returns the (corrected) filenames that
|
---|
| 198 | * can be used to call diff().
|
---|
| 199 | * if two directories are given, then a recursive diff is done.
|
---|
| 200 | * one directory and one filename compares the file with <filename>
|
---|
| 201 | * in the directory <directory> with <filename>.
|
---|
| 202 | * if two filenames are specified, no special action takes place.
|
---|
| 203 | */
|
---|
| 204 | void analyse_input_files(arg1, arg2, input1, input2)
|
---|
| 205 | char *arg1, *arg2; /* filenames on the command line */
|
---|
| 206 | char *input1, *input2; /* filenames to be used with diff() */
|
---|
| 207 | {
|
---|
| 208 | int stat1 = 0, stat2 = 0;
|
---|
| 209 |
|
---|
| 210 | if (strcmp(arg1, "-") != 0)
|
---|
| 211 | stat1 = isdir(arg1); /* != 0 <-> arg1 is directory */
|
---|
| 212 | if (strcmp(arg2, "-") != 0) stat2 = isdir(arg2);
|
---|
| 213 | #ifdef DEBUG
|
---|
| 214 | fprintf(stderr, "%s, stat = %d\n", arg1, stat1);
|
---|
| 215 | fprintf(stderr, "%s, stat = %d\n", arg2, stat2);
|
---|
| 216 | #endif
|
---|
| 217 | if (stat1 && stat2) { /* both arg1 and arg2 are directories */
|
---|
| 218 | recursive_dir = SET;
|
---|
| 219 | strcpy(input1, arg1);
|
---|
| 220 | strcpy(input2, arg2);
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
| 223 | if (stat1 != 0) { /* arg1 is a dir, arg2 not */
|
---|
| 224 | if (strcmp(arg2, "-") != 0) { /* arg2 != stdin */
|
---|
| 225 | strcpy(input1, arg1);
|
---|
| 226 | strcat(input1, "/");
|
---|
| 227 | strcat(input1, arg2);
|
---|
| 228 | strcpy(input2, arg2);
|
---|
| 229 | return;
|
---|
| 230 | } else {
|
---|
| 231 | fatal_error("cannot compare stdin (-) with a directory!", "");
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | if (stat2 != 0) { /* arg2 is a dir, arg1 not */
|
---|
| 235 | if (strcmp(arg1, "-") != 0) { /* arg1 != stdin */
|
---|
| 236 | strcpy(input1, arg1);
|
---|
| 237 | strcpy(input2, arg2);
|
---|
| 238 | strcat(input2, "/");
|
---|
| 239 | strcat(input2, arg1);
|
---|
| 240 | return;
|
---|
| 241 | } else { /* arg1 == stdin */
|
---|
| 242 | fatal_error("cannot compare stdin (-) with a directory!", "");
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /* Both arg1 and arg2 are normal files */
|
---|
| 247 | strcpy(input1, arg1);
|
---|
| 248 | strcpy(input2, arg2);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /* Diff() is the front end for all modes of the program diff, execpt
|
---|
| 252 | * the recursive_dir option.
|
---|
| 253 | * diff() expects the filenames of the two files to be compared as
|
---|
| 254 | * arguments. the mode is determined from the global variable mode.
|
---|
| 255 | */
|
---|
| 256 | void diff(filename1, filename2)
|
---|
| 257 | char *filename1, *filename2;
|
---|
| 258 | {
|
---|
| 259 | FILE *file1 = check_file(filename1);
|
---|
| 260 | FILE *file2 = check_file(filename2);
|
---|
| 261 | struct stat statbuf1, statbuf2;
|
---|
| 262 |
|
---|
| 263 | if ((file1 != Nullfp) && (file2 != Nullfp)) {
|
---|
| 264 | /* If we do a recursive diff, then we don't compare block
|
---|
| 265 | * special, character special or FIFO special files to any
|
---|
| 266 | * file. */
|
---|
| 267 | fstat(fileno(file1), &statbuf1);
|
---|
| 268 | fstat(fileno(file2), &statbuf2);
|
---|
| 269 | if ((((statbuf1.st_mode & S_IFREG) != S_IFREG) ||
|
---|
| 270 | ((statbuf2.st_mode & S_IFREG) != S_IFREG)) &&
|
---|
| 271 | (recursive_dir == SET)) {
|
---|
| 272 | file_type_error(filename1, filename2, &statbuf1, &statbuf2);
|
---|
| 273 | } else {
|
---|
| 274 | switch (mode) {
|
---|
| 275 | case context:
|
---|
| 276 | cdiff(filename1, filename2, file1, file2);
|
---|
| 277 | break;
|
---|
| 278 | case ed_mode:
|
---|
| 279 | case undefined:
|
---|
| 280 | __diff(file1, file2);
|
---|
| 281 | if (mode == ed_mode) printf("w\n");
|
---|
| 282 | break;
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | } else
|
---|
| 286 | severe_error = 1;
|
---|
| 287 | if (file1 != Nullfp) fclose(file1);
|
---|
| 288 | if (file2 != Nullfp) fclose(file2);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /* Check_file() opens the fileptr with name <filename>. if <filename>
|
---|
| 292 | * equals "-" stdin is associated with the return value.
|
---|
| 293 | */
|
---|
| 294 | FILE *check_file(name)
|
---|
| 295 | char *name;
|
---|
| 296 | {
|
---|
| 297 | FILE *temp;
|
---|
| 298 |
|
---|
| 299 | if (strcmp(name, "-") == 0) {
|
---|
| 300 | return(stdin);
|
---|
| 301 | } else {
|
---|
| 302 | temp = fopen(name, "r");
|
---|
| 303 | if (temp == Nullfp) warn(CANNOT_OPEN_FILE, name);
|
---|
| 304 | return(temp);
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /* Build_option_string() is called before recursive_dir() is called
|
---|
| 309 | * from the main() function. its purpose is to build the string that
|
---|
| 310 | * is used on the command line to get the current operation mode.
|
---|
| 311 | * e.g. "-C 6 -b".
|
---|
| 312 | */
|
---|
| 313 | void build_option_string()
|
---|
| 314 | {
|
---|
| 315 | switch (mode) {
|
---|
| 316 | case ed_mode:sprintf(options_string, "-e");
|
---|
| 317 | break;
|
---|
| 318 | case context:
|
---|
| 319 | if (context_lines == 3)
|
---|
| 320 | sprintf(options_string, "-c");
|
---|
| 321 | else
|
---|
| 322 | sprintf(options_string, "-C %d", context_lines);
|
---|
| 323 | break;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 | /* The fatal error handler.
|
---|
| 330 | * Expects a format string and a string as arguments. The arguments
|
---|
| 331 | * are printed to stderr and the program exits with an error code 2.
|
---|
| 332 | */
|
---|
| 333 | void fatal_error(fmt, s)
|
---|
| 334 | char *fmt; /* the format sttring to be printed */
|
---|
| 335 | char *s; /* string to be inserted into the format
|
---|
| 336 | * string */
|
---|
| 337 | {
|
---|
| 338 | fprintf(stderr, "%s: ", progname);
|
---|
| 339 | fprintf(stderr, fmt, s);
|
---|
| 340 | fprintf(stderr, "\n");
|
---|
| 341 | exit(2);
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /* This function prints non fatal error messages to stderr.
|
---|
| 345 | * Expects the index of the message to be printed and a pointer
|
---|
| 346 | * to the (optional) string to be printed.
|
---|
| 347 | * Returns no value.
|
---|
| 348 | */
|
---|
| 349 | void warn(number, string)
|
---|
| 350 | int number; /* index of the warning */
|
---|
| 351 | char *string; /* string to be inserted to the warning */
|
---|
| 352 | {
|
---|
| 353 | static char *warning[] = {
|
---|
| 354 | "%s: The options -c, -e, -C n are mutually exclusive! Assuming -%c\n",
|
---|
| 355 | "%s: cannot open file %s for reading\n",
|
---|
| 356 | };
|
---|
| 357 | fprintf(stderr, warning[number], progname, string);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /* Function used with the optione -b, trims the blanks in a input line:
|
---|
| 361 | * - blanks between words are reduced to one
|
---|
| 362 | * - trailing blanks are eliminated.
|
---|
| 363 | */
|
---|
| 364 | void trimming_blanks(l_text)
|
---|
| 365 | char *l_text; /* begin of the char array */
|
---|
| 366 | {
|
---|
| 367 | char *line = l_text;
|
---|
| 368 | char *copy_to, *copy_from;
|
---|
| 369 |
|
---|
| 370 | do {
|
---|
| 371 | if (*line == ' ') {
|
---|
| 372 | copy_from = line;
|
---|
| 373 | copy_to = line;
|
---|
| 374 | while (*(++copy_from) == ' ');
|
---|
| 375 | if (*copy_from != '\n') copy_to++;
|
---|
| 376 | while (*copy_from != '\0') *(copy_to++) = *(copy_from++);
|
---|
| 377 | *copy_to = '\0';
|
---|
| 378 | }
|
---|
| 379 | } while (*(++line) != '\0');
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 |
|
---|
| 383 | /* Filename separates the filename and the relative path in path_string.
|
---|
| 384 | * Returns the filename with a leading /
|
---|
| 385 | */
|
---|
| 386 | char *filename(path_string)
|
---|
| 387 | char *path_string;
|
---|
| 388 | {
|
---|
| 389 | char name[NAME_MAX + 2]; /* filename plus / */
|
---|
| 390 | char *ptr;
|
---|
| 391 |
|
---|
| 392 | name[0] = '/';
|
---|
| 393 | ptr = strrchr(path_string, '/');
|
---|
| 394 |
|
---|
| 395 | if (ptr == 0) { /* no / in path_string, only a filename */
|
---|
| 396 | strcat(name, path_string);
|
---|
| 397 | } else {
|
---|
| 398 | strcat(name, ptr);
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | return(name);
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | /* The line module: one member in a linked list of lines. */
|
---|
| 405 | struct line {
|
---|
| 406 | struct line *l_next; /* pointer to the next line */
|
---|
| 407 | char l_eof; /* == 0 if last line in file */
|
---|
| 408 | char *l_text; /* array with the text */
|
---|
| 409 | };
|
---|
| 410 |
|
---|
| 411 | struct line *freelist = 0;
|
---|
| 412 | #define stepup(ll) ( ((ll) && ((ll)->l_eof==0)) ? (ll)->l_next : (ll) )
|
---|
| 413 |
|
---|
| 414 | /* Function to allocate space for a new line containing SIZE chars */
|
---|
| 415 | struct line *new_line(size)
|
---|
| 416 | int size;
|
---|
| 417 | {
|
---|
| 418 | register struct line *l;
|
---|
| 419 |
|
---|
| 420 | if ((l = freelist) != NullStructLine)
|
---|
| 421 | freelist = freelist->l_next;
|
---|
| 422 | else {
|
---|
| 423 | l = (struct line *) xmalloc(3 * sizeof(void *));
|
---|
| 424 | l->l_text = (char *) xmalloc((size + 2) * sizeof(char));
|
---|
| 425 | if ((l == 0) || (l->l_text == 0)) fatal_error("Out of memory", "");
|
---|
| 426 | }
|
---|
| 427 | return l;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | /* Free_line() releases storage allocated for <l>. */
|
---|
| 432 | void free_line(l)
|
---|
| 433 | register struct line *l;
|
---|
| 434 | {
|
---|
| 435 | l->l_next = freelist;
|
---|
| 436 | freelist = l;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | /* Equal_line() compares two lines, <l1> and <l2>.
|
---|
| 440 | * the returned value is the result of the strcmp() function.
|
---|
| 441 | */
|
---|
| 442 | int equal_line(l1, l2)
|
---|
| 443 | struct line *l1, *l2;
|
---|
| 444 | {
|
---|
| 445 | if (l1 == 0 || l2 == 0)
|
---|
| 446 | return(0);
|
---|
| 447 | else if (l1->l_eof || l2->l_eof)
|
---|
| 448 | return(l1->l_eof == l2->l_eof);
|
---|
| 449 | else
|
---|
| 450 | return(strcmp(l1->l_text, l2->l_text) == 0);
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | int equal_3(l1, l2)
|
---|
| 454 | struct line *l1, *l2;
|
---|
| 455 | {
|
---|
| 456 | register int i, ansr;
|
---|
| 457 |
|
---|
| 458 | ansr = 1;
|
---|
| 459 | #ifdef DEBUG
|
---|
| 460 | if (l1 == 0)
|
---|
| 461 | fprintf(stderr, "\t(null)\n");
|
---|
| 462 | else if (l1->l_eof)
|
---|
| 463 | fprintf(stderr, "\t(eof)\n");
|
---|
| 464 | else
|
---|
| 465 | fprintf(stderr, "\t%s", l1->l_text);
|
---|
| 466 | if (l2 == 0)
|
---|
| 467 | fprintf(stderr, "\t(null)\n");
|
---|
| 468 | else if (l2->l_eof)
|
---|
| 469 | fprintf(stderr, "\t(eof)\n");
|
---|
| 470 | else
|
---|
| 471 | fprintf(stderr, "\t%s", l2->l_text);
|
---|
| 472 | #endif
|
---|
| 473 | for (i = 0; i < 3; ++i) {
|
---|
| 474 | if (!equal_line(l1, l2)) {
|
---|
| 475 | ansr = 0;
|
---|
| 476 | break;
|
---|
| 477 | }
|
---|
| 478 | l1 = stepup(l1);
|
---|
| 479 | l2 = stepup(l2);
|
---|
| 480 | }
|
---|
| 481 | #ifdef DEBUG
|
---|
| 482 | fprintf(stderr, "\t%d\n", ansr);
|
---|
| 483 | #endif
|
---|
| 484 | return(ansr);
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | struct line *
|
---|
| 488 | read_line(fp)
|
---|
| 489 | FILE *fp;
|
---|
| 490 | {
|
---|
| 491 | register struct line *l = new_line(LINELEN);
|
---|
| 492 | register char *p;
|
---|
| 493 | register int c;
|
---|
| 494 |
|
---|
| 495 | (p = &(l->l_text[LINELEN]))[1] = '\377';
|
---|
| 496 | l->l_eof = 0;
|
---|
| 497 | if (fgets(l->l_text, LINELEN + 2, fp) == 0) {
|
---|
| 498 | l->l_eof = 1;
|
---|
| 499 | l->l_text[0] = 0;
|
---|
| 500 | } else if ((p[1] & 0377) != 0377 && *p != '\n') {
|
---|
| 501 | while ((c = fgetc(fp)) != '\n' && c != EOF) {
|
---|
| 502 | }
|
---|
| 503 | *p++ = '\n';
|
---|
| 504 | *p = '\0';
|
---|
| 505 | }
|
---|
| 506 | l->l_next = 0;
|
---|
| 507 | if (trim_blanks == SET) {
|
---|
| 508 | #ifdef DEBUG
|
---|
| 509 | printf("xxx %s xxx\n", l->l_text);
|
---|
| 510 | #endif
|
---|
| 511 | trimming_blanks(l->l_text);
|
---|
| 512 | #ifdef DEBUG
|
---|
| 513 | printf("xxx %s xxx\n", l->l_text);
|
---|
| 514 | #endif
|
---|
| 515 | }
|
---|
| 516 | return l;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /* File window handler */
|
---|
| 520 | struct f {
|
---|
| 521 | struct line *f_bwin, *f_ewin;
|
---|
| 522 | struct line *f_aside;
|
---|
| 523 | int f_linecnt; /* line number in file of last advanced line */
|
---|
| 524 | FILE *f_fp;
|
---|
| 525 | };
|
---|
| 526 |
|
---|
| 527 | void advance(f)
|
---|
| 528 | register struct f *f;
|
---|
| 529 | {
|
---|
| 530 | register struct line *l;
|
---|
| 531 |
|
---|
| 532 | if ((l = f->f_bwin) != NullStructLine) {
|
---|
| 533 | if (f->f_ewin == l)
|
---|
| 534 | f->f_bwin = f->f_ewin = 0;
|
---|
| 535 | else
|
---|
| 536 | f->f_bwin = l->l_next;
|
---|
| 537 | free_line(l);
|
---|
| 538 | (f->f_linecnt)++;
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | void aside(f, l)
|
---|
| 543 | struct f *f;
|
---|
| 544 | struct line *l;
|
---|
| 545 | {
|
---|
| 546 | register struct line *ll;
|
---|
| 547 |
|
---|
| 548 | if (l == 0) return;
|
---|
| 549 | if ((ll = l->l_next) != NullStructLine) {
|
---|
| 550 | while (ll->l_next) ll = ll->l_next;
|
---|
| 551 | ll->l_next = f->f_aside;
|
---|
| 552 | f->f_aside = l->l_next;
|
---|
| 553 | l->l_next = 0;
|
---|
| 554 | f->f_ewin = l;
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 | struct line *next(f)
|
---|
| 560 | register struct f *f;
|
---|
| 561 | {
|
---|
| 562 | register struct line *l;
|
---|
| 563 |
|
---|
| 564 | if ((l = f->f_aside) != NullStructLine) {
|
---|
| 565 | f->f_aside = l->l_next;
|
---|
| 566 | l->l_next = 0;
|
---|
| 567 | } else
|
---|
| 568 | l = read_line(f->f_fp);
|
---|
| 569 | if (l) {
|
---|
| 570 | if (f->f_bwin == 0)
|
---|
| 571 | f->f_bwin = f->f_ewin = l;
|
---|
| 572 | else {
|
---|
| 573 | if (f->f_ewin->l_eof && l->l_eof) {
|
---|
| 574 | free_line(l);
|
---|
| 575 | return(f->f_ewin);
|
---|
| 576 | }
|
---|
| 577 | f->f_ewin->l_next = l;
|
---|
| 578 | f->f_ewin = l;
|
---|
| 579 | }
|
---|
| 580 | }
|
---|
| 581 | return l;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 |
|
---|
| 585 | /* Init_f() initialises a window structure (struct f). <fp> is the
|
---|
| 586 | * file associated with <f>.
|
---|
| 587 | */
|
---|
| 588 | void init_f(f, fp)
|
---|
| 589 | register struct f *f;
|
---|
| 590 | FILE *fp;
|
---|
| 591 | {
|
---|
| 592 | f->f_bwin = f->f_ewin = f->f_aside = 0;
|
---|
| 593 | f->f_linecnt = 0;
|
---|
| 594 | f->f_fp = fp;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 |
|
---|
| 598 | /* Update() prints a window. <f> is a pointer to the window, <s> is the
|
---|
| 599 | * string containing the "prefix" to the printout( either "<" or ">").
|
---|
| 600 | * after completion of update(), the window is empty.
|
---|
| 601 | */
|
---|
| 602 | void update(f, s)
|
---|
| 603 | register struct f *f;
|
---|
| 604 | char *s;
|
---|
| 605 | {
|
---|
| 606 | char *help;
|
---|
| 607 | int only_dot = 0;
|
---|
| 608 |
|
---|
| 609 | if (firstoutput && (recursive_dir == SET)) {
|
---|
| 610 | printf("diff %s %s %s\n", options_string, oldfile, newfile);
|
---|
| 611 | firstoutput = 0;
|
---|
| 612 | }
|
---|
| 613 | while (f->f_bwin && f->f_bwin != f->f_ewin) {
|
---|
| 614 | if (mode != ed_mode) {
|
---|
| 615 | printf("%s%s", s, f->f_bwin->l_text);
|
---|
| 616 | } else {
|
---|
| 617 | #ifdef DEBUG
|
---|
| 618 | printf("ed_mode: test for only dot");
|
---|
| 619 | printf("%s", f->f_bwin->l_text);
|
---|
| 620 | #endif
|
---|
| 621 | help = f->f_bwin->l_text;
|
---|
| 622 | while ((*help == ' ') ||
|
---|
| 623 | (*help == '.') ||
|
---|
| 624 | (*help == '\t')) {
|
---|
| 625 | if (*(help++) == '.') only_dot++;
|
---|
| 626 | if (only_dot > 1) break;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | /* If only_dot is equal 1, there is only one dot on
|
---|
| 630 | * the line, so we have to take special actions.
|
---|
| 631 | * f the line with only one dot is found, we output
|
---|
| 632 | * two dots (".."), terminate the append modus and
|
---|
| 633 | * substitute "." for "..". Afterwards we restart
|
---|
| 634 | * with the append command. */
|
---|
| 635 | if (*help == '\n' && only_dot == 1) {
|
---|
| 636 | help = f->f_bwin->l_text;
|
---|
| 637 | while (*help != '\0') {
|
---|
| 638 | if (*help == '.') printf(".");
|
---|
| 639 | putchar((int) *(help++));
|
---|
| 640 | }
|
---|
| 641 | printf(".\n");
|
---|
| 642 | printf(".s/\\.\\././\n");
|
---|
| 643 | printf("a\n");
|
---|
| 644 | } else {
|
---|
| 645 | printf("%s%s", s, f->f_bwin->l_text);
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 | advance(f);
|
---|
| 649 | }
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | /* __Diff(), performs the "core operation" of the program.
|
---|
| 653 | * Expects two file-pointers as arguments. This functions does
|
---|
| 654 | * *not* check if the file-pointers are valid.
|
---|
| 655 | */
|
---|
| 656 |
|
---|
| 657 | void __diff(fp1, fp2)
|
---|
| 658 | FILE *fp1, *fp2;
|
---|
| 659 | {
|
---|
| 660 | struct f f1, f2;
|
---|
| 661 | struct line *l1, *s1, *b1, *l2, *s2, *b2;
|
---|
| 662 | register struct line *ll;
|
---|
| 663 |
|
---|
| 664 | init_f(&f1, fp1);
|
---|
| 665 | init_f(&f2, fp2);
|
---|
| 666 | l1 = next(&f1);
|
---|
| 667 | l2 = next(&f2);
|
---|
| 668 | while ((l1->l_eof == 0) || (l2->l_eof == 0)) {
|
---|
| 669 | if (equal_line(l1, l2)) {
|
---|
| 670 | equal:
|
---|
| 671 | advance(&f1);
|
---|
| 672 | advance(&f2);
|
---|
| 673 | l1 = next(&f1);
|
---|
| 674 | l2 = next(&f2);
|
---|
| 675 | continue;
|
---|
| 676 | }
|
---|
| 677 | s1 = b1 = l1;
|
---|
| 678 | s2 = b2 = l2;
|
---|
| 679 | /* Read several more lines */
|
---|
| 680 | next(&f1);
|
---|
| 681 | next(&f1);
|
---|
| 682 | next(&f2);
|
---|
| 683 | next(&f2);
|
---|
| 684 | /* Start searching */
|
---|
| 685 | search:
|
---|
| 686 | next(&f2);
|
---|
| 687 | ll = s1;
|
---|
| 688 | do {
|
---|
| 689 | if (equal_3(ll, b2)) {
|
---|
| 690 | l1 = ll;
|
---|
| 691 | l2 = b2;
|
---|
| 692 | aside(&f1, ll);
|
---|
| 693 | aside(&f2, b2);
|
---|
| 694 | differ(&f1, &f2);
|
---|
| 695 | goto equal;
|
---|
| 696 | }
|
---|
| 697 | if (ll->l_eof) break;
|
---|
| 698 | ll = stepup(ll);
|
---|
| 699 | } while (ll);
|
---|
| 700 | b2 = stepup(b2);
|
---|
| 701 |
|
---|
| 702 | next(&f1);
|
---|
| 703 | ll = s2;
|
---|
| 704 | do {
|
---|
| 705 | if (equal_3(b1, ll)) {
|
---|
| 706 | l1 = b1;
|
---|
| 707 | l2 = ll;
|
---|
| 708 | aside(&f2, ll);
|
---|
| 709 | aside(&f1, b1);
|
---|
| 710 | differ(&f1, &f2);
|
---|
| 711 | goto equal;
|
---|
| 712 | }
|
---|
| 713 | if (ll->l_eof != 0) break;
|
---|
| 714 | ll = stepup(ll);
|
---|
| 715 | } while (ll);
|
---|
| 716 | b1 = stepup(b1);
|
---|
| 717 |
|
---|
| 718 | goto search;
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | /* Both of the files reached EOF */
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | /* Differ() prints the differences between files. the arguments <f1> and
|
---|
| 725 | * <f2> are pointers to the two windows, where the differences are.
|
---|
| 726 | */
|
---|
| 727 | void differ(f1, f2)
|
---|
| 728 | register struct f *f1, *f2;
|
---|
| 729 | {
|
---|
| 730 | int cnt1 = f1->f_linecnt, len1 = wlen(f1);
|
---|
| 731 | int cnt2 = f2->f_linecnt, len2 = wlen(f2);
|
---|
| 732 | if ((len1 != 0) || (len2 != 0)) {
|
---|
| 733 | if (len1 == 0) {
|
---|
| 734 | if (mode == ed_mode) {
|
---|
| 735 | cnt1 += offset;
|
---|
| 736 | printf("%d a\n", cnt1);
|
---|
| 737 | update(f2, "");
|
---|
| 738 | printf(".\n");
|
---|
| 739 | offset += len2;
|
---|
| 740 | } else {
|
---|
| 741 | printf("%da", cnt1);
|
---|
| 742 | range(cnt2 + 1, cnt2 + len2);
|
---|
| 743 | }
|
---|
| 744 | } else if (len2 == 0) {
|
---|
| 745 | if (mode == ed_mode) {
|
---|
| 746 | cnt1 += offset;
|
---|
| 747 | range(cnt1 + 1, cnt1 + len1);
|
---|
| 748 | printf("d\n");
|
---|
| 749 | offset -= len1;
|
---|
| 750 | while (f1->f_bwin && f1->f_bwin != f1->f_ewin)
|
---|
| 751 | advance(f1);
|
---|
| 752 | } else {
|
---|
| 753 | range(cnt1 + 1, cnt1 + len1);
|
---|
| 754 | printf("d%d", cnt2);
|
---|
| 755 | }
|
---|
| 756 | } else {
|
---|
| 757 | if (mode != ed_mode) {
|
---|
| 758 | range(cnt1 + 1, cnt1 + len1);
|
---|
| 759 | putchar('c');
|
---|
| 760 | range(cnt2 + 1, cnt2 + len2);
|
---|
| 761 | } else {
|
---|
| 762 | cnt1 += offset;
|
---|
| 763 | if (len1 == len2) {
|
---|
| 764 | range(cnt1 + 1, cnt1 + len1);
|
---|
| 765 | printf("c\n");
|
---|
| 766 | update(f2, "");
|
---|
| 767 | printf(".\n");
|
---|
| 768 | } else {
|
---|
| 769 | range(cnt1 + 1, cnt1 + len1);
|
---|
| 770 | printf("d\n");
|
---|
| 771 | printf("%d a\n", cnt1);
|
---|
| 772 | update(f2, "");
|
---|
| 773 | printf(".\n");
|
---|
| 774 | offset -= len1 - len2;
|
---|
| 775 | }
|
---|
| 776 | while (f1->f_bwin && f1->f_bwin != f1->f_ewin)
|
---|
| 777 | advance(f1);
|
---|
| 778 | }
|
---|
| 779 | }
|
---|
| 780 | if (mode != ed_mode) {
|
---|
| 781 | putchar('\n');
|
---|
| 782 | if (len1 != 0) update(f1, "< ");
|
---|
| 783 | if ((len1 != 0) && (len2 != 0)) printf("---\n");
|
---|
| 784 | if (len2 != 0) update(f2, "> ");
|
---|
| 785 | }
|
---|
| 786 | diffs++;
|
---|
| 787 | }
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 |
|
---|
| 791 | /* Function wlen() calculates the number of lines in a window. */
|
---|
| 792 | int wlen(f)
|
---|
| 793 | struct f *f;
|
---|
| 794 | {
|
---|
| 795 | register cnt = 0;
|
---|
| 796 | register struct line *l = f->f_bwin, *e = f->f_ewin;
|
---|
| 797 |
|
---|
| 798 | while (l && l != e) {
|
---|
| 799 | cnt++;
|
---|
| 800 | l = l->l_next;
|
---|
| 801 | }
|
---|
| 802 | return cnt;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 |
|
---|
| 806 | /* Range() prints the line numbers of a range. the arguments <a> and <b>
|
---|
| 807 | * are the beginning and the ending line number of the range. if
|
---|
| 808 | * <a> == <b>, only one line number is printed. otherwise <a> and <b> are
|
---|
| 809 | * separated by a ",".
|
---|
| 810 | */
|
---|
| 811 | void range(a, b)
|
---|
| 812 | int a, b;
|
---|
| 813 | {
|
---|
| 814 | printf(((a == b) ? "%d" : "%d,%d"), a, b);
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | /* Here follows the code for option -c.
|
---|
| 818 | * This code is from the cdiff program by Larry Wall. I changed it only
|
---|
| 819 | * slightly to reflect the POSIX standard and to call the main routine
|
---|
| 820 | * as function context_diff().
|
---|
| 821 | */
|
---|
| 822 |
|
---|
| 823 | /* Cdiff - context diff Author: Larry Wall */
|
---|
| 824 |
|
---|
| 825 | /* These global variables are still here from the original cdiff program...
|
---|
| 826 | * I was to lazy just to sort them out...
|
---|
| 827 | */
|
---|
| 828 | char buff[512];
|
---|
| 829 | FILE *oldfp, *newfp;
|
---|
| 830 |
|
---|
| 831 | int oldmin, oldmax, newmin, newmax;
|
---|
| 832 | int oldbeg, oldend, newbeg, newend;
|
---|
| 833 | int preoldmax, prenewmax;
|
---|
| 834 | int preoldbeg, preoldend, prenewbeg, prenewend;
|
---|
| 835 | int oldwanted, newwanted;
|
---|
| 836 |
|
---|
| 837 | char *oldhunk, *newhunk;
|
---|
| 838 | size_t oldsize, oldalloc, newsize, newalloc;
|
---|
| 839 |
|
---|
| 840 | int oldline, newline; /* Jose */
|
---|
| 841 |
|
---|
| 842 | void cdiff(old, new, file1, file2)
|
---|
| 843 | char *old, *new; /* The names of the two files to be compared */
|
---|
| 844 | FILE *file1, *file2; /* The corresponding file-pointers */
|
---|
| 845 | {
|
---|
| 846 | FILE *inputfp;
|
---|
| 847 | struct stat statbuf;
|
---|
| 848 | register char *s;
|
---|
| 849 | char op;
|
---|
| 850 | char *newmark, *oldmark;
|
---|
| 851 | int len;
|
---|
| 852 | char *line;
|
---|
| 853 | int i, status;
|
---|
| 854 |
|
---|
| 855 | oldfp = file1;
|
---|
| 856 | newfp = file2;
|
---|
| 857 |
|
---|
| 858 | oldalloc = 512;
|
---|
| 859 | oldhunk = (char *) xmalloc(oldalloc);
|
---|
| 860 | newalloc = 512;
|
---|
| 861 | newhunk = (char *) xmalloc(newalloc);
|
---|
| 862 |
|
---|
| 863 |
|
---|
| 864 | /* The context diff spawns a new process that executes a normal diff
|
---|
| 865 | * and parses the output.
|
---|
| 866 | */
|
---|
| 867 | if (trim_blanks == SET)
|
---|
| 868 | sprintf(buff, "diff -b %s %s", old, new);
|
---|
| 869 | else
|
---|
| 870 | sprintf(buff, "diff %s %s", old, new);
|
---|
| 871 |
|
---|
| 872 | inputfp = popen(buff, "r");
|
---|
| 873 | if (!inputfp) {
|
---|
| 874 | fprintf(stderr, "Can't execute diff %s %s, popen failed with %s\n",
|
---|
| 875 | old, new, strerror(errno));
|
---|
| 876 | exit(2);
|
---|
| 877 | }
|
---|
| 878 | preoldend = -1000;
|
---|
| 879 | firstoutput = 1;
|
---|
| 880 | oldline = newline = 0;
|
---|
| 881 | while (fgets(buff, sizeof buff, inputfp) != Nullch) {
|
---|
| 882 | if (firstoutput) {
|
---|
| 883 | if (recursive_dir == SET) {
|
---|
| 884 | printf("diff %s %s %s\n", options_string,
|
---|
| 885 | oldfile, newfile);
|
---|
| 886 | }
|
---|
| 887 | fstat(fileno(oldfp), &statbuf);
|
---|
| 888 | printf("*** %s %s", old, ctime(&statbuf.st_mtime));
|
---|
| 889 | fstat(fileno(newfp), &statbuf);
|
---|
| 890 | printf("--- %s %s", new, ctime(&statbuf.st_mtime));
|
---|
| 891 | firstoutput = 0;
|
---|
| 892 | }
|
---|
| 893 | if (isdigit(*buff)) {
|
---|
| 894 | oldmin = atoi(buff);
|
---|
| 895 | for (s = buff; isdigit(*s); s++);
|
---|
| 896 | if (*s == ',') {
|
---|
| 897 | s++;
|
---|
| 898 | oldmax = atoi(s);
|
---|
| 899 | for (; isdigit(*s); s++);
|
---|
| 900 | } else {
|
---|
| 901 | oldmax = oldmin;
|
---|
| 902 | }
|
---|
| 903 | if (*s != 'a' && *s != 'd' && *s != 'c') {
|
---|
| 904 | fprintf(stderr, "Unparseable input: %s", s);
|
---|
| 905 | exit(2);
|
---|
| 906 | }
|
---|
| 907 | op = *s;
|
---|
| 908 | s++;
|
---|
| 909 | newmin = atoi(s);
|
---|
| 910 | for (; isdigit(*s); s++);
|
---|
| 911 | if (*s == ',') {
|
---|
| 912 | s++;
|
---|
| 913 | newmax = atoi(s);
|
---|
| 914 | for (; isdigit(*s); s++);
|
---|
| 915 | } else {
|
---|
| 916 | newmax = newmin;
|
---|
| 917 | }
|
---|
| 918 | if (*s != '\n' && *s != ' ') {
|
---|
| 919 | fprintf(stderr, "Unparseable input: %s", s);
|
---|
| 920 | exit(2);
|
---|
| 921 | }
|
---|
| 922 | newmark = oldmark = "! ";
|
---|
| 923 | if (op == 'a') {
|
---|
| 924 | oldmin++;
|
---|
| 925 | newmark = "+ ";
|
---|
| 926 | }
|
---|
| 927 | if (op == 'd') {
|
---|
| 928 | newmin++;
|
---|
| 929 | oldmark = "- ";
|
---|
| 930 | }
|
---|
| 931 | oldbeg = oldmin - context_lines;
|
---|
| 932 | oldend = oldmax + context_lines;
|
---|
| 933 | if (oldbeg < 1) oldbeg = 1;
|
---|
| 934 | newbeg = newmin - context_lines;
|
---|
| 935 | newend = newmax + context_lines;
|
---|
| 936 | if (newbeg < 1) newbeg = 1;
|
---|
| 937 |
|
---|
| 938 | if (preoldend < oldbeg - 1) {
|
---|
| 939 | if (preoldend >= 0) {
|
---|
| 940 | dumphunk();
|
---|
| 941 | }
|
---|
| 942 | preoldbeg = oldbeg;
|
---|
| 943 | prenewbeg = newbeg;
|
---|
| 944 | oldwanted = newwanted = 0;
|
---|
| 945 | oldsize = newsize = 0;
|
---|
| 946 | } else { /* we want to append to previous hunk */
|
---|
| 947 | oldbeg = preoldmax + 1;
|
---|
| 948 | newbeg = prenewmax + 1;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | for (i = oldbeg; i <= oldmax; i++) {
|
---|
| 952 | line = getold(i);
|
---|
| 953 | if (!line) {
|
---|
| 954 | oldend = oldmax = i - 1;
|
---|
| 955 | break;
|
---|
| 956 | }
|
---|
| 957 | len = strlen(line) + 2;
|
---|
| 958 | if (oldsize + len + 1 >= oldalloc) {
|
---|
| 959 | oldalloc *= 2;
|
---|
| 960 | oldhunk = (char *) xrealloc(oldhunk, oldalloc);
|
---|
| 961 | }
|
---|
| 962 | if (i >= oldmin) {
|
---|
| 963 | strcpy(oldhunk + oldsize, oldmark);
|
---|
| 964 | oldwanted++;
|
---|
| 965 | } else {
|
---|
| 966 | strcpy(oldhunk + oldsize, " ");
|
---|
| 967 | }
|
---|
| 968 | strcpy(oldhunk + oldsize + 2, line);
|
---|
| 969 | oldsize += len;
|
---|
| 970 | }
|
---|
| 971 | preoldmax = oldmax;
|
---|
| 972 | preoldend = oldend;
|
---|
| 973 |
|
---|
| 974 | for (i = newbeg; i <= newmax; i++) {
|
---|
| 975 | line = getnew(i);
|
---|
| 976 | if (!line) {
|
---|
| 977 | newend = newmax = i - 1;
|
---|
| 978 | break;
|
---|
| 979 | }
|
---|
| 980 | len = strlen(line) + 2;
|
---|
| 981 | if (newsize + len + 1 >= newalloc) {
|
---|
| 982 | newalloc *= 2;
|
---|
| 983 | newhunk = (char *) xrealloc(newhunk, newalloc);
|
---|
| 984 | }
|
---|
| 985 | if (i >= newmin) {
|
---|
| 986 | strcpy(newhunk + newsize, newmark);
|
---|
| 987 | newwanted++;
|
---|
| 988 | } else {
|
---|
| 989 | strcpy(newhunk + newsize, " ");
|
---|
| 990 | }
|
---|
| 991 | strcpy(newhunk + newsize + 2, line);
|
---|
| 992 | newsize += len;
|
---|
| 993 | }
|
---|
| 994 | prenewmax = newmax;
|
---|
| 995 | prenewend = newend;
|
---|
| 996 | }
|
---|
| 997 | }
|
---|
| 998 | status = pclose(inputfp);
|
---|
| 999 | if (status != 0) diffs++;
|
---|
| 1000 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 1) severe_error = 1;
|
---|
| 1001 |
|
---|
| 1002 | if (preoldend >= 0) {
|
---|
| 1003 | dumphunk();
|
---|
| 1004 | }
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | void dumphunk()
|
---|
| 1008 | {
|
---|
| 1009 | int i;
|
---|
| 1010 | char *line;
|
---|
| 1011 | int len;
|
---|
| 1012 |
|
---|
| 1013 | for (i = preoldmax + 1; i <= preoldend; i++) {
|
---|
| 1014 | line = getold(i);
|
---|
| 1015 | if (!line) {
|
---|
| 1016 | preoldend = i - 1;
|
---|
| 1017 | break;
|
---|
| 1018 | }
|
---|
| 1019 | len = strlen(line) + 2;
|
---|
| 1020 | if (oldsize + len + 1 >= oldalloc) {
|
---|
| 1021 | oldalloc *= 2;
|
---|
| 1022 | oldhunk = (char *) xrealloc(oldhunk, oldalloc);
|
---|
| 1023 | }
|
---|
| 1024 | strcpy(oldhunk + oldsize, " ");
|
---|
| 1025 | strcpy(oldhunk + oldsize + 2, line);
|
---|
| 1026 | oldsize += len;
|
---|
| 1027 | }
|
---|
| 1028 | for (i = prenewmax + 1; i <= prenewend; i++) {
|
---|
| 1029 | line = getnew(i);
|
---|
| 1030 | if (!line) {
|
---|
| 1031 | prenewend = i - 1;
|
---|
| 1032 | break;
|
---|
| 1033 | }
|
---|
| 1034 | len = strlen(line) + 2;
|
---|
| 1035 | if (newsize + len + 1 >= newalloc) {
|
---|
| 1036 | newalloc *= 2;
|
---|
| 1037 | newhunk = (char *) xrealloc(newhunk, newalloc);
|
---|
| 1038 | }
|
---|
| 1039 | strcpy(newhunk + newsize, " ");
|
---|
| 1040 | strcpy(newhunk + newsize + 2, line);
|
---|
| 1041 | newsize += len;
|
---|
| 1042 | }
|
---|
| 1043 | fputs("***************\n", stdout);
|
---|
| 1044 | if (preoldbeg >= preoldend) {
|
---|
| 1045 | printf("*** %d ****\n", preoldend);
|
---|
| 1046 | } else {
|
---|
| 1047 | printf("*** %d,%d ****\n", preoldbeg, preoldend);
|
---|
| 1048 | }
|
---|
| 1049 | if (oldwanted) {
|
---|
| 1050 | fputs(oldhunk, stdout);
|
---|
| 1051 | }
|
---|
| 1052 | oldsize = 0;
|
---|
| 1053 | *oldhunk = '\0';
|
---|
| 1054 | if (prenewbeg >= prenewend) {
|
---|
| 1055 | printf("--- %d ----\n", prenewend);
|
---|
| 1056 | } else {
|
---|
| 1057 | printf("--- %d,%d ----\n", prenewbeg, prenewend);
|
---|
| 1058 | }
|
---|
| 1059 | if (newwanted) {
|
---|
| 1060 | fputs(newhunk, stdout);
|
---|
| 1061 | }
|
---|
| 1062 | newsize = 0;
|
---|
| 1063 | *newhunk = '\0';
|
---|
| 1064 | }
|
---|
| 1065 |
|
---|
| 1066 | char *getold(targ)
|
---|
| 1067 | int targ;
|
---|
| 1068 | {
|
---|
| 1069 | while (fgets(buff, sizeof buff, oldfp) != Nullch) {
|
---|
| 1070 | oldline++;
|
---|
| 1071 | if (oldline == targ) return buff;
|
---|
| 1072 | }
|
---|
| 1073 | return Nullch;
|
---|
| 1074 | }
|
---|
| 1075 |
|
---|
| 1076 | char *getnew(targ)
|
---|
| 1077 | int targ;
|
---|
| 1078 | {
|
---|
| 1079 | while (fgets(buff, sizeof buff, newfp) != Nullch) {
|
---|
| 1080 | newline++;
|
---|
| 1081 | if (newline == targ) return buff;
|
---|
| 1082 | }
|
---|
| 1083 | return Nullch;
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 |
|
---|
| 1087 | /* Isdir() checks, if <path> is the name of a directory. a return value
|
---|
| 1088 | * is 0, <path> is a normal file. otherwise the <path> is a directory.
|
---|
| 1089 | */
|
---|
| 1090 | int isdir(path)
|
---|
| 1091 | char *path;
|
---|
| 1092 | {
|
---|
| 1093 | struct stat buf;
|
---|
| 1094 | stat(path, &buf);
|
---|
| 1095 | if (buf.st_mode & S_IFDIR) { /* path is a directory */
|
---|
| 1096 | return(~0);
|
---|
| 1097 | } else {
|
---|
| 1098 | return(0);
|
---|
| 1099 | }
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
| 1102 |
|
---|
| 1103 |
|
---|
| 1104 | /* This is the "main" function if a diff of two directories has to be
|
---|
| 1105 | * done. diff_recursive() expects the names of the two directories to
|
---|
| 1106 | * be compared. */
|
---|
| 1107 | void diff_recursive(dir1, dir2)
|
---|
| 1108 | char *dir1, *dir2;
|
---|
| 1109 | {
|
---|
| 1110 | FILE *ls1, *ls2;
|
---|
| 1111 | char file1[PATH_MAX], file2[PATH_MAX];
|
---|
| 1112 | char jointfile1[PATH_MAX], jointfile2[PATH_MAX];
|
---|
| 1113 | char command[PATH_MAX];
|
---|
| 1114 | int difference, eof1, eof2;
|
---|
| 1115 |
|
---|
| 1116 | sprintf(command, "ls %s", dir1);
|
---|
| 1117 | ls1 = popen(command, "r");
|
---|
| 1118 | sprintf(command, "ls %s", dir2);
|
---|
| 1119 | ls2 = popen(command, "r");
|
---|
| 1120 |
|
---|
| 1121 | if ((ls1 == NULL) || (ls2 == NULL))
|
---|
| 1122 | fatal_error("cannot execute ls!", "");
|
---|
| 1123 |
|
---|
| 1124 | file1[0] = '\0';
|
---|
| 1125 | eof1 = fscanf(ls1, "%s\n", file1);
|
---|
| 1126 | file2[0] = '\0';
|
---|
| 1127 | eof2 = fscanf(ls2, "%s\n", file2);
|
---|
| 1128 |
|
---|
| 1129 | while ((file1[0] != '\0') && (file2[0] != '\0')) {
|
---|
| 1130 | difference = strcmp(file1, file2);
|
---|
| 1131 | while (difference != 0) {
|
---|
| 1132 | if (difference < 0) {
|
---|
| 1133 | printf("Only in %s: %s\n", dir1, file1);
|
---|
| 1134 | file1[0] = '\0';
|
---|
| 1135 | eof1 = fscanf(ls1, "%s\n", file1);
|
---|
| 1136 | if (file1[0] == '\0') break;
|
---|
| 1137 | } else {
|
---|
| 1138 | printf("Only in %s: %s\n", dir2, file2);
|
---|
| 1139 | file2[0] = '\0';
|
---|
| 1140 | eof2 = fscanf(ls2, "%s\n", file2);
|
---|
| 1141 | if (file2[0] == '\0') break;
|
---|
| 1142 | }
|
---|
| 1143 | difference = strcmp(file1, file2);
|
---|
| 1144 | }
|
---|
| 1145 | if (eof1 != EOF && eof2 != EOF) {
|
---|
| 1146 | strcpy(jointfile1, dir1);
|
---|
| 1147 | strcat(jointfile1, "/");
|
---|
| 1148 | strcat(jointfile1, file1);
|
---|
| 1149 | strcpy(jointfile2, dir2);
|
---|
| 1150 | strcat(jointfile2, "/");
|
---|
| 1151 | strcat(jointfile2, file2);
|
---|
| 1152 |
|
---|
| 1153 | if ((isdir(jointfile1) != 0) && (isdir(jointfile2) != 0)) {
|
---|
| 1154 | printf("Common subdirectories: %s and %s\n",
|
---|
| 1155 | jointfile1, jointfile2);
|
---|
| 1156 | diff_recursive(jointfile1, jointfile2);
|
---|
| 1157 | } else {
|
---|
| 1158 | firstoutput = 1;
|
---|
| 1159 | strcpy(oldfile, jointfile1);
|
---|
| 1160 | strcpy(newfile, jointfile2);
|
---|
| 1161 | diff(jointfile1, jointfile2);
|
---|
| 1162 | }
|
---|
| 1163 | file1[0] = '\0';
|
---|
| 1164 | eof1 = fscanf(ls1, "%s\n", file1);
|
---|
| 1165 | file2[0] = '\0';
|
---|
| 1166 | eof2 = fscanf(ls2, "%s\n", file2);
|
---|
| 1167 | }
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
| 1170 | if (file1[0] != '\0') { /* first arg still has files */
|
---|
| 1171 | do {
|
---|
| 1172 | printf("Only in %s: %s\n", dir1, file1);
|
---|
| 1173 | eof1 = fscanf(ls1, " %s\n", file1);
|
---|
| 1174 | } while (eof1 != EOF);
|
---|
| 1175 | }
|
---|
| 1176 | if (file2[0] != '\0') {
|
---|
| 1177 | do {
|
---|
| 1178 | printf("Only in %s: %s\n", dir2, file2);
|
---|
| 1179 | eof2 = fscanf(ls2, " %s\n", file2);
|
---|
| 1180 | } while (eof2 != EOF);
|
---|
| 1181 | }
|
---|
| 1182 | if (pclose(ls1) != 0) severe_error = 1;
|
---|
| 1183 | if (pclose(ls2) != 0) severe_error = 1;
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 |
|
---|
| 1187 | /* File_type_error is called, if in a recursive diff ( -r) one of the two
|
---|
| 1188 | * files a block special, a character special or a FIFO special file is.
|
---|
| 1189 | * The corresponding error message is printed here. */
|
---|
| 1190 | void file_type_error(filename1, filename2, statbuf1, statbuf2)
|
---|
| 1191 | char *filename1, *filename2;
|
---|
| 1192 | struct stat *statbuf1, *statbuf2;
|
---|
| 1193 | {
|
---|
| 1194 | char type1[25], type2[25];
|
---|
| 1195 |
|
---|
| 1196 | switch (statbuf1->st_mode & S_IFMT) { /* select only file mode */
|
---|
| 1197 | case S_IFREG:
|
---|
| 1198 | sprintf(type1, "regular file ");
|
---|
| 1199 | break;
|
---|
| 1200 | case S_IFBLK:
|
---|
| 1201 | sprintf(type1, "block special file ");
|
---|
| 1202 | break;
|
---|
| 1203 | case S_IFDIR: sprintf(type1, "directory "); break;
|
---|
| 1204 | case S_IFCHR:
|
---|
| 1205 | sprintf(type1, "character special file ");
|
---|
| 1206 | break;
|
---|
| 1207 | case S_IFIFO:
|
---|
| 1208 | sprintf(type1, "FIFO special file ");
|
---|
| 1209 | break;
|
---|
| 1210 | }
|
---|
| 1211 |
|
---|
| 1212 | switch (statbuf2->st_mode & S_IFMT) { /* select only file mode */
|
---|
| 1213 | case S_IFREG:
|
---|
| 1214 | sprintf(type2, "regular file ");
|
---|
| 1215 | break;
|
---|
| 1216 | case S_IFBLK:
|
---|
| 1217 | sprintf(type2, "block special file ");
|
---|
| 1218 | break;
|
---|
| 1219 | case S_IFDIR: sprintf(type2, "directory "); break;
|
---|
| 1220 | case S_IFCHR:
|
---|
| 1221 | sprintf(type2, "character special file ");
|
---|
| 1222 | break;
|
---|
| 1223 | case S_IFIFO:
|
---|
| 1224 | sprintf(type2, "FIFO special file ");
|
---|
| 1225 | break;
|
---|
| 1226 | }
|
---|
| 1227 | printf("File %s is a %s while file %s is a %s\n",
|
---|
| 1228 | filename1, type1, filename2, type2);
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | void *xmalloc(size)
|
---|
| 1232 | size_t size;
|
---|
| 1233 | {
|
---|
| 1234 | void *ptr;
|
---|
| 1235 |
|
---|
| 1236 | ptr = malloc(size);
|
---|
| 1237 | if (ptr == NULL) {
|
---|
| 1238 | fprintf(stderr, "%s: out of memory\n", progname);
|
---|
| 1239 | exit(2);
|
---|
| 1240 | }
|
---|
| 1241 | return(ptr);
|
---|
| 1242 | }
|
---|
| 1243 |
|
---|
| 1244 | void *xrealloc(ptr, size)
|
---|
| 1245 | void *ptr;
|
---|
| 1246 | size_t size;
|
---|
| 1247 | {
|
---|
| 1248 | ptr = realloc(ptr, size);
|
---|
| 1249 | if (ptr == NULL) {
|
---|
| 1250 | fprintf(stderr, "%s: out of memory\n", progname);
|
---|
| 1251 | exit(2);
|
---|
| 1252 | }
|
---|
| 1253 | return(ptr);
|
---|
| 1254 | }
|
---|