Книга: UNIX — универсальная среда программирования
3.8.27 idiff.c
3.8.27 idiff.c
/* idiff: interactive diff */
#include <stdio.h>
#include <ctype.h>
char *progname;
#define HUGE 10000 /* large number of lines */
main(argc, argv)
int argc;
char *argv[];
{
FILE *fin, *fout, *f1, *f2, *efopen();
char buf[BUFSIZ], *mktemp();
char *diffout = "idiff.XXXXXX";
progname = argv[0];
if (argc != 3) {
fprintf(stderr, "Usage: idiff file1 file2n");
exit(1);
}
f1 = efopen(argv[1], "r");
f2 = efopen(argv[2], "r");
fout = efopen("idiff.out", "w");
mktemp(diffout);
sprintf(buf, "diff %s %s >%s", argv[1], argv[2], diffout);
system(buf);
fin = efopen(diffout, "r");
idiff(f1, f2, fin, fout); unlink(diffout);
printf("%s output in file idiff.outn", progname);
exit(0);
}
idiff(f1, f2, fin, fout) /* process diffs */
FILE *f1, *f2, *fin, *fout;
{
char *tempfile = "idiff.XXXXXX";
char buf[BUFSIZ], buf2[BUFSIZ], *mktemp();
FILE *ft, *efopen();
int cmd, n, from1, to1, from2, to2, nf1, nf2;
mktemp(tempfile);
nf1 = nf2 = 0;
while (fgets(buf, sizeof buf, fin) != NULL) {
parse(buf, &from1, &to1, &cmd, &from2, &to2);
n = to1-from1 + to2-from2 + 1; /* #lines from diff */
if (cmd == 'c')
n += 2;
else if (cmd == 'a')
from1++;
else if (cmd == 'd')
from2++;
printf("%s", buf);
while (n-- > 0) {
fgets(buf, sizeof buf, fin);
printf("%s", buf);
}
do {
printf("? ");
fflush(stdout);
fgets(buf, sizeof buf, stdin);
switch (buf[0]) {
case '>':
nskip(f1, to1-nf1);
ncopy(f2, to2-nf2, fout);
break;
case '<':
nskip(f2, to2-nf2);
ncopy(f1, to1-nf1, fout);
break;
case 'e':
ncopy(f1, from1-1-nf1, fout);
nskip(f2, from2-1-nf2);
ft = efopen(tempfile, "w");
ncopy(f1, to1+1-from1, ft);
fprintf(ft, "--- n");
ncopy(f2, to2+1-from2, ft);
fclose(ft);
sprintf(buf2, "ed %s", tempfile);
system(buf2);
ft = efopen(tempfile, "r");
ncopy(ft, HUGE, fout);
fclose(ft);
break;
case '!':
system(buf+1);
printf("!n");
break;
default:
printf("< or > or e or !n");
break;
}
} while (buf[0]!= '<' && buf[0]!= '>' && buf[0]! = 'e');
nf1 = to1;
nf2 = to2;
}
ncopy(f1, HUGE, fout); /* can fail on very long files */
unlink(tempfile);
}
parse(s, pfrom1, pto1, pcmd, pfrom2, pto2)
char *s;
int *pcmd, *pfrom1, *pto1, *pfrom2, *pto2;
{
#define a2i(p) while (isdigit(*s))
p = 10*(p) + *s++ - '0'
*pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;
a2i(*pfrom1);
if (*s == ',') {
s++;
a2i(*pto1);
} else
*pto1 = *pfrom1;
*pcmd = *s++;
a2i(*pfrom2);
if (*s == ',') {
s++;
a2i(*pto2);
} else
*pto2 = *pfrom2;
}
nskip(fin, n) /* skip n lines of file fin */
FILE *fin;
{
char buf[BUFSIZ];
while (n-- > 0)
fgets(buf, sizeof buf, fin);
}
ncopy(fin, n, fout) /* copy n lines from fin to fout */
FILE *fin, *fout;
{
char buf[BUFSIZ];
while (n-- > 0) {
if (fgets(buf, sizeof buf, fin) == NULL)
return;
fputs(buf, fout);
}
}
#include "efopen.c"
- 3.8.1 addup1
- 3.8.2. addup2
- 3.8.3 backup
- 3.8.4 backwards
- 3.8.5 badpick.c
- 3.8.6 bundle
- 3.8.7 cal
- 3.8.8 calendar1
- 3.8.9 calendar2
- 3.8.10 calendar3
- 3.8.11 cat0.c
- 3.8.12 checkmail.c
- 3.8.13 checkmail.sh
- 3.8.14 cp.c
- 3.8.15 doctype
- 3.8.16 double
- 3.8.17 efopen.c
- 3.8.18 error.c
- 3.8.19 field1
- 3.8.20 field2
- 3.8.21 fold
- 3.8.22 frequent
- 3.8.23 frequent2
- 3.8.24 get
- 3.8.25 get.с
- 3.8.26 getname
- 3.8.27 idiff.c
- 3.8.28 makefile
- 3.8.29 newer
- 3.8.30 news1
- 3.8.31 news2
- 3.8.32 news3
- 3.8.33 nohup
- 3.8.34 older
- 3.8.35 overwrite1
- 3.8.36 overwrite2
- 3.8.37 overwrite3
- 3.8.38 p1.c
- 3.8.39 p2.c
- 3.8.40 p3.c
- 3.8.41 p4.c
- 3.8.42 pick1
- 3.8.43 pick.c
- 3.8.44 prpages
- 3.8.45 put
- 3.8.46 readslow.c
- 3.8.47 replace
- 3.8.48 signaltest.c
- 3.8.49 spname.c
- 3.8.50 strindex.c
- 3.8.51 sv.c
- 3.8.52 system1.c
- 3.8.53 system.c
- 3.8.54 timeout.c
- 3.8.55 toolong
- 3.8.56 ttyin1.c
- 3.8.57 ttyin2.c
- 3.5.58 vis1.c
- 3.5.59 vis2.c
- 3.8.60 vis3.c
- 3.8.61 waitfile.c
- 3.8.62 watchfor
- 3.8.63 watchwho
- 3.8.64 which1
- 3.8.65 which1.H
- 3.8.66 which2
- 3.8.67 wordfreq
- 3.8.68 zap1
- 3.8.69 zap2
- 3.8.70 zap.c