Annotation of src/usr.bin/cmp/cmp.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: cmp.c,v 1.10 2003/06/03 02:56:06 millert Exp $ */
1.1 deraadt 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.
1.10 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94";
42: #else
1.11 ! deraadt 43: static char rcsid[] = "$OpenBSD: cmp.c,v 1.10 2003/06/03 02:56:06 millert Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
47: #include <sys/types.h>
48: #include <sys/stat.h>
49:
50: #include <err.h>
51: #include <fcntl.h>
52: #include <stdio.h>
53: #include <stdlib.h>
54: #include <string.h>
55: #include <unistd.h>
56: #include <locale.h>
57:
58: #include "extern.h"
59:
60: int lflag, sflag;
61:
1.9 millert 62: static void usage(void);
1.1 deraadt 63:
64: int
1.11 ! deraadt 65: main(int argc, char *argv[])
1.1 deraadt 66: {
67: struct stat sb1, sb2;
68: off_t skip1, skip2;
69: int ch, fd1, fd2, special;
70: char *file1, *file2;
71:
72: setlocale(LC_ALL, "");
73:
1.5 millert 74: while ((ch = getopt(argc, argv, "ls")) != -1)
1.1 deraadt 75: switch (ch) {
76: case 'l': /* print all differences */
77: lflag = 1;
78: break;
79: case 's': /* silent run */
80: sflag = 1;
81: break;
82: case '?':
83: default:
84: usage();
85: }
1.8 mickey 86:
1.1 deraadt 87: argv += optind;
88: argc -= optind;
89:
90: if (lflag && sflag)
91: errx(ERR_EXIT, "only one of -l and -s may be specified");
92:
93: if (argc < 2 || argc > 4)
94: usage();
95:
96: /* Backward compatibility -- handle "-" meaning stdin. */
97: special = 0;
98: if (strcmp(file1 = argv[0], "-") == 0) {
99: special = 1;
100: fd1 = 0;
101: file1 = "stdin";
1.11 ! deraadt 102: } else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
1.3 michaels 103: if (sflag)
104: exit(ERR_EXIT);
105: else
106: err(ERR_EXIT, "%s", file1);
107: }
1.1 deraadt 108: if (strcmp(file2 = argv[1], "-") == 0) {
1.8 mickey 109: if (special) {
1.4 michaels 110: if (sflag)
111: exit(ERR_EXIT);
112: else
113: errx(ERR_EXIT,
114: "standard input may only be specified once");
1.8 mickey 115: }
1.1 deraadt 116: special = 1;
117: fd2 = 0;
118: file2 = "stdin";
1.11 ! deraadt 119: } else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
1.4 michaels 120: if (sflag)
121: exit(ERR_EXIT);
122: else
123: err(ERR_EXIT, "%s", file2);
1.8 mickey 124: }
1.1 deraadt 125:
1.6 deraadt 126: skip1 = argc > 2 ? strtoq(argv[2], NULL, 0) : 0;
127: skip2 = argc == 4 ? strtoq(argv[3], NULL, 0) : 0;
1.1 deraadt 128:
129: if (!special) {
1.8 mickey 130: if (fstat(fd1, &sb1)) {
1.4 michaels 131: if (sflag)
132: exit(ERR_EXIT);
133: else
134: err(ERR_EXIT, "%s", file1);
1.8 mickey 135: }
1.1 deraadt 136: if (!S_ISREG(sb1.st_mode))
137: special = 1;
138: else {
1.8 mickey 139: if (fstat(fd2, &sb2)) {
1.4 michaels 140: if (sflag)
141: exit(ERR_EXIT);
142: else
143: err(ERR_EXIT, "%s", file2);
1.8 mickey 144: }
1.1 deraadt 145: if (!S_ISREG(sb2.st_mode))
146: special = 1;
147: }
148: }
149:
150: if (special)
151: c_special(fd1, file1, skip1, fd2, file2, skip2);
152: else
153: c_regular(fd1, file1, skip1, sb1.st_size,
154: fd2, file2, skip2, sb2.st_size);
1.3 michaels 155: return 0;
1.1 deraadt 156: }
157:
158: static void
1.11 ! deraadt 159: usage(void)
1.1 deraadt 160: {
161:
162: (void)fprintf(stderr,
1.7 deraadt 163: "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
1.1 deraadt 164: exit(ERR_EXIT);
165: }