[fae4d30] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 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.
|
---|
| 16 | *
|
---|
| 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.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <errno.h>
|
---|
[f3a37e28] | 30 | #include <getopt.h>
|
---|
[fae4d30] | 31 | #include <mem.h>
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 | #include <stdlib.h>
|
---|
| 34 | #include <vfs/vfs.h>
|
---|
| 35 |
|
---|
| 36 | #include "cmds.h"
|
---|
| 37 | #include "cmp.h"
|
---|
| 38 | #include "config.h"
|
---|
| 39 | #include "entry.h"
|
---|
| 40 | #include "errors.h"
|
---|
| 41 | #include "util.h"
|
---|
| 42 |
|
---|
| 43 | static const char *cmdname = "cmp";
|
---|
| 44 | #define CMP_VERSION "0.0.1"
|
---|
| 45 | #define CMP_BUFLEN 1024
|
---|
| 46 |
|
---|
[f3a37e28] | 47 | static struct option const long_options[] = {
|
---|
| 48 | { "help", no_argument, 0, 'h' },
|
---|
| 49 | { "version", no_argument, 0, 'v' },
|
---|
| 50 | { 0, 0, 0, 0 }
|
---|
| 51 | };
|
---|
| 52 |
|
---|
[fae4d30] | 53 | /* Dispays help for cat in various levels */
|
---|
| 54 | void help_cmd_cmp(unsigned int level)
|
---|
| 55 | {
|
---|
| 56 | if (level == HELP_SHORT) {
|
---|
| 57 | printf("`%s' compares the contents of two files\n", cmdname);
|
---|
| 58 | } else {
|
---|
| 59 | help_cmd_cmp(HELP_SHORT);
|
---|
| 60 | printf(
|
---|
| 61 | "Usage: %s [options] <file1> <file2>\n"
|
---|
[f3a37e28] | 62 | "Options:\n"
|
---|
| 63 | " -h, --help A short option summary\n"
|
---|
| 64 | " -v, --version Print version information and exit\n"
|
---|
[fae4d30] | 65 | "No output is printed; the return code is 1 if the files differ.\n",
|
---|
| 66 | cmdname);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[b7fd2a0] | 72 | static errno_t cmp_files(const char *fn0, const char *fn1)
|
---|
[fae4d30] | 73 | {
|
---|
[b7fd2a0] | 74 | errno_t rc = EOK;
|
---|
[f3a37e28] | 75 | const char *fn[2] = {fn0, fn1};
|
---|
| 76 | int fd[2] = {-1, -1};
|
---|
| 77 | char buffer[2][CMP_BUFLEN];
|
---|
[8e3498b] | 78 | size_t offset[2];
|
---|
[58898d1d] | 79 | aoff64_t pos[2] = {};
|
---|
[fae4d30] | 80 |
|
---|
[f3a37e28] | 81 | for (int i = 0; i < 2; i++) {
|
---|
[f77c1c9] | 82 | rc = vfs_lookup_open(fn[i], WALK_REGULAR, MODE_READ, &(fd[i]));
|
---|
| 83 | if (rc != EOK) {
|
---|
[f3a37e28] | 84 | printf("Unable to open %s\n", fn[i]);
|
---|
| 85 | goto end;
|
---|
| 86 | }
|
---|
[fae4d30] | 87 | }
|
---|
| 88 |
|
---|
| 89 | do {
|
---|
[f3a37e28] | 90 | for (int i = 0; i < 2; i++) {
|
---|
[8e3498b] | 91 | rc = vfs_read(fd[i], &pos[i], buffer[i], CMP_BUFLEN,
|
---|
| 92 | &offset[i]);
|
---|
| 93 | if (rc != EOK) {
|
---|
| 94 | printf("Error reading from %s\n",
|
---|
| 95 | fn[i]);
|
---|
| 96 | goto end;
|
---|
| 97 | }
|
---|
[f3a37e28] | 98 | }
|
---|
[fae4d30] | 99 |
|
---|
[f3a37e28] | 100 | if (offset[0] != offset[1] ||
|
---|
[44ecf89] | 101 | memcmp(buffer[0], buffer[1], offset[0]) != 0) {
|
---|
[8e3498b] | 102 | printf("Return 1\n");
|
---|
[1569a9b] | 103 | rc = EBUSY;
|
---|
[f3a37e28] | 104 | goto end;
|
---|
| 105 | }
|
---|
| 106 | } while (offset[0] == CMP_BUFLEN);
|
---|
[fae4d30] | 107 |
|
---|
[f3a37e28] | 108 | end:
|
---|
| 109 | if (fd[0] >= 0)
|
---|
[9c4cf0d] | 110 | vfs_put(fd[0]);
|
---|
[f3a37e28] | 111 | if (fd[1] >= 0)
|
---|
[9c4cf0d] | 112 | vfs_put(fd[1]);
|
---|
[f3a37e28] | 113 | return rc;
|
---|
[fae4d30] | 114 | }
|
---|
| 115 |
|
---|
| 116 | /* Main entry point for cmd, accepts an array of arguments */
|
---|
| 117 | int cmd_cmp(char **argv)
|
---|
| 118 | {
|
---|
[b7fd2a0] | 119 | errno_t rc;
|
---|
[f3a37e28] | 120 | unsigned int argc;
|
---|
| 121 | int c, opt_ind;
|
---|
[a35b458] | 122 |
|
---|
[fae4d30] | 123 | argc = cli_count_args(argv);
|
---|
[f3a37e28] | 124 |
|
---|
[948222e4] | 125 | c = 0;
|
---|
| 126 | optreset = 1;
|
---|
| 127 | optind = 0;
|
---|
| 128 | opt_ind = 0;
|
---|
| 129 |
|
---|
| 130 | while (c != -1) {
|
---|
[f3a37e28] | 131 | c = getopt_long(argc, argv, "hv", long_options, &opt_ind);
|
---|
| 132 | switch (c) {
|
---|
| 133 | case 'h':
|
---|
| 134 | help_cmd_cmp(HELP_LONG);
|
---|
| 135 | return CMD_SUCCESS;
|
---|
| 136 | case 'v':
|
---|
| 137 | printf("%s\n", CMP_VERSION);
|
---|
| 138 | return CMD_SUCCESS;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (argc - optind != 2) {
|
---|
[fae4d30] | 143 | printf("%s - incorrect number of arguments. Try `%s --help'\n",
|
---|
| 144 | cmdname, cmdname);
|
---|
| 145 | return CMD_FAILURE;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[f3a37e28] | 148 | rc = cmp_files(argv[optind], argv[optind + 1]);
|
---|
[8e3498b] | 149 | if (rc != EOK)
|
---|
[fae4d30] | 150 | return CMD_FAILURE;
|
---|
| 151 | else
|
---|
| 152 | return CMD_SUCCESS;
|
---|
| 153 | }
|
---|