[Libguestfs] [PATCH nbdkit 1/4] common: Move get_current_dir_name(3) compatibility function.

Richard W.M. Jones rjones at redhat.com
Sun Oct 28 10:13:25 UTC 2018


So that it can be used in plugins.
---
 common/include/Makefile.am            |  1 +
 common/include/get-current-dir-name.h | 62 +++++++++++++++++++++++++++
 src/Makefile.am                       |  4 +-
 src/utils.c                           | 14 +-----
 4 files changed, 68 insertions(+), 13 deletions(-)

diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index bb73eec..81f4804 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -37,6 +37,7 @@ include $(top_srcdir)/common-rules.mk
 EXTRA_DIST = \
 	byte-swapping.h \
 	exit-with-parent.h \
+	get-current-dir-name.h \
 	isaligned.h \
 	ispowerof2.h \
 	iszero.h \
diff --git a/common/include/get-current-dir-name.h b/common/include/get-current-dir-name.h
new file mode 100644
index 0000000..d99c1ad
--- /dev/null
+++ b/common/include/get-current-dir-name.h
@@ -0,0 +1,62 @@
+/* nbdkit
+ * Copyright (C) 2018 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Implement get_current_dir_name(3) on platforms which don't have it. */
+
+#ifndef NBDKIT_GET_CURRENT_DIR_NAME_H
+#define NBDKIT_GET_CURRENT_DIR_NAME_H
+
+#include <config.h>
+
+#ifndef HAVE_GET_CURRENT_DIR_NAME
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <limits.h>
+
+static inline char *
+get_current_dir_name (void)
+{
+  char *ret;
+
+  ret = malloc (PATH_MAX);
+  if (ret == NULL)
+    return NULL;
+  ret = getcwd (ret, PATH_MAX);
+  if (ret == NULL)
+    return NULL;
+  return realloc (ret, strlen (ret) + 1);
+}
+#endif
+
+#endif /* NBDKIT_GET_CURRENT_DIR_NAME_H */
diff --git a/src/Makefile.am b/src/Makefile.am
index 6b9e0a9..999aeae 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -102,5 +102,7 @@ test_utils_SOURCES = \
 	test-utils.c \
 	utils.c \
 	cleanup.c
-test_utils_CPPFLAGS = -I$(top_srcdir)/include
+test_utils_CPPFLAGS = \
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/common/include
 test_utils_CFLAGS = $(WARNINGS_CFLAGS) $(VALGRIND_CFLAGS)
diff --git a/src/utils.c b/src/utils.c
index 0e48d52..c9e1e14 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -44,20 +44,10 @@
 #include <termios.h>
 #include <errno.h>
 
+#include "get-current-dir-name.h"
+
 #include "internal.h"
 
-#ifndef HAVE_GET_CURRENT_DIR_NAME
-static char *
-get_current_dir_name (void)
-{
-  char *ret = malloc (PATH_MAX);
-
-  ret = getcwd (ret, PATH_MAX);
-  ret = realloc (ret, strlen (ret) + 1);
-  return ret;
-}
-#endif
-
 char *
 nbdkit_absolute_path (const char *path)
 {
-- 
2.19.0.rc0




More information about the Libguestfs mailing list