1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94"; 41 #endif 42 #endif 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/types.h> 48 #include <sys/stat.h> 49 50 #include <capsicum_helpers.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <fcntl.h> 54 #include <getopt.h> 55 #include <nl_types.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include <libutil.h> 62 63 #include "extern.h" 64 65 bool bflag, lflag, sflag, xflag, zflag; 66 67 static const struct option long_opts[] = 68 { 69 {"print-bytes", no_argument, NULL, 'b'}, 70 {"ignore-initial", required_argument, NULL, 'i'}, 71 {"verbose", no_argument, NULL, 'l'}, 72 {"bytes", required_argument, NULL, 'n'}, 73 {"silent", no_argument, NULL, 's'}, 74 {"quiet", no_argument, NULL, 's'}, 75 {NULL, no_argument, NULL, 0} 76 }; 77 78 #ifdef SIGINFO 79 volatile sig_atomic_t info; 80 81 static void 82 siginfo(int signo) 83 { 84 85 info = signo; 86 } 87 #endif 88 89 static void usage(void); 90 91 static bool 92 parse_iskipspec(char *spec, off_t *skip1, off_t *skip2) 93 { 94 char *colon; 95 96 colon = strchr(spec, ':'); 97 if (colon != NULL) 98 *colon++ = '\0'; 99 100 if (expand_number(spec, skip1) < 0) 101 return (false); 102 103 if (colon != NULL) 104 return (expand_number(colon, skip2) == 0); 105 106 *skip2 = *skip1; 107 return (true); 108 } 109 110 int 111 main(int argc, char *argv[]) 112 { 113 struct stat sb1, sb2; 114 off_t skip1, skip2, limit; 115 int ch, fd1, fd2, oflag; 116 bool special; 117 const char *file1, *file2; 118 119 limit = skip1 = skip2 = 0; 120 oflag = O_RDONLY; 121 while ((ch = getopt_long(argc, argv, "+bhi:ln:sxz", long_opts, NULL)) != -1) 122 switch (ch) { 123 case 'b': /* Print bytes */ 124 bflag = true; 125 break; 126 case 'h': /* Don't follow symlinks */ 127 oflag |= O_NOFOLLOW; 128 break; 129 case 'i': 130 if (!parse_iskipspec(optarg, &skip1, &skip2)) { 131 fprintf(stderr, 132 "Invalid --ignore-initial: %s\n", 133 optarg); 134 usage(); 135 } 136 break; 137 case 'l': /* print all differences */ 138 lflag = true; 139 break; 140 case 'n': /* Limit */ 141 if (expand_number(optarg, &limit) < 0 || limit < 0) { 142 fprintf(stderr, "Invalid --bytes: %s\n", 143 optarg); 144 usage(); 145 } 146 break; 147 case 's': /* silent run */ 148 sflag = true; 149 break; 150 case 'x': /* hex output */ 151 lflag = true; 152 xflag = true; 153 break; 154 case 'z': /* compare size first */ 155 zflag = true; 156 break; 157 case '?': 158 default: 159 usage(); 160 } 161 argv += optind; 162 argc -= optind; 163 164 if (lflag && sflag) 165 errx(ERR_EXIT, "specifying -s with -l or -x is not permitted"); 166 167 if (argc < 2 || argc > 4) 168 usage(); 169 170 /* Don't limit rights on stdin since it may be one of the inputs. */ 171 if (caph_limit_stream(STDOUT_FILENO, CAPH_WRITE | CAPH_IGNORE_EBADF)) 172 err(ERR_EXIT, "unable to limit rights on stdout"); 173 if (caph_limit_stream(STDERR_FILENO, CAPH_WRITE | CAPH_IGNORE_EBADF)) 174 err(ERR_EXIT, "unable to limit rights on stderr"); 175 176 /* Backward compatibility -- handle "-" meaning stdin. */ 177 special = false; 178 if (strcmp(file1 = argv[0], "-") == 0) { 179 special = true; 180 fd1 = STDIN_FILENO; 181 file1 = "stdin"; 182 } else if ((fd1 = open(file1, oflag, 0)) < 0 && errno != EMLINK) { 183 if (!sflag) 184 err(ERR_EXIT, "%s", file1); 185 else 186 exit(ERR_EXIT); 187 } 188 if (strcmp(file2 = argv[1], "-") == 0) { 189 if (special) 190 errx(ERR_EXIT, 191 "standard input may only be specified once"); 192 special = true; 193 fd2 = STDIN_FILENO; 194 file2 = "stdin"; 195 } else if ((fd2 = open(file2, oflag, 0)) < 0 && errno != EMLINK) { 196 if (!sflag) 197 err(ERR_EXIT, "%s", file2); 198 else 199 exit(ERR_EXIT); 200 } 201 202 if (argc > 2 && expand_number(argv[2], &skip1) < 0) { 203 fprintf(stderr, "Invalid skip1: %s\n", argv[2]); 204 usage(); 205 } 206 207 if (argc == 4 && expand_number(argv[3], &skip2) < 0) { 208 fprintf(stderr, "Invalid skip2: %s\n", argv[3]); 209 usage(); 210 } 211 212 if (sflag && skip1 == 0 && skip2 == 0) 213 zflag = true; 214 215 if (fd1 == -1) { 216 if (fd2 == -1) { 217 c_link(file1, skip1, file2, skip2, limit); 218 exit(0); 219 } else if (!sflag) 220 errx(ERR_EXIT, "%s: Not a symbolic link", file2); 221 else 222 exit(ERR_EXIT); 223 } else if (fd2 == -1) { 224 if (!sflag) 225 errx(ERR_EXIT, "%s: Not a symbolic link", file1); 226 else 227 exit(ERR_EXIT); 228 } 229 230 /* FD rights are limited in c_special() and c_regular(). */ 231 caph_cache_catpages(); 232 233 if (!special) { 234 if (fstat(fd1, &sb1)) { 235 if (!sflag) 236 err(ERR_EXIT, "%s", file1); 237 else 238 exit(ERR_EXIT); 239 } 240 if (!S_ISREG(sb1.st_mode)) 241 special = true; 242 else { 243 if (fstat(fd2, &sb2)) { 244 if (!sflag) 245 err(ERR_EXIT, "%s", file2); 246 else 247 exit(ERR_EXIT); 248 } 249 if (!S_ISREG(sb2.st_mode)) 250 special = true; 251 } 252 } 253 254 #ifdef SIGINFO 255 (void)signal(SIGINFO, siginfo); 256 #endif 257 if (special) 258 c_special(fd1, file1, skip1, fd2, file2, skip2, limit); 259 else { 260 if (zflag && sb1.st_size != sb2.st_size) { 261 if (!sflag) 262 (void) printf("%s %s differ: size\n", 263 file1, file2); 264 exit(DIFF_EXIT); 265 } 266 c_regular(fd1, file1, skip1, sb1.st_size, 267 fd2, file2, skip2, sb2.st_size, limit); 268 } 269 exit(0); 270 } 271 272 static void 273 usage(void) 274 { 275 276 (void)fprintf(stderr, 277 "usage: cmp [-l | -s | -x] [-hz] file1 file2 [skip1 [skip2]]\n"); 278 exit(ERR_EXIT); 279 } 280