Annotation of src/usr.bin/cmp/cmp.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: cmp.c,v 1.9 2002/02/16 21:27:45 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.10 ! millert 43: static char rcsid[] = "$OpenBSD: cmp.c,v 1.9 2002/02/16 21:27:45 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
65: main(argc, argv)
66: int argc;
67: char *argv[];
68: {
69: struct stat sb1, sb2;
70: off_t skip1, skip2;
71: int ch, fd1, fd2, special;
72: char *file1, *file2;
73:
74: setlocale(LC_ALL, "");
75:
1.5 millert 76: while ((ch = getopt(argc, argv, "ls")) != -1)
1.1 deraadt 77: switch (ch) {
78: case 'l': /* print all differences */
79: lflag = 1;
80: break;
81: case 's': /* silent run */
82: sflag = 1;
83: break;
84: case '?':
85: default:
86: usage();
87: }
1.8 mickey 88:
1.1 deraadt 89: argv += optind;
90: argc -= optind;
91:
92: if (lflag && sflag)
93: errx(ERR_EXIT, "only one of -l and -s may be specified");
94:
95: if (argc < 2 || argc > 4)
96: usage();
97:
98: /* Backward compatibility -- handle "-" meaning stdin. */
99: special = 0;
100: if (strcmp(file1 = argv[0], "-") == 0) {
101: special = 1;
102: fd1 = 0;
103: file1 = "stdin";
104: }
1.3 michaels 105: else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
106: if (sflag)
107: exit(ERR_EXIT);
108: else
109: err(ERR_EXIT, "%s", file1);
110: }
1.1 deraadt 111: if (strcmp(file2 = argv[1], "-") == 0) {
1.8 mickey 112: if (special) {
1.4 michaels 113: if (sflag)
114: exit(ERR_EXIT);
115: else
116: errx(ERR_EXIT,
117: "standard input may only be specified once");
1.8 mickey 118: }
1.1 deraadt 119: special = 1;
120: fd2 = 0;
121: file2 = "stdin";
122: }
1.8 mickey 123: else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
1.4 michaels 124: if (sflag)
125: exit(ERR_EXIT);
126: else
127: err(ERR_EXIT, "%s", file2);
1.8 mickey 128: }
1.1 deraadt 129:
1.6 deraadt 130: skip1 = argc > 2 ? strtoq(argv[2], NULL, 0) : 0;
131: skip2 = argc == 4 ? strtoq(argv[3], NULL, 0) : 0;
1.1 deraadt 132:
133: if (!special) {
1.8 mickey 134: if (fstat(fd1, &sb1)) {
1.4 michaels 135: if (sflag)
136: exit(ERR_EXIT);
137: else
138: err(ERR_EXIT, "%s", file1);
1.8 mickey 139: }
1.1 deraadt 140: if (!S_ISREG(sb1.st_mode))
141: special = 1;
142: else {
1.8 mickey 143: if (fstat(fd2, &sb2)) {
1.4 michaels 144: if (sflag)
145: exit(ERR_EXIT);
146: else
147: err(ERR_EXIT, "%s", file2);
1.8 mickey 148: }
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,
1.7 deraadt 167: "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n");
1.1 deraadt 168: exit(ERR_EXIT);
169: }