From ae079d204cac559bacf776f22f8911c1dd828509 Mon Sep 17 00:00:00 2001 From: Timothy Elliott Date: Mon, 6 Feb 2012 01:28:40 -0800 Subject: Implement cmp --- toys/cmp.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 toys/cmp.c diff --git a/toys/cmp.c b/toys/cmp.c new file mode 100644 index 00000000..f1e980e7 --- /dev/null +++ b/toys/cmp.c @@ -0,0 +1,97 @@ +/* vi: set sw=4 ts=4: + * + * cmp.c - Compare two files. + * + * Copyright 2012 Timothy Elliott + * + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cmp.html + +USE_CMP(NEWTOY(cmp, "<2>2ls", TOYFLAG_USR|TOYFLAG_BIN)) + +config CMP + bool "cmp" + default y + help + usage: cmp [-l] [-s] FILE1 FILE2 + + Compare the contents of two files. + + -l show all differing bytes + -s silent +*/ + +#include "toys.h" + +#define FLAG_s 1 +#define FLAG_l 2 + +int get_fd(char *file) +{ + int fd; + + if (!strcmp(file,"-")) fd=0; + else if (0>(fd = open(file, O_RDONLY, 0))) { + perror_exit("%s", file); + } + return fd; +} + +void do_cmp(int fd1, int fd2, char *file1, char *file2, char *buf1, char *buf2, + size_t size) { + int i, len1, len2, min_len; + size_t byte_no = 1, line_no = 1; + + for (;;) { + len1 = read(fd1, buf1, size); + len2 = read(fd2, buf2, size); + + min_len = len1 < len2 ? len1 : len2; + for (i=0; i