[PATCH 1/4] util.c: Add helper for string to int conversions

Nahim El Atmani nahim+dev at naam.me
Wed Jul 27 18:41:14 UTC 2016


From: Nahim El Atmani <naam at lse.epita.fr>

From: Nahim El Atmani <nahim+dev at naam.me>

* util.c (string_to_int): New function.
* defs.h: Add it.

Signed-off-by: Nahim El Atmani <nahim+dev at naam.me>
Reviewed-By: Gabriel Laskar <gabriel at lse.epita.fr>
---
 defs.h |  1 +
 util.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/defs.h b/defs.h
index 4b7e8ed..2edf943 100644
--- a/defs.h
+++ b/defs.h
@@ -592,6 +592,7 @@ extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t
 
 extern unsigned long get_pagesize(void);
 extern int string_to_uint(const char *str);
+extern int string_to_int(const char *str);
 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
 
 #define QUOTE_0_TERMINATED			0x01
diff --git a/util.c b/util.c
index e926f93..881d895 100644
--- a/util.c
+++ b/util.c
@@ -59,6 +59,21 @@ string_to_uint(const char *str)
 }
 
 int
+string_to_int(const char *str)
+{
+	char *error;
+	long value;
+
+	if (!*str)
+		return -1;
+	errno = 0;
+	value = strtol(str, &error, 10);
+	if (errno || *error || (long)(int)value != value)
+		return -1;
+	return (int)value;
+}
+
+int
 tv_nz(const struct timeval *a)
 {
 	return a->tv_sec || a->tv_usec;
-- 
Nahim El Atmani





More information about the Strace-devel mailing list