summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlexander Amelkin <alexander@amelkin.msk.ru>2019-05-27 20:05:36 +0300
committerAlexander Amelkin <mocbuhtig@amelkin.msk.ru>2019-06-10 13:56:31 +0300
commite11f463b4ea988dfe4314b2fd3d9c91315f10965 (patch)
treed2b9ba8c0270d28d040e2fdee291ab561752a71e /lib
parent619a02cf5dc5b85a62730d82277fa8e78be396f5 (diff)
downloadipmitool-e11f463b4ea988dfe4314b2fd3d9c91315f10965.tar.gz
Add a helper args2buf() function
The function converts a set of command line arguments representing byte values into a byte buffer and verifies each individual value to be a valid data byte. Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
Diffstat (limited to 'lib')
-rw-r--r--lib/helper.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/helper.c b/lib/helper.c
index f19fbce..46c4282 100644
--- a/lib/helper.c
+++ b/lib/helper.c
@@ -1080,3 +1080,35 @@ ipmi_get_oem_id(struct ipmi_intf *intf)
return oem_id;
}
+
+/** Parse command line arguments as numeric byte values (dec or hex)
+ * and store them in a \p len sized buffer \p out.
+ *
+ * @param[in] argc Number of arguments
+ * @param[in] argv Array of arguments
+ * @param[out] out The output buffer
+ * @param[in] len Length of the output buffer in bytes (no null-termination
+ * is assumed, the input data is treated as raw byte values,
+ * not as a string.
+ *
+ * @returns A success status indicator
+ * @return false Error
+ * @return true Success
+ */
+bool
+args2buf(int argc, char *argv[], uint8_t *out, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len && i < (size_t)argc; ++i) {
+ uint8_t byte;
+
+ if (str2uchar(argv[i], &byte)) {
+ lprintf(LOG_ERR, "Bad byte value: %s", argv[i]);
+ return false;
+ }
+
+ out[i] = byte;
+ }
+ return true;
+}