[Toybox] [PATCH 08/10] Use fprintf(stderr, ...) instead of dprintf().

Georgi Chorbadzhiyski gf at unixsol.org
Wed Mar 7 16:39:00 PST 2012


This patch is needed for compilation on OS X and with bionic libc.
---
 lib/lib.c         |    2 +-
 lib/portability.h |    3 ---
 toys/count.c      |    4 ++--
 toys/patch.c      |   22 +++++++++++-----------
 toys/sed.c        |    2 +-
 5 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index d31e9fa..8fbab51 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -803,7 +803,7 @@ int yesno(char *prompt, int def)
 	for (i=0; i<3 && !isatty(i); i++);
 	if (i == 3) return 1;
 
-	fdprintf(i, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
+	fprintf(stderr, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
 	while (read(i, &buf, 1)) {
 		if (isspace(buf)) break;
 		if (tolower(buf) == 'y') return 1;
diff --git a/lib/portability.h b/lib/portability.h
index faa71ca..5161a0e 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -7,10 +7,7 @@
 
 #undef _FORTIFY_SOURCE
 
-// Humor glibc to get dprintf, then #define it to something more portable.
 #define _GNU_SOURCE
-#include <stdio.h>
-#define fdprintf(...) dprintf(__VA_ARGS__)
 
 #ifdef __ANDROID__
 #define statvfs statfs
diff --git a/toys/count.c b/toys/count.c
index acc0c69..287d3fb 100644
--- a/toys/count.c
+++ b/toys/count.c
@@ -29,7 +29,7 @@ void count_main(void)
 		if (!len) break;
 		size += len;
 		xwrite(1, toybuf, len);
-		fdprintf(2, "%"PRIu64" bytes\r", size);
+		fprintf(stderr, "%"PRIu64" bytes\r", size);
 	}
-	fdprintf(2,"\n");
+	fprintf(stderr, "\n");
 }
diff --git a/toys/patch.c b/toys/patch.c
index 38c7194..5449fea 100644
--- a/toys/patch.c
+++ b/toys/patch.c
@@ -76,10 +76,10 @@ static void do_line(void *data)
 	struct double_list *dlist = (struct double_list *)data;
 
 	if (TT.state>1 && *dlist->data != TT.state)
-		fdprintf(TT.state == 2 ? 2 : TT.fileout,
+		fprintf(TT.state == 2 ? stderr : stdout,
 			"%s\n", dlist->data+(TT.state>3 ? 1 : 0));
 
-	if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
+	if (PATCH_DEBUG) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
 
 	free(dlist->data);
 	free(data);
@@ -96,7 +96,7 @@ static void fail_hunk(void)
 	if (!TT.current_hunk) return;
 	TT.current_hunk->prev->next = 0;
 
-	fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline);
+	fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline);
 	toys.exitval = 1;
 
 	// If we got to this point, we've seeked to the end.  Discard changes to
@@ -128,11 +128,11 @@ static int apply_one_hunk(void)
 	for (plist = TT.current_hunk; plist; plist = plist->next) {
 		if (plist->data[0]==' ') matcheof++;
 		else matcheof = 0;
-		if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data);
+		if (PATCH_DEBUG) fprintf(stderr, "HUNK:%s\n", plist->data);
 	}
 	matcheof = matcheof < TT.context;
 
-	if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
+	if (PATCH_DEBUG) fprintf(stderr, "MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
 
 	// Loop through input data searching for this hunk.  Match all context
 	// lines and all lines to be removed until we've found the end of a
@@ -155,19 +155,19 @@ static int apply_one_hunk(void)
 
 		// Is this EOF?
 		if (!data) {
-			if (PATCH_DEBUG) fdprintf(2, "INEOF\n");
+			if (PATCH_DEBUG) fprintf(stderr, "INEOF\n");
 
 			// Does this hunk need to match EOF?
 			if (!plist && matcheof) break;
 
 			if (backwarn)
-				fdprintf(2,"Possibly reversed hunk %d at %ld\n",
+				fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
 						TT.hunknum, TT.linenum);
 
 			// File ended before we found a place for this hunk.
 			fail_hunk();
 			goto done;
-		} else if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data);
+		} else if (PATCH_DEBUG) fprintf(stderr, "IN: %s\n", data);
 		check = dlist_add(&buf, data);
 
 		// Compare this line with next expected line of hunk.
@@ -186,7 +186,7 @@ static int apply_one_hunk(void)
 				// recheck remaining buffered data for a new match.
 	
 				if (PATCH_DEBUG)
-					fdprintf(2, "NOT: %s\n", plist->data);
+					fprintf(stderr, "NOT: %s\n", plist->data);
 
 				TT.state = 3;
 				check = llist_pop(&buf);
@@ -204,7 +204,7 @@ static int apply_one_hunk(void)
 				check = buf;
 			} else {
 				if (PATCH_DEBUG)
-					fdprintf(2, "MAYBE: %s\n", plist->data);
+					fprintf(stderr, "MAYBE: %s\n", plist->data);
 				// This line matches.  Advance plist, detect successful match.
 				plist = plist->next;
 				if (!plist && !matcheof) goto out;
@@ -257,7 +257,7 @@ void patch_main(void)
 		if (strip || !patchlinenum++) {
 			int len = strlen(patchline);
 			if (patchline[len-1] == '\r') {
-				if (!strip) fdprintf(2, "Removing DOS newlines\n");
+				if (!strip) fprintf(stderr, "Removing DOS newlines\n");
 				strip = 1;
 				patchline[len-1]=0;
 			}
diff --git a/toys/sed.c b/toys/sed.c
index c4eb1a4..55eeaed 100644
--- a/toys/sed.c
+++ b/toys/sed.c
@@ -57,7 +57,7 @@ void sed_main(void)
 	struct arg_list *test;
 
 	for (test = TT.commands; test; test = test->next)
-		dprintf(2,"command=%s\n",test->arg);
+		fprintf(stderr, "command=%s\n", test->arg);
 
 	printf("Hello world\n");
 }
-- 
1.7.5.1




More information about the Toybox mailing list