Annotation of src/usr.bin/cmp/cmp.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: cmp.c,v 1.5 1997/01/15 23:42:19 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.
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.6 ! deraadt 47: static char rcsid[] = "$OpenBSD: cmp.c,v 1.5 1997/01/15 23:42:19 millert 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:
1.5 millert 80: while ((ch = getopt(argc, argv, "ls")) != -1)
1.1 deraadt 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)
1.4 michaels 117: if (sflag)
118: exit(ERR_EXIT);
119: else
120: errx(ERR_EXIT,
121: "standard input may only be specified once");
1.1 deraadt 122: special = 1;
123: fd2 = 0;
124: file2 = "stdin";
125: }
126: else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
1.4 michaels 127: if (sflag)
128: exit(ERR_EXIT);
129: else
130: err(ERR_EXIT, "%s", file2);
1.1 deraadt 131:
1.6 ! deraadt 132: skip1 = argc > 2 ? strtoq(argv[2], NULL, 0) : 0;
! 133: skip2 = argc == 4 ? strtoq(argv[3], NULL, 0) : 0;
1.1 deraadt 134:
135: if (!special) {
136: if (fstat(fd1, &sb1))
1.4 michaels 137: if (sflag)
138: exit(ERR_EXIT);
139: else
140: err(ERR_EXIT, "%s", file1);
1.1 deraadt 141: if (!S_ISREG(sb1.st_mode))
142: special = 1;
143: else {
144: if (fstat(fd2, &sb2))
1.4 michaels 145: if (sflag)
146: exit(ERR_EXIT);
147: else
148: err(ERR_EXIT, "%s", file2);
1.1 deraadt 149: if (!S_ISREG(sb2.st_mode))
150: special = 1;
151: }
152: }
153:
154: if (special)
155: c_special(fd1, file1, skip1, fd2, file2, skip2);
156: else
157: c_regular(fd1, file1, skip1, sb1.st_size,
158: fd2, file2, skip2, sb2.st_size);
1.3 michaels 159: return 0;
1.1 deraadt 160: }
161:
162: static void
163: usage()
164: {
165:
166: (void)fprintf(stderr,
167: "usage: cmp [-l | s] file1 file2 [skip1 [skip2]]\n");
168: exit(ERR_EXIT);
169: }