Home | History | Annotate | Line # | Download | only in cmp
cmp.c revision 1.7
      1 /*      $NetBSD: cmp.c,v 1.7 1995/09/08 03:22:56 tls Exp $      */
      2 
      3 /*
      4  * Copyright (c) 1987, 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)cmp.c	8.3 (Berkeley) 4/2/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: cmp.c,v 1.7 1995/09/08 03:22:56 tls Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 #include <sys/stat.h>
     52 
     53 #include <err.h>
     54 #include <fcntl.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include <locale.h>
     60 
     61 #include "extern.h"
     62 
     63 int	lflag, sflag;
     64 
     65 static void usage __P((void));
     66 
     67 int
     68 main(argc, argv)
     69 	int argc;
     70 	char *argv[];
     71 {
     72 	struct stat sb1, sb2;
     73 	off_t skip1, skip2;
     74 	int ch, fd1, fd2, special;
     75 	char *file1, *file2;
     76 
     77 	setlocale(LC_ALL, "");
     78 
     79 	while ((ch = getopt(argc, argv, "ls")) != EOF)
     80 		switch (ch) {
     81 		case 'l':		/* print all differences */
     82 			lflag = 1;
     83 			break;
     84 		case 's':		/* silent run */
     85 			sflag = 1;
     86 			break;
     87 		case '?':
     88 		default:
     89 			usage();
     90 		}
     91 endargs:
     92 	argv += optind;
     93 	argc -= optind;
     94 
     95 	if (lflag && sflag)
     96 		errx(ERR_EXIT, "only one of -l and -s may be specified");
     97 
     98 	if (argc < 2 || argc > 4)
     99 		usage();
    100 
    101 	/* Backward compatibility -- handle "-" meaning stdin. */
    102 	special = 0;
    103 	if (strcmp(file1 = argv[0], "-") == 0) {
    104 		special = 1;
    105 		fd1 = 0;
    106 		file1 = "stdin";
    107 	}
    108 	else if ((fd1 = open(file1, O_RDONLY, 0)) < 0)
    109 		err(ERR_EXIT, "%s", file1);
    110 	if (strcmp(file2 = argv[1], "-") == 0) {
    111 		if (special)
    112 			errx(ERR_EXIT,
    113 				"standard input may only be specified once");
    114 		special = 1;
    115 		fd2 = 0;
    116 		file2 = "stdin";
    117 	}
    118 	else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
    119 		err(ERR_EXIT, "%s", file2);
    120 
    121 	skip1 = argc > 2 ? strtoq(argv[2], NULL, 10) : 0;
    122 	skip2 = argc == 4 ? strtoq(argv[3], NULL, 10) : 0;
    123 
    124 	if (!special) {
    125 		if (fstat(fd1, &sb1))
    126 			err(ERR_EXIT, "%s", file1);
    127 		if (!S_ISREG(sb1.st_mode))
    128 			special = 1;
    129 		else {
    130 			if (fstat(fd2, &sb2))
    131 				err(ERR_EXIT, "%s", file2);
    132 			if (!S_ISREG(sb2.st_mode))
    133 				special = 1;
    134 		}
    135 	}
    136 
    137 	if (special)
    138 		c_special(fd1, file1, skip1, fd2, file2, skip2);
    139 	else
    140 		c_regular(fd1, file1, skip1, sb1.st_size,
    141 		    fd2, file2, skip2, sb2.st_size);
    142 	exit(0);
    143 }
    144 
    145 static void
    146 usage()
    147 {
    148 
    149 	(void)fprintf(stderr,
    150 	    "usage: cmp [-l | s] file1 file2 [skip1 [skip2]]\n");
    151 	exit(ERR_EXIT);
    152 }
    153