4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / usr.bin / cmp / cmp.c
CommitLineData
94721995
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
cde75055
KB
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
94721995
KB
6 */
7
8#ifndef lint
9char copyright[] =
152df645 10"@(#) Copyright (c) 1987, 1990 Regents of the University of California.\n\
94721995 11 All rights reserved.\n";
b8c620d6 12#endif /* not lint */
94721995
KB
13
14#ifndef lint
409b7fd4 15static char sccsid[] = "@(#)cmp.c 5.6 (Berkeley) %G%";
b8c620d6 16#endif /* not lint */
34cbacef 17
dd197a33 18#include <sys/types.h>
34cbacef 19#include <sys/stat.h>
51ea332c
KB
20#include <fcntl.h>
21#include <errno.h>
22#include <unistd.h>
1b88b85d 23#include <stdio.h>
51ea332c
KB
24#include <stdlib.h>
25#include <string.h>
dd197a33 26#include "extern.h"
1b88b85d 27
dd197a33 28int lflag, sflag;
1b88b85d 29
dd197a33 30static void usage __P((void));
51ea332c 31
ec37e2d9 32main(argc, argv)
1b7289a3 33 int argc;
152df645 34 char *argv[];
1b88b85d 35{
dd197a33
KB
36 struct stat sb1, sb2;
37 off_t skip1, skip2;
38 int ch, fd1, fd2, special;
39 char *file1, *file2;
34cbacef 40
1b7289a3 41 while ((ch = getopt(argc, argv, "-ls")) != EOF)
152df645 42 switch (ch) {
ec37e2d9 43 case 'l': /* print all differences */
dd197a33 44 lflag = 1;
ec37e2d9
KB
45 break;
46 case 's': /* silent run */
dd197a33 47 sflag = 1;
ec37e2d9 48 break;
dd197a33 49 case '-': /* stdin (must be after options) */
1b7289a3
KB
50 --optind;
51 goto endargs;
ec37e2d9
KB
52 case '?':
53 default:
54 usage();
34cbacef 55 }
1b7289a3 56endargs:
34cbacef
KB
57 argv += optind;
58 argc -= optind;
1b88b85d 59
dd197a33
KB
60 if (lflag && sflag)
61 err("only one of -l and -s may be specified");
62
34cbacef
KB
63 if (argc < 2 || argc > 4)
64 usage();
65
dd197a33
KB
66 /* Backward compatibility -- handle "-" meaning stdin. */
67 special = 0;
68 if (strcmp(file1 = argv[0], "-") == 0) {
69 special = 1;
1b7289a3 70 fd1 = 0;
dd197a33
KB
71 file1 = "stdin";
72 }
1b7289a3 73 else if ((fd1 = open(file1, O_RDONLY, 0)) < 0)
dd197a33
KB
74 err("%s: %s", file1, strerror(errno));
75 if (strcmp(file2 = argv[1], "-") == 0) {
76 if (special)
77 err("standard input may only be specified once");
78 special = 1;
1b7289a3 79 fd2 = 0;
dd197a33 80 file2 = "stdin";
1b88b85d 81 }
dd197a33
KB
82 else if ((fd2 = open(file2, O_RDONLY, 0)) < 0)
83 err("%s: %s", file2, strerror(errno));
84
85 skip1 = argc > 2 ? strtol(argv[2], NULL, 10) : 0;
86 skip2 = argc == 4 ? strtol(argv[3], NULL, 10) : 0;
87
88 if (!special) {
89 if (fstat(fd1, &sb1))
90 err("%s: %s", file1, strerror(errno));
91 if (!S_ISREG(sb1.st_mode))
92 special = 1;
93 else {
94 if (fstat(fd2, &sb2))
95 err("%s: %s", file2, strerror(errno));
96 if (!S_ISREG(sb2.st_mode))
97 special = 1;
34cbacef 98 }
1b88b85d 99 }
ec37e2d9 100
dd197a33
KB
101 if (special)
102 c_special(fd1, file1, skip1, fd2, file2, skip2);
103 else
104 c_regular(fd1, file1, skip1, sb1.st_size,
105 fd2, file2, skip2, sb2.st_size);
106 exit(0);
ec37e2d9
KB
107}
108
dd197a33 109static void
34cbacef 110usage()
1b88b85d 111{
51ea332c 112 (void)fprintf(stderr,
409b7fd4 113 "usage: cmp [-l | s] file1 file2 [skip1 [skip2]]\n");
dd197a33 114 exit(2);
1b88b85d 115}