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