[BusyBox] [PATCH] add optional SKIP arguments to cmp

Hideki IWAMOTO h-iwamoto at kit.hi-ho.ne.jp
Wed Mar 17 06:39:00 UTC 2004



Index: coreutils/Config.in
===================================================================
RCS file: /var/cvs/busybox/coreutils/Config.in,v
retrieving revision 1.24
diff -u -r1.24 Config.in
--- a/coreutils/Config.in	15 Mar 2004 08:28:19 -0000	1.24
+++ b/coreutils/Config.in	17 Mar 2004 06:16:27 -0000
@@ -59,6 +59,14 @@
 	  cmp is used to compare two files and returns the result
 	  to standard output.
 
+config CONFIG_FEATURE_CMP_SKIP
+	bool "  Enable optional arguments SKIP1 and SKIP2"
+	default n
+	depends on CONFIG_CMP
+	help
+	  SKIP1 and SKIP2 specify how many bytes to ignore
+	  at the start of each file.
+
 config CONFIG_CP
 	bool "cp"
 	default n
Index: coreutils/cmp.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/cmp.c,v
retrieving revision 1.9
diff -u -r1.9 cmp.c
--- a/coreutils/cmp.c	19 Mar 2003 09:11:32 -0000	1.9
+++ b/coreutils/cmp.c	17 Mar 2004 06:16:27 -0000
@@ -39,6 +39,12 @@
 #include <unistd.h>
 #include "busybox.h"
 
+#ifdef CONFIG_FEATURE_CMP_SKIP
+#define MAX_OPTIONAL_ARGS	3
+#else
+#define MAX_OPTIONAL_ARGS	1
+#endif
+
 static FILE *cmp_xfopen_input(const char *filename)
 {
 	FILE *fp;
@@ -65,6 +71,27 @@
 	OPT_l = 2
 };
 
+#ifdef CONFIG_FEATURE_CMP_SKIP
+static const struct suffix_mult suffixes[] = {
+	{ "k", KILOBYTE },
+	{ "M", MEGABYTE },
+	{ "G", GIGABYTE },
+	{ NULL, 0 }
+};
+
+static void skip_input(FILE *fp, unsigned long skip, const char *filename)
+{
+	if (skip > 0 && lseek(fileno(fp), skip, SEEK_CUR) < 0) {
+		while (skip-- > 0) {
+			if (getc(fp) == EOF) {
+				bb_xferror(fp, filename);
+				break;
+			}
+		}
+	}
+}
+#endif
+
 int cmp_main(int argc, char **argv)
 {
 	FILE *fp1, *fp2, *outfile = stdout;
@@ -73,12 +100,15 @@
 	int c1, c2, char_pos, line_pos;
 	int opt_flags;
 	int exit_val = 0;
+#ifdef CONFIG_FEATURE_CMP_SKIP
+	unsigned long skip1 = 0, skip2 = 0;
+#endif
 
 	bb_default_error_retval = 2;	/* 1 is returned if files are different. */
 
 	opt_flags = bb_getopt_ulflags(argc, argv, opt_chars);
 
-	if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > 1)) {
+	if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > MAX_OPTIONAL_ARGS)) {
 		bb_show_usage();
 	}
 
@@ -87,6 +117,13 @@
 	filename2 = "-";
 	if (*++argv) {
 		filename2 = *argv;
+#ifdef CONFIG_FEATURE_CMP_SKIP
+		if (*++argv) {
+			skip1 = bb_xparse_number(*argv, suffixes);
+			if (*++argv)
+				skip2 = bb_xparse_number(*argv, suffixes);
+		}
+#endif
 	}
 	fp2 = cmp_xfopen_input(filename2);
 
@@ -98,6 +135,11 @@
 		return 0;
 	}
 
+#ifdef CONFIG_FEATURE_CMP_SKIP
+	skip_input(fp1, skip1, filename1);
+	skip_input(fp2, skip2, filename2);
+#endif
+
 	fmt = fmt_differ;
 	if (opt_flags == OPT_l) {
 		fmt = fmt_l_opt;
Index: include/usage.h
===================================================================
RCS file: /var/cvs/busybox/include/usage.h,v
retrieving revision 1.196
diff -u -r1.196 usage.h
--- a/include/usage.h	13 Mar 2004 08:33:09 -0000	1.196
+++ b/include/usage.h	17 Mar 2004 06:16:28 -0000
@@ -186,10 +186,18 @@
 #define clear_full_usage \
 	"Clear screen."
 
+#ifdef CONFIG_FEATURE_CMP_SKIP
+#define USAGE_CMP_SKIP(a) a
+#else
+#define USAGE_CMP_SKIP(a)
+#endif
+
 #define cmp_trivial_usage \
-	"[OPTION]... FILE1 [FILE2]"
+	"[OPTION]... FILE1 [FILE2" USAGE_CMP_SKIP(" [SKIP1 [SKIP2]]") "]"
 #define cmp_full_usage \
-	"Compare files.\n\n" \
+	"Compare files.\n" \
+	USAGE_CMP_SKIP("SKIP1 and SKIP2 are the number of bytes to skip in each file.\n") \
+	"\n" \
 	"Options:\n" \
 	"\t-l\tWrite the byte numbers (decimal) and values (octal)\n" \
 	"\t\t  for all differing bytes.\n" \

----
Hideki IWAMOTO  h-iwamoto at kit.hi-ho.ne.jp




More information about the busybox mailing list