diff mbox series

[kvm-unit-tests,v2,2/2] lib: Add ctype.h and collect is* functions

Message ID 20220520132404.700626-3-drjones@redhat.com (mailing list archive)
State New, archived
Headers show
Series lib: Cleanups | expand

Commit Message

Andrew Jones May 20, 2022, 1:24 p.m. UTC
We've been slowly adding ctype functions to different files without
even exporting them. Let's change that.

Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 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

Comments

Thomas Huth May 24, 2022, 12:21 p.m. UTC | #1
On 20/05/2022 15.24, Andrew Jones wrote:
> We've been slowly adding ctype functions to different files without
> even exporting them. Let's change that.
> 
> Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> ---
...
> diff --git a/lib/ctype.h b/lib/ctype.h
> new file mode 100644
> index 000000000000..0b43d626478a
> --- /dev/null
> +++ b/lib/ctype.h
> @@ -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:
Reviewed-by: Thomas Huth <thuth@redhat.com>
Andrew Jones May 26, 2022, 7:14 a.m. UTC | #2
On Tue, May 24, 2022 at 02:21:40PM +0200, Thomas Huth wrote:
> On 20/05/2022 15.24, Andrew Jones wrote:
> > We've been slowly adding ctype functions to different files without
> > even exporting them. Let's change that.
> > 
> > Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
> > Signed-off-by: Andrew Jones <drjones@redhat.com>
> > ---
> ...
> > diff --git a/lib/ctype.h b/lib/ctype.h
> > new file mode 100644
> > index 000000000000..0b43d626478a
> > --- /dev/null
> > +++ b/lib/ctype.h
> > @@ -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.

> 
> Apart from that:
> Reviewed-by: Thomas Huth <thuth@redhat.com>
> 

Thanks,
drew
diff mbox series

Patch

diff --git a/lib/argv.c b/lib/argv.c
index 0312d74011d3..9ffa673ef3a1 100644
--- a/lib/argv.c
+++ b/lib/argv.c
@@ -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;
 		}
diff --git a/lib/ctype.h b/lib/ctype.h
new file mode 100644
index 000000000000..0b43d626478a
--- /dev/null
+++ b/lib/ctype.h
@@ -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_ */
diff --git a/lib/string.c b/lib/string.c
index a3a8f3b1ce0b..6d8a6380db92 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -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)
 {