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