aboutsummaryrefslogtreecommitdiff
path: root/pc-bios/s390-ccw/libc.c
diff options
context:
space:
mode:
authorCollin L. Walling <walling@linux.vnet.ibm.com>2018-02-23 10:43:10 -0500
committerThomas Huth <thuth@redhat.com>2018-02-26 07:56:54 +0100
commitfc0e208774364c2a8013aa028b742a8dde6d2c2b (patch)
treee35f4d176dc33c79079a9a44b5d59535c4549acd /pc-bios/s390-ccw/libc.c
parentac4c5958b1e6165971303cb02598b190485481f5 (diff)
downloadqemu-fc0e208774364c2a8013aa028b742a8dde6d2c2b.zip
qemu-fc0e208774364c2a8013aa028b742a8dde6d2c2b.tar.gz
qemu-fc0e208774364c2a8013aa028b742a8dde6d2c2b.tar.bz2
s390-ccw: update libc
Moved: memcmp from bootmap.h to libc.h (renamed from _memcmp) strlen from sclp.c to libc.h (renamed from _strlen) Added C standard functions: isdigit Added non C-standard function: uitoa atoui Signed-off-by: Collin L. Walling <walling@linux.vnet.ibm.com> Acked-by: Christian Borntraeger <borntraeger@de.ibm.com> Reviewed-by: Janosch Frank <frankja@linux.vnet.ibm.com> Reviewed-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com>
Diffstat (limited to 'pc-bios/s390-ccw/libc.c')
-rw-r--r--pc-bios/s390-ccw/libc.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/pc-bios/s390-ccw/libc.c b/pc-bios/s390-ccw/libc.c
new file mode 100644
index 0000000..38ea77d
--- /dev/null
+++ b/pc-bios/s390-ccw/libc.c
@@ -0,0 +1,88 @@
+/*
+ * libc-style definitions and functions
+ *
+ * Copyright 2018 IBM Corp.
+ * Author(s): Collin L. Walling <walling@linux.vnet.ibm.com>
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ */
+
+#include "libc.h"
+#include "s390-ccw.h"
+
+/**
+ * atoui:
+ * @str: the string to be converted.
+ *
+ * Given a string @str, convert it to an integer. Leading spaces are
+ * ignored. Any other non-numerical value will terminate the conversion
+ * and return 0. This function only handles numbers between 0 and
+ * UINT64_MAX inclusive.
+ *
+ * Returns: an integer converted from the string @str, or the number 0
+ * if an error occurred.
+ */
+uint64_t atoui(const char *str)
+{
+ int val = 0;
+
+ if (!str || !str[0]) {
+ return 0;
+ }
+
+ while (*str == ' ') {
+ str++;
+ }
+
+ while (*str) {
+ if (!isdigit(*str)) {
+ break;
+ }
+ val = val * 10 + *str - '0';
+ str++;
+ }
+
+ return val;
+}
+
+/**
+ * uitoa:
+ * @num: an integer (base 10) to be converted.
+ * @str: a pointer to a string to store the conversion.
+ * @len: the length of the passed string.
+ *
+ * Given an integer @num, convert it to a string. The string @str must be
+ * allocated beforehand. The resulting string will be null terminated and
+ * returned. This function only handles numbers between 0 and UINT64_MAX
+ * inclusive.
+ *
+ * Returns: the string @str of the converted integer @num
+ */
+char *uitoa(uint64_t num, char *str, size_t len)
+{
+ size_t num_idx = 1; /* account for NUL */
+ uint64_t tmp = num;
+
+ IPL_assert(str != NULL, "uitoa: no space allocated to store string");
+
+ /* Count indices of num */
+ while ((tmp /= 10) != 0) {
+ num_idx++;
+ }
+
+ /* Check if we have enough space for num and NUL */
+ IPL_assert(len > num_idx, "uitoa: array too small for conversion");
+
+ str[num_idx--] = '\0';
+
+ /* Convert int to string */
+ while (num_idx >= 0) {
+ str[num_idx--] = num % 10 + '0';
+ num /= 10;
+ }
+
+ return str;
+}