aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.c19
-rw-r--r--lib/portability.h21
-rw-r--r--toys/count.c5
-rw-r--r--toys/patch.c31
4 files changed, 47 insertions, 29 deletions
diff --git a/lib/lib.c b/lib/lib.c
index a1f4a528..6a2e7d98 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -794,18 +794,21 @@ void terminal_size(unsigned *x, unsigned *y)
// 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;
+
+ 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;
- 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;
+ break;
}
+ fclose(fp);
+
return def;
}
diff --git a/lib/portability.h b/lib/portability.h
index 832dd123..9b8d294f 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -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>
-#include <stdio.h>
-#define fdprintf(...) dprintf(__VA_ARGS__)
+//#define _POSIX_C_SOURCE 200809L
+//#define _XOPEN_SOURCE 600
+//#define _BSD_SOURCE
+//#define _SVID_SOURCE
+
+//#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))
diff --git a/toys/count.c b/toys/count.c
index acc0c69f..0ec3919d 100644
--- a/toys/count.c
+++ b/toys/count.c
@@ -23,13 +23,14 @@ void count_main(void)
{
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);
}
diff --git a/toys/patch.c b/toys/patch.c
index 38c71941..95702ae6 100644
--- a/toys/patch.c
+++ b/toys/patch.c
@@ -75,11 +75,15 @@ 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,
- "%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 @@ 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 +133,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 +160,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 +191,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 +209,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 +262,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;
}