diff options
author | Antonio Larrosa <alarrosa@suse.com> | 2016-02-02 18:31:27 +0100 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2018-05-05 10:37:21 -0700 |
commit | 13385d8add69156805f824cedcdad2986a23662d (patch) | |
tree | e6de005c57f87ae87cc599ad66c62555afc24ae2 | |
parent | 5fb14ee51f849ec86c109bae101ae3f7b7ed7e39 (diff) |
Quote colons in filenames/paths
Makefile doesn't like colons in filenames/paths so they must
be quoted in the output. Otherwise makedepend doesn't work with
full paths that contain a colon.
V2: Use quoted filename when measuring name length
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | pr.c | 48 |
1 files changed, 44 insertions, 4 deletions
@@ -63,25 +63,65 @@ add_include(struct filepointer *filep, struct inclist *file, } } +/** + * Replaces all ":" occurrences in @p input with "\:" using @p outputbuffer (of size @p bufsize) + * possibly to hold the result. @p returns the string with quoted colons + */ +static const char * +quoteColons(const char *input, char *outputbuffer, size_t bufsize) +{ + const char *tmp=input; + const char *loc; + char *output=outputbuffer; + + loc = strchr(input, ':'); + if (loc == NULL) { + return input; + } + + tmp=input; + while (loc != NULL && bufsize > loc-tmp+2 ) { + memcpy(output, tmp, loc-tmp); + output+=loc-tmp; + bufsize-=loc-tmp+2; + tmp=loc+1; + *output='\\'; + output++; + *output=':'; + output++; + loc = strchr(tmp, ':'); + } + + if (strlen(tmp) <= bufsize) + strcpy(output, tmp); + else { + strncpy(output, tmp, bufsize-1); + output[bufsize]=0; + } + return outputbuffer; +} + static void pr(struct inclist *ip, const char *file, const char *base) { static const char *lastfile; static int current_len; register int len, i; + const char * quoted; char buf[ BUFSIZ ]; + char quotebuf[ BUFSIZ ]; printed = TRUE; - len = strlen(ip->i_file)+1; + quoted = quoteColons(ip->i_file, quotebuf, sizeof(quotebuf)); + len = strlen(quoted)+1; if (current_len + len > width || file != lastfile) { lastfile = file; snprintf(buf, sizeof(buf), "\n%s%s%s: %s", - objprefix, base, objsuffix, ip->i_file); + objprefix, base, objsuffix, quoted); len = current_len = strlen(buf); } else { - buf[0] = ' '; - strcpy(buf+1, ip->i_file); + snprintf(buf, sizeof(buf), " %s", quoted); current_len += len; } fwrite(buf, len, 1, stdout); |