source: mainline/uspace/app/bdsh/cmds/modules/cmp/cmp.c@ b19e892

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b19e892 was b19e892, checked in by Jakub Jermar <jakub@…>, 8 years ago

Merge open() with posix_open() and provide vfs_lookup_open() instead

vfs_lookup_open() is really just a convenience wrapper around
vfs_lookup() and vfs_open().

  • Property mode set to 100644
File size: 4.0 KB
Line 
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>
30#include <getopt.h>
31#include <mem.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <vfs/vfs.h>
36
37#include "cmds.h"
38#include "cmp.h"
39#include "config.h"
40#include "entry.h"
41#include "errors.h"
42#include "util.h"
43
44static const char *cmdname = "cmp";
45#define CMP_VERSION "0.0.1"
46#define CMP_BUFLEN 1024
47
48static struct option const long_options[] = {
49 { "help", no_argument, 0, 'h' },
50 { "version", no_argument, 0, 'v' },
51 { 0, 0, 0, 0 }
52};
53
54/* Dispays help for cat in various levels */
55void help_cmd_cmp(unsigned int level)
56{
57 if (level == HELP_SHORT) {
58 printf("`%s' compares the contents of two files\n", cmdname);
59 } else {
60 help_cmd_cmp(HELP_SHORT);
61 printf(
62 "Usage: %s [options] <file1> <file2>\n"
63 "Options:\n"
64 " -h, --help A short option summary\n"
65 " -v, --version Print version information and exit\n"
66 "No output is printed; the return code is 1 if the files differ.\n",
67 cmdname);
68 }
69
70 return;
71}
72
73static int cmp_files(const char *fn0, const char *fn1)
74{
75 int rc = 0;
76 const char *fn[2] = {fn0, fn1};
77 int fd[2] = {-1, -1};
78 char buffer[2][CMP_BUFLEN];
79 ssize_t offset[2];
80 aoff64_t pos[2] = {};
81
82 for (int i = 0; i < 2; i++) {
83 fd[i] = vfs_lookup_open(fn[i], WALK_REGULAR, MODE_READ);
84 if (fd[i] < 0) {
85 rc = fd[i];
86 printf("Unable to open %s\n", fn[i]);
87 goto end;
88 }
89 }
90
91 do {
92 for (int i = 0; i < 2; i++) {
93 offset[i] = 0;
94 ssize_t size;
95 do {
96 size = read(fd[i], &pos[i],
97 buffer[i] + offset[i],
98 CMP_BUFLEN - offset[i]);
99 if (size < 0) {
100 rc = errno;
101 printf("Error reading from %s\n",
102 fn[i]);
103 goto end;
104 }
105 offset[i] += size;
106 } while (size && offset[i] < CMP_BUFLEN);
107 }
108
109 if (offset[0] != offset[1] ||
110 memcmp(buffer[0], buffer[1], offset[0]) != 0) {
111 rc = 1;
112 goto end;
113 }
114 } while (offset[0] == CMP_BUFLEN);
115
116end:
117 if (fd[0] >= 0)
118 close(fd[0]);
119 if (fd[1] >= 0)
120 close(fd[1]);
121 return rc;
122}
123
124/* Main entry point for cmd, accepts an array of arguments */
125int cmd_cmp(char **argv)
126{
127 int rc;
128 unsigned int argc;
129 int c, opt_ind;
130
131 argc = cli_count_args(argv);
132
133 for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) {
134 c = getopt_long(argc, argv, "hv", long_options, &opt_ind);
135 switch (c) {
136 case 'h':
137 help_cmd_cmp(HELP_LONG);
138 return CMD_SUCCESS;
139 case 'v':
140 printf("%s\n", CMP_VERSION);
141 return CMD_SUCCESS;
142 }
143 }
144
145 if (argc - optind != 2) {
146 printf("%s - incorrect number of arguments. Try `%s --help'\n",
147 cmdname, cmdname);
148 return CMD_FAILURE;
149 }
150
151 rc = cmp_files(argv[optind], argv[optind + 1]);
152 if (rc)
153 return CMD_FAILURE;
154 else
155 return CMD_SUCCESS;
156}
Note: See TracBrowser for help on using the repository browser.