17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
2378eb75caSchin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*247ccfcdSToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate * diff - differential file comparison
427c478bd9Sstevel@tonic-gate *
437c478bd9Sstevel@tonic-gate * Uses an algorithm which finds
447c478bd9Sstevel@tonic-gate * a pair of longest identical subsequences in the two
457c478bd9Sstevel@tonic-gate * files.
467c478bd9Sstevel@tonic-gate *
477c478bd9Sstevel@tonic-gate * The major goal is to generate the match vector J.
487c478bd9Sstevel@tonic-gate * J[i] is the index of the line in file1 corresponding
497c478bd9Sstevel@tonic-gate * to line i file0. J[i] = 0 if there is no
507c478bd9Sstevel@tonic-gate * such line in file1.
517c478bd9Sstevel@tonic-gate *
527c478bd9Sstevel@tonic-gate * Lines are hashed so as to work in core. All potential
537c478bd9Sstevel@tonic-gate * matches are located by sorting the lines of each file
547c478bd9Sstevel@tonic-gate * on the hash (called value). In particular, this
557c478bd9Sstevel@tonic-gate * collects the equivalence classes in file1 together.
567c478bd9Sstevel@tonic-gate * Subroutine equiv replaces the value of each line in
577c478bd9Sstevel@tonic-gate * file0 by the index of the first element of its
587c478bd9Sstevel@tonic-gate * matching equivalence in (the reordered) file1.
597c478bd9Sstevel@tonic-gate * To save space equiv squeezes file1 into a single
607c478bd9Sstevel@tonic-gate * array member in which the equivalence classes
617c478bd9Sstevel@tonic-gate * are simply concatenated, except that their first
627c478bd9Sstevel@tonic-gate * members are flagged by changing sign.
637c478bd9Sstevel@tonic-gate *
647c478bd9Sstevel@tonic-gate * Next the indices that point into member are unsorted into
657c478bd9Sstevel@tonic-gate * array class according to the original order of file0.
667c478bd9Sstevel@tonic-gate *
677c478bd9Sstevel@tonic-gate * The cleverness lies in routine stone. This marches
687c478bd9Sstevel@tonic-gate * through the lines of file0, developing a vector klist
697c478bd9Sstevel@tonic-gate * of "k-candidates". At step i a k-candidate is a matched
707c478bd9Sstevel@tonic-gate * pair of lines x,y (x in file0 y in file1) such that
717c478bd9Sstevel@tonic-gate * there is a common subsequence of lenght k
727c478bd9Sstevel@tonic-gate * between the first i lines of file0 and the first y
737c478bd9Sstevel@tonic-gate * lines of file1, but there is no such subsequence for
747c478bd9Sstevel@tonic-gate * any smaller y. x is the earliest possible mate to y
757c478bd9Sstevel@tonic-gate * that occurs in such a subsequence.
767c478bd9Sstevel@tonic-gate *
777c478bd9Sstevel@tonic-gate * Whenever any of the members of the equivalence class of
787c478bd9Sstevel@tonic-gate * lines in file1 matable to a line in file0 has serial number
797c478bd9Sstevel@tonic-gate * less than the y of some k-candidate, that k-candidate
807c478bd9Sstevel@tonic-gate * with the smallest such y is replaced. The new
817c478bd9Sstevel@tonic-gate * k-candidate is chained (via pred) to the current
827c478bd9Sstevel@tonic-gate * k-1 candidate so that the actual subsequence can
837c478bd9Sstevel@tonic-gate * be recovered. When a member has serial number greater
847c478bd9Sstevel@tonic-gate * that the y of all k-candidates, the klist is extended.
857c478bd9Sstevel@tonic-gate * At the end, the longest subsequence is pulled out
867c478bd9Sstevel@tonic-gate * and placed in the array J by unravel.
877c478bd9Sstevel@tonic-gate *
887c478bd9Sstevel@tonic-gate * With J in hand, the matches there recorded are
897c478bd9Sstevel@tonic-gate * checked against reality to assure that no spurious
907c478bd9Sstevel@tonic-gate * matches have crept in due to hashing. If they have,
917c478bd9Sstevel@tonic-gate * they are broken, and "jackpot " is recorded--a harmless
927c478bd9Sstevel@tonic-gate * matter except that a true match for a spuriously
937c478bd9Sstevel@tonic-gate * mated line may now be unnecessarily reported as a change.
947c478bd9Sstevel@tonic-gate *
957c478bd9Sstevel@tonic-gate * Much of the complexity of the program comes simply
967c478bd9Sstevel@tonic-gate * from trying to minimize core utilization and
977c478bd9Sstevel@tonic-gate * maximize the range of doable problems by dynamically
987c478bd9Sstevel@tonic-gate * allocating what is needed and reusing what is not.
997c478bd9Sstevel@tonic-gate * The core requirements for problems larger than somewhat
1007c478bd9Sstevel@tonic-gate * are (in words) 2*length(file0) + length(file1) +
1017c478bd9Sstevel@tonic-gate * 3*(number of k-candidates installed), typically about
1027c478bd9Sstevel@tonic-gate * 6n words for files of length n.
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate #include <stdio.h>
1057c478bd9Sstevel@tonic-gate #include <wchar.h>
1067c478bd9Sstevel@tonic-gate #include <ctype.h>
1077c478bd9Sstevel@tonic-gate #include <stdlib.h>
1087c478bd9Sstevel@tonic-gate #include <limits.h>
1097c478bd9Sstevel@tonic-gate #include <sys/types.h>
1107c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1117c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1127c478bd9Sstevel@tonic-gate #include <unistd.h>
1137c478bd9Sstevel@tonic-gate #include <signal.h>
1147c478bd9Sstevel@tonic-gate #include <fcntl.h>
1157c478bd9Sstevel@tonic-gate #include <dirent.h>
1167c478bd9Sstevel@tonic-gate #include <locale.h>
1177c478bd9Sstevel@tonic-gate #include <stdarg.h>
1187c478bd9Sstevel@tonic-gate #include <errno.h>
1197c478bd9Sstevel@tonic-gate #include <string.h>
1207c478bd9Sstevel@tonic-gate #include "diff.h"
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate #define CHRTRAN(x) (iflag ? (iswupper(x) ? towlower(x) : (x)) : (x))
1237c478bd9Sstevel@tonic-gate #define NCCHRTRAN(x) (iswupper(x) ? towlower(x) : (x))
1247c478bd9Sstevel@tonic-gate #define max(a, b) ((a) < (b) ? (b) : (a))
1257c478bd9Sstevel@tonic-gate #define min(a, b) ((a) > (b) ? (b) : (a))
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate int pref, suff; /* length of prefix and suffix */
1287c478bd9Sstevel@tonic-gate int *class; /* will be overlaid on file[0] */
1297c478bd9Sstevel@tonic-gate int *member; /* will be overlaid on file[1] */
1307c478bd9Sstevel@tonic-gate int *klist; /* will be overlaid on file[0] after class */
1317c478bd9Sstevel@tonic-gate struct cand *clist; /* merely a free storage pot for candidates */
1327c478bd9Sstevel@tonic-gate int clen = 0;
1337c478bd9Sstevel@tonic-gate int *J; /* will be overlaid on class */
1347c478bd9Sstevel@tonic-gate long *ixold; /* will be overlaid on klist */
1357c478bd9Sstevel@tonic-gate long *ixnew; /* will be overlaid on file[1] */
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate static int mbcurmax;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate static void error(const char *);
1407c478bd9Sstevel@tonic-gate static void unravel(int);
1417c478bd9Sstevel@tonic-gate static void check(void);
1427c478bd9Sstevel@tonic-gate static void output(void);
1437c478bd9Sstevel@tonic-gate static void change(int, int, int, int);
1447c478bd9Sstevel@tonic-gate static void range(int, int, char *);
1457c478bd9Sstevel@tonic-gate static void fetch(long *, int, int, int, char *, int);
1467c478bd9Sstevel@tonic-gate static void dump_context_vec(void);
1477c478bd9Sstevel@tonic-gate static void diffdir(char **);
1487c478bd9Sstevel@tonic-gate static void setfile(char **, char **, char *);
1497c478bd9Sstevel@tonic-gate static void scanpr(struct dir *, int, char *, char *,
1507c478bd9Sstevel@tonic-gate char *, char *, char *);
1517c478bd9Sstevel@tonic-gate static void only(struct dir *, int);
1527c478bd9Sstevel@tonic-gate static void sort(struct line *, int);
1537c478bd9Sstevel@tonic-gate static void unsort(struct line *, int, int *);
1547c478bd9Sstevel@tonic-gate static void filename(char **, char **, struct stat *, char **);
1557c478bd9Sstevel@tonic-gate static void prepare(int, char *);
1567c478bd9Sstevel@tonic-gate static void prune(void);
1577c478bd9Sstevel@tonic-gate static void equiv(struct line *, int, struct line *, int, int *);
1587c478bd9Sstevel@tonic-gate static void done(void);
1597c478bd9Sstevel@tonic-gate static void noroom(void);
1607c478bd9Sstevel@tonic-gate static void usage(void);
1617c478bd9Sstevel@tonic-gate static void initbuf(FILE *, int, long);
1627c478bd9Sstevel@tonic-gate static void resetbuf(int);
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate static int stone(int *, int, int *, int *);
1657c478bd9Sstevel@tonic-gate static int newcand(int, int, int);
1667c478bd9Sstevel@tonic-gate static int search(int *, int, int);
1677c478bd9Sstevel@tonic-gate static int skipline(int);
1687c478bd9Sstevel@tonic-gate static int readhash(FILE *, int, char *);
1697c478bd9Sstevel@tonic-gate static int entcmp(struct dir *, struct dir *);
1707c478bd9Sstevel@tonic-gate static int compare(struct dir *);
1717c478bd9Sstevel@tonic-gate static int calldiff(char *);
1727c478bd9Sstevel@tonic-gate static int binary(int);
1737c478bd9Sstevel@tonic-gate static int filebinary(FILE *);
1747c478bd9Sstevel@tonic-gate static int isbinary(char *, int);
1757c478bd9Sstevel@tonic-gate static int useless(char *);
1767c478bd9Sstevel@tonic-gate static char *copytemp(char *);
1777c478bd9Sstevel@tonic-gate static char *pfiletype(mode_t);
1787c478bd9Sstevel@tonic-gate static struct dir *setupdir(char *);
1797c478bd9Sstevel@tonic-gate static wint_t getbufwchar(int, int *);
1807c478bd9Sstevel@tonic-gate static wint_t wcput(wint_t);
1817c478bd9Sstevel@tonic-gate static long ftellbuf(int);
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate * error message string constants
1867c478bd9Sstevel@tonic-gate */
1877c478bd9Sstevel@tonic-gate #define BAD_MB_ERR "invalid multibyte character encountered"
1887c478bd9Sstevel@tonic-gate #define NO_PROCS_ERR "no more processes"
1897c478bd9Sstevel@tonic-gate #define NO_MEM_ERR "out of memory"
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate static void *
talloc(size_t n)1927c478bd9Sstevel@tonic-gate talloc(size_t n)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate void *p;
1957c478bd9Sstevel@tonic-gate p = malloc(n);
1967c478bd9Sstevel@tonic-gate if (p == NULL)
1977c478bd9Sstevel@tonic-gate noroom();
1987c478bd9Sstevel@tonic-gate return (p);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate static void *
ralloc(void * p,size_t n)2027c478bd9Sstevel@tonic-gate ralloc(void *p, size_t n) /* compacting reallocation */
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate void *q;
2057c478bd9Sstevel@tonic-gate #if 0
2067c478bd9Sstevel@tonic-gate free(p);
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate q = realloc(p, n);
2097c478bd9Sstevel@tonic-gate if (q == NULL)
2107c478bd9Sstevel@tonic-gate noroom();
2117c478bd9Sstevel@tonic-gate return (q);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate
21578eb75caSchin int
main(int argc,char ** argv)2167c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate int k;
2197c478bd9Sstevel@tonic-gate char *argp;
2207c478bd9Sstevel@tonic-gate int flag; /* option flag read by getopt() */
2217c478bd9Sstevel@tonic-gate int i, j;
2227c478bd9Sstevel@tonic-gate char buf1[BUFSIZ], buf2[BUFSIZ];
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2267c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
2277c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
2287c478bd9Sstevel@tonic-gate #endif
2297c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate mbcurmax = MB_CUR_MAX;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate diffargv = argv;
2347c478bd9Sstevel@tonic-gate whichtemp = 0;
2357c478bd9Sstevel@tonic-gate while ((flag = getopt(argc, argv, "bitwcuefhnlrsC:D:S:U:")) != EOF) {
2367c478bd9Sstevel@tonic-gate switch (flag) {
2377c478bd9Sstevel@tonic-gate case 'D':
2387c478bd9Sstevel@tonic-gate opt = D_IFDEF;
2397c478bd9Sstevel@tonic-gate wantelses = 1;
2407c478bd9Sstevel@tonic-gate ifdef1 = "";
2417c478bd9Sstevel@tonic-gate ifdef2 = optarg;
2427c478bd9Sstevel@tonic-gate break;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate case 'b':
2457c478bd9Sstevel@tonic-gate bflag = 1;
2467c478bd9Sstevel@tonic-gate break;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate case 'C':
2497c478bd9Sstevel@tonic-gate case 'U':
2507c478bd9Sstevel@tonic-gate opt = D_CONTEXT;
2517c478bd9Sstevel@tonic-gate argp = optarg;
2527c478bd9Sstevel@tonic-gate context = 0;
2537c478bd9Sstevel@tonic-gate while (*argp >= '0' && *argp <= '9')
2547c478bd9Sstevel@tonic-gate context *= 10, context += *argp++ - '0';
2557c478bd9Sstevel@tonic-gate if (*argp)
2567c478bd9Sstevel@tonic-gate error(gettext("use [ -C num | -U num ]"));
2577c478bd9Sstevel@tonic-gate if (flag == 'U')
2587c478bd9Sstevel@tonic-gate uflag++;
2597c478bd9Sstevel@tonic-gate else
2607c478bd9Sstevel@tonic-gate uflag = 0;
2617c478bd9Sstevel@tonic-gate break;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate case 'c':
2647c478bd9Sstevel@tonic-gate case 'u':
2657c478bd9Sstevel@tonic-gate opt = D_CONTEXT;
2667c478bd9Sstevel@tonic-gate context = 3;
2677c478bd9Sstevel@tonic-gate if (flag == 'u')
2687c478bd9Sstevel@tonic-gate uflag++;
2697c478bd9Sstevel@tonic-gate else
2707c478bd9Sstevel@tonic-gate uflag = 0;
2717c478bd9Sstevel@tonic-gate break;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate case 'e':
2747c478bd9Sstevel@tonic-gate opt = D_EDIT;
2757c478bd9Sstevel@tonic-gate break;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate case 'f':
2787c478bd9Sstevel@tonic-gate opt = D_REVERSE;
2797c478bd9Sstevel@tonic-gate break;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate case 'h':
2827c478bd9Sstevel@tonic-gate hflag++;
2837c478bd9Sstevel@tonic-gate break;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate case 'i':
2867c478bd9Sstevel@tonic-gate iflag = 1;
2877c478bd9Sstevel@tonic-gate break;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate case 'l':
2907c478bd9Sstevel@tonic-gate lflag = 1;
2917c478bd9Sstevel@tonic-gate break;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate case 'n':
2947c478bd9Sstevel@tonic-gate opt = D_NREVERSE;
2957c478bd9Sstevel@tonic-gate break;
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate case 'r':
2987c478bd9Sstevel@tonic-gate rflag = 1;
2997c478bd9Sstevel@tonic-gate break;
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate case 'S':
3027c478bd9Sstevel@tonic-gate (void) strcpy(start, optarg);
3037c478bd9Sstevel@tonic-gate break;
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate case 's':
3067c478bd9Sstevel@tonic-gate sflag = 1;
3077c478bd9Sstevel@tonic-gate break;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate case 't':
3107c478bd9Sstevel@tonic-gate tflag = 1;
3117c478bd9Sstevel@tonic-gate break;
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate case 'w':
3147c478bd9Sstevel@tonic-gate wflag = 1;
3157c478bd9Sstevel@tonic-gate break;
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate case '?':
3187c478bd9Sstevel@tonic-gate usage();
3197c478bd9Sstevel@tonic-gate break;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate default:
3227c478bd9Sstevel@tonic-gate /* Not sure how it would get here, but just in case */
3237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
3247c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
325*247ccfcdSToomas Soome gettext("invalid option -%c\n"), flag);
3267c478bd9Sstevel@tonic-gate usage();
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate argc -= optind;
3317c478bd9Sstevel@tonic-gate argv = &argv[optind];
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (opt != D_CONTEXT && uflag)
3347c478bd9Sstevel@tonic-gate uflag = 0;
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate if (argc != 2)
3377c478bd9Sstevel@tonic-gate error(gettext("two filename arguments required"));
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate file1 = argv[0];
3407c478bd9Sstevel@tonic-gate file2 = argv[1];
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate if (hflag) {
3437c478bd9Sstevel@tonic-gate if (opt) {
344*247ccfcdSToomas Soome error(gettext(
345*247ccfcdSToomas Soome "-h doesn't support -e, -f, -n, -c, or -I"));
3467c478bd9Sstevel@tonic-gate } else {
3477c478bd9Sstevel@tonic-gate diffargv[0] = "diffh";
3487c478bd9Sstevel@tonic-gate (void) execv(diffh, diffargv);
3497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diffh: ");
3507c478bd9Sstevel@tonic-gate perror(diffh);
3517c478bd9Sstevel@tonic-gate status = 2;
3527c478bd9Sstevel@tonic-gate done();
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate if (strcmp(file1, "-") == 0) {
3587c478bd9Sstevel@tonic-gate if (fstat(fileno(stdin), &stb1) == 0)
3597c478bd9Sstevel@tonic-gate stb1.st_mode = S_IFREG;
3607c478bd9Sstevel@tonic-gate else {
3617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
3627c478bd9Sstevel@tonic-gate perror("stdin");
3637c478bd9Sstevel@tonic-gate done();
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate } else if (stat(file1, &stb1) < 0) {
3667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
3677c478bd9Sstevel@tonic-gate perror(file1);
3687c478bd9Sstevel@tonic-gate done();
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate if (strcmp(file2, "-") == 0) {
3727c478bd9Sstevel@tonic-gate if (strcmp(file1, "-") == 0)
3737c478bd9Sstevel@tonic-gate error(gettext("cannot specify - -"));
3747c478bd9Sstevel@tonic-gate else {
3757c478bd9Sstevel@tonic-gate if (fstat(fileno(stdin), &stb2) == 0)
3767c478bd9Sstevel@tonic-gate stb2.st_mode = S_IFREG;
3777c478bd9Sstevel@tonic-gate else {
3787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
3797c478bd9Sstevel@tonic-gate perror("stdin");
3807c478bd9Sstevel@tonic-gate done();
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate } else if (stat(file2, &stb2) < 0) {
3847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
3857c478bd9Sstevel@tonic-gate perror(file2);
3867c478bd9Sstevel@tonic-gate done();
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate if ((stb1.st_mode & S_IFMT) == S_IFDIR &&
3907c478bd9Sstevel@tonic-gate (stb2.st_mode & S_IFMT) == S_IFDIR) {
3917c478bd9Sstevel@tonic-gate diffdir(argv);
3927c478bd9Sstevel@tonic-gate done();
393*247ccfcdSToomas Soome }
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate filename(&file1, &file2, &stb1, &input_file1);
3967c478bd9Sstevel@tonic-gate filename(&file2, &file1, &stb2, &input_file2);
3977c478bd9Sstevel@tonic-gate if ((input[0] = fopen(file1, "r")) == NULL) {
3987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
3997c478bd9Sstevel@tonic-gate perror(file1);
4007c478bd9Sstevel@tonic-gate status = 2;
4017c478bd9Sstevel@tonic-gate done();
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate initbuf(input[0], 0, 0);
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate if ((input[1] = fopen(file2, "r")) == NULL) {
4067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
4077c478bd9Sstevel@tonic-gate perror(file2);
4087c478bd9Sstevel@tonic-gate status = 2;
4097c478bd9Sstevel@tonic-gate done();
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate initbuf(input[1], 1, 0);
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate if (stb1.st_size != stb2.st_size)
4147c478bd9Sstevel@tonic-gate goto notsame;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate for (;;) {
4177c478bd9Sstevel@tonic-gate i = fread(buf1, 1, BUFSIZ, input[0]);
4187c478bd9Sstevel@tonic-gate j = fread(buf2, 1, BUFSIZ, input[1]);
4197c478bd9Sstevel@tonic-gate if (ferror(input[0]) || ferror(input[1])) {
4207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
4217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Error reading "));
4227c478bd9Sstevel@tonic-gate perror(ferror(input[0])? file1:file2);
4237c478bd9Sstevel@tonic-gate (void) fclose(input[0]);
4247c478bd9Sstevel@tonic-gate (void) fclose(input[1]);
4257c478bd9Sstevel@tonic-gate status = 2;
4267c478bd9Sstevel@tonic-gate done();
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate if (i != j)
4297c478bd9Sstevel@tonic-gate goto notsame;
4307c478bd9Sstevel@tonic-gate if (i == 0 && j == 0) {
4317c478bd9Sstevel@tonic-gate /* files are the same; diff -D needs to print one */
4327c478bd9Sstevel@tonic-gate if (opt == D_IFDEF) {
4337c478bd9Sstevel@tonic-gate rewind(input[0]);
4347c478bd9Sstevel@tonic-gate while (i = fread(buf1, 1, BUFSIZ, input[0]))
4357c478bd9Sstevel@tonic-gate (void) fwrite(buf1, 1, i, stdout);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate (void) fclose(input[0]);
4387c478bd9Sstevel@tonic-gate (void) fclose(input[1]);
4397c478bd9Sstevel@tonic-gate status = 0;
4407c478bd9Sstevel@tonic-gate goto same; /* files don't differ */
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate for (j = 0; j < i; j++)
4437c478bd9Sstevel@tonic-gate if (buf1[j] != buf2[j])
4447c478bd9Sstevel@tonic-gate goto notsame;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate notsame:
4487c478bd9Sstevel@tonic-gate status = 1;
4497c478bd9Sstevel@tonic-gate if (filebinary(input[0]) || filebinary(input[1])) {
4507c478bd9Sstevel@tonic-gate if (ferror(input[0]) || ferror(input[1])) {
4517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "diff: ");
4527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Error reading "));
4537c478bd9Sstevel@tonic-gate perror(ferror(input[0])? file1:file2);
4547c478bd9Sstevel@tonic-gate (void) fclose(input[0]);
4557c478bd9Sstevel@tonic-gate (void) fclose(input[1]);
4567c478bd9Sstevel@tonic-gate status = 2;
4577c478bd9Sstevel@tonic-gate done();
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate (void) printf(gettext("Binary files %s and %s differ\n"),
4607c478bd9Sstevel@tonic-gate file1, file2);
4617c478bd9Sstevel@tonic-gate (void) fclose(input[0]);
4627c478bd9Sstevel@tonic-gate (void) fclose(input[1]);
4637c478bd9Sstevel@tonic-gate done();
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate prepare(0, file1);
4667c478bd9Sstevel@tonic-gate prepare(1, file2);
4677c478bd9Sstevel@tonic-gate prune();
4687c478bd9Sstevel@tonic-gate sort(sfile[0], slen[0]);
4697c478bd9Sstevel@tonic-gate sort(sfile[1], slen[1]);
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate member = (int *)file[1];
4727c478bd9Sstevel@tonic-gate equiv(sfile[0], slen[0], sfile[1], slen[1], member);
4737c478bd9Sstevel@tonic-gate member = (int *)ralloc((void *)member, (slen[1] + 2) * sizeof (int));
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate class = (int *)file[0];
4767c478bd9Sstevel@tonic-gate unsort(sfile[0], slen[0], class);
4777c478bd9Sstevel@tonic-gate class = (int *)ralloc((void *)class, (slen[0] + 2) * sizeof (int));
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate klist = (int *)talloc((slen[0] + 2) * sizeof (int));
4807c478bd9Sstevel@tonic-gate clist = (struct cand *)talloc(sizeof (cand));
4817c478bd9Sstevel@tonic-gate k = stone(class, slen[0], member, klist);
4827c478bd9Sstevel@tonic-gate free((void *)member);
4837c478bd9Sstevel@tonic-gate free((void *)class);
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate J = (int *)talloc((len[0] + 2) * sizeof (int));
4867c478bd9Sstevel@tonic-gate unravel(klist[k]);
4877c478bd9Sstevel@tonic-gate free((char *)clist);
4887c478bd9Sstevel@tonic-gate free((char *)klist);
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate ixold = (long *)talloc((len[0] + 2) * sizeof (long));
4917c478bd9Sstevel@tonic-gate ixnew = (long *)talloc((len[1] + 2) * sizeof (long));
4927c478bd9Sstevel@tonic-gate check();
4937c478bd9Sstevel@tonic-gate output();
4947c478bd9Sstevel@tonic-gate status = anychange;
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate same:
4977c478bd9Sstevel@tonic-gate if (opt == D_CONTEXT && anychange == 0)
4987c478bd9Sstevel@tonic-gate (void) printf(gettext("No differences encountered\n"));
4997c478bd9Sstevel@tonic-gate done();
5007c478bd9Sstevel@tonic-gate /*NOTREACHED*/
50178eb75caSchin return (0);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate static int
stone(int * a,int n,int * b,int * c)5057c478bd9Sstevel@tonic-gate stone(int *a, int n, int *b, int *c)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate int i, k, y;
5087c478bd9Sstevel@tonic-gate int j, l;
5097c478bd9Sstevel@tonic-gate int oldc, tc;
5107c478bd9Sstevel@tonic-gate int oldl;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate k = 0;
5137c478bd9Sstevel@tonic-gate c[0] = newcand(0, 0, 0);
5147c478bd9Sstevel@tonic-gate for (i = 1; i <= n; i++) {
5157c478bd9Sstevel@tonic-gate j = a[i];
5167c478bd9Sstevel@tonic-gate if (j == 0)
5177c478bd9Sstevel@tonic-gate continue;
5187c478bd9Sstevel@tonic-gate y = -b[j];
5197c478bd9Sstevel@tonic-gate oldl = 0;
5207c478bd9Sstevel@tonic-gate oldc = c[0];
5217c478bd9Sstevel@tonic-gate do {
5227c478bd9Sstevel@tonic-gate if (y <= clist[oldc].y)
5237c478bd9Sstevel@tonic-gate continue;
5247c478bd9Sstevel@tonic-gate l = search(c, k, y);
5257c478bd9Sstevel@tonic-gate if (l != oldl+1)
5267c478bd9Sstevel@tonic-gate oldc = c[l-1];
5277c478bd9Sstevel@tonic-gate if (l <= k) {
5287c478bd9Sstevel@tonic-gate if (clist[c[l]].y <= y)
5297c478bd9Sstevel@tonic-gate continue;
5307c478bd9Sstevel@tonic-gate tc = c[l];
5317c478bd9Sstevel@tonic-gate c[l] = newcand(i, y, oldc);
5327c478bd9Sstevel@tonic-gate oldc = tc;
5337c478bd9Sstevel@tonic-gate oldl = l;
5347c478bd9Sstevel@tonic-gate } else {
5357c478bd9Sstevel@tonic-gate c[l] = newcand(i, y, oldc);
5367c478bd9Sstevel@tonic-gate k++;
5377c478bd9Sstevel@tonic-gate break;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate } while ((y = b[++j]) > 0);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate return (k);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate static int
newcand(int x,int y,int pred)5457c478bd9Sstevel@tonic-gate newcand(int x, int y, int pred)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate struct cand *q;
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate clist = (struct cand *)ralloc((void *)clist, ++clen * sizeof (cand));
5507c478bd9Sstevel@tonic-gate q = clist + clen -1;
5517c478bd9Sstevel@tonic-gate q->x = x;
5527c478bd9Sstevel@tonic-gate q->y = y;
5537c478bd9Sstevel@tonic-gate q->pred = pred;
5547c478bd9Sstevel@tonic-gate return (clen - 1);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate static int
search(int * c,int k,int y)5587c478bd9Sstevel@tonic-gate search(int *c, int k, int y)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate int i, j, l;
5617c478bd9Sstevel@tonic-gate int t;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate if (clist[c[k]].y < y) /* quick look for typical case */
5647c478bd9Sstevel@tonic-gate return (k + 1);
5657c478bd9Sstevel@tonic-gate i = 0;
5667c478bd9Sstevel@tonic-gate j = k+1;
5677c478bd9Sstevel@tonic-gate while ((l = (i + j) / 2) > i) {
5687c478bd9Sstevel@tonic-gate t = clist[c[l]].y;
5697c478bd9Sstevel@tonic-gate if (t > y)
5707c478bd9Sstevel@tonic-gate j = l;
5717c478bd9Sstevel@tonic-gate else if (t < y)
5727c478bd9Sstevel@tonic-gate i = l;
5737c478bd9Sstevel@tonic-gate else
5747c478bd9Sstevel@tonic-gate return (l);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate return (l + 1);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate static void
unravel(int p)5807c478bd9Sstevel@tonic-gate unravel(int p)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate int i;
5837c478bd9Sstevel@tonic-gate struct cand *q;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate for (i = 0; i <= len[0]; i++)
5867c478bd9Sstevel@tonic-gate J[i] = i <= pref ? i :
587*247ccfcdSToomas Soome i > len[0] - suff ? i + len[1] - len[0]: 0;
5887c478bd9Sstevel@tonic-gate for (q = clist + p; q->y != 0; q = clist + q->pred)
5897c478bd9Sstevel@tonic-gate J[q->x + pref] = q->y + pref;
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate * check does double duty:
5947c478bd9Sstevel@tonic-gate * 1. ferret out any fortuitous correspondences due to confounding by
5957c478bd9Sstevel@tonic-gate * hashing (which result in "jackpot")
5967c478bd9Sstevel@tonic-gate * 2. collect random access indexes to the two files
5977c478bd9Sstevel@tonic-gate */
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate static void
check(void)6007c478bd9Sstevel@tonic-gate check(void)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate wint_t c, d;
6037c478bd9Sstevel@tonic-gate int i, j;
6047c478bd9Sstevel@tonic-gate /* int jackpot; */
6057c478bd9Sstevel@tonic-gate int mlen;
6067c478bd9Sstevel@tonic-gate long ctold, ctnew;
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate resetbuf(0);
6097c478bd9Sstevel@tonic-gate resetbuf(1);
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate j = 1;
6127c478bd9Sstevel@tonic-gate ixold[0] = ixnew[0] = 0;
6137c478bd9Sstevel@tonic-gate /* jackpot = 0; */
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate * ctold and ctnew are byte positions within the file (suitable for
6177c478bd9Sstevel@tonic-gate * lseek()). After we get a character with getwc(), instead of
6187c478bd9Sstevel@tonic-gate * just incrementing the byte position by 1, we have to determine
6197c478bd9Sstevel@tonic-gate * how many bytes the character actually is. This is the reason for
6207c478bd9Sstevel@tonic-gate * the wctomb() calls here and in skipline().
6217c478bd9Sstevel@tonic-gate */
6227c478bd9Sstevel@tonic-gate ctold = ctnew = 0;
6237c478bd9Sstevel@tonic-gate for (i = 1; i <= len[0]; i++) {
6247c478bd9Sstevel@tonic-gate if (J[i] == 0) {
6257c478bd9Sstevel@tonic-gate ixold[i] = ctold += skipline(0);
6267c478bd9Sstevel@tonic-gate continue;
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate while (j < J[i]) {
6297c478bd9Sstevel@tonic-gate ixnew[j] = ctnew += skipline(1);
6307c478bd9Sstevel@tonic-gate j++;
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate if (bflag || wflag || iflag) {
6337c478bd9Sstevel@tonic-gate for (;;) {
6347c478bd9Sstevel@tonic-gate c = getbufwchar(0, &mlen);
6357c478bd9Sstevel@tonic-gate ctold += mlen;
6367c478bd9Sstevel@tonic-gate d = getbufwchar(1, &mlen);
6377c478bd9Sstevel@tonic-gate ctnew += mlen;
6387c478bd9Sstevel@tonic-gate
6397c478bd9Sstevel@tonic-gate if (bflag && iswspace(c) && iswspace(d)) {
6407c478bd9Sstevel@tonic-gate while (iswspace(c)) {
6417c478bd9Sstevel@tonic-gate if (c == '\n' || c == WEOF)
6427c478bd9Sstevel@tonic-gate break;
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate c = getbufwchar(0, &mlen);
6457c478bd9Sstevel@tonic-gate ctold += mlen;
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate while (iswspace(d)) {
6487c478bd9Sstevel@tonic-gate if (d == '\n' || d == WEOF)
6497c478bd9Sstevel@tonic-gate break;
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate d = getbufwchar(1, &mlen);
6527c478bd9Sstevel@tonic-gate ctnew += mlen;
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate } else if (wflag) {
6557c478bd9Sstevel@tonic-gate while (iswspace(c) && c != '\n') {
6567c478bd9Sstevel@tonic-gate c = getbufwchar(0, &mlen);
6577c478bd9Sstevel@tonic-gate ctold += mlen;
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate while (iswspace(d) && d != '\n') {
6607c478bd9Sstevel@tonic-gate d = getbufwchar(1, &mlen);
6617c478bd9Sstevel@tonic-gate ctnew += mlen;
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate if (c == WEOF || d == WEOF) {
6657c478bd9Sstevel@tonic-gate if (c != d) {
6667c478bd9Sstevel@tonic-gate /* jackpot++; */
6677c478bd9Sstevel@tonic-gate J[i] = 0;
6687c478bd9Sstevel@tonic-gate if (c != '\n' && c != WEOF)
6697c478bd9Sstevel@tonic-gate ctold += skipline(0);
6707c478bd9Sstevel@tonic-gate if (d != '\n' && d != WEOF)
6717c478bd9Sstevel@tonic-gate ctnew += skipline(1);
6727c478bd9Sstevel@tonic-gate break;
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate break;
6757c478bd9Sstevel@tonic-gate } else {
6767c478bd9Sstevel@tonic-gate if (CHRTRAN(c) != CHRTRAN(d)) {
6777c478bd9Sstevel@tonic-gate /* jackpot++; */
6787c478bd9Sstevel@tonic-gate J[i] = 0;
6797c478bd9Sstevel@tonic-gate if (c != '\n')
6807c478bd9Sstevel@tonic-gate ctold += skipline(0);
6817c478bd9Sstevel@tonic-gate if (d != '\n')
6827c478bd9Sstevel@tonic-gate ctnew += skipline(1);
6837c478bd9Sstevel@tonic-gate break;
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate if (c == '\n')
6867c478bd9Sstevel@tonic-gate break;
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate } else {
6907c478bd9Sstevel@tonic-gate for (;;) {
6917c478bd9Sstevel@tonic-gate c = getbufwchar(0, &mlen);
6927c478bd9Sstevel@tonic-gate ctold += mlen;
6937c478bd9Sstevel@tonic-gate d = getbufwchar(1, &mlen);
6947c478bd9Sstevel@tonic-gate ctnew += mlen;
6957c478bd9Sstevel@tonic-gate if (c != d) {
6967c478bd9Sstevel@tonic-gate /* jackpot++; */
6977c478bd9Sstevel@tonic-gate J[i] = 0;
6987c478bd9Sstevel@tonic-gate if (c != '\n' && c != WEOF)
6997c478bd9Sstevel@tonic-gate ctold += skipline(0);
7007c478bd9Sstevel@tonic-gate if (d != '\n' && d != WEOF)
7017c478bd9Sstevel@tonic-gate ctnew += skipline(1);
7027c478bd9Sstevel@tonic-gate break;
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate if (c == '\n' || c == WEOF)
7057c478bd9Sstevel@tonic-gate break;
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate ixold[i] = ctold;
7097c478bd9Sstevel@tonic-gate ixnew[j] = ctnew;
7107c478bd9Sstevel@tonic-gate j++;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate for (; j <= len[1]; j++) {
7137c478bd9Sstevel@tonic-gate ixnew[j] = ctnew += skipline(1);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate /* if(jackpot) */
7177c478bd9Sstevel@tonic-gate /* fprintf(stderr, "diff: jackpot\n"); */
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate static int
skipline(int f)7217c478bd9Sstevel@tonic-gate skipline(int f)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate int i;
7247c478bd9Sstevel@tonic-gate wint_t c;
7257c478bd9Sstevel@tonic-gate int mlen;
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate for (i = 1; c = getbufwchar(f, &mlen); ) {
7287c478bd9Sstevel@tonic-gate if (c == '\n' || c == WEOF)
7297c478bd9Sstevel@tonic-gate return (i);
7307c478bd9Sstevel@tonic-gate i += mlen;
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate return (i);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate static void
output(void)7367c478bd9Sstevel@tonic-gate output(void)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate int m;
7397c478bd9Sstevel@tonic-gate wint_t wc;
7407c478bd9Sstevel@tonic-gate int i0, i1, j1;
7417c478bd9Sstevel@tonic-gate int j0;
7427c478bd9Sstevel@tonic-gate int mlen;
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate resetbuf(0);
7457c478bd9Sstevel@tonic-gate resetbuf(1);
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate m = len[0];
7487c478bd9Sstevel@tonic-gate J[0] = 0;
7497c478bd9Sstevel@tonic-gate J[m + 1] = len[1] + 1;
7507c478bd9Sstevel@tonic-gate if (opt != D_EDIT)
7517c478bd9Sstevel@tonic-gate for (i0 = 1; i0 <= m; i0 = i1+1) {
7527c478bd9Sstevel@tonic-gate while (i0 <= m && J[i0] == J[i0 - 1] + 1)
7537c478bd9Sstevel@tonic-gate i0++;
7547c478bd9Sstevel@tonic-gate j0 = J[i0 - 1] + 1;
7557c478bd9Sstevel@tonic-gate i1 = i0 - 1;
7567c478bd9Sstevel@tonic-gate while (i1 < m && J[i1 + 1] == 0)
7577c478bd9Sstevel@tonic-gate i1++;
7587c478bd9Sstevel@tonic-gate j1 = J[i1 + 1] - 1;
7597c478bd9Sstevel@tonic-gate J[i1] = j1;
7607c478bd9Sstevel@tonic-gate change(i0, i1, j0, j1);
7617c478bd9Sstevel@tonic-gate } else for (i0 = m; i0 >= 1; i0 = i1 - 1) {
7627c478bd9Sstevel@tonic-gate while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0)
7637c478bd9Sstevel@tonic-gate i0--;
7647c478bd9Sstevel@tonic-gate j0 = J[i0 + 1] - 1;
7657c478bd9Sstevel@tonic-gate i1 = i0 + 1;
7667c478bd9Sstevel@tonic-gate while (i1 > 1 && J[i1 - 1] == 0)
7677c478bd9Sstevel@tonic-gate i1--;
7687c478bd9Sstevel@tonic-gate j1 = J[i1 - 1] + 1;
7697c478bd9Sstevel@tonic-gate J[i1] = j1;
7707c478bd9Sstevel@tonic-gate change(i1, i0, j1, j0);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate if (m == 0)
7737c478bd9Sstevel@tonic-gate change(1, 0, 1, len[1]);
7747c478bd9Sstevel@tonic-gate if (opt == D_IFDEF) {
7757c478bd9Sstevel@tonic-gate for (;;) {
7767c478bd9Sstevel@tonic-gate wc = getbufwchar(0, &mlen);
7777c478bd9Sstevel@tonic-gate if (wc == WEOF)
7787c478bd9Sstevel@tonic-gate return;
7797c478bd9Sstevel@tonic-gate (void) wcput(wc);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate if (anychange && opt == D_CONTEXT)
7837c478bd9Sstevel@tonic-gate dump_context_vec();
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate * indicate that there is a difference between lines a and b of the from file
7897c478bd9Sstevel@tonic-gate * to get to lines c to d of the to file.
7907c478bd9Sstevel@tonic-gate * If a is greater then b then there are no lines in the from file involved
7917c478bd9Sstevel@tonic-gate * and this means that there were lines appended (beginning at b).
7927c478bd9Sstevel@tonic-gate * If c is greater than d then there are lines missing from the to file.
7937c478bd9Sstevel@tonic-gate */
7947c478bd9Sstevel@tonic-gate static void
change(int a,int b,int c,int d)7957c478bd9Sstevel@tonic-gate change(int a, int b, int c, int d)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate char time_buf[BUFSIZ];
7987c478bd9Sstevel@tonic-gate char *dcmsg;
7997c478bd9Sstevel@tonic-gate
8007c478bd9Sstevel@tonic-gate if (opt != D_IFDEF && a > b && c > d)
8017c478bd9Sstevel@tonic-gate return;
8027c478bd9Sstevel@tonic-gate if (anychange == 0) {
8037c478bd9Sstevel@tonic-gate anychange = 1;
8047c478bd9Sstevel@tonic-gate if (opt == D_CONTEXT) {
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE_FOR_DC
8077c478bd9Sstevel@tonic-gate * This message is the format of file
8087c478bd9Sstevel@tonic-gate * timestamps written with the -C and
8097c478bd9Sstevel@tonic-gate * -c options.
8107c478bd9Sstevel@tonic-gate * %a -- locale's abbreviated weekday name
8117c478bd9Sstevel@tonic-gate * %b -- locale's abbreviated month name
8127c478bd9Sstevel@tonic-gate * %e -- day of month [1,31]
8137c478bd9Sstevel@tonic-gate * %T -- Time as %H:%M:%S
8147c478bd9Sstevel@tonic-gate * %Y -- Year, including the century
8157c478bd9Sstevel@tonic-gate */
8167c478bd9Sstevel@tonic-gate dcmsg = dcgettext(NULL, "%a %b %e %T %Y", LC_TIME);
8177c478bd9Sstevel@tonic-gate (void) cftime(time_buf, dcmsg, &stb1.st_mtime);
8187c478bd9Sstevel@tonic-gate if (uflag)
8197c478bd9Sstevel@tonic-gate (void) printf("--- %s %s\n", input_file1,
8207c478bd9Sstevel@tonic-gate time_buf);
8217c478bd9Sstevel@tonic-gate else
8227c478bd9Sstevel@tonic-gate (void) printf("*** %s %s\n", input_file1,
8237c478bd9Sstevel@tonic-gate time_buf);
8247c478bd9Sstevel@tonic-gate (void) cftime(time_buf, dcmsg, &stb2.st_mtime);
8257c478bd9Sstevel@tonic-gate if (uflag)
8267c478bd9Sstevel@tonic-gate (void) printf("+++ %s %s\n", input_file2,
8277c478bd9Sstevel@tonic-gate time_buf);
8287c478bd9Sstevel@tonic-gate else
8297c478bd9Sstevel@tonic-gate (void) printf("--- %s %s\n", input_file2,
8307c478bd9Sstevel@tonic-gate time_buf);
8317c478bd9Sstevel@tonic-gate
832*247ccfcdSToomas Soome context_vec_start = malloc(MAX_CONTEXT *
833*247ccfcdSToomas Soome sizeof (struct context_vec));
8347c478bd9Sstevel@tonic-gate if (context_vec_start == NULL)
8357c478bd9Sstevel@tonic-gate error(gettext(NO_MEM_ERR));
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate context_vec_end = context_vec_start + (MAX_CONTEXT - 1);
8387c478bd9Sstevel@tonic-gate context_vec_ptr = context_vec_start - 1;
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate if (opt == D_CONTEXT) {
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate * if this new change is within 'context' lines of
8457c478bd9Sstevel@tonic-gate * the previous change, just add it to the change
8467c478bd9Sstevel@tonic-gate * record. If the record is full or if this
8477c478bd9Sstevel@tonic-gate * change is more than 'context' lines from the previous
8487c478bd9Sstevel@tonic-gate * change, dump the record, reset it & add the new change.
8497c478bd9Sstevel@tonic-gate */
8507c478bd9Sstevel@tonic-gate if (context_vec_ptr >= context_vec_end ||
8517c478bd9Sstevel@tonic-gate (context_vec_ptr >= context_vec_start &&
8527c478bd9Sstevel@tonic-gate a > (context_vec_ptr->b + 2 * context) &&
8537c478bd9Sstevel@tonic-gate c > (context_vec_ptr->d + 2 * context)))
8547c478bd9Sstevel@tonic-gate dump_context_vec();
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate context_vec_ptr++;
8577c478bd9Sstevel@tonic-gate context_vec_ptr->a = a;
8587c478bd9Sstevel@tonic-gate context_vec_ptr->b = b;
8597c478bd9Sstevel@tonic-gate context_vec_ptr->c = c;
8607c478bd9Sstevel@tonic-gate context_vec_ptr->d = d;
8617c478bd9Sstevel@tonic-gate return;
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate switch (opt) {
8657c478bd9Sstevel@tonic-gate case D_NORMAL:
8667c478bd9Sstevel@tonic-gate case D_EDIT:
8677c478bd9Sstevel@tonic-gate range(a, b, ",");
8687c478bd9Sstevel@tonic-gate (void) putchar(a > b ? 'a' : c > d ? 'd' : 'c');
8697c478bd9Sstevel@tonic-gate if (opt == D_NORMAL) range(c, d, ",");
8707c478bd9Sstevel@tonic-gate (void) printf("\n");
8717c478bd9Sstevel@tonic-gate break;
8727c478bd9Sstevel@tonic-gate case D_REVERSE:
8737c478bd9Sstevel@tonic-gate (void) putchar(a > b ? 'a' : c > d ? 'd' : 'c');
8747c478bd9Sstevel@tonic-gate range(a, b, " ");
8757c478bd9Sstevel@tonic-gate (void) printf("\n");
8767c478bd9Sstevel@tonic-gate break;
8777c478bd9Sstevel@tonic-gate case D_NREVERSE:
8787c478bd9Sstevel@tonic-gate if (a > b)
8797c478bd9Sstevel@tonic-gate (void) printf("a%d %d\n", b, d - c + 1);
8807c478bd9Sstevel@tonic-gate else {
8817c478bd9Sstevel@tonic-gate (void) printf("d%d %d\n", a, b - a + 1);
8827c478bd9Sstevel@tonic-gate if (!(c > d))
8837c478bd9Sstevel@tonic-gate /* add changed lines */
8847c478bd9Sstevel@tonic-gate (void) printf("a%d %d\n", b, d - c + 1);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate break;
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate if (opt == D_NORMAL || opt == D_IFDEF) {
8897c478bd9Sstevel@tonic-gate fetch(ixold, a, b, 0, "< ", 1);
8907c478bd9Sstevel@tonic-gate if (a <= b && c <= d && opt == D_NORMAL)
8917c478bd9Sstevel@tonic-gate (void) prints("---\n");
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate fetch(ixnew, c, d, 1, opt == D_NORMAL?"> ":empty, 0);
8947c478bd9Sstevel@tonic-gate if ((opt == D_EDIT || opt == D_REVERSE) && c <= d)
8957c478bd9Sstevel@tonic-gate (void) prints(".\n");
8967c478bd9Sstevel@tonic-gate if (inifdef) {
8977c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "#endif /* %s */\n", endifname);
8987c478bd9Sstevel@tonic-gate inifdef = 0;
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate static void
range(int a,int b,char * separator)9037c478bd9Sstevel@tonic-gate range(int a, int b, char *separator)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate (void) printf("%d", a > b ? b : a);
9067c478bd9Sstevel@tonic-gate if (a < b) {
9077c478bd9Sstevel@tonic-gate (void) printf("%s%d", separator, b);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate static void
fetch(long * f,int a,int b,int filen,char * s,int oldfile)9127c478bd9Sstevel@tonic-gate fetch(long *f, int a, int b, int filen, char *s, int oldfile)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate int i;
9157c478bd9Sstevel@tonic-gate int col;
9167c478bd9Sstevel@tonic-gate int nc;
9177c478bd9Sstevel@tonic-gate int mlen = 0;
9187c478bd9Sstevel@tonic-gate wint_t ch;
9197c478bd9Sstevel@tonic-gate FILE *lb;
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate lb = input[filen];
9227c478bd9Sstevel@tonic-gate /*
9237c478bd9Sstevel@tonic-gate * When doing #ifdef's, copy down to current line
9247c478bd9Sstevel@tonic-gate * if this is the first file, so that stuff makes it to output.
9257c478bd9Sstevel@tonic-gate */
9267c478bd9Sstevel@tonic-gate if (opt == D_IFDEF && oldfile) {
9277c478bd9Sstevel@tonic-gate long curpos = ftellbuf(filen);
9287c478bd9Sstevel@tonic-gate /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
9297c478bd9Sstevel@tonic-gate nc = f[(a > b) ? b : (a - 1) ] - curpos;
9307c478bd9Sstevel@tonic-gate for (i = 0; i < nc; i += mlen) {
9317c478bd9Sstevel@tonic-gate ch = getbufwchar(filen, &mlen);
9327c478bd9Sstevel@tonic-gate if (ch == WEOF) {
9337c478bd9Sstevel@tonic-gate (void) putchar('\n');
9347c478bd9Sstevel@tonic-gate break;
9357c478bd9Sstevel@tonic-gate } else {
9367c478bd9Sstevel@tonic-gate (void) wcput(ch);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate if (a > b)
9417c478bd9Sstevel@tonic-gate return;
9427c478bd9Sstevel@tonic-gate if (opt == D_IFDEF) {
9437c478bd9Sstevel@tonic-gate int oneflag = (*ifdef1 != '\0') != (*ifdef2 != '\0');
9447c478bd9Sstevel@tonic-gate if (inifdef)
9457c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "#else /* %s%s */\n",
9467c478bd9Sstevel@tonic-gate oneflag && oldfile == 1 ? "!" : "", ifdef2);
9477c478bd9Sstevel@tonic-gate else {
9487c478bd9Sstevel@tonic-gate if (oneflag) {
9497c478bd9Sstevel@tonic-gate /* There was only one ifdef given */
9507c478bd9Sstevel@tonic-gate endifname = ifdef2;
9517c478bd9Sstevel@tonic-gate if (oldfile)
9527c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
9537c478bd9Sstevel@tonic-gate "#ifndef %s\n", endifname);
9547c478bd9Sstevel@tonic-gate else
9557c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
9567c478bd9Sstevel@tonic-gate "#ifdef %s\n", endifname);
9577c478bd9Sstevel@tonic-gate } else {
9587c478bd9Sstevel@tonic-gate endifname = oldfile ? ifdef1 : ifdef2;
9597c478bd9Sstevel@tonic-gate (void) fprintf(stdout,
960*247ccfcdSToomas Soome "#ifdef %s\n", endifname);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate inifdef = 1 + oldfile;
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate
9667c478bd9Sstevel@tonic-gate for (i = a; i <= b; i++) {
9677c478bd9Sstevel@tonic-gate (void) fseek(lb, f[i - 1], SEEK_SET);
9687c478bd9Sstevel@tonic-gate initbuf(lb, filen, f[i - 1]);
9697c478bd9Sstevel@tonic-gate if (opt != D_IFDEF)
9707c478bd9Sstevel@tonic-gate (void) prints(s);
9717c478bd9Sstevel@tonic-gate col = 0;
9727c478bd9Sstevel@tonic-gate while (ch = getbufwchar(filen, &mlen)) {
9737c478bd9Sstevel@tonic-gate if (ch != '\n' && ch != WEOF) {
9747c478bd9Sstevel@tonic-gate if (ch == '\t' && tflag)
975*247ccfcdSToomas Soome do {
9767c478bd9Sstevel@tonic-gate (void) putchar(' ');
977*247ccfcdSToomas Soome } while (++col & 7);
9787c478bd9Sstevel@tonic-gate else {
9797c478bd9Sstevel@tonic-gate (void) wcput(ch);
9807c478bd9Sstevel@tonic-gate col++;
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate } else
9837c478bd9Sstevel@tonic-gate break;
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate (void) putchar('\n');
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate
9897c478bd9Sstevel@tonic-gate /*
9907c478bd9Sstevel@tonic-gate * hashing has the effect of
9917c478bd9Sstevel@tonic-gate * arranging line in 7-bit bytes and then
9927c478bd9Sstevel@tonic-gate * summing 1-s complement in 16-bit hunks
9937c478bd9Sstevel@tonic-gate */
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate static int
readhash(FILE * f,int filen,char * str)9967c478bd9Sstevel@tonic-gate readhash(FILE *f, int filen, char *str)
9977c478bd9Sstevel@tonic-gate {
9987c478bd9Sstevel@tonic-gate long sum;
9997c478bd9Sstevel@tonic-gate unsigned int shift;
10007c478bd9Sstevel@tonic-gate int space;
10017c478bd9Sstevel@tonic-gate int t;
10027c478bd9Sstevel@tonic-gate wint_t wt;
10037c478bd9Sstevel@tonic-gate int mlen;
10047c478bd9Sstevel@tonic-gate
10057c478bd9Sstevel@tonic-gate sum = 1;
10067c478bd9Sstevel@tonic-gate space = 0;
10077c478bd9Sstevel@tonic-gate if (!bflag && !wflag) {
10087c478bd9Sstevel@tonic-gate if (iflag)
10097c478bd9Sstevel@tonic-gate if (mbcurmax == 1) {
10107c478bd9Sstevel@tonic-gate /* In this case, diff doesn't have to take */
10117c478bd9Sstevel@tonic-gate /* care of multibyte characters. */
10127c478bd9Sstevel@tonic-gate for (shift = 0; (t = getc(f)) != '\n';
1013*247ccfcdSToomas Soome shift += 7) {
10147c478bd9Sstevel@tonic-gate if (t == EOF) {
10157c478bd9Sstevel@tonic-gate if (shift) {
10167c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10177c478bd9Sstevel@tonic-gate gettext("Warning: missing newline at end of file %s\n"), str);
10187c478bd9Sstevel@tonic-gate break;
10197c478bd9Sstevel@tonic-gate } else
10207c478bd9Sstevel@tonic-gate return (0);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate sum += (isupper(t) ? tolower(t) : t) <<
1023*247ccfcdSToomas Soome (shift &= HALFMASK);
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate } else {
10267c478bd9Sstevel@tonic-gate /* In this case, diff needs to take care of */
10277c478bd9Sstevel@tonic-gate /* multibyte characters. */
10287c478bd9Sstevel@tonic-gate for (shift = 0;
1029*247ccfcdSToomas Soome (wt = getbufwchar(filen, &mlen)) != '\n';
1030*247ccfcdSToomas Soome shift += 7) {
10317c478bd9Sstevel@tonic-gate if (wt == WEOF) {
10327c478bd9Sstevel@tonic-gate if (shift) {
10337c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10347c478bd9Sstevel@tonic-gate gettext("Warning: missing newline at end of file %s\n"), str);
10357c478bd9Sstevel@tonic-gate break;
10367c478bd9Sstevel@tonic-gate } else
10377c478bd9Sstevel@tonic-gate return (0);
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate sum += NCCHRTRAN(wt) <<
1040*247ccfcdSToomas Soome (shift &= HALFMASK);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate else
10447c478bd9Sstevel@tonic-gate /* In this case, diff doesn't have to take care of */
10457c478bd9Sstevel@tonic-gate /* multibyte characters. */
10467c478bd9Sstevel@tonic-gate for (shift = 0; (t = getc(f)) != '\n'; shift += 7) {
10477c478bd9Sstevel@tonic-gate if (t == EOF) {
10487c478bd9Sstevel@tonic-gate if (shift) {
10497c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10507c478bd9Sstevel@tonic-gate gettext("Warning: missing newline at end of file %s\n"), str);
10517c478bd9Sstevel@tonic-gate break;
10527c478bd9Sstevel@tonic-gate } else
10537c478bd9Sstevel@tonic-gate return (0);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate sum += (long)t << (shift &= HALFMASK);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate } else {
10587c478bd9Sstevel@tonic-gate /* In this case, diff needs to take care of */
10597c478bd9Sstevel@tonic-gate /* multibyte characters. */
10607c478bd9Sstevel@tonic-gate for (shift = 0; ; ) {
10617c478bd9Sstevel@tonic-gate wt = getbufwchar(filen, &mlen);
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate if (wt != '\n' && iswspace(wt)) {
10647c478bd9Sstevel@tonic-gate space++;
10657c478bd9Sstevel@tonic-gate continue;
10667c478bd9Sstevel@tonic-gate } else {
10677c478bd9Sstevel@tonic-gate switch (wt) {
10687c478bd9Sstevel@tonic-gate case WEOF:
10697c478bd9Sstevel@tonic-gate if (shift) {
10707c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10717c478bd9Sstevel@tonic-gate gettext("Warning: missing newline at end of file %s\n"), str);
10727c478bd9Sstevel@tonic-gate break;
10737c478bd9Sstevel@tonic-gate } else
10747c478bd9Sstevel@tonic-gate return (0);
10757c478bd9Sstevel@tonic-gate default:
10767c478bd9Sstevel@tonic-gate if (space && !wflag) {
10777c478bd9Sstevel@tonic-gate shift += 7;
10787c478bd9Sstevel@tonic-gate space = 0;
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate sum += CHRTRAN(wt) <<
1081*247ccfcdSToomas Soome (shift &= HALFMASK);
10827c478bd9Sstevel@tonic-gate shift += 7;
10837c478bd9Sstevel@tonic-gate continue;
10847c478bd9Sstevel@tonic-gate case L'\n':
10857c478bd9Sstevel@tonic-gate break;
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate break;
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate return (sum);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate /* dump accumulated "context" diff changes */
10967c478bd9Sstevel@tonic-gate static void
dump_context_vec(void)10977c478bd9Sstevel@tonic-gate dump_context_vec(void)
10987c478bd9Sstevel@tonic-gate {
1099*247ccfcdSToomas Soome int a, b = 0, c, d = 0;
11007c478bd9Sstevel@tonic-gate char ch;
11017c478bd9Sstevel@tonic-gate struct context_vec *cvp = context_vec_start;
11027c478bd9Sstevel@tonic-gate int lowa, upb, lowc, upd;
11037c478bd9Sstevel@tonic-gate int do_output;
11047c478bd9Sstevel@tonic-gate
11057c478bd9Sstevel@tonic-gate if (cvp > context_vec_ptr)
11067c478bd9Sstevel@tonic-gate return;
11077c478bd9Sstevel@tonic-gate
11087c478bd9Sstevel@tonic-gate lowa = max(1, cvp->a - context);
11097c478bd9Sstevel@tonic-gate upb = min(len[0], context_vec_ptr->b + context);
11107c478bd9Sstevel@tonic-gate lowc = max(1, cvp->c - context);
11117c478bd9Sstevel@tonic-gate upd = min(len[1], context_vec_ptr->d + context);
11127c478bd9Sstevel@tonic-gate
11137c478bd9Sstevel@tonic-gate if (uflag) {
11147c478bd9Sstevel@tonic-gate (void) printf("@@ -%d,%d +%d,%d @@\n",
11157c478bd9Sstevel@tonic-gate lowa, upb - lowa + 1,
11167c478bd9Sstevel@tonic-gate lowc, upd - lowc + 1);
11177c478bd9Sstevel@tonic-gate } else {
11187c478bd9Sstevel@tonic-gate (void) printf("***************\n*** ");
11197c478bd9Sstevel@tonic-gate range(lowa, upb, ",");
11207c478bd9Sstevel@tonic-gate (void) printf(" ****\n");
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate * output changes to the "old" file. The first loop suppresses
11257c478bd9Sstevel@tonic-gate * output if there were no changes to the "old" file (we'll see
11267c478bd9Sstevel@tonic-gate * the "old" lines as context in the "new" list).
11277c478bd9Sstevel@tonic-gate */
11287c478bd9Sstevel@tonic-gate if (uflag)
11297c478bd9Sstevel@tonic-gate do_output = 1;
11307c478bd9Sstevel@tonic-gate else
11317c478bd9Sstevel@tonic-gate for (do_output = 0; cvp <= context_vec_ptr; cvp++)
11327c478bd9Sstevel@tonic-gate if (cvp->a <= cvp->b) {
11337c478bd9Sstevel@tonic-gate cvp = context_vec_start;
11347c478bd9Sstevel@tonic-gate do_output++;
11357c478bd9Sstevel@tonic-gate break;
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate
11387c478bd9Sstevel@tonic-gate if (do_output) {
11397c478bd9Sstevel@tonic-gate while (cvp <= context_vec_ptr) {
11407c478bd9Sstevel@tonic-gate a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->d;
11417c478bd9Sstevel@tonic-gate
11427c478bd9Sstevel@tonic-gate if (a <= b && c <= d)
11437c478bd9Sstevel@tonic-gate ch = 'c';
11447c478bd9Sstevel@tonic-gate else
11457c478bd9Sstevel@tonic-gate ch = (a <= b) ? 'd' : 'a';
11467c478bd9Sstevel@tonic-gate
11477c478bd9Sstevel@tonic-gate if (ch == 'a') {
11487c478bd9Sstevel@tonic-gate /* The last argument should not affect */
11497c478bd9Sstevel@tonic-gate /* the behavior of fetch() */
11507c478bd9Sstevel@tonic-gate fetch(ixold, lowa, b, 0, uflag ? " " : " ", 1);
11517c478bd9Sstevel@tonic-gate if (uflag)
11527c478bd9Sstevel@tonic-gate fetch(ixnew, c, d, 1, "+", 0);
11537c478bd9Sstevel@tonic-gate } else if (ch == 'd') {
11547c478bd9Sstevel@tonic-gate fetch(ixold, lowa, a - 1, 0, uflag ? " " :
1155*247ccfcdSToomas Soome " ", 1);
11567c478bd9Sstevel@tonic-gate fetch(ixold, a, b, 0, uflag ? "-" : "- ", 1);
11577c478bd9Sstevel@tonic-gate } else {
11587c478bd9Sstevel@tonic-gate /* The last argument should not affect */
11597c478bd9Sstevel@tonic-gate /* the behavior of fetch() */
11607c478bd9Sstevel@tonic-gate fetch(ixold, lowa, a-1, 0, uflag ? " " : " ",
11617c478bd9Sstevel@tonic-gate 1);
11627c478bd9Sstevel@tonic-gate if (uflag) {
11637c478bd9Sstevel@tonic-gate fetch(ixold, a, b, 0, "-", 1);
11647c478bd9Sstevel@tonic-gate fetch(ixnew, c, d, 1, "+", 0);
11657c478bd9Sstevel@tonic-gate } else
11667c478bd9Sstevel@tonic-gate fetch(ixold, a, b, 0, "! ", 1);
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate lowa = b + 1;
11697c478bd9Sstevel@tonic-gate cvp++;
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate /* The last argument should not affect the behavior */
11727c478bd9Sstevel@tonic-gate /* of fetch() */
11737c478bd9Sstevel@tonic-gate fetch(ixold, b+1, upb, 0, uflag ? " " : " ", 1);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate
11767c478bd9Sstevel@tonic-gate if (uflag) {
11777c478bd9Sstevel@tonic-gate context_vec_ptr = context_vec_start - 1;
11787c478bd9Sstevel@tonic-gate return;
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate
11817c478bd9Sstevel@tonic-gate /* output changes to the "new" file */
11827c478bd9Sstevel@tonic-gate (void) printf("--- ");
11837c478bd9Sstevel@tonic-gate range(lowc, upd, ",");
11847c478bd9Sstevel@tonic-gate (void) printf(" ----\n");
11857c478bd9Sstevel@tonic-gate
11867c478bd9Sstevel@tonic-gate do_output = 0;
11877c478bd9Sstevel@tonic-gate for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
11887c478bd9Sstevel@tonic-gate if (cvp->c <= cvp->d) {
11897c478bd9Sstevel@tonic-gate cvp = context_vec_start;
11907c478bd9Sstevel@tonic-gate do_output++;
11917c478bd9Sstevel@tonic-gate break;
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate
11947c478bd9Sstevel@tonic-gate if (do_output) {
11957c478bd9Sstevel@tonic-gate while (cvp <= context_vec_ptr) {
11967c478bd9Sstevel@tonic-gate a = cvp->a; b = cvp->b; c = cvp->c; d = cvp->