1 /*******************************************************************/
2 /* slibtool: a skinny libtool implementation, written in C */
3 /* Copyright (C) 2016--2020 Z. Gilboa */
4 /* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
5 /*******************************************************************/
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include <slibtool/slibtool.h>
13
14 #include "slibtool_driver_impl.h"
15 #include "slibtool_dprintf_impl.h"
16
17 static const char aclr_reset[] = "\x1b[0m";
18 static const char aclr_bold[] = "\x1b[1m";
19
20 static const char aclr_green[] = "\x1b[32m";
21 static const char aclr_blue[] = "\x1b[34m";
22 static const char aclr_magenta[] = "\x1b[35m";
23
slbt_output_fdcwd_plain(const struct slbt_driver_ctx * dctx)24 static int slbt_output_fdcwd_plain(const struct slbt_driver_ctx * dctx)
25 {
26 char path[PATH_MAX];
27 char scwd[20];
28
29 int fdcwd = slbt_driver_fdcwd(dctx);
30 int fderr = slbt_driver_fderr(dctx);
31 int ferror = 0;
32
33 if (fdcwd == AT_FDCWD) {
34 strcpy(scwd,"AT_FDCWD");
35 } else {
36 sprintf(scwd,"%d",fdcwd);
37 }
38
39 if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) {
40 ferror = 1;
41 memset(path,0,sizeof(path));
42 strerror_r(errno,path,sizeof(path));
43 }
44
45 if (slbt_dprintf(
46 fderr,
47 "%s: %s: {.fdcwd=%s, .realpath%s=%c%s%c}.\n",
48 dctx->program,
49 "fdcwd",
50 scwd,
51 ferror ? ".error" : "",
52 ferror ? '[' : '"',
53 path,
54 ferror ? ']' : '"') < 0)
55 return -1;
56
57 return 0;
58 }
59
slbt_output_fdcwd_annotated(const struct slbt_driver_ctx * dctx)60 static int slbt_output_fdcwd_annotated(const struct slbt_driver_ctx * dctx)
61 {
62 char path[PATH_MAX];
63 char scwd[20];
64
65 int fdcwd = slbt_driver_fdcwd(dctx);
66 int fderr = slbt_driver_fderr(dctx);
67 int ferror = 0;
68
69 if (fdcwd == AT_FDCWD) {
70 strcpy(scwd,"AT_FDCWD");
71 } else {
72 sprintf(scwd,"%d",fdcwd);
73 }
74
75 if (slbt_realpath(fdcwd,".",0,path,sizeof(path)) < 0) {
76 ferror = 1;
77 memset(path,0,sizeof(path));
78 strerror_r(errno,path,sizeof(path));
79 }
80
81 if (slbt_dprintf(
82 fderr,
83 "%s%s%s%s: %s%s%s: {.fdcwd=%s%s%s%s, .realpath%s=%s%s%c%s%c%s}.\n",
84
85 aclr_bold,aclr_magenta,
86 dctx->program,
87 aclr_reset,
88
89 aclr_bold,
90 "fdcwd",
91 aclr_reset,
92
93 aclr_bold,aclr_blue,
94 scwd,
95 aclr_reset,
96
97 ferror ? ".error" : "",
98 aclr_bold,aclr_green,
99 ferror ? '[' : '"',
100 path,
101 ferror ? ']' : '"',
102 aclr_reset) < 0)
103 return -1;
104
105 return 0;
106 }
107
slbt_output_fdcwd(const struct slbt_driver_ctx * dctx)108 int slbt_output_fdcwd(const struct slbt_driver_ctx * dctx)
109 {
110 int fderr = slbt_driver_fderr(dctx);
111
112 if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER)
113 return slbt_output_fdcwd_plain(dctx);
114
115 else if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_ALWAYS)
116 return slbt_output_fdcwd_annotated(dctx);
117
118 else if (isatty(fderr))
119 return slbt_output_fdcwd_annotated(dctx);
120
121 else
122 return slbt_output_fdcwd_plain(dctx);
123 }
124