File: rediff.c

package info (click to toggle)
patchutils 0.2.11-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 800 kB
  • ctags: 182
  • sloc: ansic: 3,496; sh: 2,547; xml: 997; makefile: 193; perl: 101
file content (985 lines) | stat: -rw-r--r-- 22,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/*
 * rediff - fix offset and counts of a hand-edited diff
 * Copyright (C) 2001, 2002 Tim Waugh <twaugh@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <errno.h>
#ifdef HAVE_ERROR_H
# include <error.h>
#endif /* HAVE_ERROR_H */
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h> // for ssize_t
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_UNISTD_H
# include <unistd.h> // for access
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif /* HAVE_SYS_WAIT_H */

#include "diff.h"
#include "util.h"

#ifndef DIFF
#define DIFF "diff"
#endif /* DIFF */

struct file_info
{
	const char *orig_file;
	const char *new_file;
};

struct hunk 
{
	fpos_t filepos;
	struct file_info *info;
	unsigned long line_in_diff;
	unsigned long num_lines;
	unsigned long orig_offset;
	unsigned long orig_count;
	unsigned long new_offset;
	unsigned long new_count;
	struct hunk *next; /* must be ordered by line_in_diff */
	int discard_offset;
};

/* Copy hunk from in to out with no modifications.
 * @@ line has already been read.
 * orig_lines: original line count
 * new_lines: new line count */
static unsigned long copy_hunk (FILE *in, FILE *out,
				unsigned long orig_lines,
				unsigned long new_lines)
{
	int newline = 1;
	fpos_t pos;
	char *line = NULL;
	size_t linelen = 0;
	unsigned long count = 0;

	while (orig_lines || new_lines || newline) {
		fgetpos (in, &pos);
		if (getline (&line, &linelen, in) == -1)
			break;

		if (!orig_lines && !new_lines &&
		    line[0] != '\\')
			break;

		count++;

		fputs (line, out);
		switch (line[0]) {
		case ' ':
			if (new_lines) new_lines--;
		case '-':
			if (orig_lines) orig_lines--;
			break;
		case '+':
			if (new_lines) new_lines--;
			break;
		case '\\':
			newline = 0;
			break;
		}
	}

	if (newline)
		/* Back up a line. */
		fsetpos (in, &pos);

	return count;
}

/* Copy hunk from in to out, adjusting offsets by line_offset. */
static unsigned long adjust_offsets_and_copy (long *offset, FILE *in,
					      FILE *out)
{
	char *line = NULL;
	size_t linelen = 0;
	unsigned long orig_offset, orig_lines, new_offset, new_lines;
	unsigned long count = 0;

	if (getline (&line, &linelen, in) == -1)
		goto out;
	count++;

	if (!strncmp (line, "--- ", 4)) {
		/* This is the first hunk of a group.  Copy the
		 * file info. */
		fputs (line, out);
		if (getline (&line, &linelen, in) == -1)
			goto out;
		count++;
		fputs (line, out);
		if (getline (&line, &linelen, in) == -1)
			goto out;
		count++;

		/* The line offsets no longer apply. */
		*offset = 0;
	}

	if (read_atatline (line, &orig_offset, &orig_lines,
			   &new_offset, &new_lines)) {
		line[strlen (line) - 1] = '\0';
		error (EXIT_FAILURE, 0, "Line not understood: %s", line);
	}

	free (line);

	/* Adjust offsets */
	fprintf (out, "@@ -%lu", orig_offset);
	if (orig_lines != 1)
		fprintf (out, ",%lu", orig_lines);
	fprintf (out, " +%lu", new_offset + *offset);
	if (new_lines != 1)
		fprintf (out, ",%lu", new_lines);
	fprintf (out, " @@\n");

	/* Copy remaining lines of hunk */
	count += copy_hunk (in, out, orig_lines, new_lines);

out:
	return count;
}

/* Copy n lines from in to out. */
static unsigned long copy_lines (FILE *in, FILE *out, unsigned long n)
{
	char *line = NULL;
	size_t linelen = 0;
	unsigned long count = 0;
	while (n--) {
		if (getline (&line, &linelen, in) == -1)
			break;
		count++;
		fputs (line, out);
	}
	if (line)
		free (line);
	return count;
}

/* Copy trailing non-diff lines in hunk from in to out.
 * done: number of lines of hunk already copied. */
static void copy_trailing (struct hunk *hunk, FILE *in, FILE *out,
			   unsigned long done)
{
	if (hunk->next) {
		/* Copy trailing non-diff text. */
		unsigned long todo;
       		todo = hunk->next->line_in_diff - hunk->line_in_diff - done;
		/* Take one off to account for the difference. */
		if (todo > 1)
			copy_lines (in, out, todo - 1);
	} else {
		/* Copy the rest of the file. */
		while (copy_lines (in, out, 100))
			;
	}
}

/* Copy hunks from in to out.
 * from: starting hunk.
 * upto: hunk to stop before, or NULL.
 * line_offset: offset adjustment to apply.
 * is_first: zero unless this is the first hunk in the file. */
static void copy_to (struct hunk *from, struct hunk *upto,
		     long *line_offset, FILE *in, FILE *out, int is_first)
{
	if (is_first && from) {
		/* Copy leading non-diff text. */
		fseek (in, 0, SEEK_SET);
		copy_lines (in, out, from->line_in_diff - 1);
	}

	for (; from && from != upto; from = from->next) {
		unsigned long count;
		fsetpos (in, &from->filepos);
		count = adjust_offsets_and_copy (line_offset, in, out);
		copy_trailing (from, in, out, count);
	}
}

/* Deal with an added hunk. */
static long added_hunk (const char *meta, long offset, FILE *modify, FILE *t,
			unsigned long morig_count, unsigned long mnew_count)
{
	long this_offset = 0;
	char *line = NULL;
	size_t linelen = 0;
	char *p = strchr (meta, '-'), *q;
	unsigned long orig_offset = 0, new_offset;
	unsigned long orig_count = 0, new_count = 0;
	FILE *newhunk = tmpfile ();

	if (!newhunk)
		error (EXIT_FAILURE, errno, "Couldn't create temporary file");

	if (p) {
		p++;
		orig_offset = strtoul (p, &q, 10);
	}

	if (!p || p == q) {
		p[strlen (p) - 1] = '\0';
		error (EXIT_FAILURE, 0,
		       "Hunk addition requires original line: %s", meta);
	}

	while (morig_count || mnew_count) {
		if (getline (&line, &linelen, modify) == -1)
			break;

		if (line[0] != '+')
			error (EXIT_FAILURE, 0,
			       "Only whole hunks may be added");

		mnew_count--;

		switch (line[1]) {
		case ' ':
			orig_count++;
			new_count++;
			break;
		case '+':
			new_count++;
			this_offset++;
			break;
		case '-':
			orig_count++;
			this_offset--;
			break;
		default:
			error (EXIT_FAILURE, 0,
			       "Multiple added hunks not supported");
		}

		fputs (line + 1, newhunk);
	}

	new_offset = orig_offset + offset;

	if (!new_count)
		new_offset--;

	fprintf (t, "@@ -%lu", orig_offset);
	if (orig_count != 1)
		fprintf (t, ",%lu", orig_count);
	fprintf (t, " +%lu", new_offset);
	if (new_count != 1)
		fprintf (t, ",%lu", new_count);
	fprintf (t, " @@\n");

	rewind (newhunk);
	while (copy_lines (newhunk, t, 100));
	fclose (newhunk);

	if (line)
		free (line);

	return this_offset;
}

/* Deal with a removed hunk. */
static long removed_hunk (const char *meta, long offset, FILE *modify,
			  FILE *t, int *info_written, struct hunk *hunk,
			  unsigned long morig_count,
			  unsigned long mnew_count, unsigned long *replaced)
{
	long this_offset = 0;
	char *line = NULL;
	size_t linelen = 0;
	unsigned long orig_offset, new_offset;
	unsigned long orig_count, new_count;

	*replaced = 0;
	if (read_atatline (meta, &orig_offset, &orig_count,
			   &new_offset, &new_count))
		goto out;

	if (getline (&line, &linelen, modify) == -1)
		goto out;

	if (line[0] == '+' && line[1] == '@') {
		/* Minimally correct modified @@ banners. */
		unsigned long oo, no;
		if (read_atatline (line + 1, &oo, NULL, &no, NULL))
			goto out;

		/* Display a file name banner. */
		if (hunk->info && !*info_written) {
			fputs (hunk->info->orig_file, t);
			fputs (hunk->info->new_file, t);
			*info_written = 1;
		}

		if (oo != orig_offset)
			no = new_offset + oo - orig_offset;
		else
			oo = orig_offset + no - new_offset;
		fprintf (t, "@@ -%lu", oo);
		if (orig_count != 1)
			fprintf (t, ",%lu", orig_count);
		fprintf (t, " +%lu", no);
		if (new_count != 1)
			fprintf (t, ",%lu", new_count);
		fprintf (t, " @@\n");
		goto out;
	}

	morig_count--;
	*replaced = morig_count;
	while ((orig_count || new_count) && morig_count) {
		if (line[0] != '-')
			error (EXIT_FAILURE, 0,
			       "Only whole hunks may be added");

		switch (line[1]) {
		case ' ':
			orig_count--;
			new_count--;
			break;
		case '+':
			new_count--;
			this_offset--;
			break;
		case '-':
			orig_count--;
			this_offset++;
			break;
		default:
			error (EXIT_FAILURE, 0,
			       "Multiple removed hunks not supported");
		}

		if (!--morig_count)
			break;

		assert (getline (&line, &linelen, modify) != -1);
	}

 out:
	if (line)
		free (line);

	return this_offset;
}

/* Output a modified hunk to out.
 * hunk: original hunk
 * line_offset: offset modification to apply
 * modify: diff output that applies to this hunk
 * original: original diff */
static long show_modified_hunk (struct hunk *hunk, long line_offset,
				FILE *modify, FILE *original, FILE *out)
{
	long this_offset = 0;
	unsigned long calc_orig_count;
	unsigned long calc_new_offset, calc_new_count;
	unsigned long orig_offset, orig_count, new_offset, new_count;
	unsigned long morig_offset, morig_count, mnew_offset, mnew_count;
	char *line = NULL;
	size_t linelen = 0;
	FILE *t = tmpfile ();
	int t_written_to = 0;
	unsigned long i, at = 1;
	unsigned long replaced, unaltered;
	int info_written = 0;

	if (!t)
		error (EXIT_FAILURE, errno, "Couldn't open temporary file");

	rewind (modify);

	fsetpos (original, &hunk->filepos);
	assert (getline (&line, &linelen, original) != -1);
	if (hunk->info) {
		assert (getline (&line, &linelen, original) != -1);
		assert (getline (&line, &linelen, original) != -1);
		at += 2;
	}
	assert (!read_atatline (line, &orig_offset, &orig_count,
				&new_offset, &new_count));
	calc_orig_count = orig_count;
	calc_new_offset = new_offset;
	calc_new_count = new_count;

	assert (getline (&line, &linelen, modify) != -1);
	assert (!read_atatline (line, &morig_offset, &morig_count,
				&mnew_offset, &mnew_count));
	replaced = morig_count;

	do {
		/* Lines before the modification are unaltered. */
		int trim = 0;
		unaltered = morig_offset - hunk->line_in_diff;
		if (!morig_count)
			unaltered++;
		if (unaltered > at)
			unaltered -= at;
		else unaltered = 0;

		if (!unaltered)
			trim = 1;
#ifdef DEBUG
		fprintf (stderr, "First %lu lines unaltered\n", unaltered);
#endif /* DEBUG */
		for (i = unaltered; i; i--) {
			assert (getline (&line, &linelen, original) != -1);
			fputs (line, t);
			at++;
			t_written_to = 1;

			switch (line[0]) {
			case ' ':
				if (new_count) new_count--;
			case '-':
				if (orig_count) orig_count--;
				break;
			case '+':
				if (new_count) new_count--;
				break;
			}
		}

		while (morig_count || mnew_count) {
			if (getline (&line, &linelen, modify) == -1)
				break;

			if (line[0] == '\\' || line[1] == '\\')
				error (EXIT_FAILURE, 0,
				       "Don't know how to handle newline "
				       "issues yet.");

#ifdef DEBUG
			fprintf (stderr, "Modify using: %s", line);
#endif /* DEBUG */
			switch (line[0]) {
			case '-':
				switch (line[1]) {
				case '+':
					this_offset--;
					calc_new_count--;
					trim = 0;
					break;
				case '-':
					this_offset++;
					calc_orig_count--;
					break;
				case ' ':
					calc_new_count--;
					calc_orig_count--;
					break;
				case '@':
					goto hunk_end;
				default:
					error (EXIT_FAILURE, 0,
					       "Not supported: %c%c",
					       line[0], line[1]);
				}

				if (trim) {
					orig_offset++;
					calc_new_offset++;
				}

				if (morig_count)
					morig_count--;

				break;
			case '+':
				switch (line[1]) {
				case '+':
					this_offset++;
					calc_new_count++;
					fputs (line + 1, t);
					t_written_to = 1;
					break;
				case '-':
					this_offset--;
					calc_orig_count++;
					trim = 0;
					fputs (line + 1, t);
					t_written_to = 1;
					break;
				case ' ':
					calc_orig_count++;
					calc_new_count++;
					fputs (line + 1, t);
					t_written_to = 1;
					break;
				case '@':
					goto hunk_end;
				default:
					error (EXIT_FAILURE, 0,
					       "Not supported: %c%c",
					       line[0], line[1]);
				}

				if (trim) {
					orig_offset--;
					calc_new_offset--;
				}

				if (mnew_count)
					mnew_count--;

				break;
			}
		}

		/* Skip replaced lines in original hunk. */
#ifdef DEBUG
		fprintf (stderr, "Skip %lu replaced lines\n", replaced);
#endif /* DEBUG */
		while (replaced) {
			replaced--;
			assert (getline (&line, &linelen, original) != -1);
			at++;
			switch (line[0]) {
			case ' ':
				if (new_count) new_count--;
			case '-':
				if (orig_count) orig_count--;
				break;
			case '+':
				if (new_count) new_count--;
				break;
			}
		}

	hunk_end:
		if (line[0] && line[1] == '@') {
			if (line[0] == '+') {
				long loff;
				FILE *write_to;
				write_to = t_written_to ? t : out;
				if (hunk->info && !info_written) {
					fputs (hunk->info->orig_file, out);
					fputs (hunk->info->new_file, out);
					info_written = 1;
				}
				loff = added_hunk (line + 1,
						   this_offset,
						   modify, write_to,
						   morig_count,
						   mnew_count);

				if (!hunk->discard_offset)
					line_offset += loff;
#ifdef DEBUG
				else
					fprintf (stderr,
						 "Discarding offset: %ld\n",
						 loff);
#endif /* DEBUG */
			} else if (line[0] == '-') {
				FILE *write_to;
				write_to = t_written_to ? t : out;
				this_offset += removed_hunk (line + 1,
							     this_offset,
							     modify, write_to,
							     &info_written,
							     hunk, morig_count,
							     mnew_count,
							     &replaced);

#ifdef DEBUG
				fprintf (stderr, "Skip %lu replaced lines\n",
					 replaced);
#endif /* DEBUG */

				/* Prevent an offset banner for this
				 * hunk being generated. */
				calc_orig_count = 0;
				calc_new_count = 0;

				while (replaced) {
					replaced--;
					assert (getline (&line, &linelen,
							 original) != -1);
					at++;
					switch (line[0]) {
					case ' ':
						if (new_count) new_count--;
					case '-':
						if (orig_count) orig_count--;
						break;
					case '+':
						if (new_count) new_count--;
						break;
					}
				}
			} else error (EXIT_FAILURE, 0,
				    "diff output not understood");
		}

		if (getline (&line, &linelen, modify) == -1)
			break;

		assert (!read_atatline (line, &morig_offset, &morig_count,
				        &mnew_offset, &mnew_count));
		replaced = morig_count;
	} while (!feof (modify));

#ifdef DEBUG
	fprintf (stderr, "Copy remaining lines of original hunk (%lu,%lu)\n",
		 orig_count, new_count);
#endif /* DEBUG */
	while (orig_count || new_count) {
		if (getline (&line, &linelen, original) == -1)
			break;
		fputs (line, t);
#ifdef DEBUG
		fputs (line, stderr);
#endif /* DEBUG */
		at++;
		switch (line[0]) {
		case ' ':
			if (new_count) new_count--;
		case '-':
			if (orig_count) orig_count--;
			break;
		case '+':
			if (new_count) new_count--;
			break;
		}
		assert (line[0] != '@');
	}

#ifdef DEBUG
	fprintf (stderr, "Result:\n");
#endif /* DEBUG */

	if (hunk->info && !info_written) {
		fputs (hunk->info->orig_file, out);
		fputs (hunk->info->new_file, out);
		info_written = 1;
	}
	rewind (t);

	if (calc_orig_count && calc_new_count) {
		fprintf (out, "@@ -%lu", orig_offset);
		if (calc_orig_count != 1)
			fprintf (out, ",%lu", calc_orig_count);
		fprintf (out, " +%lu", calc_new_offset + line_offset);
		if (calc_new_count != 1)
			fprintf (out, ",%lu", calc_new_count);
		fprintf (out, " @@\n");
	}

	while (getline (&line, &linelen, t) != -1)
		fputs (line, out);

	if (line)
		free (line);
	fclose (modify);
	fclose (t);

#ifdef DEBUG
	fprintf (stderr, "Trailing:\n");
#endif /* DEBUG */
	copy_trailing (hunk, original, out, at - 1);
#ifdef DEBUG
	fprintf (stderr, "(End, offset %ld)\n", this_offset);
#endif /* DEBUG */
	return this_offset;
}

/* Write a corrected version of the edited diff to standard output.
 *
 * This works by comparing the modified lines in the edited diff with
 * the hunks in the original diff. */
static int rediff (const char *original, const char *edited, FILE *out)
{
	pid_t child;
	FILE *o;
	FILE *m;
	FILE *t = NULL;
	char *line = NULL;
	size_t linelen = 0;
	unsigned long linenum = 0;
	struct hunk *hunks = NULL, **p = &hunks, *last = NULL;
	struct hunk *current_hunk = NULL;
	long line_offset = 0;
	fpos_t first_hunk;

	/* Let's take a look at what hunks are in the original diff. */
	o = xopen (original, "r");
	while (!feof (o)) {
		unsigned long o_count, n_count;
		struct hunk *newhunk;
		fpos_t pos;

		/* Search for start of hunk (or file info). */
		do {
			fgetpos (o, &pos);
			if (getline (&line, &linelen, o) == -1)
				break;
			linenum++;
		} while (strncmp (line, "@@ ", 3) &&
			 strncmp (line, "--- ", 4));

		if (feof (o))
			break;

		if (last)
			last->num_lines = linenum - last->line_in_diff + 1;

		newhunk = xmalloc (sizeof *newhunk);
		newhunk->filepos = pos;
		newhunk->line_in_diff = linenum;
		newhunk->num_lines = 0;

		if (!strncmp (line, "--- ", 4)) {
			struct file_info *info = xmalloc (sizeof *info);
			info->orig_file = xstrdup (line);
			if (getline (&line, &linelen, o) == -1)
				error (EXIT_FAILURE, errno,
				       "Premature end of file");
			info->new_file = xstrdup (line);
			newhunk->info = info;
			if (getline (&line, &linelen, o) == -1)
				error (EXIT_FAILURE, errno,
				       "Premature end of file");
			linenum += 2;
		} else newhunk->info = NULL;

		read_atatline (line, &newhunk->orig_offset,
			       &newhunk->orig_count,
			       &newhunk->new_offset,
			       &newhunk->new_count);
		o_count = newhunk->orig_count;
		n_count = newhunk->new_count;

		newhunk->next = NULL;
		if (*p)
			(*p)->next = newhunk;
		*p = last = newhunk;
		p = &newhunk->next;

#ifdef DEBUG
		if (newhunk->info)
			fprintf (stderr, "This is the first of a group\n");
		fprintf (stderr, "Original hunk at line %lu: "
			 "-%lu,%lu +%lu,%lu\n", newhunk->line_in_diff,
			 newhunk->orig_offset, newhunk->orig_count,
			 newhunk->new_offset, newhunk->new_count);
#endif /* DEBUG */

		/* Skip to next hunk. */
		while (o_count || n_count) {
			if (getline (&line, &linelen, o) == -1)
				break;

			linenum++;
			switch (line[0]) {
			case ' ':
				if (n_count) n_count--;
			case '-':
				if (o_count) o_count--;
				break;
			case '+':
				if (n_count) n_count--;
				break;
			}
		}
	}

	if (!hunks)
		error (EXIT_FAILURE, 0, "Original patch seems empty");

	last->num_lines = linenum - last->line_in_diff + 1;

	/* Run diff between original and edited. */
	t = xpipe (DIFF, &child, "r", DIFF, "-U0",
		   original, edited, NULL);
	m = tmpfile ();
	if (m) {
		size_t buffer_size = 10000;
		char *buffer = xmalloc (buffer_size);
		while (!feof (t)) {
			size_t got = fread (buffer, 1, buffer_size, t);
			fwrite (buffer, 1, got, m);
		}
		fclose (t);
		waitpid (child, NULL, 0);
		rewind (m);
		free (buffer);
	} else error (EXIT_FAILURE, errno, "Couldn't create temporary file");

	/* For each hunk in m, identify which hunk in o has been
	 * touched.  Display unmodified hunks before that one
	 * (adjusting offsets), then step through the touched hunk
	 * applying changes as necessary. */
	*line = '\0';
	while (!feof (m)) {
		unsigned long orig_line;
		unsigned long orig_count;
		struct hunk *which;
		fpos_t pos;

		while (strncmp (line, "@@ ", 3)) {
			fgetpos (m, &pos);
			if (getline (&line, &linelen, m) == -1)
				break;
		}
	
		if (feof (m))
			break;

		read_atatline (line, &orig_line, &orig_count, NULL, NULL);
		if (!orig_count)
			orig_line++;

		/* Find out which hunk that is. */
		for (which = hunks; which; which = which->next) {
			int header;

			if (!which->next) {
				/* Last one; this must be it. */
				break;
			}

			header = which->next->info ? 2 : 0;
			if (which->next->line_in_diff + header > orig_line)
				/* Next one is past that point. */
				break;
		}
		assert (which);

		if (which->line_in_diff + which->num_lines <= orig_line)
			which->discard_offset = 1;

#ifdef DEBUG
		fprintf (stderr, "Modified hunk starts on line %lu\n",
			 which->line_in_diff);
		if (which->discard_offset)
			fprintf (stderr, "(But discarding offset)\n");
#endif /* DEBUG */

		/* If this is modifying a new hunk, we need to write
		 * out what we had and all the intervening hunks,
		 * adjusting offsets as we go. */
		if (current_hunk != which) {
			if (current_hunk) {
				line_offset += show_modified_hunk
					(current_hunk, line_offset,
					 t, o, out);
				current_hunk = current_hunk->next;
			}

			/* Copy hunks, adjusting offsets. */
			copy_to (current_hunk ? current_hunk : hunks,
				 which, &line_offset, o, out,
				 current_hunk == NULL);

			/* This meta hunk is the first pertaining to
			 * the hunk in the original. */
			first_hunk = pos;
			t = tmpfile ();
		}

		current_hunk = which;

		/* Append the meta hunk to a temporary file. */
		fputs (line, t);
		while (!feof (m)) {
			if (getline (&line, &linelen, m) == -1)
				break;
			if (!strncmp (line, "@@ ", 3))
				break;
			fputs (line, t);
		}
	}

	/* Now display the remaining hunks, adjusting offsets. */
	if (current_hunk) {
		line_offset += show_modified_hunk (current_hunk, line_offset,
						   t, o, out);
		current_hunk = current_hunk->next;
		if (current_hunk)
			copy_to (current_hunk, NULL, &line_offset, o, out, 0);
	} else
		copy_to (hunks, NULL, &line_offset, o, out, 1);

	fclose (o);
	fclose (m);
	if (line)
		free (line);

	return 0;
}
static char * syntax_str = "usage: %s ORIGINAL EDITED\n";

NORETURN
static void syntax (int err)
{
	fprintf (err ? stderr : stdout, syntax_str, progname);
	exit (err);
}

int main (int argc, char *argv[])
{
	/* name to use in error messages */
	set_progname ("rediff");
	
	while (1) {
		static struct option long_options[] = {
	       		{"help", 0, 0, 'h'},
			{"version", 0, 0, 'v'},
			{0, 0, 0, 0}
		};
		int c = getopt_long (argc, argv, "vh",
				long_options, NULL);
		if (c == -1)
			break;
		
		switch (c) {
		case 'v':
			printf("rediff - patchutils version %s\n", VERSION);
			exit(0);
		case 'h':
			syntax (0);
			break;
		default:
			syntax(1);
		}
				
	}
	
	if (argc - optind < 2)
		syntax (1);

	if (access (argv[optind + 1], R_OK))
		error (EXIT_FAILURE, errno, "can't read edited file");

	return rediff (argv[optind], argv[optind + 1], stdout);
}