From 6529aa199a616eaeb8f32841d29ec5afd7dcc4f4 Mon Sep 17 00:00:00 2001
From: midipix <writeonce@midipix.org>
Date: Fri, 8 Apr 2016 12:53:52 -0400
Subject: internal: slbt_create_symlink(): initial implementation and
 integration.

---
 src/internal/slibtool_symlink_impl.c | 62 ++++++++++++++++++++++++++++++++++++
 src/internal/slibtool_symlink_impl.h | 15 +++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 src/internal/slibtool_symlink_impl.c
 create mode 100644 src/internal/slibtool_symlink_impl.h

(limited to 'src/internal')

diff --git a/src/internal/slibtool_symlink_impl.c b/src/internal/slibtool_symlink_impl.c
new file mode 100644
index 0000000..88af538
--- /dev/null
+++ b/src/internal/slibtool_symlink_impl.c
@@ -0,0 +1,62 @@
+/*******************************************************************/
+/*  slibtool: a skinny libtool implementation, written in C        */
+/*  Copyright (C) 2016  Z. Gilboa                                  */
+/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include "slibtool_symlink_impl.h"
+
+int slbt_create_symlink(
+	const struct slbt_driver_ctx *	dctx,
+	struct slbt_exec_ctx *		ectx,
+	const char *			target,
+	char *				lnkname,
+	bool				flawrapper)
+{
+	const char *	slash;
+	char *		ln[5];
+	char *		dotdot;
+	char		tmplnk [PATH_MAX];
+	char		atarget[PATH_MAX];
+
+	/* atarget */
+	if ((slash = strrchr(target,'/')))
+		slash++;
+	else
+		slash = target;
+
+	dotdot = flawrapper ? "../" : "";
+
+	if ((size_t)snprintf(atarget,sizeof(atarget),"%s%s",
+			dotdot,slash) >= sizeof(atarget))
+		return -1;
+
+	/* tmplnk */
+	if ((size_t)snprintf(tmplnk,sizeof(tmplnk),"%s.symlink.tmp",
+			lnkname) >= sizeof(tmplnk))
+		return -1;
+
+	/* ln argv (fake) */
+	ln[0] = "ln";
+	ln[1] = "-s";
+	ln[2] = atarget;
+	ln[3] = lnkname;
+	ln[4] = 0;
+	ectx->argv = ln;
+
+	/* step output */
+	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
+		if (slbt_output_link(dctx,ectx))
+			return -1;
+
+	/* create symlink */
+	if (symlink(atarget,tmplnk))
+		return -1;
+
+	return rename(tmplnk,lnkname);
+}
diff --git a/src/internal/slibtool_symlink_impl.h b/src/internal/slibtool_symlink_impl.h
new file mode 100644
index 0000000..5ca65d5
--- /dev/null
+++ b/src/internal/slibtool_symlink_impl.h
@@ -0,0 +1,15 @@
+/*******************************************************************/
+/*  slibtool: a skinny libtool implementation, written in C        */
+/*  Copyright (C) 2016  Z. Gilboa                                  */
+/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdbool.h>
+#include <slibtool/slibtool.h>
+
+int slbt_create_symlink(
+	const struct slbt_driver_ctx *	dctx,
+	struct slbt_exec_ctx *		ectx,
+	const char *			target,
+	char *				lnkname,
+	bool				flawrapper);
-- 
cgit v1.2.3