changeset 550:b2194045c40e

Remove "feature test macros", replace non-portable fdprintf() with standard fprintf().
author Rob Landley <rob@landley.net>
date Mon, 19 Mar 2012 19:19:21 -0500
parents 1f5bd8c93093
children 2548e6e590b2
files lib/lib.c lib/portability.h toys/count.c toys/patch.c
diffstat 4 files changed, 47 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Fri Mar 16 06:42:08 2012 -0500
+++ b/lib/lib.c	Mon Mar 19 19:19:21 2012 -0500
@@ -794,18 +794,21 @@
 // This should use a raw tty, fixit later.
 int yesno(char *prompt, int def)
 {
+	FILE *fp = fopen("/dev/tty", "rw");
 	char buf;
-	int i;
 
-	for (i=0; i<3 && !isatty(i); i++);
-	if (i == 3) return 1;
+	if (!fp) return 1;
 
-	fdprintf(i, "%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;
-		if (tolower(buf) == 'n') return 0;
+	fprintf(fp, "%s (%c/%c):", prompt, def ? 'Y' : 'y', def ? 'n' : 'N');
+	while (fread(&buf, 1, 1, fp)) {
+		if (tolower(buf) == 'y') def = 1;
+		if (tolower(buf) == 'n') def = 0;
+		else if (!isspace(buf)) continue;
+
+		break;
 	}
+	fclose(fp);
+
 	return def;
 }
 
--- a/lib/portability.h	Fri Mar 16 06:42:08 2012 -0500
+++ b/lib/portability.h	Mon Mar 19 19:19:21 2012 -0500
@@ -9,13 +9,22 @@
 
 #define _FILE_OFFSET_BITS 64
 
-#define _POSIX_C_SOURCE 200809L
-#define _XOPEN_SOURCE 600
-#define _BSD_SOURCE
-#define _SVID_SOURCE
+#include <features.h>
+
+//#define _POSIX_C_SOURCE 200809L
+//#define _XOPEN_SOURCE 600
+//#define _BSD_SOURCE
+//#define _SVID_SOURCE
 
-#include <stdio.h>
-#define fdprintf(...) dprintf(__VA_ARGS__)
+//#include <stdio.h>
+//#define fdprintf(...) dprintf(__VA_ARGS__)
+
+#ifdef __GLIBC__
+// An SUSv4 function that glibc refuses to #define without crazy #defines,
+// see http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html
+#include <time.h>
+char *strptime(const char *buf, const char *format, struct tm *tm);
+#endif
 
 #ifdef __GNUC__
 #define noreturn	__attribute__((noreturn))
--- a/toys/count.c	Fri Mar 16 06:42:08 2012 -0500
+++ b/toys/count.c	Mon Mar 19 19:19:21 2012 -0500
@@ -23,13 +23,14 @@
 {
 	uint64_t size = 0;
 	int len;
+	char buf[32];
 
 	for (;;) {
 		len = xread(0, toybuf, sizeof(toybuf));
 		if (!len) break;
 		size += len;
 		xwrite(1, toybuf, len);
-		fdprintf(2, "%"PRIu64" bytes\r", size);
+		xwrite(2, buf, sprintf(buf, "%"PRIu64" bytes\r", size));
 	}
-	fdprintf(2,"\n");
+	xwrite(2, "\n", 1);
 }
--- a/toys/patch.c	Fri Mar 16 06:42:08 2012 -0500
+++ b/toys/patch.c	Mon Mar 19 19:19:21 2012 -0500
@@ -75,11 +75,15 @@
 {
 	struct double_list *dlist = (struct double_list *)data;
 
-	if (TT.state>1 && *dlist->data != TT.state)
-		fdprintf(TT.state == 2 ? 2 : TT.fileout,
-			"%s\n", dlist->data+(TT.state>3 ? 1 : 0));
+	if (TT.state>1 && *dlist->data != TT.state) {
+		char *s = dlist->data+(TT.state>3 ? 1 : 0);
+		int i = TT.state == 2 ? 2 : TT.fileout;
 
-	if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
+		xwrite(i, s, strlen(s));
+		xwrite(i, "\n", 1);
+	}
+
+	if (PATCH_DEBUG) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
 
 	free(dlist->data);
 	free(data);
@@ -96,7 +100,8 @@
 	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 +133,11 @@
 	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 +160,19 @@
 
 		// 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 +191,7 @@
 				// 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 +209,7 @@
 				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 +262,7 @@
 		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;
 			}