Fossil SCM

Redo the enhancement of check-in [1e881f59786bd891] in a way that is portable to legacy systems. The strnlen() function is now always available.

drh 2020-02-25 15:58 trunk
Commit 1a84fe09c7a09db713e53746d2527eeb4330ff72a757679820a72f41f9d59b1a
1 file changed +18 -2
+18 -2
--- src/printf.c
+++ src/printf.c
@@ -198,10 +198,26 @@
198198
/*
199199
** Size of temporary conversion buffer.
200200
*/
201201
#define etBUFSIZE 500
202202
203
+/*
204
+** Find the length of a string as long as that length does not
205
+** exceed N bytes. If no zero terminator is seen in the first
206
+** N bytes then return N. If N is negative, then this routine
207
+** is an alias for strlen().
208
+*/
209
+#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
210
+# define StrNLen32(Z,N) (int)strnlen(Z,N)
211
+#else
212
+static int StrNLen32(const char *z, int N){
213
+ int n = 0;
214
+ while( (N-- != 0) && *(z++)!=0 ){ n++; }
215
+ return n;
216
+}
217
+#endif
218
+
203219
/*
204220
** Return an appropriate set of flags for wiki_convert() for displaying
205221
** comments on a timeline. These flag settings are determined by
206222
** configuration parameters.
207223
**
@@ -640,11 +656,11 @@
640656
case etPATH: {
641657
int i;
642658
int limit = flag_alternateform ? va_arg(ap,int) : -1;
643659
char *e = va_arg(ap,char*);
644660
if( e==0 ){e="";}
645
- length = (int)strnlen(e,limit);
661
+ length = StrNLen32(e, limit);
646662
zExtra = bufpt = fossil_malloc(length+1);
647663
for( i=0; i<length; i++ ){
648664
if( e[i]=='\\' ){
649665
bufpt[i]='/';
650666
}else{
@@ -669,11 +685,11 @@
669685
}else if( xtype==etDYNSTRING ){
670686
zExtra = bufpt;
671687
}else if( xtype==etSTRINGID ){
672688
precision = hash_digits(flag_altform2);
673689
}
674
- length = (int)strnlen(bufpt,limit);
690
+ length = StrNLen32(bufpt, limit);
675691
if( precision>=0 && precision<length ) length = precision;
676692
break;
677693
}
678694
case etBLOB: {
679695
int limit = flag_alternateform ? va_arg(ap, int) : -1;
680696
--- src/printf.c
+++ src/printf.c
@@ -198,10 +198,26 @@
198 /*
199 ** Size of temporary conversion buffer.
200 */
201 #define etBUFSIZE 500
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203 /*
204 ** Return an appropriate set of flags for wiki_convert() for displaying
205 ** comments on a timeline. These flag settings are determined by
206 ** configuration parameters.
207 **
@@ -640,11 +656,11 @@
640 case etPATH: {
641 int i;
642 int limit = flag_alternateform ? va_arg(ap,int) : -1;
643 char *e = va_arg(ap,char*);
644 if( e==0 ){e="";}
645 length = (int)strnlen(e,limit);
646 zExtra = bufpt = fossil_malloc(length+1);
647 for( i=0; i<length; i++ ){
648 if( e[i]=='\\' ){
649 bufpt[i]='/';
650 }else{
@@ -669,11 +685,11 @@
669 }else if( xtype==etDYNSTRING ){
670 zExtra = bufpt;
671 }else if( xtype==etSTRINGID ){
672 precision = hash_digits(flag_altform2);
673 }
674 length = (int)strnlen(bufpt,limit);
675 if( precision>=0 && precision<length ) length = precision;
676 break;
677 }
678 case etBLOB: {
679 int limit = flag_alternateform ? va_arg(ap, int) : -1;
680
--- src/printf.c
+++ src/printf.c
@@ -198,10 +198,26 @@
198 /*
199 ** Size of temporary conversion buffer.
200 */
201 #define etBUFSIZE 500
202
203 /*
204 ** Find the length of a string as long as that length does not
205 ** exceed N bytes. If no zero terminator is seen in the first
206 ** N bytes then return N. If N is negative, then this routine
207 ** is an alias for strlen().
208 */
209 #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
210 # define StrNLen32(Z,N) (int)strnlen(Z,N)
211 #else
212 static int StrNLen32(const char *z, int N){
213 int n = 0;
214 while( (N-- != 0) && *(z++)!=0 ){ n++; }
215 return n;
216 }
217 #endif
218
219 /*
220 ** Return an appropriate set of flags for wiki_convert() for displaying
221 ** comments on a timeline. These flag settings are determined by
222 ** configuration parameters.
223 **
@@ -640,11 +656,11 @@
656 case etPATH: {
657 int i;
658 int limit = flag_alternateform ? va_arg(ap,int) : -1;
659 char *e = va_arg(ap,char*);
660 if( e==0 ){e="";}
661 length = StrNLen32(e, limit);
662 zExtra = bufpt = fossil_malloc(length+1);
663 for( i=0; i<length; i++ ){
664 if( e[i]=='\\' ){
665 bufpt[i]='/';
666 }else{
@@ -669,11 +685,11 @@
685 }else if( xtype==etDYNSTRING ){
686 zExtra = bufpt;
687 }else if( xtype==etSTRINGID ){
688 precision = hash_digits(flag_altform2);
689 }
690 length = StrNLen32(bufpt, limit);
691 if( precision>=0 && precision<length ) length = precision;
692 break;
693 }
694 case etBLOB: {
695 int limit = flag_alternateform ? va_arg(ap, int) : -1;
696

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button