svn commit: branches/busybox_scratch/coreutils

aldot at busybox.net aldot at busybox.net
Sun Aug 20 11:14:16 UTC 2006


Author: aldot
Date: 2006-08-20 04:14:15 -0700 (Sun, 20 Aug 2006)
New Revision: 15836

Log:
- shrink and cleanup dd: merge ints "sync_flag", "noerror", "trunc_flag", "twobufs_flag"
  into one int flags.
   text	   data	    bss	    dec	    hex	filename
   2159	      0	     16	   2175	    87f	coreutils/dd.o.orig
   2123	      0	     16	   2139	    85b	coreutils/dd.o.01b



Modified:
   branches/busybox_scratch/coreutils/dd.c


Changeset:
Modified: branches/busybox_scratch/coreutils/dd.c
===================================================================
--- branches/busybox_scratch/coreutils/dd.c	2006-08-20 10:54:31 UTC (rev 15835)
+++ branches/busybox_scratch/coreutils/dd.c	2006-08-20 11:14:15 UTC (rev 15836)
@@ -8,16 +8,10 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
-#include <signal.h>  // For FEATURE_DD_SIGNAL_HANDLING
 #include "busybox.h"
+#include <signal.h>  /* For FEATURE_DD_SIGNAL_HANDLING */
 
+
 static const struct suffix_mult dd_suffixes[] = {
 	{ "c", 1 },
 	{ "w", 2 },
@@ -31,14 +25,11 @@
 	{ NULL, 0 }
 };
 
-static size_t out_full;
-static size_t out_part;
-static size_t in_full;
-static size_t in_part;
+static size_t out_full, out_part, in_full, in_part;
 
-static void dd_output_status(int cur_signal)
+static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
 {
-	fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
+	bb_fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
 			(long)in_full, (long)in_part,
 			(long)out_full, (long)out_part);
 }
@@ -48,8 +39,12 @@
 	size_t count = -1, oc = 0, ibs = 512, obs = 512;
 	ssize_t n;
 	off_t seek = 0, skip = 0;
-	int sync_flag = FALSE, noerror = FALSE, trunc_flag = TRUE, twobufs_flag = 0,
-		oflag, ifd, ofd, i;
+#define sync_flag	(1<<0)
+#define noerror		(1<<1)
+#define trunc_flag	(1<<2)
+#define twobufs_flag (1<<3)
+	int flags = trunc_flag;
+	int oflag, ifd, ofd, i;
 	const char *infile = NULL, *outfile = NULL;
 	char *ibuf, *obuf;
 
@@ -58,19 +53,19 @@
 		struct sigaction sa;
 
 		memset(&sa, 0, sizeof(sa));
-		sa.sa_handler = dd_output_status; 
+		sa.sa_handler = dd_output_status;
 		sa.sa_flags = SA_RESTART;
 		sigemptyset(&sa.sa_mask);
-		sigaction(SIGUSR1, &sa, 0); 
+		sigaction(SIGUSR1, &sa, 0);
 	}
 
 	for (i = 1; i < argc; i++) {
 		if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[i], 4)) {
 			ibs = bb_xparse_number(argv[i]+4, dd_suffixes);
-			twobufs_flag++;
+			flags |= twobufs_flag;
 		} else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[i], 4)) {
 			obs = bb_xparse_number(argv[i]+4, dd_suffixes);
-			twobufs_flag++;
+			flags |= twobufs_flag;
 		} else if (!strncmp("bs=", argv[i], 3)) {
 			ibs = obs = bb_xparse_number(argv[i]+3, dd_suffixes);
 		} else if (!strncmp("count=", argv[i], 6))
@@ -87,13 +82,13 @@
 			ibuf = argv[i]+5;
 			while (1) {
 				if (!strncmp("notrunc", ibuf, 7)) {
-					trunc_flag = FALSE;
+					flags ^= trunc_flag;
 					ibuf += 7;
 				} else if (!strncmp("sync", ibuf, 4)) {
-					sync_flag = TRUE;
+					flags |= sync_flag;
 					ibuf += 4;
 				} else if (!strncmp("noerror", ibuf, 7)) {
-					noerror = TRUE;
+					flags |= noerror;
 					ibuf += 7;
 				} else {
 					bb_error_msg_and_die(bb_msg_invalid_arg, argv[i]+5, "conv");
@@ -106,7 +101,7 @@
 	}
 	ibuf = xmalloc(ibs);
 
-	if (twobufs_flag) obuf = xmalloc(obs);
+	if (flags & twobufs_flag) obuf = xmalloc(obs);
 	else obuf = ibuf;
 
 	if (infile != NULL) {
@@ -119,13 +114,13 @@
 	if (outfile != NULL) {
 		oflag = O_WRONLY | O_CREAT;
 
-		if (!seek && trunc_flag) {
+		if (!seek && (flags & trunc_flag)) {
 			oflag |= O_TRUNC;
 		}
 
 		ofd = bb_xopen3(outfile, oflag, 0666);
 
-		if (seek && trunc_flag) {
+		if (seek && (flags & trunc_flag)) {
 			if (ftruncate(ofd, seek * obs) < 0) {
 				struct stat st;
 
@@ -159,7 +154,7 @@
 	}
 
 	while (in_full + in_part != count) {
-		if (noerror) {
+		if (flags & noerror) {
 			/* Pre-zero the buffer when doing the noerror thing */
 			memset(ibuf, '\0', ibs);
 		}
@@ -169,7 +164,7 @@
 			break;
 		}
 		if (n < 0) {
-			if (noerror) {
+			if (flags & noerror) {
 				n = ibs;
 				bb_perror_msg("%s", infile);
 			} else {
@@ -185,7 +180,7 @@
 				n = ibs;
 			}
 		}
-		if (twobufs_flag) {
+		if (flags & twobufs_flag) {
 			char *tmp = ibuf;
 			while (n) {
 				size_t d = obs - oc;




More information about the busybox-cvs mailing list