2
* Copyright (c) 2012 Sean Bartell
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
static const char *cmdname = "cmp";
46
#define CMP_VERSION "0.0.1"
47
#define CMP_BUFLEN 1024
49
static struct option const long_options[] = {
50
{ "help", no_argument, 0, 'h' },
51
{ "version", no_argument, 0, 'v' },
55
/* Dispays help for cat in various levels */
56
void help_cmd_cmp(unsigned int level)
58
if (level == HELP_SHORT) {
59
printf("`%s' compares the contents of two files\n", cmdname);
61
help_cmd_cmp(HELP_SHORT);
63
"Usage: %s [options] <file1> <file2>\n"
65
" -h, --help A short option summary\n"
66
" -v, --version Print version information and exit\n"
67
"No output is printed; the return code is 1 if the files differ.\n",
74
static int cmp_files(const char *fn0, const char *fn1)
77
const char *fn[2] = {fn0, fn1};
79
char buffer[2][CMP_BUFLEN];
82
for (int i = 0; i < 2; i++) {
83
fd[i] = open(fn[i], O_RDONLY);
86
printf("Unable to open %s\n", fn[i]);
92
for (int i = 0; i < 2; i++) {
96
size = read(fd[i], buffer[i] + offset[i],
97
CMP_BUFLEN - offset[i]);
100
printf("Error reading from %s\n",
105
} while (size && offset[i] < CMP_BUFLEN);
108
if (offset[0] != offset[1] ||
109
bcmp(buffer[0], buffer[1], offset[0])) {
113
} while (offset[0] == CMP_BUFLEN);
123
/* Main entry point for cmd, accepts an array of arguments */
124
int cmd_cmp(char **argv)
130
argc = cli_count_args(argv);
132
for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
133
c = getopt_long(argc, argv, "hv", long_options, &opt_ind);
136
help_cmd_cmp(HELP_LONG);
139
printf("%s\n", CMP_VERSION);
144
if (argc - optind != 2) {
145
printf("%s - incorrect number of arguments. Try `%s --help'\n",
150
rc = cmp_files(argv[optind], argv[optind + 1]);