summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/slibtool/slibtool.h1
-rw-r--r--project/common.mk1
-rw-r--r--src/output/slbt_output_fdcwd.c123
3 files changed, 125 insertions, 0 deletions
diff --git a/include/slibtool/slibtool.h b/include/slibtool/slibtool.h
index ee70c79..31252d6 100644
--- a/include/slibtool/slibtool.h
+++ b/include/slibtool/slibtool.h
@@ -308,6 +308,7 @@ slbt_api int slbt_main (char **, char **,
slbt_api int slbt_output_config (const struct slbt_driver_ctx *);
slbt_api int slbt_output_features (const struct slbt_driver_ctx *);
+slbt_api int slbt_output_fdcwd (const struct slbt_driver_ctx *);
slbt_api int slbt_output_exec (const struct slbt_driver_ctx *, const struct slbt_exec_ctx *, const char *);
slbt_api int slbt_output_compile (const struct slbt_driver_ctx *, const struct slbt_exec_ctx *);
slbt_api int slbt_output_execute (const struct slbt_driver_ctx *, const struct slbt_exec_ctx *);
diff --git a/project/common.mk b/project/common.mk
index cd653bc..e756abe 100644
--- a/project/common.mk
+++ b/project/common.mk
@@ -14,6 +14,7 @@ API_SRCS = \
src/output/slbt_output_config.c \
src/output/slbt_output_error.c \
src/output/slbt_output_exec.c \
+ src/output/slbt_output_fdcwd.c \
src/output/slbt_output_features.c \
src/skin/slbt_skin_default.c \
src/skin/slbt_skin_install.c \
diff --git a/src/output/slbt_output_fdcwd.c b/src/output/slbt_output_fdcwd.c
new file mode 100644
index 0000000..5e1d6da
--- /dev/null
+++ b/src/output/slbt_output_fdcwd.c
@@ -0,0 +1,123 @@
+/*******************************************************************/
+/* slibtool: a skinny libtool implementation, written in C */
+/* Copyright (C) 2016--2020 Z. Gilboa */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <slibtool/slibtool.h>
+
+#include "slibtool_driver_impl.h"
+#include "slibtool_dprintf_impl.h"
+
+static const char aclr_reset[] = "\x1b[0m";
+static const char aclr_bold[] = "\x1b[1m";
+
+static const char aclr_green[] = "\x1b[32m";
+static const char aclr_blue[] = "\x1b[34m";
+static const char aclr_magenta[] = "\x1b[35m";
+
+static int slbt_output_fdcwd_plain(const struct slbt_driver_ctx * dctx)
+{
+ char path[PATH_MAX];
+ char scwd[20];
+
+ int fdcwd = slbt_driver_fdcwd(dctx);
+ int fderr = slbt_driver_fderr(dctx);
+ int ferror = 0;
+
+ if (fdcwd == AT_FDCWD) {
+ strcpy(scwd,"AT_FDCWD");
+ } else {
+ sprintf(scwd,"%d",fdcwd);
+ }
+
+ if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) {
+ ferror = 1;
+ memset(path,0,sizeof(path));
+ strerror_r(errno,path,sizeof(path));
+ }
+
+ if (slbt_dprintf(
+ fderr,
+ "%s: %s: {.fdcwd=%s, .realpath%s=%c%s%c}.\n",
+ dctx->program,
+ "fdcwd",
+ scwd,
+ ferror ? ".error" : "",
+ ferror ? '[' : '"',
+ path,
+ ferror ? ']' : '"') < 0)
+ return -1;
+
+ return 0;
+}
+
+static int slbt_output_fdcwd_annotated(const struct slbt_driver_ctx * dctx)
+{
+ char path[PATH_MAX];
+ char scwd[20];
+
+ int fdcwd = slbt_driver_fdcwd(dctx);
+ int fderr = slbt_driver_fderr(dctx);
+ int ferror = 0;
+
+ if (fdcwd == AT_FDCWD) {
+ strcpy(scwd,"AT_FDCWD");
+ } else {
+ sprintf(scwd,"%d",fdcwd);
+ }
+
+ if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) {
+ ferror = 1;
+ memset(path,0,sizeof(path));
+ strerror_r(errno,path,sizeof(path));
+ }
+
+ if (slbt_dprintf(
+ fderr,
+ "%s%s%s%s: %s%s%s: {.fdcwd=%s%s%s%s, .realpath%s=%s%s%c%s%c%s}.\n",
+
+ aclr_bold,aclr_magenta,
+ dctx->program,
+ aclr_reset,
+
+ aclr_bold,
+ "fdcwd",
+ aclr_reset,
+
+ aclr_bold,aclr_blue,
+ scwd,
+ aclr_reset,
+
+ ferror ? ".error" : "",
+ aclr_bold,aclr_green,
+ ferror ? '[' : '"',
+ path,
+ ferror ? ']' : '"',
+ aclr_reset) < 0)
+ return -1;
+
+ return 0;
+}
+
+int slbt_output_fdcwd(const struct slbt_driver_ctx * dctx)
+{
+ int fderr = slbt_driver_fderr(dctx);
+
+ if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER)
+ return slbt_output_fdcwd_plain(dctx);
+
+ else if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_ALWAYS)
+ return slbt_output_fdcwd_annotated(dctx);
+
+ else if (isatty(fderr))
+ return slbt_output_fdcwd_annotated(dctx);
+
+ else
+ return slbt_output_fdcwd_plain(dctx);
+}