Add strncmp(), strtol() and strtoul() functions
cover Add strncmp(), strtol() and strtoul() functions
Add FreeBSD's implementation of strtol() and strtoul() functions from:
https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/strtoul.c
The FreeBSD code being added as a separate file (common/libc/strtol.c) is under the BSD 3-clause license. Modification to COPYING file might be needed.
Also add simple implementation of the strncmp() function.
Paul Semel (1):
string: add freebds libc implementation of strtol()
Pawel Wieczorkiewicz (3):
libc, strtol: Add isspace(), isdigit(), isxdigit(), isascii() libc, strtol: Add FreeBSD libc implementation of strtoul()
libc: add strncmp() function
build/files.mk | 1 + common/lib.c | 8 -- common/libc/string.c | 11 +++ common/libc/strtol.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ common/libc/vsnprintf.c | 8 -- include/xtf/libc.h | 35 +++++++++ 6 files changed, 248 insertions(+), 16 deletions(-) create mode 100644 common/libc/strtol.c
Signature · 12 lines
2.16.6 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879
PATCH 1/4 string: add freebds libc implementation of strtol()
From: Paul Semel <[email protected]>
Added freebsd libc implementation of the strtol() function. Original can be found here :
https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/strtol.c
--- build/files.mk | 1 + common/libc/strtol.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/xtf/libc.h | 2 + 3 files changed, 138 insertions(+) create mode 100644 common/libc/strtol.c
build/files.mk +1 -0
@@ -13,6 +13,7 @@ obj-perarch += $(ROOT)/common/lib.o obj-perarch += $(ROOT)/common/libc/stdio.o obj-perarch += $(ROOT)/common/libc/string.o obj-perarch += $(ROOT)/common/libc/vsnprintf.o +obj-perarch += $(ROOT)/common/libc/strtol.o obj-perarch += $(ROOT)/common/report.o obj-perarch += $(ROOT)/common/setup.o obj-perarch += $(ROOT)/common/xenbus.o
common/libc/strtol.c new file +135 -0
135 lines not on this page · raw message
include/xtf/libc.h +2 -0
@@ -35,6 +35,8 @@ void *memcpy(void *dst, const void *src, size_t n);int memcmp (const void *s1,const void *s2,size_t n);#define memcmp (s1, s2, n) __builtin_memcmp(s1, s2, n) +long strtol (const char *nptr,char **endptr,int base); +size_t strnlen (const char *str,size_t max); int __printf(3, 0)
Signature · 12 lines
2.16.6 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879
PATCH 2/4 libc, strtol: Add isspace(), isdigit(), isxdigit(), isascii()
In reply to Pawel WieczorkiewiczThese ctype.h derived helper functions simplify strtol() code and will help simplify strtoul().
--- common/lib.c | 8 -------- common/libc/strtol.c | 9 +++------ common/libc/vsnprintf.c | 8 -------- include/xtf/libc.h | 29 +++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 22 deletions(-)
common/lib.c +0 -8
@@ -3,14 +3,6 @@#include <xtf/hypercall.h> #include <xtf/xenstore.h> -#ifndef isdigit -/* Avoid pulling in all of ctypes just for this. */ -static int isdigit (int c) -{ -return c >='0' && c <='9' ; -} -#endif -void __noreturn panic (const char *fmt, ...) {va_list args;
common/libc/strtol.c +3 -6
@@ -57,7 +57,7 @@ long strtol(const char *nptr, char **endptr, int base) s = nptr;do { c = *s++; - }while ( c ==' ' ); + }while (isspace (c) );if (c =='-' ) { neg =1 ; c = *s++;
@@ -67,10 +67,7 @@ long strtol(const char *nptr, char **endptr, int base) c = *s++; }if ((base ==0 || base ==16 ) && - c =='0' && (*s =='x' || *s =='X' ) && - ((s[1 ] >='0' && s[1 ] <='9' ) || - (s[1 ] >='A' && s[1 ] <='F' ) || - (s[1 ] >='a' && s[1 ] <='f' ))) { + c =='0' && (*s =='x' || *s =='X' ) &&isxdigit (s[1 ])) { c = s[1 ]; s +=2 ; base =16 ;
@@ -104,7 +101,7 @@ long strtol(const char *nptr, char **endptr, int base) cutlim = cutoff % base; cutoff /= base;for ( ; ; c = *s++) { -if (c >='0' && c <='9' ) +if (isdigit (c)) c -='0' ;else if (c >='A' && c <='Z' ) c -='A' -10 ;
common/libc/vsnprintf.c +0 -8
@@ -2,14 +2,6 @@#include <xtf/libc.h> #include <xtf/compiler.h> -#ifndef isdigit -/* Avoid pulling in all of ctypes just for this. */ -static int isdigit (int c) -{ -return c >='0' && c <='9' ; -} -#endif -/* * The subset of formatting supported: *
include/xtf/libc.h +29 -0
@@ -56,6 +56,35 @@ bool arch_fmt_pointer( char **str, char *end, const char **fmt_ptr, const void *arg, int width, int precision, unsigned int flags); +#ifndef isspace +static inline int isspace (int c) +{ +return c ==' ' || c =='\t' ; +} +#endif + +#ifndef isdigit +static inline int isdigit (int c) +{ +return c >='0' && c <='9' ; +} +#endif + +#ifndef isxdigit +static inline int isxdigit (int c) +{ +return (isdigit (c) || (c >='A' && c <='F' ) || + (c >='a' && c <='f' )); +} +#endif + +#ifndef isascii +static inline int isascii (int c) +{ +return c >=0 && c <=127 ; +} +#endif +#endif /* XTF_LIBC_H */ /*
Signature · 12 lines
2.16.6 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879
PATCH 3/4 libc, strtol: Add FreeBSD libc implementation of strtoul()
In reply to Pawel WieczorkiewiczThe function was derived from:
https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/strtoul.c
Sligthly modified to use ctypes helpers and ignore locale.
--- common/libc/strtol.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/xtf/libc.h | 1 + 2 files changed, 70 insertions(+)
common/libc/strtol.c +69 -0
@@ -130,3 +130,72 @@ long strtol(const char *nptr, char **endptr, int base) *endptr = (char *)(any ? s -1 : nptr);return (acc); } + +/* +* Convert a string to an unsigned long integer. +* +* Ignores `locale' stuff. Assumes that the upper and lower case +* alphabets and digits are each contiguous. +*/ +unsigned long strtoul (const char *nptr,char **endptr,int base) +{ +const char *s = nptr; +unsigned long acc; +unsigned char c; +unsigned long cutoff; +int neg =0 , any, cutlim; + +/* +* See strtol for comments as to the logic used. +*/ +do { + c = *s++; + }while (isspace (c)); +if (c =='-' ) { + neg =1 ; + c = *s++; + }else if (c =='+' ) + c = *s++; +if ((base ==0 || base ==16 ) && + c =='0' && (*s =='x' || *s =='X' ) &&isxdigit (s[1 ])) { + c = s[1 ]; + s +=2 ; + base =16 ; + } +if (base ==0 ) + base = c =='0' ?8 :10 ; + acc = any =0 ; +if (base <2 || base >36 ) + goto noconv; + + cutoff = (unsigned long )ULONG_MAX / (unsigned long )base; + cutlim = (unsigned long )ULONG_MAX % (unsigned long )base; +for ( ; ; c = *s++) { +if (isdigit (c)) + c -='0' ; +else if (c >='A' && c <='Z' ) + c -='A' -10 ; +else if (c >='a' && c <='z' ) + c -='a' -10 ; +else +break ; +if (c >= base) +break ; +if (any <0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any =-1 ; +else { + any =1 ; + acc *= base; + acc += c; + } + } +if (any <0 ) { + acc =ULONG_MAX ; + }else if (!any) { +noconv: ; + }else if (neg) + acc = -acc; +if (endptr !=NULL ) + *endptr = (char *)(any ? s -1 : nptr); +return (acc); +}
include/xtf/libc.h +1 -0
@@ -36,6 +36,7 @@ int memcmp(const void *s1, const void *s2, size_t n);#define memcmp (s1, s2, n) __builtin_memcmp(s1, s2, n)long strtol (const char *nptr,char **endptr,int base); +unsigned long strtoul (const char *nptr,char **endptr,int base);size_t strnlen (const char *str,size_t max);
Signature · 12 lines
2.16.6 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879
PATCH 4/4 libc: add strncmp() function
In reply to Pawel Wieczorkiewicz--- common/libc/string.c | 11 +++++++++++ include/xtf/libc.h | 3 +++ 2 files changed, 14 insertions(+)
common/libc/string.c +11 -0
@@ -56,6 +56,17 @@ int (strcmp)(const char *_s1, const char *_s2)return (s1 < s2) ?-1 : (s1 > s2); } +int (strncmp)(const char *_s1,const char *_s2,size_t n) +{ +size_t ctr; +for (ctr =0 ; ctr < n; ctr++) { +if (_s1[ctr] != _s2[ctr]) +return (int )(_s1[ctr] - _s2[ctr]); + } + +return 0 ; +} +void *(memset)(void *s,int c,size_t n) {char *p = s;
include/xtf/libc.h +3 -0
@@ -26,6 +26,9 @@ char *strncpy(char *dst, const char *src, size_t n);int strcmp (const char *s1,const char *s2);#define strcmp (s1, s2) __builtin_strcmp(s1, s2) +int strncmp (const char *s1,const char *s2,size_t n); +#define strncmp (s1, s2, n) __builtin_strncmp(s1, s2, n) +void *memset (void *s,int c,size_t n);#define memset (d, c, n) __builtin_memset(d, c, n)
Signature · 12 lines
2.16.6 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879
Quoted 2 lines · Pawel Wieczorkiewicz
Add FreeBSD's implementation of
strtol()andstrtoul()functions from:
https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/strtoul.c
I would suggest to specify the baseline used in each commit. This will allows us to track any changes that was made afterwards (even if it seems unlikely) in the FreeBSD code base.
Quoted 24 lines
The FreeBSD code being added as a separate file (
common/libc/strtol.c) is under the BSD 3-clause license. Modification to COPYING file might be needed.Also add simple implementation of the
strncmp()function.Paul Semel (1):
string: add freebds libc implementation ofstrtol()Pawel Wieczorkiewicz (3):
libc, strtol: Addisspace(),isdigit(),isxdigit(),isascii()libc, strtol: Add FreeBSD libc implementation ofstrtoul()
libc: addstrncmp()functionbuild/files.mk | 1 + common/lib.c | 8 -- common/libc/string.c | 11 +++ common/libc/strtol.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ common/libc/vsnprintf.c | 8 -- include/xtf/libc.h | 35 +++++++++ 6 files changed, 248 insertions(+), 16 deletions(-)create mode 100644
common/libc/strtol.c
Signature · 1 line
Julien Grall