lib: Cleanups
Subsystems (3)
v2 cover lib: Cleanups
v2
--
- remove a trailing whitespace [Nikos]
- add Nikos' r-b's
- add back a check for '_' in argv.c that got dropped
1) Finally, finally, finally reformat printf.c and string.c, the last two files that had weird formatting.
2) Collect is* ctype functions into a new lib/ctype.h file.
Andrew Jones (2):
lib: Fix whitespace
lib: Add ctype.h and collect is* functions
lib/argv.c | 9 +- lib/ctype.h | 40 +++++ lib/printf.c | 427 +++++++++++++++++++++++++-------------------------- lib/string.c | 356 +++++++++++++++++++++--------------------- 4 files changed, 432 insertions(+), 400 deletions(-) create mode 100644 lib/ctype.h
Signature · 1 line
2.34.3
PATCH v2 1/2 lib: Fix whitespace
printf.c and string.c are a couple of the original files and are the last that still have the original formatting. Let's finally clean them up!
The change was done by modifying Linux's scripts/Lindent to use 100 columns instead of 80 and then manually reverting a few changes that I didn't like, which I found by diffing with -b.
--- lib/printf.c | 427 +++++++++++++++++++++++++-------------------------- lib/string.c | 354 +++++++++++++++++++++--------------------- 2 files changed, 390 insertions(+), 391 deletions(-)
lib/printf.c +213 -214
@@ -10,285 +10,284 @@#define BUFSZ 2000typedef struct pstream { -char *buffer; -int remain; -int added; +char *buffer; +int remain; +int added; }pstream_t ;typedef struct strprops { -char pad; -int npad; -bool alternate; +char pad; +int npad; +bool alternate; }strprops_t ;static void addchar (pstream_t *p,char c) { -if (p->remain) { - *p->buffer++ = c; - --p->remain; - } - ++p->added; +if (p->remain) { + *p->buffer++ = c; + --p->remain; + } + ++p->added; }static void print_str (pstream_t *p,const char *s,strprops_t props) { -const char *s_orig = s; -int npad = props.npad; - -if (npad >0 ) { - npad -=strlen (s_orig); -while (npad >0 ) { -addchar (p, props.pad); - --npad; +const char *s_orig = s; +int npad = props.npad; + +if (npad >0 ) { + npad -=strlen (s_orig); +while (npad >0 ) { +addchar (p, props.pad); + --npad; + } } - } -while (*s) -addchar (p, *s++); +while (*s) +addchar (p, *s++); -if (npad <0 ) { - props.pad =' ' ;/* ignore '0' flag with '-' flag */ - npad +=strlen (s_orig); -while (npad <0 ) { -addchar (p, props.pad); - ++npad; +if (npad <0 ) { + props.pad =' ' ;/* ignore '0' flag with '-' flag */ + npad +=strlen (s_orig); +while (npad <0 ) { +addchar (p, props.pad); + ++npad; + } } - } }static char digits[16 ] ="0123456789abcdef" ;static void print_int (pstream_t *ps,long long n,int base,strprops_t props) { -char buf[sizeof (long ) *3 +2 ], *p = buf; -int s =0 , i; +char buf[sizeof (long ) *3 +2 ], *p = buf; +int s =0 , i; -if (n <0 ) { - n = -n; - s =1 ; - } +if (n <0 ) { + n = -n; + s =1 ; + } -while (n) { - *p++ = digits[n % base]; - n /= base; - } +while (n) { + *p++ = digits[n % base]; + n /= base; + } -if (s) - *p++ ='-' ; +if (s) + *p++ ='-' ; -if (p == buf) - *p++ ='0' ; +if (p == buf) + *p++ ='0' ; -for (i =0 ; i < (p - buf) /2 ; ++i) { -char tmp; +for (i =0 ; i < (p - buf) /2 ; ++i) { +char tmp; - tmp = buf[i]; - buf[i] = p[-1 -i]; - p[-1 -i] = tmp; - } + tmp = buf[i]; + buf[i] = p[-1 - i]; + p[-1 - i] = tmp; + } - *p =0 ; + *p =0 ; -print_str (ps, buf, props); +print_str (ps, buf, props); }static void print_unsigned (pstream_t *ps,unsigned long long n,int base,strprops_t props) { -char buf[sizeof (long ) *3 +3 ], *p = buf; -int i; - -while (n) { - *p++ = digits[n % base]; - n /= base; - } - -if (p == buf) - *p++ ='0' ; -else if (props.alternate && base ==16 ) { -if (props.pad =='0' ) { -addchar (ps,'0' ); -addchar (ps,'x' ); - -if (props.npad >0 ) - props.npad =MAX (props.npad -2 ,0 ); - }else { - *p++ ='x' ; - *p++ ='0' ; +char buf[sizeof (long ) *3 +3 ], *p = buf; +int i; + +while (n) { + *p++ = digits[n % base]; + n /= base; + } + +if (p == buf) + *p++ ='0' ; +else if (props.alternate && base ==16 ) { +if (props.pad =='0' ) { +addchar (ps,'0' ); +addchar (ps,'x' ); + +if (props.npad >0 ) + props.npad =MAX (props.npad -2 ,0 ); + }else { + *p++ ='x' ; + *p++ ='0' ; + } } - } -for (i =0 ; i < (p - buf) /2 ; ++i) { -char tmp; +for (i =0 ; i < (p - buf) /2 ; ++i) { +char tmp; - tmp = buf[i]; - buf[i] = p[-1 -i]; - p[-1 -i] = tmp; - } + tmp = buf[i]; + buf[i] = p[-1 - i]; + p[-1 - i] = tmp; + } - *p =0 ; + *p =0 ; -print_str (ps, buf, props); +print_str (ps, buf, props); }static int fmtnum (const char **fmt) { -const char *f = *fmt; -int len =0 , num; +const char *f = *fmt; +int len =0 , num; -if (*f =='-' ) - ++f, ++len; +if (*f =='-' ) + ++f, ++len; -while (*f >='0' && *f <='9' ) - ++f, ++len; +while (*f >='0' && *f <='9' ) + ++f, ++len; - num =atol (*fmt); - *fmt += len; -return num; + num =atol (*fmt); + *fmt += len; +return num; }int vsnprintf (char *buf,int size,const char *fmt,va_list va) { -pstream_t s; - - s.buffer = buf; - s.remain = size -1 ; - s.added =0 ; -while (*fmt) { -char f = *fmt++; -int nlong =0 ; -strprops_t props; -memset (&props,0 ,sizeof (props)); - props.pad =' ' ; - -if (f !='%' ) { -addchar (&s, f); -continue ; - } - morefmt: - f = *fmt++; -switch (f) { -case '%' : -addchar (&s,'%' ); -break ; -case 'c' : -addchar (&s,va_arg (va, int)); -break ; -case '\0' : - --fmt; -break ; -case '#' : - props.alternate = true; - goto morefmt; -case '0' : - props.pad ='0' ; - ++fmt; -/* fall through */ - case '1'...'9': -case '-' : - --fmt; - props.npad =fmtnum (&fmt); - goto morefmt; -case 'l' : - ++nlong; - goto morefmt; -case 't' : -case 'z' : -/* Here we only care that sizeof(size_t) == sizeof(long). -* On a 32-bit platform it doesn't matter that size_t is -* typedef'ed to int or long; va_arg will work either way. -* Same for ptrdiff_t (%td). -*/ - nlong =1 ; - goto morefmt; -case 'd' : -switch (nlong) { -case 0 : -print_int (&s,va_arg (va, int),10 , props); -break ; -case 1 : -print_int (&s,va_arg (va, long),10 , props); -break ; -default : - print_int(&s, va_arg(va, long long), 10, props); -break ; - } -break ; -case 'u' : -switch (nlong) { -case 0 : -print_unsigned (&s,va_arg (va, unsigned),10 , props); -break ; -case 1 : - print_unsigned(&s, va_arg(va, unsigned long), 10, props); -break ; -default : - print_unsigned(&s, va_arg(va, unsigned long long), 10, props); -break ; - } -break ; -case 'x' : -switch (nlong) { -case 0 : -print_unsigned (&s,va_arg (va, unsigned),16 , props); -break ; -case 1 : - print_unsigned(&s, va_arg(va, unsigned long), 16, props); -break ; -default : - print_unsigned(&s, va_arg(va, unsigned long long), 16, props); -break ; - } -break ; -case 'p' : - props.alternate = true; - print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props); -break ; -case 's' : - print_str(&s, va_arg(va, const char *), props); -break ; -default : -addchar (&s, f); -break ; +pstream_t s; + + s.buffer = buf; + s.remain = size -1 ; + s.added =0 ; +while (*fmt) { +char f = *fmt++; +int nlong =0 ; +strprops_t props; +memset (&props,0 ,sizeof (props)); + props.pad =' ' ; + +if (f !='%' ) { +addchar (&s, f); +continue ; + } +morefmt: + f = *fmt++; +switch (f) { +case '%' : +addchar (&s,'%' ); +break ; +case 'c' : +addchar (&s,va_arg (va, int)); +break ; +case '\0' : + --fmt; +break ; +case '#' : + props.alternate = true; + goto morefmt; +case '0' : + props.pad ='0' ; + ++fmt; +/* fall through */ + case '1' ... '9': +case '-' : + --fmt; + props.npad =fmtnum (&fmt); + goto morefmt; +case 'l' : + ++nlong; + goto morefmt; +case 't' : +case 'z' : +/* Here we only care that sizeof(size_t) == sizeof(long). +* On a 32-bit platform it doesn't matter that size_t is +* typedef'ed to int or long; va_arg will work either way. +* Same for ptrdiff_t (%td). +*/ + nlong =1 ; + goto morefmt; +case 'd' : +switch (nlong) { +case 0 : +print_int (&s,va_arg (va, int),10 , props); +break ; +case 1 : +print_int (&s,va_arg (va, long),10 , props); +break ; +default : + print_int(&s, va_arg(va, long long), 10, props); +break ; + } +break ; +case 'u' : +switch (nlong) { +case 0 : +print_unsigned (&s,va_arg (va, unsigned),10 , props); +break ; +case 1 : + print_unsigned(&s, va_arg(va, unsigned long), 10, props); +break ; +default : + print_unsigned(&s, va_arg(va, unsigned long long), 10, props); +break ; + } +break ; +case 'x' : +switch (nlong) { +case 0 : +print_unsigned (&s,va_arg (va, unsigned),16 , props); +break ; +case 1 : + print_unsigned(&s, va_arg(va, unsigned long), 16, props); +break ; +default : + print_unsigned(&s, va_arg(va, unsigned long long), 16, props); +break ; + } +break ; +case 'p' : + props.alternate = true; + print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props); +break ; +case 's' : + print_str(&s, va_arg(va, const char *), props); +break ; +default : +addchar (&s, f); +break ; + } } - } - *s.buffer =0 ; -return s.added; + *s.buffer =0 ; +return s.added; } -int snprintf (char *buf,int size,const char *fmt, ...) { -va_list va; -int r; +va_list va; +int r; -va_start (va, fmt); - r =vsnprintf (buf, size, fmt, va); -va_end (va); -return r; +va_start (va, fmt); + r =vsnprintf (buf, size, fmt, va); +va_end (va); +return r; }int vprintf (const char *fmt,va_list va) { -char buf[BUFSZ ]; -int r; +char buf[BUFSZ ]; +int r; - r =vsnprintf (buf,sizeof (buf), fmt, va); -puts (buf); -return r; + r =vsnprintf (buf,sizeof (buf), fmt, va); +puts (buf); +return r; }int printf (const char *fmt, ...) { -va_list va; -char buf[BUFSZ ]; -int r; - -va_start (va, fmt); - r =vsnprintf (buf,sizeof buf, fmt, va); -va_end (va); -puts (buf); -return r; +va_list va; +char buf[BUFSZ ]; +int r; + +va_start (va, fmt); + r =vsnprintf (buf,sizeof buf, fmt, va); +va_end (va); +puts (buf); +return r; }void binstr (unsigned long x,char out[BINSTR_SZ ])
lib/string.c +177 -177
458 lines not on this page · raw message
Signature · 1 line
2.34.3
Re: [PATCH kvm-unit-tests v2 1/2] lib: Fix whitespace
Quoted 8 lines · Andrew Jones
printf.candstring.care a couple of the original files and are the last that still have the original formatting. Let's finally clean them up!The change was done by modifying Linux's
scripts/Lindentto use 100 columns instead of 80 and then manually reverting a few changes that I didn't like, which I found by diffing with -b.
Quoted 6 lines
Reviewed-by Nikos Nikoleris <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- lib/printf.c | 427 +++++++++++++++++++++++++-------------------------- lib/string.c | 354 +++++++++++++++++++++--------------------- 2 files changed, 390 insertions(+), 391 deletions(-)
PATCH v2 2/2 lib: Add ctype.h and collect is* functions
In reply to Andrew JonesWe've been slowly adding ctype functions to different files without even exporting them. Let's change that.
--- lib/argv.c | 9 +++------ lib/ctype.h | 40 ++++++++++++++++++++++++++++++++++++++++ lib/string.c | 6 +----- 3 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 lib/ctype.h
lib/argv.c +3 -6
@@ -6,6 +6,7 @@*/ #include "libcflat.h" +#include "ctype.h" #include "argv.h" #include "auxinfo.h"
@@ -19,10 +20,6 @@ char **environ = __environ;static char args_copy[1000 ];static char *copy_ptr = args_copy; -#define isblank (c) ((c) == ' ' || (c) == '\t') -#define isalpha (c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || (c) == '_') -#define isalnum (c) (isalpha(c) || ((c) >= '0' && (c) <= '9')) -static const char *skip_blanks (const char *p) {while (isblank (*p))
@@ -92,12 +89,12 @@ static char *env_next(char *env)if (!*env)return env; -if (isalpha (*env)) { +if (isalpha (*env) || *env =='_' ) {bool invalid = false; p = env +1 ;while (*p && *p !='=' && *p !='\n' ) { -if (!isalnum (*p)) +if (!(isalnum (*p) || *p =='_' )) invalid = true; ++p; }
lib/ctype.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _CTYPE_H_ +#define _CTYPE_H_ + +static inline int isblank (int c) +{ +return c ==' ' || c =='\t' ; +} + +static inline int islower (int c) +{ +return c >='a' && c <='z' ; +} + +static inline int isupper (int c) +{ +return c >='A' && c <='Z' ; +} + +static inline int isalpha (int c) +{ +return isupper (c) ||islower (c); +} + +static inline int isdigit (int c) +{ +return c >='0' && c <='9' ; +} + +static inline int isalnum (int c) +{ +return isalpha (c) ||isdigit (c); +} + +static inline int isspace (int c) +{ +return c ==' ' || c =='\t' || c =='\r' || c =='\n' || c =='\v' || c =='\f' ; +} + +#endif /* _CTYPE_H_ */
lib/string.c +1 -5
@@ -6,6 +6,7 @@*/ #include "libcflat.h" +#include "ctype.h" #include "stdlib.h" #include "linux/compiler.h"
@@ -163,11 +164,6 @@ void *memchr(const void *s, int c, size_t n)return NULL ; } -static int isspace (int c) -{ -return c ==' ' || c =='\t' || c =='\r' || c =='\n' || c =='\v' || c =='\f' ; -} -static unsigned long long __strtoll (const char *nptr,char **endptr,int base,bool is_signed,bool is_longlong) {
Signature · 1 line
2.34.3
Re: [PATCH kvm-unit-tests v2 2/2] lib: Add ctype.h and collect is* functions
Quoted 6 lines · Andrew Jones
We've been slowly adding ctype functions to different files without even exporting them. Let's change that.
Reviewed-by Nikos Nikoleris <[email protected]>Signed-off-by Andrew Jones <[email protected]>---
...
lib/ctype.h line 1
Quoted 7 lines
lib/ctype.h new file +1
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */
Maybe we should use LGPL for the C library stuff? ... most other libc related files use LGPL, too.
Apart from that:
Re: [PATCH kvm-unit-tests v2 2/2] lib: Add ctype.h and collect is* functions
Quoted 8 lines · Thomas Huth
Quoted 6 lines · Andrew Jones
We've been slowly adding ctype functions to different files without even exporting them. Let's change that.
Reviewed-by Nikos Nikoleris <[email protected]>Signed-off-by Andrew Jones <[email protected]>---
...
Quoted 10 lines
lib/ctype.h line 1
Quoted 7 lines
lib/ctype.h new file +1
-0@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ Maybe we should use LGPL for the C library stuff? ... most other libc related files use LGPL, too.
Right. Will do.
Quoted 4 lines
Apart from that:
Reviewed-by Thomas Huth <[email protected]>
Thanks,
drew
Quoted 15 lines · Andrew Jones
v2
--
- remove a trailing whitespace [Nikos]
- add Nikos' r-b's
- add back a check for '_' inargv.cthat got dropped1) Finally, finally, finally reformat
printf.candstring.c, the last two files that had weird formatting.2) Collect is* ctype functions into a new
lib/ctype.hfile.Andrew Jones (2):
lib: Fix whitespace
lib: Addctype.hand collect is* functions
Quoted 10 lines
lib/argv.c | 9 +- lib/ctype.h | 40 +++++ lib/printf.c | 427 +++++++++++++++++++++++++-------------------------- lib/string.c | 356 +++++++++++++++++++++--------------------- 4 files changed, 432 insertions(+), 400 deletions(-) create mode 100644 lib/ctype.hSignature · 1 line
2.34.3
Merged
https://gitlab.com/kvm-unit-tests/kvm-unit-tests/-/merge_requests/32
Thanks,
drew