summaryrefslogtreecommitdiff
path: root/src/driver/slbt_unit_ctx.c
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2016-03-04 13:32:05 -0500
committermidipix <writeonce@midipix.org>2016-03-08 14:21:05 -0500
commit9ca8c4c3c1b2cb1b22a3ff04c1c37b0863dd87f6 (patch)
treed1659a6814efef596730d6da5247b3fdd58d179b /src/driver/slbt_unit_ctx.c
parent0ff546cab29192407bf991bf915dce52c049c387 (diff)
downloadslibtool-9ca8c4c3c1b2cb1b22a3ff04c1c37b0863dd87f6.tar.bz2
slibtool-9ca8c4c3c1b2cb1b22a3ff04c1c37b0863dd87f6.tar.xz
created skeleton.
Diffstat (limited to 'src/driver/slbt_unit_ctx.c')
-rw-r--r--src/driver/slbt_unit_ctx.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/driver/slbt_unit_ctx.c b/src/driver/slbt_unit_ctx.c
new file mode 100644
index 0000000..3fab392
--- /dev/null
+++ b/src/driver/slbt_unit_ctx.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 <stdint.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#include <slibtool/slibtool.h>
+#include "slibtool_driver_impl.h"
+
+static int slbt_free_unit_ctx_impl(struct slbt_unit_ctx_impl * ctx, int status)
+{
+ if (ctx) {
+ slbt_unmap_input(&ctx->map);
+ free(ctx);
+ }
+
+ return status;
+}
+
+int slbt_get_unit_ctx(
+ const struct slbt_driver_ctx * dctx,
+ const char * path,
+ struct slbt_unit_ctx ** pctx)
+{
+ struct slbt_unit_ctx_impl * ctx;
+
+ if (!dctx || !(ctx = calloc(1,sizeof(*ctx))))
+ return -1;
+
+ if (slbt_map_input(-1,path,PROT_READ,&ctx->map))
+ return slbt_free_unit_ctx_impl(ctx,-1);
+
+ memcpy(&ctx->cctx,dctx->cctx,
+ sizeof(ctx->cctx));
+
+ ctx->path = path;
+
+ ctx->uctx.path = &ctx->path;
+ ctx->uctx.map = &ctx->map;
+ ctx->uctx.cctx = &ctx->cctx;
+
+ *pctx = &ctx->uctx;
+ return 0;
+}
+
+void slbt_free_unit_ctx(struct slbt_unit_ctx * ctx)
+{
+ struct slbt_unit_ctx_impl * ictx;
+ uintptr_t addr;
+
+ if (ctx) {
+ addr = (uintptr_t)ctx - offsetof(struct slbt_unit_ctx_impl,uctx);
+ ictx = (struct slbt_unit_ctx_impl *)addr;
+ slbt_free_unit_ctx_impl(ictx,0);
+ }
+}