1 package org.jnode.command.file;
3 import java.io.BufferedInputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import org.jnode.shell.AbstractCommand;
9 import org.jnode.shell.syntax.Argument;
10 import org.jnode.shell.syntax.FileArgument;
13 * Compare two files and report the first difference.
15 * @author Levente S\u00e1ntha
17 public class CmpCommand extends AbstractCommand {
19 private static final int BUFFER_SIZE = 64 * 1024;
21 private static final String HELP_SUPER = "Compare two files";
22 private static final String HELP_FILE = "a file to compare";
23 private static final String ERR_FILE_INVALID = "%s is not a file%n";
24 private static final String MSG_DIFFER = "%s %s differ: byte %d, line %d%n";
25 private static final String MSG_EOF = "cmp: EOF on %s%n";
27 private final FileArgument file1Arg;
28 private final FileArgument file2Arg;
33 file1Arg = new FileArgument("file1", Argument.MANDATORY | Argument.EXISTING, HELP_FILE);
34 file2Arg = new FileArgument("file2", Argument.MANDATORY | Argument.EXISTING, HELP_FILE);
36 registerArguments(file1Arg, file2Arg);
39 public static void main(String[] args) throws Exception {
40 new CmpCommand().execute(args);
44 public void execute() throws IOException {
46 File file1 = file1Arg.getValue();
47 File file2 = file2Arg.getValue();
49 PrintWriter err = getError().getPrintWriter();
51 if (!file1.isFile()) {
52 err.format(ERR_FILE_INVALID, file1);
56 if (!file2.isFile()) {
57 err.format(ERR_FILE_INVALID, file2);
61 BufferedInputStream bis1 = null;
62 BufferedInputStream bis2 = null;
65 bis1 = new BufferedInputStream(new FileInputStream(file1), BUFFER_SIZE);
66 bis2 = new BufferedInputStream(new FileInputStream(file2), BUFFER_SIZE);
75 if (b1 == -1 && b2 == -1)
80 PrintWriter out = getOutput().getPrintWriter();
81 out.format(MSG_EOF, file1.toString());
87 PrintWriter out = getOutput().getPrintWriter();
88 out.format(MSG_EOF, file2.toString());
94 PrintWriter out = getOutput().getPrintWriter();
95 out.format(MSG_DIFFER, file1.toString(), file2.toString(), bc, lc);
102 if (b1 == (byte) '\n')
109 } catch (IOException x) {
117 } catch (IOException x) {