date and time created 80/10/01 17:25:39 by bill
authorBill Joy <bill@ucbvax.Berkeley.EDU>
Thu, 2 Oct 1980 09:25:39 +0000 (01:25 -0800)
committerBill Joy <bill@ucbvax.Berkeley.EDU>
Thu, 2 Oct 1980 09:25:39 +0000 (01:25 -0800)
SCCS-vsn: usr.bin/cmp/cmp.c 4.1

usr/src/usr.bin/cmp/cmp.c [new file with mode: 0644]

diff --git a/usr/src/usr.bin/cmp/cmp.c b/usr/src/usr.bin/cmp/cmp.c
new file mode 100644 (file)
index 0000000..820d94e
--- /dev/null
@@ -0,0 +1,122 @@
+static char *sccsid = "@(#)cmp.c       4.1 (Berkeley) %G%";
+#include <stdio.h>
+#include <ctype.h>
+
+FILE   *file1,*file2;
+int    eflg;
+int    lflg    = 1;
+long   line    = 1;
+long   chr     = 0;
+long   skip1;
+long   skip2;
+
+long   otoi();
+
+main(argc, argv)
+char **argv;
+{
+       register c1, c2;
+       char *arg;
+
+       if(argc < 3)
+               goto narg;
+       arg = argv[1];
+       if(arg[0] == '-' && arg[1] == 's') {
+               lflg--;
+               argv++;
+               argc--;
+       }
+       arg = argv[1];
+       if(arg[0] == '-' && arg[1] == 'l') {
+               lflg++;
+               argv++;
+               argc--;
+       }
+       if(argc < 3)
+               goto narg;
+       arg = argv[1];
+       if( arg[0]=='-' && arg[1]==0 )
+               file1 = stdin;
+       else if((file1 = fopen(arg, "r")) == NULL)
+               goto barg;
+       arg = argv[2];
+       if((file2 = fopen(arg, "r")) == NULL)
+               goto barg;
+       if (argc>3)
+               skip1 = otoi(argv[3]);
+       if (argc>4)
+               skip2 = otoi(argv[4]);
+       while (skip1) {
+               if ((c1 = getc(file1)) == EOF) {
+                       arg = argv[1];
+                       goto earg;
+               }
+               skip1--;
+       }
+       while (skip2) {
+               if ((c2 = getc(file2)) == EOF) {
+                       arg = argv[2];
+                       goto earg;
+               }
+               skip2--;
+       }
+
+loop:
+       chr++;
+       c1 = getc(file1);
+       c2 = getc(file2);
+       if(c1 == c2) {
+               if (c1 == '\n')
+                       line++;
+               if(c1 == EOF) {
+                       if(eflg)
+                               exit(1);
+                       exit(0);
+               }
+               goto loop;
+       }
+       if(lflg == 0)
+               exit(1);
+       if(c1 == EOF) {
+               arg = argv[1];
+               goto earg;
+       }
+       if(c2 == EOF)
+               goto earg;
+       if(lflg == 1) {
+               printf("%s %s differ: char %ld, line %ld\n", argv[1], arg,
+                       chr, line);
+               exit(1);
+       }
+       eflg = 1;
+       printf("%6ld %3o %3o\n", chr, c1, c2);
+       goto loop;
+
+narg:
+       printf("cmp: arg count\n");
+       exit(2);
+
+barg:
+       if (lflg)
+       printf("cmp: cannot open %s\n", arg);
+       exit(2);
+
+earg:
+       printf("cmp: EOF on %s\n", arg);
+       exit(1);
+}
+
+long otoi(s)
+char *s;
+{
+       long v;
+       int base;
+
+       v = 0;
+       base = 10;
+       if (*s == '0')
+               base = 8;
+       while(isdigit(*s))
+               v = v*base + *s++ - '0';
+       return(v);
+}