Annotation of src/usr.bin/cmp/cmp.c, Revision 1.3
1.3 ! michaels 1: /* $OpenBSD: cmp.c,v 1.2 1996/06/26 05:32:05 deraadt 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1987, 1990, 1993, 1994\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94";
46: #else
1.3 ! michaels 47: static char rcsid[] = "$OpenBSD: cmp.c,v 1.2 1996/06/26 05:32:05 deraadt Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: #include <sys/types.h>
52: #include <sys/stat.h>
53:
54: #include <err.h>
55: #include <fcntl.h>
56: #include <stdio.h>
57: #include <stdlib.h>
58: #include <string.h>
59: #include <unistd.h>
60: #include <locale.h>
61:
62: #include "extern.h"
63:
64: int lflag, sflag;
65:
66: static void usage __P((void));
67:
68: int
69: main(argc, argv)
70: int argc;
71: char *argv[];
72: {
73: struct stat sb1, sb2;
74: off_t skip1, skip2;
75: int ch, fd1, fd2, special;
76: char *file1, *file2;
77:
78: setlocale(LC_ALL, "");
79:
80: while ((ch = getopt(argc, argv, "ls")) != EOF)
81: switch (ch) {
82: case 'l': /* print all differences */
83: lflag = 1;
84: break;
85: case 's': /* silent run */
86: sflag = 1;
87: break;
88: case '?':
89: default:
90: usage();
91: }
92: endargs:
93: argv += optind;
94: argc -= optind;
95:
96: if (lflag && sflag)
97: errx(ERR_EXIT, "only one of -l and -s may be specified");
98:
99: if (argc < 2 || argc > 4)
100: usage();
101:
102: /* Backward compatibility -- handle "-" meaning stdin. */
103: special = 0;
104: if (strcmp(file1 = argv[0], "-") == 0) {
105: special = 1;
106: fd1 = 0;
107: file1 = "stdin";
108: }
1.3 ! michaels 109: else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
! 110: if (sflag)
! 111: exit(ERR_EXIT);
! 112: else
! 113: err(ERR_EXIT, "%s", file1);
! 114: }
1.1 deraadt 115: if (strcmp(file2 = argv[1], "-") == 0) {
116: if (special)
117: errx(ERR_EXIT,
118: "standard input may only be specified once");
119: special = 1;
120: fd2 = 0;
121: file2 = "stdin";
122: }
123: else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
124: err(ERR_EXIT, "%s", file2);
125:
126: skip1 = argc > 2 ? strtoq(argv[2], NULL, 10) : 0;
127: skip2 = argc == 4 ? strtoq(argv[3], NULL, 10) : 0;
128:
129: if (!special) {
130: if (fstat(fd1, &sb1))
131: err(ERR_EXIT, "%s", file1);
132: if (!S_ISREG(sb1.st_mode))
133: special = 1;
134: else {
135: if (fstat(fd2, &sb2))
136: err(ERR_EXIT, "%s", file2);
137: if (!S_ISREG(sb2.st_mode))
138: special = 1;
139: }
140: }
141:
142: if (special)
143: c_special(fd1, file1, skip1, fd2, file2, skip2);
144: else
145: c_regular(fd1, file1, skip1, sb1.st_size,
146: fd2, file2, skip2, sb2.st_size);
1.3 ! michaels 147: return 0;
1.1 deraadt 148: }
149:
150: static void
151: usage()
152: {
153:
154: (void)fprintf(stderr,
155: "usage: cmp [-l | s] file1 file2 [skip1 [skip2]]\n");
156: exit(ERR_EXIT);
157: }