aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Finck <mail@colinfinck.de>2024-10-21 13:34:25 +0200
committerDavid Gibson <david@gibson.dropbear.id.au>2024-10-24 15:44:47 +1100
commit18aa49a9f68dfa2a00edee071a95019ed30da9f7 (patch)
treee8601e5ecf4caf4674bb769955fa35465d1e5daa
parentf9968fa06921b2ab82f9a7e39a0042d5d57511fa (diff)
downloaddtc-18aa49a9f68dfa2a00edee071a95019ed30da9f7.zip
dtc-18aa49a9f68dfa2a00edee071a95019ed30da9f7.tar.gz
dtc-18aa49a9f68dfa2a00edee071a95019ed30da9f7.tar.bz2
Escape spaces in depfile with backslashes.
This matches how Linux escapes spaces in paths. The same syntax is also used by other build tools that output depfiles, e.g. https://github.com/rust-lang/cargo/blob/edd36eba5e0d6e0cfcb84bd0cc651ba8bf5e7f83/src/cargo/core/compiler/output_depinfo.rs#L19 Signed-off-by: Colin Finck <mail@colinfinck.de> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--dtc.c4
-rw-r--r--srcpos.c6
-rw-r--r--util.c16
-rw-r--r--util.h5
4 files changed, 28 insertions, 3 deletions
diff --git a/dtc.c b/dtc.c
index 0655c2e..beda8d5 100644
--- a/dtc.c
+++ b/dtc.c
@@ -289,7 +289,9 @@ int main(int argc, char *argv[])
if (!depfile)
die("Couldn't open dependency file %s: %s\n", depname,
strerror(errno));
- fprintf(depfile, "%s:", outname);
+
+ fprint_path_escaped(depfile, outname);
+ fputc(':', depfile);
}
if (inform == NULL)
diff --git a/srcpos.c b/srcpos.c
index 8e4d18a..5e2f7dd 100644
--- a/srcpos.c
+++ b/srcpos.c
@@ -160,8 +160,10 @@ FILE *srcfile_relative_open(const char *fname, char **fullnamep)
strerror(errno));
}
- if (depfile)
- fprintf(depfile, " %s", fullname);
+ if (depfile) {
+ fputc(' ', depfile);
+ fprint_path_escaped(depfile, fullname);
+ }
if (fullnamep)
*fullnamep = fullname;
diff --git a/util.c b/util.c
index 507f012..4125923 100644
--- a/util.c
+++ b/util.c
@@ -23,6 +23,22 @@
#include "util.h"
#include "version_gen.h"
+void fprint_path_escaped(FILE *fp, const char *path)
+{
+ const char *p = path;
+
+ while (*p) {
+ if (*p == ' ') {
+ fputc('\\', fp);
+ fputc(' ', fp);
+ } else {
+ fputc(*p, fp);
+ }
+
+ p++;
+ }
+}
+
char *xstrdup(const char *s)
{
int len = strlen(s) + 1;
diff --git a/util.h b/util.h
index b448cd7..800f2e2 100644
--- a/util.h
+++ b/util.h
@@ -42,6 +42,11 @@ static inline void NORETURN PRINTF(1, 2) die(const char *str, ...)
exit(1);
}
+/**
+ * Writes path to fp, escaping spaces with a backslash.
+ */
+void fprint_path_escaped(FILE *fp, const char *path);
+
static inline void *xmalloc(size_t len)
{
void *new = malloc(len);