exception_lists: remove ucb from check_rtime list
[unleashed.git] / bin / cmp / cmp.c
blob1634f58eecf2e95cbbedd68ad9fcd79834eaf63a
1 /* $OpenBSD: cmp.c,v 1.18 2018/03/05 16:57:37 cheloha Exp $ */
2 /* $NetBSD: cmp.c,v 1.7 1995/09/08 03:22:56 tls Exp $ */
4 /*
5 * Copyright (c) 1987, 1990, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
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.
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.
33 #include <sys/types.h>
34 #include <sys/stat.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include "extern.h"
47 int lflag, sflag;
49 static off_t get_skip(const char *, const char *);
50 static void __dead usage(void);
52 int
53 main(int argc, char *argv[])
55 struct stat sb1, sb2;
56 off_t skip1, skip2;
57 int ch, fd1, fd2, special;
58 char *file1, *file2;
60 if (pledge("stdio rpath", NULL) == -1)
61 err(ERR_EXIT, "pledge");
63 while ((ch = getopt(argc, argv, "ls")) != -1)
64 switch (ch) {
65 case 'l': /* print all differences */
66 lflag = 1;
67 break;
68 case 's': /* silent run */
69 sflag = 1;
70 break;
71 default:
72 usage();
75 argv += optind;
76 argc -= optind;
78 if (lflag && sflag)
79 errx(ERR_EXIT, "only one of -l and -s may be specified");
81 if (argc < 2 || argc > 4)
82 usage();
84 /* Backward compatibility -- handle "-" meaning stdin. */
85 special = 0;
86 if (strcmp(file1 = argv[0], "-") == 0) {
87 special = 1;
88 fd1 = 0;
89 file1 = "stdin";
90 } else if ((fd1 = open(file1, O_RDONLY, 0)) == -1)
91 fatal("%s", file1);
92 if (strcmp(file2 = argv[1], "-") == 0) {
93 if (special)
94 fatalx("standard input may only be specified once");
95 special = 1;
96 fd2 = 0;
97 file2 = "stdin";
98 } else if ((fd2 = open(file2, O_RDONLY, 0)) == -1)
99 fatal("%s", file2);
101 if (pledge("stdio", NULL) == -1)
102 err(ERR_EXIT, "pledge");
104 skip1 = (argc > 2) ? get_skip(argv[2], "skip1") : 0;
105 skip2 = (argc == 4) ? get_skip(argv[3], "skip2") : 0;
107 if (!special) {
108 if (fstat(fd1, &sb1) == -1)
109 fatal("%s", file1);
110 if (!S_ISREG(sb1.st_mode))
111 special = 1;
112 else {
113 if (fstat(fd2, &sb2) == -1)
114 fatal("%s", file2);
115 if (!S_ISREG(sb2.st_mode))
116 special = 1;
120 if (special)
121 c_special(fd1, file1, skip1, fd2, file2, skip2);
122 else
123 c_regular(fd1, file1, skip1, sb1.st_size,
124 fd2, file2, skip2, sb2.st_size);
125 return 0;
128 static off_t
129 get_skip(const char *arg, const char *name)
131 off_t skip;
132 char *ep;
134 errno = 0;
135 skip = strtoll(arg, &ep, 0);
136 if (arg[0] == '\0' || *ep != '\0')
137 fatalx("%s is invalid: %s", name, arg);
138 if (skip < 0)
139 fatalx("%s is too small: %s", name, arg);
140 if (skip == LLONG_MAX && errno == ERANGE)
141 fatalx("%s is too large: %s", name, arg);
142 return skip;
145 static void __dead
146 usage(void)
149 (void)fprintf(stderr,
150 "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
151 exit(ERR_EXIT);