Home | History | Annotate | Download | only in cmp
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /* Portions Copyright 2006 Stephen P. Potter */
     22 
     23 /*
     24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
     25  * Use is subject to license terms.
     26  */
     27 
     28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
     29 /*	  All Rights Reserved  	*/
     30 
     31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     32 
     33 /*
     34  *	compare two files
     35  */
     36 
     37 #include	<stdio.h>
     38 #include	<stdlib.h>
     39 #include	<ctype.h>
     40 #include 	<locale.h>
     41 #include	<sys/types.h>
     42 
     43 FILE	*file1, *file2;
     44 
     45 char	*arg;
     46 
     47 int	eflg;
     48 int	lflg = 1;
     49 
     50 offset_t	line = 1;
     51 offset_t	chr = 0;
     52 offset_t	skip1;
     53 offset_t	skip2;
     54 
     55 offset_t 	otoi(char *);
     56 
     57 static void narg(void);
     58 static void barg(void);
     59 static void earg(void);
     60 
     61 int
     62 main(int argc, char **argv)
     63 {
     64 	int		c;
     65 	int		c1, c2;
     66 
     67 	(void) setlocale(LC_ALL, "");
     68 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
     69 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
     70 #endif
     71 	(void) textdomain(TEXT_DOMAIN);
     72 
     73 	while ((c = getopt(argc, argv, "ls")) != EOF)
     74 		switch (c) {
     75 			case 'l':
     76 				lflg = 2;
     77 				break;
     78 			case 's':
     79 				lflg = 0;
     80 				break;
     81 			case '?':
     82 			default:
     83 				narg();
     84 		}
     85 	argv += optind;
     86 	argc -= optind;
     87 	if (argc < 2 || argc > 4)
     88 		narg();
     89 
     90 	arg = argv[0];
     91 	if (arg[0] == '-' && arg[1] == 0)
     92 		file1 = stdin;
     93 	else if ((file1 = fopen(arg, "r")) == NULL)
     94 		barg();
     95 
     96 	arg = argv[1];
     97 	if (arg[0] == '-' && arg[1] == 0)
     98 		file2 = stdin;
     99 	else if ((file2 = fopen(arg, "r")) == NULL)
    100 		barg();
    101 
    102 	if (file1 == stdin && file2 == stdin)
    103 		narg();
    104 
    105 	if (argc > 2)
    106 		skip1 = otoi(argv[2]);
    107 	if (argc > 3)
    108 		skip2 = otoi(argv[3]);
    109 	while (skip1) {
    110 		if ((c1 = getc(file1)) == EOF) {
    111 			arg = argv[0];
    112 			earg();
    113 		}
    114 		skip1--;
    115 	}
    116 	while (skip2) {
    117 		if ((c2 = getc(file2)) == EOF) {
    118 			arg = argv[1];
    119 			earg();
    120 		}
    121 		skip2--;
    122 	}
    123 
    124 	for (;;) {
    125 		chr++;
    126 		c1 = getc(file1);
    127 		c2 = getc(file2);
    128 		if (c1 == c2) {
    129 			if (c1 == '\n')
    130 				line++;
    131 			if (c1 == EOF) {
    132 				if (eflg)
    133 					return (1);
    134 				return (0);
    135 			}
    136 			continue;
    137 		}
    138 		if (lflg == 0)
    139 			return (1);
    140 		if (c1 == EOF) {
    141 			arg = argv[0];
    142 			earg();
    143 		}
    144 		if (c2 == EOF)
    145 			earg();
    146 		if (lflg == 1) {
    147 			(void) printf(
    148 			    gettext("%s %s differ: char %lld, line %lld\n"),
    149 			    argv[0], arg, chr, line);
    150 			return (1);
    151 		}
    152 		eflg = 1;
    153 		(void) printf("%6lld %3o %3o\n", chr, c1, c2);
    154 	}
    155 }
    156 
    157 offset_t
    158 otoi(s)
    159 char *s;
    160 {
    161 	offset_t v;
    162 	int base;
    163 
    164 	v = 0;
    165 	base = 10;
    166 	if (*s == '0')
    167 		base = 8;
    168 	while (isdigit(*s))
    169 		v = v*base + *s++ - '0';
    170 	return (v);
    171 }
    172 
    173 static void
    174 narg()
    175 {
    176 	(void) fprintf(stderr,
    177 	    gettext("usage: cmp [-l | -s] file1 file2 [skip1] [skip2]\n"));
    178 	exit(2);
    179 }
    180 
    181 static void
    182 barg()
    183 {
    184 	if (lflg)
    185 		(void) fprintf(stderr, gettext("cmp: cannot open %s\n"), arg);
    186 	exit(2);
    187 }
    188 
    189 static void
    190 earg()
    191 {
    192 	(void) fprintf(stderr, gettext("cmp: EOF on %s\n"), arg);
    193 	exit(1);
    194 }
    195