summaryrefslogtreecommitdiff
path: root/src/output/slbt_output_fdcwd.c
blob: fa7e8a7e01af2d8c96565e14c79f6d492bc2ce6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************/
/*  slibtool: a skinny libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  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_util_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_util_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);
}