* PATCH: ztrftime: more workarounds for broken strftime interface
2015-11-22 17:27 Fix strftime (prompts) before 5.2? Bart Schaefer
@ 2015-11-22 17:43 ` Mikael Magnusson
2015-11-22 18:06 ` Fix strftime (prompts) before 5.2? Peter Stephenson
1 sibling, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2015-11-22 17:43 UTC (permalink / raw)
To: zsh-workers
On Sun, Nov 22, 2015 at 6:27 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:
> Do we want to try to fix users/20859 ? It shouldn't be that hard for
> someone in an environment where it fails (I'm having trouble creating
> one).
The problem is that the strftime() function is defined by an idiot. There
is no way to differentiate the success of a zero-length result and any
error. If there is any error then the return result is undefined so we
cannot use it.
I guess we could append some fixed string always, and then discard this
string, for example an x character.
This somehow works and doesn't break the tests, nor my prompt.
---
Src/utils.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/Src/utils.c b/Src/utils.c
index c19cca8..f42b25b 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -3154,8 +3154,9 @@ strftimehandling:
{
int size = fmt - fmtstart;
char *tmp, *last;
- tmp = zhalloc(size + 1);
+ tmp = zhalloc(size + 2);
strncpy(tmp, fmtstart, size);
+ tmp[size] = 'x';
last = fmt-1;
if (*last == Meta) {
/*
@@ -3168,7 +3169,7 @@ strftimehandling:
*/
*last = *++fmt ^ 32;
}
- tmp[size] = '\0';
+ tmp[size+1] = '\0';
*buf = '\1';
if (!strftime(buf, bufsize + 2, tmp, tm))
{
@@ -3178,7 +3179,7 @@ strftimehandling:
}
return 0;
}
- decr = strlen(buf);
+ decr = strlen(buf) - 1;
buf += decr;
bufsize -= decr - 2;
}
--
2.6.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Fix strftime (prompts) before 5.2?
2015-11-22 17:27 Fix strftime (prompts) before 5.2? Bart Schaefer
2015-11-22 17:43 ` PATCH: ztrftime: more workarounds for broken strftime interface Mikael Magnusson
@ 2015-11-22 18:06 ` Peter Stephenson
2015-11-22 18:38 ` Bart Schaefer
1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2015-11-22 18:06 UTC (permalink / raw)
To: Bart Schaefer, zsh-workers
On Sun, 22 Nov 2015 09:27:11 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> Do we want to try to fix users/20859 ? It shouldn't be that hard for
> someone in an environment where it fails (I'm having trouble creating
> one).
Putting it all together, the reported failing case is:
% (LC_ALL=de_CH.UTF-8; print -P "%D{%a %b %d} %D{%I:%M:%S%P}")
Son Nov 22
5:36% (LC_ALL=de_CH.UTF-8; print -P "%D{%a %b %d} %D{%I:%M:%S}")
Son Nov 22 05:37:02
and that's indeed the feature --- no time if %P is present.
The date component is irrelevant (except that it shosw strftime isn't
completely screwed), so it just reduces to the second %D substitution in
that locale. Not specific to Switzerland or in fact to German; happens
in de_DE.UTF-8 and fr_CH.UTF-8, too. Suspected metafication, but it
wasn't.
This suggests the test for a successful expansion isn't safe after all,
but probably there aren't too many expansions like %p and %P.
pws
diff --git a/Src/utils.c b/Src/utils.c
index 0afa8c9..4640970 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -3143,6 +3143,7 @@ strftimehandling:
* in the accounting in bufsize (but nowhere else).
*/
{
+ char origchar = fmt[-1];
int size = fmt - fmtstart;
char *tmp, *last;
tmp = zhalloc(size + 1);
@@ -3163,11 +3164,17 @@ strftimehandling:
*buf = '\1';
if (!strftime(buf, bufsize + 2, tmp, tm))
{
- if (*buf) {
- buf[0] = '\0';
- return -1;
+ /*
+ * Some locales don't have strings for
+ * AM/PM, so empty output is valid.
+ */
+ if (*buf || (origchar != 'p' && origchar != 'P')) {
+ if (*buf) {
+ buf[0] = '\0';
+ return -1;
+ }
+ return 0;
}
- return 0;
}
decr = strlen(buf);
buf += decr;
^ permalink raw reply [flat|nested] 4+ messages in thread