svn commit: branches/busybox_scratch/coreutils

aldot at busybox.net aldot at busybox.net
Sun Aug 20 10:54:31 UTC 2006


Author: aldot
Date: 2006-08-20 03:54:31 -0700 (Sun, 20 Aug 2006)
New Revision: 15835

Log:
- shrink cmp a little bit: It's smaller to use bitsmasks than the enum
   text	   data	    bss	    dec	    hex	filename
    493	      0	      0	    493	    1ed	coreutils/cmp.o.orig
    486	      0	      0	    486	    1e6	coreutils/cmp.o



Modified:
   branches/busybox_scratch/coreutils/cmp.c


Changeset:
Modified: branches/busybox_scratch/coreutils/cmp.c
===================================================================
--- branches/busybox_scratch/coreutils/cmp.c	2006-08-20 10:51:49 UTC (rev 15834)
+++ branches/busybox_scratch/coreutils/cmp.c	2006-08-20 10:54:31 UTC (rev 15835)
@@ -4,20 +4,7 @@
  *
  * Copyright (C) 2000,2001 by Matt Kraai <kraai at alumni.carnegiemellon.edu>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 /* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
@@ -34,12 +21,9 @@
  *    in the '-l' case.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
 #include "busybox.h"
 
-static FILE *cmp_xfopen_input(const char *filename)
+static FILE *cmp_xfopen_input(const char * const filename)
 {
 	FILE *fp;
 
@@ -56,32 +40,28 @@
 static const char fmt_l_opt[] = "%.0s%.0s%d %3o %3o\n";
 
 static const char opt_chars[] = "sl";
+#define CMP_OPT_s (1<<0)
+#define CMP_OPT_l (1<<1)
 
-enum {
-	OPT_s = 1,
-	OPT_l = 2
-};
-
 int cmp_main(int argc, char **argv)
 {
 	FILE *fp1, *fp2, *outfile = stdout;
-	const char *filename1, *filename2;
+	const char *filename1, *filename2 = "-";
 	const char *fmt;
-	int c1, c2, char_pos, line_pos;
-	int opt_flags;
-	int exit_val = 0;
+	int c1, c2, char_pos = 0, line_pos = 1;
+	unsigned opt;
+	int retval = 0;
 
 	bb_default_error_retval = 2;	/* 1 is returned if files are different. */
 
-	opt_flags = bb_getopt_ulflags(argc, argv, opt_chars);
+	opt = bb_getopt_ulflags(argc, argv, opt_chars);
 
-	if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > 1)) {
+	if ((opt & (CMP_OPT_s|CMP_OPT_l))
+			|| (((unsigned int)(--argc - optind)) > 1))
 		bb_show_usage();
-	}
 
 	fp1 = cmp_xfopen_input(filename1 = *(argv += optind));
 
-	filename2 = "-";
 	if (*++argv) {
 		filename2 = *argv;
 	}
@@ -95,19 +75,17 @@
 		return 0;
 	}
 
-	fmt = fmt_differ;
-	if (opt_flags == OPT_l) {
+	if (opt & CMP_OPT_l)
 		fmt = fmt_l_opt;
-	}
+	else
+		fmt = fmt_differ;
 
-	char_pos = 0;
-	line_pos = 1;
 	do {
 		c1 = getc(fp1);
 		c2 = getc(fp2);
 		++char_pos;
-		if (c1 != c2) {			/* Remember -- a read error may have occurred. */
-			exit_val = 1;		/* But assume the files are different for now. */
+		if (c1 != c2) {			/* Remember: a read error may have occurred. */
+			retval = 1;		/* But assume the files are different for now. */
 			if (c2 == EOF) {
 				/* We know that fp1 isn't at EOF or in an error state.  But to
 				 * save space below, things are setup to expect an EOF in fp1
@@ -125,13 +103,14 @@
 				 * make sure we fflush before writing to stderr. */
 				bb_xfflush_stdout();
 			}
-			if (opt_flags != OPT_s) {
-				if (opt_flags == OPT_l) {
+			if (!opt & CMP_OPT_s) {
+				if (opt & CMP_OPT_l) {
 					line_pos = c1;	/* line_pos is unused in the -l case. */
 				}
 				bb_fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
-				if (opt_flags) {	/* This must be -l since not -s. */
-					/* If we encountered and EOF, the while check will catch it. */
+				if (opt) {	/* This must be -l since not -s. */
+					/* If we encountered an EOF,
+					 * the while check will catch it. */
 					continue;
 				}
 			}
@@ -145,5 +124,5 @@
 	bb_xferror(fp1, filename1);
 	bb_xferror(fp2, filename2);
 
-	bb_fflush_stdout_and_exit(exit_val);
+	bb_fflush_stdout_and_exit(retval);
 }




More information about the busybox-cvs mailing list