Handle file2 being a directory. Rename patch files.

This commit is contained in:
ian 2000-09-20 21:32:16 +00:00
parent f2f18154a8
commit d6e15c13fa
5 changed files with 43 additions and 37 deletions

View File

@ -1,6 +1,6 @@
--- idiff.1.orig Tue Sep 29 17:36:58 1998 --- idiff.1.orig Wed Sep 20 13:25:31 2000
+++ idiff.1 Tue Sep 29 17:36:58 1998 +++ idiff.1 Wed Sep 20 13:34:03 2000
@@ -0,0 +1,76 @@ @@ -0,0 +1,79 @@
+.TH IDIFF Local +.TH IDIFF Local
+.SH NAME +.SH NAME
+idiff \- interactive diff of two files +idiff \- interactive diff of two files
@ -69,11 +69,14 @@
+.I diff +.I diff
+through +through
+.I idiff. +.I idiff.
+Giving a file and a directory for +Nor can you use `-' as a filename argument.
+.I diff
+to search for the second file will fail in strange ways.
+.SH HISTORY +.SH HISTORY
+.I Idiff +.I Idiff
+appears in the book +appears in the book
+.I ``The UNIX Programming Environment'' +.I ``The UNIX Programming Environment''
+by Brian Kernighan and Rob Pike. +by Brian Kernighan and Rob Pike.
+.SH SEE ALSO
+.I sdiff (1)
+(side-by-side diff), a GNU tool that is included in OpenBSD.
+.I mgdiff (1),
+an X11 tool for comparing two files visually.

View File

@ -1,10 +1,7 @@
--- idiff.c.orig Wed Sep 16 15:58:16 1998 --- idiff.c.orig Wed Sep 16 15:58:16 1998
+++ idiff.c Tue Sep 29 18:06:05 1998 +++ idiff.c Wed Sep 20 13:12:40 2000
@@ -1,45 +1,98 @@ @@ -1,45 +1,109 @@
-/* idiff: interactive diff */ /* idiff: interactive diff */
+/*
+ * idiff - interactive diff
+ */
#include <stdio.h> #include <stdio.h>
+#include <stdlib.h> +#include <stdlib.h>
@ -15,14 +12,9 @@
-#define HUGE 10000 /* large number of lines */ -#define HUGE 10000 /* large number of lines */
+#include <signal.h> +#include <signal.h>
+#include <assert.h> +#include <assert.h>
+#include <sys/stat.h>
-main(argc, argv) +#include <err.h>
- int argc; +
- char *argv[];
-{
- FILE *fin, *fout, *f1, *f2, *efopen();
- char buf[BUFSIZ], *mktemp();
- char *diffout = "idiff.XXXXXX";
+#define HUGE 1000000L +#define HUGE 1000000L
+ +
+char *progname; /* for error messages */ +char *progname; /* for error messages */
@ -36,8 +28,14 @@
+void parse(char *s, int* pfrom1, int* pto1, int* pcmd, int* pfrom2, int* pto2); +void parse(char *s, int* pfrom1, int* pto1, int* pcmd, int* pfrom2, int* pto2);
+void nskip(FILE *fin, int n); +void nskip(FILE *fin, int n);
+void ncopy(FILE *fin, int n, FILE *fout); +void ncopy(FILE *fin, int n, FILE *fout);
+void die(char *fmt, char *arg);
+ -main(argc, argv)
- int argc;
- char *argv[];
-{
- FILE *fin, *fout, *f1, *f2, *efopen();
- char buf[BUFSIZ], *mktemp();
- char *diffout = "idiff.XXXXXX";
+int +int
+main(int argc, char *argv[]) +main(int argc, char *argv[])
+{ +{
@ -47,6 +45,7 @@
+ extern int optind; + extern int optind;
+ extern char *optarg; + extern char *optarg;
+ int use_b = 0; /* true --> use diff -b */ + int use_b = 0; /* true --> use diff -b */
+ struct stat sbuf;
progname = argv[0]; progname = argv[0];
- if (argc != 3) { - if (argc != 3) {
@ -77,13 +76,24 @@
+ inname1 = argv[optind+0]; + inname1 = argv[optind+0];
+ inname2 = argv[optind+1]; + inname2 = argv[optind+1];
+ f1 = efopen(inname1, "r"); + f1 = efopen(inname1, "r");
+ if (stat(inname2, &sbuf) == -1) {
+ err(1, "Can't open %s", inname2);
+ }
+ /* If arg2 is a directory, do what diff would do, but we do it
+ * because we need to read the file back in, in bitsies, later on.
+ */
+ if (S_ISDIR(sbuf.st_mode)) {
+ char*tmp = (char*)malloc(strlen(inname2)+1+strlen(inname1));
+ sprintf(tmp, "%s/%s", inname2, inname1);
+ inname2 = tmp;
+ }
+ f2 = efopen(inname2, "r"); + f2 = efopen(inname2, "r");
fout = efopen("idiff.out", "w"); fout = efopen("idiff.out", "w");
- mktemp(diffout); - mktemp(diffout);
- sprintf(buf,"diff %s %s >%s",argv[1],argv[2],diffout); - sprintf(buf,"diff %s %s >%s",argv[1],argv[2],diffout);
- system(buf); - system(buf);
+ if ((diffout = mktemp(strdup(DIFFOUT))) == NULL) + if ((diffout = mktemp(strdup(DIFFOUT))) == NULL)
+ die("Can't mktemp(%s)", diffout); + err(1, "Can't mktemp(%s)", diffout);
+ (void) sprintf(cmdBuf, "diff %s %s %s >%s", + (void) sprintf(cmdBuf, "diff %s %s %s >%s",
+ use_b ? "-b" : "", + use_b ? "-b" : "",
+ inname1, inname2, diffout); + inname1, inname2, diffout);
@ -116,14 +126,14 @@
+ assert(fout != NULL); + assert(fout != NULL);
+ +
+ if ((tempfile = mktemp(strdup(TEMPFILE))) == NULL) + if ((tempfile = mktemp(strdup(TEMPFILE))) == NULL)
+ die("Can't mktemp(%s)", tempfile); + err(1, "Can't mktemp(%s)", tempfile);
+ if ((ed=getenv("EDITOR")) == NULL) + if ((ed=getenv("EDITOR")) == NULL)
+ ed = "/bin/ed"; + ed = "/bin/ed";
+ +
nf1 = nf2 = 0; nf1 = nf2 = 0;
while (fgets(buf, sizeof buf, fin) != NULL) { while (fgets(buf, sizeof buf, fin) != NULL) {
parse(buf, &from1, &to1, &cmd, &from2, &to2); parse(buf, &from1, &to1, &cmd, &from2, &to2);
@@ -52,13 +105,13 @@ @@ -52,13 +116,13 @@
from2++; from2++;
printf("%s", buf); printf("%s", buf);
while (n-- > 0) { while (n-- > 0) {
@ -140,7 +150,7 @@
switch (buf[0]) { switch (buf[0]) {
case '>': case '>':
nskip(f1, to1-nf1); nskip(f1, to1-nf1);
@@ -75,34 +128,57 @@ @@ -75,34 +139,57 @@
ncopy(f1, to1+1-from1, ft); ncopy(f1, to1+1-from1, ft);
fprintf(ft, "---\n"); fprintf(ft, "---\n");
ncopy(f2, to2+1-from2, ft); ncopy(f2, to2+1-from2, ft);
@ -211,7 +221,7 @@
*pfrom1 = *pto1 = *pfrom2 = *pto2 = 0; *pfrom1 = *pto1 = *pfrom2 = *pto2 = 0;
a2i(*pfrom1); a2i(*pfrom1);
@@ -120,20 +196,25 @@ @@ -120,20 +207,25 @@
*pto2 = *pfrom2; *pto2 = *pfrom2;
} }
@ -242,7 +252,7 @@
while (n-- > 0) { while (n-- > 0) {
if (fgets(buf, sizeof buf, fin) == NULL) if (fgets(buf, sizeof buf, fin) == NULL)
return; return;
@@ -141,4 +222,31 @@ @@ -141,4 +233,24 @@
} }
} }
@ -258,12 +268,6 @@
+ exit(1); + exit(1);
+} +}
+ +
+void die(char *fmt, char *arg) {
+ fprintf(stderr, "%s: ", progname);
+ fprintf(stderr, fmt, arg);
+ exit(1);
+}
+
+FILE * +FILE *
+efopen(const char *file, const char *mode) /* fopen file, die if can't */ +efopen(const char *file, const char *mode) /* fopen file, die if can't */
+{ +{
@ -271,7 +275,6 @@
+ +
+ if ((fp = fopen(file, mode)) != NULL) + if ((fp = fopen(file, mode)) != NULL)
+ return fp; + return fp;
+ fprintf(stderr, "%s: can't open file %s mode %s\n", + err(1, "can't open file %s mode %s", file, mode);
+ progname, file, mode); + /*NOTREACHED*/
+ exit(1);
+} +}