Blame src/output/slbt_output_fdcwd.c

89ace8
/*******************************************************************/
89ace8
/*  slibtool: a skinny libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
89ace8
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
89ace8
/*******************************************************************/
89ace8
89ace8
#include <fcntl.h>
89ace8
#include <stdio.h>
89ace8
#include <string.h>
89ace8
#include <errno.h>
89ace8
#include <unistd.h>
89ace8
#include <slibtool/slibtool.h>
89ace8
89ace8
#include "slibtool_driver_impl.h"
89ace8
#include "slibtool_dprintf_impl.h"
89ace8
89ace8
static const char aclr_reset[]   = "\x1b[0m";
89ace8
static const char aclr_bold[]    = "\x1b[1m";
89ace8
89ace8
static const char aclr_green[]   = "\x1b[32m";
89ace8
static const char aclr_blue[]    = "\x1b[34m";
89ace8
static const char aclr_magenta[] = "\x1b[35m";
89ace8
89ace8
static int slbt_output_fdcwd_plain(const struct slbt_driver_ctx * dctx)
89ace8
{
89ace8
	char         path[PATH_MAX];
89ace8
	char         scwd[20];
89ace8
89ace8
	int          fdcwd   = slbt_driver_fdcwd(dctx);
89ace8
	int          fderr   = slbt_driver_fderr(dctx);
89ace8
	int          ferror  = 0;
89ace8
89ace8
	if (fdcwd == AT_FDCWD) {
89ace8
		strcpy(scwd,"AT_FDCWD");
89ace8
	} else {
89ace8
		sprintf(scwd,"%d",fdcwd);
89ace8
	}
89ace8
89ace8
	if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) {
89ace8
		ferror = 1;
89ace8
		memset(path,0,sizeof(path));
89ace8
		strerror_r(errno,path,sizeof(path));
89ace8
	}
89ace8
89ace8
	if (slbt_dprintf(
89ace8
			fderr,
89ace8
			"%s: %s: {.fdcwd=%s, .realpath%s=%c%s%c}.\n",
89ace8
			dctx->program,
89ace8
			"fdcwd",
89ace8
			scwd,
89ace8
			ferror ? ".error" : "",
89ace8
			ferror ? '[' : '"',
89ace8
			path,
89ace8
			ferror ? ']' : '"') < 0)
89ace8
		return -1;
89ace8
89ace8
	return 0;
89ace8
}
89ace8
89ace8
static int slbt_output_fdcwd_annotated(const struct slbt_driver_ctx * dctx)
89ace8
{
89ace8
	char         path[PATH_MAX];
89ace8
	char         scwd[20];
89ace8
89ace8
	int          fdcwd   = slbt_driver_fdcwd(dctx);
89ace8
	int          fderr   = slbt_driver_fderr(dctx);
89ace8
	int          ferror  = 0;
89ace8
89ace8
	if (fdcwd == AT_FDCWD) {
89ace8
		strcpy(scwd,"AT_FDCWD");
89ace8
	} else {
89ace8
		sprintf(scwd,"%d",fdcwd);
89ace8
	}
89ace8
89ace8
	if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) {
89ace8
		ferror = 1;
89ace8
		memset(path,0,sizeof(path));
89ace8
		strerror_r(errno,path,sizeof(path));
89ace8
	}
89ace8
89ace8
	if (slbt_dprintf(
89ace8
			fderr,
89ace8
			"%s%s%s%s: %s%s%s: {.fdcwd=%s%s%s%s, .realpath%s=%s%s%c%s%c%s}.\n",
89ace8
89ace8
			aclr_bold,aclr_magenta,
89ace8
			dctx->program,
89ace8
			aclr_reset,
89ace8
89ace8
			aclr_bold,
89ace8
			"fdcwd",
89ace8
			aclr_reset,
89ace8
89ace8
			aclr_bold,aclr_blue,
89ace8
			scwd,
89ace8
			aclr_reset,
89ace8
89ace8
			ferror ? ".error" : "",
89ace8
			aclr_bold,aclr_green,
89ace8
			ferror ? '[' : '"',
89ace8
			path,
89ace8
			ferror ? ']' : '"',
89ace8
			aclr_reset) < 0)
89ace8
		return -1;
89ace8
89ace8
	return 0;
89ace8
}
89ace8
89ace8
int slbt_output_fdcwd(const struct slbt_driver_ctx * dctx)
89ace8
{
89ace8
	int fderr = slbt_driver_fderr(dctx);
89ace8
89ace8
	if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER)
89ace8
		return slbt_output_fdcwd_plain(dctx);
89ace8
89ace8
	else if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_ALWAYS)
89ace8
		return slbt_output_fdcwd_annotated(dctx);
89ace8
89ace8
	else if (isatty(fderr))
89ace8
		return slbt_output_fdcwd_annotated(dctx);
89ace8
89ace8
	else
89ace8
		return slbt_output_fdcwd_plain(dctx);
89ace8
}