[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] Add new helper function str_concat.
From: |
Tim Rühsen |
Subject: |
[PATCH 1/2] Add new helper function str_concat. |
Date: |
Mon, 6 Apr 2020 15:44:06 +0200 |
2020-04-06 Tim Rühsen <address@hidden>
* bootstrap.conf: Add gnulib module stdarg.
* src/Makefile.am: Add utils.c and utils.h.
* src/utils.h: New file, declaration of str_concat.
* src/utils.c: New file, implementation of str_concat.
---
ChangeLog | 7 +++++++
bootstrap.conf | 1 +
src/Makefile.am | 3 ++-
src/utils.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
src/utils.h | 23 +++++++++++++++++++++
5 files changed, 86 insertions(+), 1 deletion(-)
create mode 100644 src/utils.c
create mode 100644 src/utils.h
diff --git a/ChangeLog b/ChangeLog
index 6747997e..92d21ae6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2020-04-06 Tim Rühsen <address@hidden>
+
+ * bootstrap.conf: Add gnulib module stdarg.
+ * src/Makefile.am: Add utils.c and utils.h.
+ * src/utils.h: New file, declaration of str_concat.
+ * src/utils.c: New file, implementation of str_concat.
+
2020-04-06 Tim Rühsen <address@hidden>
* src/pkl-tab.y (load_module): Fix check for empty string.
diff --git a/bootstrap.conf b/bootstrap.conf
index 381a6f5d..71d88b87 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -43,6 +43,7 @@ gnulib_modules="
readline
secure_getenv
socket
+ stdarg
stdbool
strchrnul
streq
diff --git a/src/Makefile.am b/src/Makefile.am
index 4947c058..61c43aa0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -50,7 +50,8 @@ poke_SOURCES = poke.c poke.h \
pvm.jitter \
pkl-gen.pks pkl-asm.pks \
pkl-gen.pkc pkl-asm.pkc \
- pkl-insn.def pkl-ops.def pkl-attrs.def
+ pkl-insn.def pkl-ops.def pkl-attrs.def \
+ utils.h utils.c
if NBD
poke_SOURCES += ios-dev-nbd.c
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 00000000..27c036d1
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,53 @@
+/* Copyright (C) 2020 Tim Rühsen
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdarg.h> // va_...
+#include <stddef.h> // size_t
+#include <string.h> // strcpy
+
+#include <xalloc.h>
+#include "utils.h"
+
+/* Concatenate 2+ strings.
+ * Last argument must be NULL.
+ * Returns the malloc'ed concatenated string.
+ */
+char *
+str_concat(const char *s0, ...)
+{
+ va_list args;
+ size_t len = 0;
+ const char *s;
+
+ va_start (args, s0);
+ for (s = s0; s; s = va_arg (args, const char *))
+ len += strlen (s);
+ va_end (args);
+
+ char *res = xmalloc (len + 1);
+ char *d = res;
+
+ va_start (args, s0);
+ for (s = s0; s; s = va_arg (args, const char *))
+ {
+ strcpy (d, s);
+ d += strlen (s);
+ }
+ va_end (args);
+
+ return res;
+}
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 00000000..5f924c72
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,23 @@
+/* Copyright (C) 2020 Tim Rühsen
+ *
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SRC_UTILS_H
+#define SRC_UTILS_H
+
+char *
+str_concat(const char *s0, ...);
+
+#endif /* SRC_UTILS_H */
\ No newline at end of file
--
2.26.0
- [PATCH 1/2] Add new helper function str_concat.,
Tim Rühsen <=