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

Hideki IWAMOTO h-iwamoto at kit.hi-ho.ne.jp
Wed Mar 17 15:45:07 UTC 2004


On Wed, 17 Mar 2004 14:59:19 +0300, Vladimir N. Oleynik wrote...
> But please use off_t type for skip - this recomended for large files.
> 

This is fixed version. 
This patch also adds long long version of bb_xgetlarg10_sfx to libbb.


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 14:49:35 -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 14:49:35 -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,42 @@
 	OPT_l = 2
 };
 
+#ifdef CONFIG_LFS
+#define SUFFIX_STRUCT		suffix_mult64
+#define PARSE_FUNC		bb_xgetllarg10_sfx
+#define SEEK_FUNC		fseeko
+#else
+#define SUFFIX_STRUCT		suffix_mult
+#define PARSE_FUNC		bb_xgetlarg10_sfx
+#define SEEK_FUNC		fseek
+#endif
+
+#ifdef CONFIG_FEATURE_CMP_SKIP
+static const struct SUFFIX_STRUCT suffixes[] = {
+	{ "k", 1UL << 10 },
+	{ "M", 1UL << 20 },
+	{ "G", 1UL << 30 },
+#ifdef CONFIG_LFS
+	{ "T", 1ULL << 40 },
+	{ "P", 1ULL << 50 },
+	{ "E", 1ULL << 60 },
+#endif
+	{ NULL, 0 }
+};
+
+static void skip_input(FILE *fp, off_t skip, const char *filename)
+{
+	if (skip > 0 && SEEK_FUNC(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 +115,15 @@
 	int c1, c2, char_pos, line_pos;
 	int opt_flags;
 	int exit_val = 0;
+#ifdef CONFIG_FEATURE_CMP_SKIP
+	off_t 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 +132,13 @@
 	filename2 = "-";
 	if (*++argv) {
 		filename2 = *argv;
+#ifdef CONFIG_FEATURE_CMP_SKIP
+		if (*++argv) {
+			skip1 = PARSE_FUNC(*argv, suffixes);
+			if (*++argv)
+				skip2 = PARSE_FUNC(*argv, suffixes);
+		}
+#endif
 	}
 	fp2 = cmp_xfopen_input(filename2);
 
@@ -98,6 +150,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/libbb.h
===================================================================
RCS file: /var/cvs/busybox/include/libbb.h,v
retrieving revision 1.129
diff -u -r1.129 libbb.h
--- a/include/libbb.h	15 Mar 2004 08:28:38 -0000	1.129
+++ b/include/libbb.h	17 Mar 2004 14:49:35 -0000
@@ -217,6 +217,21 @@
 								const struct suffix_mult *suffixes);
 extern long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes);
 
+struct suffix_mult64 {
+	const char *suffix;
+	unsigned long long mult;
+};
+
+extern unsigned long long bb_xgetullarg_bnd_sfx(const char *arg, int base,
+										  unsigned long long lower,
+										  unsigned long long upper,
+										  const struct suffix_mult64 *suffixes);
+
+extern long long bb_xgetllarg_bnd_sfx(const char *arg, int base,
+								long long lower,
+								long long upper,
+								const struct suffix_mult64 *suffixes);
+extern long long bb_xgetllarg10_sfx(const char *arg, const struct suffix_mult64 *suffixes);
 
 //#warning pitchable now?
 extern unsigned long bb_xparse_number(const char *numstr,
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 14:49:36 -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" \
Index: libbb/Makefile.in
===================================================================
RCS file: /var/cvs/busybox/libbb/Makefile.in,v
retrieving revision 1.34
diff -u -r1.34 Makefile.in
--- a/libbb/Makefile.in	6 Mar 2004 22:11:45 -0000	1.34
+++ b/libbb/Makefile.in	17 Mar 2004 14:49:36 -0000
@@ -70,7 +70,8 @@
 
 LIBBB_MSRC3:=$(LIBBB_DIR)xgetularg.c
 LIBBB_MOBJ3:=xgetularg_bnd_sfx.o xgetlarg_bnd_sfx.o getlarg10_sfx.o \
-	xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o
+	xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o \
+	xgetullarg_bnd_sfx.o xgetllarg_bnd_sfx.o xgetllarg10_sfx.o
 
 LIBBB_MSRC4:=$(LIBBB_DIR)/safe_strtol.c
 LIBBB_MOBJ4:=safe_strtoi.o safe_strtod.o safe_strtol.o safe_strtoul.o
Index: libbb/xgetularg.c
===================================================================
RCS file: /var/cvs/busybox/libbb/xgetularg.c,v
retrieving revision 1.2
diff -u -r1.2 xgetularg.c
--- a/libbb/xgetularg.c	15 Mar 2004 08:28:44 -0000	1.2
+++ b/libbb/xgetularg.c	17 Mar 2004 14:49:36 -0000
@@ -158,3 +158,106 @@
 	return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
 }
 #endif
+
+#ifdef L_xgetullarg_bnd_sfx
+extern
+unsigned long long bb_xgetullarg_bnd_sfx(const char *arg, int base,
+								   unsigned long long lower,
+								   unsigned long long upper,
+								   const struct suffix_mult64 *suffixes)
+{
+	unsigned long long r;
+	int old_errno;
+	char *e;
+
+	assert(arg);
+
+	/* Disallow '-' and any leading whitespace.  Speed isn't critical here
+	 * since we're parsing commandline args.  So make sure we get the
+	 * actual isspace function rather than a larger macro implementaion. */
+	if ((*arg == '-') || (isspace)(*arg)) {
+		bb_show_usage();
+	}
+
+	/* Since this is a lib function, we're not allowed to reset errno to 0.
+	 * Doing so could break an app that is deferring checking of errno.
+	 * So, save the old value so that we can restore it if successful. */
+	old_errno = errno;
+	errno = 0;
+	r = strtoull(arg, &e, base);
+	/* Do the initial validity check.  Note: The standards do not
+	 * guarantee that errno is set if no digits were found.  So we
+	 * must test for this explicitly. */
+	if (errno || (arg == e)) {	/* error or no digits */
+		bb_show_usage();
+	}
+	errno = old_errno;	/* Ok.  So restore errno. */
+
+	/* Do optional suffix parsing.  Allow 'empty' suffix tables.
+	 * Note that we also all nul suffixes with associated multipliers,
+	 * to allow for scaling of the arg by some default multiplier. */
+
+	if (suffixes) {
+		while (suffixes->suffix) {
+			if (strcmp(suffixes->suffix, e) == 0) {
+				if (ULONG_LONG_MAX / suffixes->mult < r) {	/* Overflow! */
+					bb_show_usage();
+				}
+				++e;
+				r *= suffixes->mult;
+				break;
+ 			}
+			++suffixes;
+		}
+	}
+
+	/* Finally, check for illegal trailing chars and range limits. */
+	/* Note: although we allow leading space (via stroul), trailing space
+	 * is an error.  It would be easy enough to allow though if desired. */
+	if (*e || (r < lower) || (r > upper)) {
+		bb_show_usage();
+	}
+
+	return r;
+}
+#endif
+
+#ifdef L_xgetllarg_bnd_sfx
+extern
+long long bb_xgetllarg_bnd_sfx(const char *arg, int base,
+						 long long lower,
+						 long long upper,
+						 const struct suffix_mult64 *suffixes)
+{
+	unsigned long long u = LONG_LONG_MAX;
+	long long r;
+	const char *p = arg;
+
+	if ((*p == '-') && (p[1] != '+')) {
+		++p;
+#if LONG_LONG_MAX == (-(LONG_LONG_MIN + 1))
+		++u;	/* two's complement */
+#endif
+	}
+
+	r = bb_xgetullarg_bnd_sfx(p, base, 0, u, suffixes);
+
+	if (*arg == '-') {
+		r = -r;
+	}
+
+	if ((r < lower) || (r > upper)) {
+		bb_show_usage();
+	}
+
+	return r;
+}
+#endif
+
+#ifdef L_xgetllarg10_sfx
+extern
+long long bb_xgetllarg10_sfx(const char *arg, const struct suffix_mult64 *suffixes)
+{
+	return bb_xgetllarg_bnd_sfx(arg, 10, LONG_LONG_MIN, LONG_LONG_MAX, suffixes);
+}
+#endif

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




More information about the busybox mailing list