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 #endif 40 41 #include <sys/cdefs.h> 42 #include <sys/types.h> 43 #include <sys/stat.h> 44 45 #include <capsicum_helpers.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <getopt.h> 50 #include <nl_types.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include <libutil.h> 57 58 #include "extern.h" 59 60 bool bflag, lflag, sflag, xflag, zflag; 61 62 static const struct option long_opts[] = 63 { 64 {"print-bytes", no_argument, NULL, 'b'}, 65 {"ignore-initial", required_argument, NULL, 'i'}, 66 {"verbose", no_argument, NULL, 'l'}, 67 {"bytes", required_argument, NULL, 'n'}, 68 {"silent", no_argument, NULL, 's'}, 69 {"quiet", no_argument, NULL, 's'}, 70 {NULL, no_argument, NULL, 0} 71 }; 72 73 #ifdef SIGINFO 74 volatile sig_atomic_t info; 75 76 static void 77 siginfo(int signo) 78 { 79 80 info = signo; 81 } 82 #endif 83 84 static void usage(void) __dead2; 85 86 static bool 87 parse_iskipspec(char *spec, off_t *skip1, off_t *skip2) 88 { 89 char *colon; 90 91 colon = strchr(spec, ':'); 92 if (colon != NULL) 93 *colon++ = '\0'; 94 95 if (expand_number(spec, skip1) < 0) 96 return (false); 97 98 if (colon != NULL) 99 return (expand_number(colon, skip2) == 0); 100 101 *skip2 = *skip1; 102 return (true); 103 } 104 105 int 106 main(int argc, char *argv[]) 107 { 108 struct stat sb1, sb2; 109 off_t skip1, skip2, limit; 110 int ch, fd1, fd2, oflag; 111 bool special; 112 const char *file1, *file2; 113 114 limit = skip1 = skip2 = 0; 115 oflag = O_RDONLY; 116 while ((ch = getopt_long(argc, argv, "+bhi:ln:sxz", long_opts, NULL)) != -1) 117 switch (ch) { 118 case 'b': /* Print bytes */ 119 bflag = true; 120 break; 121 case 'h': /* Don't follow symlinks */ 122 oflag |= O_NOFOLLOW; 123 break; 124 case 'i': 125 if (!parse_iskipspec(optarg, &skip1, &skip2)) { 126 fprintf(stderr, 127 "Invalid --ignore-initial: %s\n", 128 optarg); 129 usage(); 130 } 131 break; 132 case 'l': /* print all differences */ 133 lflag = true; 134 break; 135 case 'n': /* Limit */ 136 if (expand_number(optarg, &limit) < 0 || limit < 0) { 137 fprintf(stderr, "Invalid --bytes: %s\n", 138 optarg); 139 usage(); 140 } 141 break; 142 case 's': /* silent run */ 143 sflag = true; 144 break; 145 case 'x': /* hex output */ 146 lflag = true; 147 xflag = true; 148 break; 149 case 'z': /* compare size first */ 150 zflag = true; 151 break; 152 case '?': 153 default: 154 usage(); 155 } 156 argv += optind; 157 argc -= optind; 158 159 if (lflag && sflag) 160 errx(ERR_EXIT, "specifying -s with -l or -x is not permitted"); 161 162 if (argc < 2 || argc > 4) 163 usage(); 164 165 /* Don't limit rights on stdin since it may be one of the inputs. */ 166 if (caph_limit_stream(STDOUT_FILENO, CAPH_WRITE | CAPH_IGNORE_EBADF)) 167 err(ERR_EXIT, "unable to limit rights on stdout"); 168 if (caph_limit_stream(STDERR_FILENO, CAPH_WRITE | CAPH_IGNORE_EBADF)) 169 err(ERR_EXIT, "unable to limit rights on stderr"); 170 171 /* Backward compatibility -- handle "-" meaning stdin. */ 172 special = false; 173 if (strcmp(file1 = argv[0], "-") == 0) { 174 special = true; 175 fd1 = STDIN_FILENO; 176 file1 = "stdin"; 177 } else if ((fd1 = open(file1, oflag, 0)) < 0 && errno != EMLINK) { 178 if (!sflag) 179 err(ERR_EXIT, "%s", file1); 180 else 181 exit(ERR_EXIT); 182 } 183 if (strcmp(file2 = argv[1], "-") == 0) { 184 if (special) 185 errx(ERR_EXIT, 186 "standard input may only be specified once"); 187 special = true; 188 fd2 = STDIN_FILENO; 189 file2 = "stdin"; 190 } else if ((fd2 = open(file2, oflag, 0)) < 0 && errno != EMLINK) { 191 if (!sflag) 192 err(ERR_EXIT, "%s", file2); 193 else 194 exit(ERR_EXIT); 195 } 196 197 if (argc > 2 && expand_number(argv[2], &skip1) < 0) { 198 fprintf(stderr, "Invalid skip1: %s\n", argv[2]); 199 usage(); 200 } 201 202 if (argc == 4 && expand_number(argv[3], &skip2) < 0) { 203 fprintf(stderr, "Invalid skip2: %s\n", argv[3]); 204 usage(); 205 } 206 207 if (sflag && skip1 == 0 && skip2 == 0) 208 zflag = true; 209 210 if (fd1 == -1) { 211 if (fd2 == -1) { 212 c_link(file1, skip1, file2, skip2, limit); 213 exit(0); 214 } else if (!sflag) 215 errx(ERR_EXIT, "%s: Not a symbolic link", file2); 216 else 217 exit(ERR_EXIT); 218 } else if (fd2 == -1) { 219 if (!sflag) 220 errx(ERR_EXIT, "%s: Not a symbolic link", file1); 221 else 222 exit(ERR_EXIT); 223 } 224 225 /* FD rights are limited in c_special() and c_regular(). */ 226 caph_cache_catpages(); 227 228 if (!special) { 229 if (fstat(fd1, &sb1)) { 230 if (!sflag) 231 err(ERR_EXIT, "%s", file1); 232 else 233 exit(ERR_EXIT); 234 } 235 if (!S_ISREG(sb1.st_mode)) 236 special = true; 237 else { 238 if (fstat(fd2, &sb2)) { 239 if (!sflag) 240 err(ERR_EXIT, "%s", file2); 241 else 242 exit(ERR_EXIT); 243 } 244 if (!S_ISREG(sb2.st_mode)) 245 special = true; 246 } 247 } 248 249 #ifdef SIGINFO 250 (void)signal(SIGINFO, siginfo); 251 #endif 252 if (special) 253 c_special(fd1, file1, skip1, fd2, file2, skip2, limit); 254 else { 255 if (zflag && sb1.st_size != sb2.st_size) { 256 if (!sflag) 257 (void) printf("%s %s differ: size\n", 258 file1, file2); 259 exit(DIFF_EXIT); 260 } 261 c_regular(fd1, file1, skip1, sb1.st_size, 262 fd2, file2, skip2, sb2.st_size, limit); 263 } 264 exit(0); 265 } 266 267 static void 268 usage(void) 269 { 270 271 (void)fprintf(stderr, 272 "usage: cmp [-l | -s | -x] [-hz] file1 file2 [skip1 [skip2]]\n"); 273 exit(ERR_EXIT); 274 } 275